0:00

So welcome to the reference section on Strings and Output.

Â So strings in MiniZinc are principally for output.

Â They can also be used in debugging constructs such as assertions and tracing.

Â But the key thing is that they can only be fixed.

Â Basically, we're not going to ask the solver to determine some strings.

Â So a string constant is wrapped in double quote characters.

Â It can contain Unicode characters, new line and tab using the C-style approach.

Â We can have a double quote character itself by putting a backslash beforehand.

Â And then, a backslash parenthesis and

Â then a MiniZinc expression then close parenthesis will basically add a string

Â which gives you the value of that MiniZinc expression in the middle of your string.

Â So this is an interpolated string value.

Â So there are a number of builtin string operations.

Â The show takes an arbitrary MiniZinc expression and

Â returns a string giving the value of the expression.

Â And that's exactly the same thing that happens in the interpolated string,

Â except that interpolated string is already inside another string.

Â We've got some formatted output for numbers.

Â So show_int here takes a first argument i, which its absolute value

Â is the minimum number of characters we're going to use to show the integer i.

Â If it's positive, then we're going to show it right justified, if it's negative,

Â we're going to show it left justified.

Â And show_float is similar.

Â Here we have this absolute value i.

Â This value i gives you the minimum number of characters we're going to use.

Â j gives you the number of digits after this point.

Â And justification ends on the sign of i.

Â So let's have a look at some examples of string functions.

Â So here we've got an enumerated type and some values, integer values and

Â float values.

Â If we have to show the value of this enumerated type p,

Â we just get the pink, the string that names the enumerated part.

Â If we're going to show the integer value 5 in -3, so

Â at least three characters, right justified, we get 5 and two spaces.

Â We're going to show a float value using five characters,

Â left justified with two things after this midpoint.

Â So this natural logarithm of 10.0,

Â we're going to get this space 2.30, so five characters.

Â Other string functions are concatenation, where we concatenate two strings together.

Â Or concatenate, which takes an array of strings and

Â concatenates them all together.

Â Or join, which is very similar to concatenate, but

Â adds a separator between each pair of strings we're concatenating together.

Â So if we take Hello and World, concatenate them, we get this string.

Â Concatenate the results, a, b, c from our previous example, we get pink5.

Â gets two spaces, the space, and 2.30.

Â It's easy to see the three different strings if we use the join.

Â So adding the separator in between each of these, pink, that's a.

Â And there's the separator, then 5 with its two spaces, that was b.

Â And then the separator, then the space 2.30, which was c.

Â 2:59

An output item is a list of strings, and basically it's going to take that list of

Â strings, concatenate them all together, and send them to the output.

Â There can be at most one output segment in the model.

Â And if there's no output segment, then all the declared variables in the model

Â that aren't equated to a right-hand side expression will be output as assignments.

Â So what's that mean?

Â Let's have a look at this model.

Â We've got three decision variables x, y and z.

Â But y was equated to a value when we declared it.

Â So the output will only include the values for x is 3 and z is 1.

Â So that's the solution for these constraints.

Â And notice the output is basically uniform,

Â which could be used as a MiniZinc dataform.

Â So we can do more complicated outputs.

Â So for example,

Â if you want to output a multi-dimensional array like this one here, a.

Â We'd like to show it in two dimensions, so we might use show_int 2 to make sure

Â that the sizes of each of these numbers are the same.

Â And we're going to do this extra thing, because when j is 3, so

Â when we get to the end of each row, we're going to add a new line.

Â Otherwise, we're going to have a space.

Â So basically, we're going to add the space between each two elements in the row,

Â and add a newline at the end.

Â And if you think about what this whole thing, this creates, is this.

Â So we've got space 5, which is showing the 5.

Â And then a space added by this if then else endif statement.

Â Then space 3, which is the result of this, and

Â the space added by this statement here.

Â Then 12, which is the show_int of 12.

Â And then a new line added by this statement.

Â And then some more space, 6, space, there's 2, space, there's 0, new line.

Â It's just going to print out like that, a nice aligned array.

Â So in output statements, every expression which isn't wrapped in show or

Â one of its variants, which is like show_int, show_float,

Â interpolated strings, has to be fixed.

Â So basically, once we get to the output, the solver needs to have already run,

Â we can't have any variables which aren't known to be fixed.

Â So we can ensure the variable's fixed using this fix built-in function.

Â So fix takes an expression, and it aborts if the expression does not have a fixed

Â value, and otherwise returns the fixed value.

Â So basically, this is mainly useful in output.

Â You can use it in general in models otherwise, but

Â usually it's either always going to abort or do something unuseful.

Â So if we look at this model here, we've got two variables, x and y,

Â which we're using.

Â So we're only showing this i, which is a parameter anyway, so

Â there's no problem there.

Â But we're using x and y here, and these are variables.

Â So basically, we're testing against the variable.

Â So in order for the output to be satisfied to know that it won't need to ask

Â the solver anything about this expression, we need to make sure that we actually wrap

Â these uses of variables by this fix x and fix y.

Â And of course, by the time we hit here, the solver has run, and

Â x should have a fixed value and y should have a fixed value in all cases.

Â And so this will always pass.

Â So basically, this fixes telling the output statement, or

Â MiniZinc that the output statement, that these values will be used in a fixed way.

Â We can take a little bit more complicated examples of output.

Â So imagine we had some tasks, and we had some starts and duration.

Â And presumably,

Â one of these would have been presumably a variable that's been solved by the solver.

Â We want print out a sort of, showing a graphical example of our output.

Â So we can print out the name and colon, and

Â then we could print out a number of spaces to get us to the column of the start time.

Â 6:36

And then if the duration is 1, we're just going to print out this H character,

Â saying [INAUDIBLE] that time.

Â And otherwise, we're going to print out number of dashes which is

Â the duration minus 2, so that's where the task is running, right.

Â We're going to print out the start of the task at this point,

Â the dashes where the task is running, and the end of the task at this point.

Â And then a new line up here.

Â And if we take this down to here and run this, we're going to get here, well,

Â the open task begins at 0, has duration 3, so either it starts, running, ends.

Â Read begins at time 2, second time point.

Â It runs for duration 6, so it goes up to there.

Â Fix begins at time 5 and ends at time 6, so we've got the dashes in between.

Â Study starts at time 3 and only goes for one, so we use this H character,

Â say it starts and ends at the same place, and [INAUDIBLE].

Â So, strings in MiniZinc allow us to print the result of the model.

Â They're also useful when we're using the JSON output of MiniZinc

Â to connect to a website, for example.

Â And the key functions are really show and fix and concatenation.

Â And so you can end up with fairly complicated output statements if you want

Â to display something complicated.

Â And really, if you want to build a very, very complicated output,

Â you're better off to build a program in another language to

Â convert a simple output from MiniZinc to a complex output.

Â For example, outputting a JSON string in MiniZinc and

Â then using something to process that into some more complicated form.

Â That brings us to the end of strings and output.

Â