0:02

So welcome to the reference material for the course.

Â So this material is designed to give you a different viewpoint

Â on the technical components of a MiniZinc model.

Â So the main material on the courses are problem based and

Â will introduce the concepts as they need them.

Â But here we're trying to give you a summary of the concepts all in one place.

Â And in this first project material,

Â we'll just go through the basic components of a MiniZinc model.

Â So the basic things we need to understand to build a model are firstly types, and

Â the kinds of variables we can have, which parameters and decision variables.

Â Then how are we going to build our basic parenthetic expressions and

Â then convert those into constraints.

Â And finally how are they going to put that all together in the structure of a model.

Â 0:53

So the types available in MiniZinc are rather restricted,

Â the main one because we're doing discreet optimization is integers.

Â And we'll often use a range, a fixed range, so

Â 1..n indicates a set of integers from 1 to n, and indeed l..u is just a way

Â of writing the number set of integers from l, starting at l going up to u.

Â And we also have floating point numbers which are of the type float but

Â we can also have ranges of float numbers.

Â So 1.0..f is the range of all floating point numbers in between 1.0 and

Â whatever value we've given to the floating point parameter f.

Â We have booleans, so this is true-false things, and

Â they appear in many discrete optimization problems.

Â And strings, so strings are really only made available in MiniZinc in order

Â to have some output so that you can see the result of your model.

Â Other important types are arrays and sets but

Â we'll talk about those in later chapters.

Â So variables are critical for modeling a problem, so

Â they are the key thing that goes into a constraint model,

Â variables and constraints.

Â There are two distinct types of variables in MiniZinc, they're parameters and

Â decision variables.

Â And parameters are those which basically reflect fixed information,

Â so they're going to be given to us in terms of input to the problem.

Â They're really the information that describes what the problem is or

Â what are the parameters of problems.

Â So what we'll typically try to do is build a model which can run from

Â many different inputs for many different values for its parameters.

Â And then decision variables are the decisions we

Â have to make to solve the problem.

Â And so they're the key thing that in fact we've got to be careful

Â we are not going to be responsible for giving values to those decision variables,

Â that's what's going to be the job of the solver.

Â And if you want they're the output of our model,

Â a set of decisions which solve constraints.

Â 2:51

So parameters are very much like variables in a standard programming

Â language except that they can only be assigned a value once.

Â They're not like in a procedural language where you can think of a parameter or

Â a variable as a storage place which I can put different values into at

Â different times.

Â A parameter will only take one value point in time.

Â And a parameter is declared in this form.

Â So we have this par keyword, which is optional.

Â Then the type of the parameter, then colon, then the name of the parameter.

Â And then optionally we can have this equals to some expression.

Â And then we finish off that declaration with a semicolon.

Â And in MiniZinc the semicolon is a statement terminator, so

Â it brings to the end each of the statements of the model.

Â 3:36

So type can be any of the types that we've talked about and

Â other types that we haven't talked about yet.

Â varname is the name of the parameter.

Â And the par is optional, right, and is typically omitted, we won't ever really

Â use par but it's nice to think of it there because it shows the correspondence

Â between pars and vars, which is decision variables which we'll see later.

Â Now the optional expression can give a value to the parameter, so

Â you can just give a value to the parameter straight away or

Â you can later on give it value.

Â So decision variables are our other kind of variables and

Â there you've got to think of them like variables in mathematics.

Â They get declared with a type and this var keyword, and

Â they're what the solver's going to compute.

Â And they're, again, only going to take one value,

Â that's the value that the solver's going to compute for them.

Â So what the solver's trying to do is find a value for

Â all of those decision variables that satisfies all the constraints.

Â 4:29

And the declaration is very much the same as the parameter declaration, except for

Â you're going to have this var keyword in front of the type, and

Â then everything else is the same.

Â And typically a decision variable is not equated to an expression,

Â it can make sense to equate a decision variable to an expression

Â which includes other decision variables,

Â which means it's really just if you want a macro for some named expression.

Â 5:01

So the next important component of a model is an assignment.

Â An assignment just is this varname equals expression.

Â What it is is going to give a value to the variable varname, and

Â that could be a parameter or it could be a decision variable.

Â So when it's giving a or

Â it can define an expression which it equates to a decision variable.

Â And so we've got some examples down here.

Â So here I've declared an integer m and then I've equated it to 3 times n,

Â where presumably n must be another parameter that I've

Â previously given a value to so that I can work out the value of m.

Â Here's a declaration of a decision variable d which is a variable which you

Â can make an integer value, and here I've equated d to the absolute value of

Â x- y and presumably one of x- y is a variable, at least one.

Â And so what we're really doing is saying okay, d is just a name for

Â the expression of the absolute difference between x and y, so

Â I can use that elsewhere throughout my model.

Â And down here I have a declaration.

Â Whoops.

Â And then after that an assignment, then I can put them together.

Â And really, that's what we've seen when we were showing you the format for

Â declarations of parameters and variables, is we can have this equality basing this

Â assignment part directly afterwards and it's just the same as if I had written

Â these two statements here and just wrote them as one together.

Â 6:25

So variables have an instantiation which basically determines whether

Â they're parameters or decision variables,

Â and the type or instantiation is called the type instance.

Â Sometimes you'll see error messages come out of MiniZinc which are termed in,

Â talk about type-insts, so that's why it's important to know what a type-inst is.

Â But basically it's just a combination of the instantiation type which is var or

Â par, and the type.

Â So we have var int, some are var 0 to 3, we could have a par float, or par bool.

Â And again, usually we omit the par, so when I talk about the type-inst float I'm

Â really talking about par floats, a float with a fixed value.

Â 7:05

Now it's important to put comments in the model just like it is important to put

Â comments in any kind of program, there's two ways you can do that in MiniZinc.

Â You can have a percentage character and

Â then everything after that in the line is considered as a comment and

Â won't be really seen by the MiniZinc compiler.

Â And you can also use this C style comments where you have a slash star to open

Â the comment and the star slash to close the comment,

Â and then anything between those will be ignored.

Â And it's just like in programming,

Â we should have a header comment describing the model at the top of the file.

Â You should describe each parameter when you put in into the model.

Â You should describe each decision variable when put it in the model.

Â And you should describe each constraint.

Â So really, comments are very worthwhile to have in your model.

Â You will not use so many of them during the course in some sense,

Â because when we're showing you things on slides, we have to keep it concise.

Â All right strings are really, as I said, only provided for output and

Â the output item basically takes an output keyword, and

Â then a list of strings, and again terminated with a semicolon.

Â 8:19

So what are strings, so strings,

Â literals are just like those in C they're enclosed in double quotes.

Â They can't extend across more than one line, so

Â if you need to build a big string, big thick string,

Â then you need to break it into lines and then concatenate the strings together.

Â You can use backslash to get special characters, for

Â example /n would give you a new line, /t would give you a tab, so

Â we use the same special characters as you've seen.

Â And there's some important built in functions for building strings.

Â So show(v) takes an expression v and converts that into a string,

Â so basically takes the value of that expression v and converts it into a string

Â and that expression could be complex, could be an array, could be a set.

Â There's another useful thing which is this backlash open braces and

Â then v close brace, so this is a thing we can put inside a string which will

Â replace that expression with this result of showing v, so basically

Â it's a way of putting a show inside, building a string inside another string.

Â 9:41

So the key things that we're going to deal in a discrete optimization model

Â are mathematical arithmetic expressions.

Â And so MiniZinc provides very standard arithmetic operators,

Â so the obvious ones multiplication, division, addition, and subtraction.

Â And for integers multiplication this is integer division, and

Â modulo is addition and subtraction.

Â And integer and float literals are like those in C, so they're very [INAUDIBLE].

Â You can automatically coerce, MiniZinc will automatically coerce from integers

Â to floats, so if you use an integer in a place where it requires a float,

Â it will actually coerce it for you.

Â But you can also use the builtin int2float coercion to map an integer into a float.

Â And there are lots of builtin arithmetic functions,

Â you can look through the MiniZinc documentation to find them all.

Â There are things like absolute values and sine, cos, or tan,

Â all kinds of arithmetic functions, the standard familiar ones.

Â All right, the basic arithmetic constraints, and remember what we're

Â aiming to do with that arithmetic expressions is build constraints.

Â 10:51

These relational operators so equality, disequality,

Â greater than, less than, better than or equal to, and less than or equal to.

Â And the constraint is just written with this constraint keyword and

Â then constraint-expression.

Â And so we have some simple examples,

Â here's to find a constraint where x is less than or equal to y.

Â Here's a more complicated one where I'm saying that x + 2 times y- the absolute

Â value of z is not allowed to be = 0.

Â And again, each of these expressions is terminated by a semicolon.

Â And so now we are ready to finish, we've seen all of the basic items that can be in

Â a model and a MiniZinc model is just a sequence of these items.

Â And really the order of the items doesn't matter, you'll see that we'll have a style

Â where we tend to put the parameter declarations first,

Â then the variable declarations, then the constraints, then the objective,

Â then the output, but they can be in any order.

Â And so the kinds of items you can have are inclusion items,

Â so this allows you to include a file, it's just as if the file was read

Â into your model at that point and that file name is given as a string literal.

Â There's the output item which we've discussed before,

Â which takes a list of string expressions and we'll print them out.

Â But of course we'll have variable ways, and shows, and things like that, and

Â it'll be typically printing out the value of the decision that we've made.

Â We'll have variable declarations, which could be for parameter or

Â decision variable.

Â Variable assignments which are typically your parameters but can also be made for

Â decision variables and constraints.

Â Or a constraint and then a boolean expression, so

Â we'll see later on we can go beyond those basic relations.

Â And lastly there has to be a solve item and this tells you what the solver should

Â do, and it's going to be one of these three things.

Â It's going to solve satisfy,

Â which means just look for any solution that satisfies these constraints.

Â Or it could be solve maximize and then arithmetic expressions say find me

Â a solution which maximizes the value of this arithmetic expression, or

Â similarly solve minimize.

Â 12:54

Other items which we'll see later on in the course are predicate, function, and

Â test items that these allow us to encapsulate

Â commonly used constraints with their names so they can be used multiple times.

Â And annotation items which we won't see in this course,

Â which is a way of extending the language of communication with the solver,

Â so I can tell more things to the solver by building new kind of [INAUDIBLE].

Â So we didn't really talk about par identifiers so what these parameter names

Â and variable names and also names, function names should be.

Â But basically they start with a letter, they're followed by other letters and

Â underscores or digits as the characters that can make up an identifier.

Â You can also use the underscore itself just alone as a name of an anonymous

Â decision variable.

Â So sometimes this is useful so if you want to build up an array,

Â you want to put in some fixed values and some unknown values,

Â you can use an anonymous decision variable underscore.

Â