AN INTRODUCTION TO MINIZINC MINIZINC Mini Zinc is

  • Slides: 25
Download presentation
AN INTRODUCTION TO MINIZINC

AN INTRODUCTION TO MINIZINC

MINIZINC Mini. Zinc is a language designed for specifying constrained optimization and decision problems

MINIZINC Mini. Zinc is a language designed for specifying constrained optimization and decision problems over integers and real numbers A Mini. Zinc model does not dictate how to solve the problem although the model can contain annotations which are used to guide the underlying solver Mini. Zinc is designed to interface easily to different backend solvers • An input Mini. Zinc model and data file into a Flat. Zinc model • Flat. Zinc models consist of variable declaration and constraint definitions as well as a definition of the objective function if the problem is an optimization problem • The translation from Mini. Zinc to Flat. Zinc is specializable to individual backend solvers

A FIRST EXAMPLE We wish to colour a map of Australia Seven different states

A FIRST EXAMPLE We wish to colour a map of Australia Seven different states and territories each of which must be given a colour so that adjacent regions have different colours In graph theory, graph coloring is a special case of graph labeling; it is an assignment of labels traditionally called “colours” to elements of a graph subject to certain constraints Edge coloring assigns a color to each edge so that no two adjacent edges share the same color

CODE

CODE

COMMENTS A comment starts with a ‘%’ which indicates that the rest of the

COMMENTS A comment starts with a ‘%’ which indicates that the rest of the line is a comment. Mini. Zinc has no begin/end comment symbols (such as C’s /* and */ comments) int: nc = 3; declares a variable of the model • • the number of colours to be used a parameter in the problem They must be declared and given a type. In this case the type is int They are given a value by an assignment, as part of the declaration (as above), or a separate assignment statement • int: nc; nc = 3; • It is an error for a parameter to occur in more than one assignment The basic parameter types are integers (int), floating point numbers (float), Booleans (bool) and strings (string). Arrays and sets are also supported

BACK TO THE EXAMPLE In our colouring model we associate a decision variable with

BACK TO THE EXAMPLE In our colouring model we associate a decision variable with each region, wa, nt, sa, q, nsw, v and t, which stands for the (unknown) colour to be used to fill the region For each decision variable we need to give the set of possible values the variable can take. This is called the variable’s domain • It can be given as part of the variable declaration • The type of the decision variable is inferred from the type of the values in the domain In Mini. Zinc decision variables can be booleans, integers, floating point numbers, sets, or arrays whose elements are decision variables In the example we use integers to model the different colours. • 1. . nc which is an integer range expression indicating the set {1, 2, . . . , nc}

DECISION AND PARAMETERS Mini. Zinc distinguishes between the two kinds of model variables: parameters

DECISION AND PARAMETERS Mini. Zinc distinguishes between the two kinds of model variables: parameters and decision variables • Expressions that can be constructed using decision variables are more restricted than those that can be built from parameters. • In any place that a decision variable can be used, so can a parameter of the same type Formally the distinction between parameters and decision variables is called the instantiation of the variable • The former is instantiated by you • The second is instantiated by the solver

CONSTRAINTS The next component of the model are the constraints These specify the boolean

CONSTRAINTS The next component of the model are the constraints These specify the boolean expressions that the decision variables must satisfy to be a valid solution to the model In our running example we have a number of not equal constraints between the decision variables enforcing that if two states are adjacent then they must have different colours constraint wa != nt; constraint wa != sa; constraint nt != q; constraint sa != nsw; constraint sa != v; constraint q != nsw; constraint nsw != v; Mini. Zinc provides: equal (= or ==), not equal (!=), strictly less than (<) strictly greater than (>), less than or equal to (<=), and greater than or equal to (>=).

SOLVE AND OUTPUT solve satisfy; indicates the kind of problem In this case it

SOLVE AND OUTPUT solve satisfy; indicates the kind of problem In this case it is a satisfaction problem: we wish to find a value for the decision variables that satisfies the constraints but we do not care which one The final part of the model is the output statement • An output statement is followed by a list of strings • String literals which are written between double quotes and use a C like notation for special characters • expression of the form show(X) where X is the name of a decision variable or parameter There also formatted varieties of show for numbers • show_float(n, d, X) outputs the value of float X in at least |n| characters, right justified if n > 0 and left justified otherwise, with d characters after the decimal point output ["wa=", show(wa), "t nt=", show(nt), "t sa=", show(sa), "n", "q=", show(q), "t nsw=", show(nsw), "t v=", show(v), "n", "t=", show(t), "n"];

RUN IT! Evaluate our model with $ mzn-g 12 fd aust. mzn • aust.

RUN IT! Evaluate our model with $ mzn-g 12 fd aust. mzn • aust. mzn is the name of the file which contains the whole model • mzn-g 12 fd is one of the solvers in the suite • The output is: wa=1 nt=3 sa=2 q=1 nsw=3 v=1 t=1 ----- 3 1 1 2 3 1 1 The line of 10 dashes ----- is output automatically added by the Mini. Zinc output to indicate a solution has been found

SOME MORE INFORMATION Mini. Zinc is a high-level, typed, mostly first-order, functional, modelling language.

SOME MORE INFORMATION Mini. Zinc is a high-level, typed, mostly first-order, functional, modelling language. It provides: • mathematical notation-like syntax (automatic coercions, overloading, iteration, sets, arrays); • expressive constraints (finite domain, set, linear arithmetic, integer); • support for different kinds of problems (satisfaction, explicit optimisation); • separation of data from model; • extensibility (user-defined functions and predicates); • reliability (type checking, instantiation checking, assertions); • solver-independent modelling; • simple, declarative semantics.

FROM ZINC TO FLATZINC Zinc Mini. Zinc Flat. Zinc Solver Flat. Zinc is a

FROM ZINC TO FLATZINC Zinc Mini. Zinc Flat. Zinc Solver Flat. Zinc is a low-level solver input language that is the target language for Mini. Zinc. It is designed to be easy to translate into the form required by a solver.

YOU MODEL WE SOLVE Flat. Zinc Implementations Gecode/Flat. Zinc. The Gecode generic constraint development

YOU MODEL WE SOLVE Flat. Zinc Implementations Gecode/Flat. Zinc. The Gecode generic constraint development environment provides a Flat. Zinc interface. The source code for the interface stripped of all Gecode-specific code is also available. ECLi. PSe. The ECLi. PSe Constraint Programming System provides support for evaluating Flat. Zinc using ECLi. PSe's constraint solvers. Mini. Zinc models can be embedded into ECLi. PSe code in order to add user-defined search and I/O facilities to the models. SICStus Prolog. SICStus (from version 4. 0. 5) includes a library for evaluating Flat. Zinc. Ja. Co. P. The Ja. Co. P constraint solver (from version 4. 2) has an interface to Flat. Zinc. SCIP, a framework for Constraint Integer Programming, has an interface to Flat. Zinc. Opturion CPX, a Constraint Programming solver with e. Xplanation system, has an interface to Flat. Zinc. Minisat. ID, an implementation of a search algorithm combining techniques from the fields of SAT, SAT Module Theories, Constraint Programming and Answer Set Programming, has an interface to Flat. Zinc.

RESOURCES Mini. Zinc 2. 0 • Windows, Mac. OS, Linux, Installation from source code.

RESOURCES Mini. Zinc 2. 0 • Windows, Mac. OS, Linux, Installation from source code. • http: //www. minizinc. org/2. 0/index. html The Mini. Zinc IDE is a tool for writing and running Mini. Zinc models • Windows, Mac. OS, Linux, source code. • http: //www. minizinc. org/ide/index. html Mini. Zinc 2. 0 Specification • http: //www. minizinc. org/2. 0/doc-lib/minizinc-spec. pdf Tutorial • http: //www. minizinc. org/downloads/doc-latest/minizinc-tute. pdf Global constraints and built-in functions • http: //www. minizinc. org/2. 0/doc-lib/doc. html

EXERCISE Use the IDE to run the colouring example

EXERCISE Use the IDE to run the colouring example

MORE EXAMPLES BAKING! We know how to make two sorts of cakes. A banana

MORE EXAMPLES BAKING! We know how to make two sorts of cakes. A banana cake which takes 250 g of self-raising flour, 2 mashed bananas, 75 g sugar and 100 g of butter, and a chocolate cake which takes 200 g of self-raising flour, 75 g of cocoa, 150 g sugar and 150 g of butter. We can sell a chocolate cake for $4. 50 and a banana cake for $4. 00. And we have 4 kg selfraising flour, 6 bananas, 2 kg of sugar, 500 g of butter and 500 g of cocoa. How many of each sort of cake should we bake for the fete to maximise the profit?

BAKING CAKES

BAKING CAKES

INTEGER ARITHMETIC EXPR. Mini. Zinc provides the standard integer arithmetic operators. • Addition (+),

INTEGER ARITHMETIC EXPR. Mini. Zinc provides the standard integer arithmetic operators. • Addition (+), • subtraction (-), • multiplication (*), • integer division (div), • integer modulus (mod), Mini. Zin provides Integer functions as • absolute value (abs(-4) = 4), • power function (pow(2, 5) = 32) Integer literals can be decimal, hexadecimal or octal. • For instance 0, 005, 123, 0 x 1 b 7, 0 o 777

OPTIMISATION solve maximize 400 * b + 450 * c; • We want to

OPTIMISATION solve maximize 400 * b + 450 * c; • We want to find a solution that maximises the expression in the solve statement called the objective. • The objective can be any kind of arithmetic expression. • One can replace the key word maximize by minimize to specify a minimisation problem. no. of banana cakes = 2 no. of chocolate cakes = 2 -----===== The line ===== is output automatically for optimisation problems when the system has proved that a solution is optimal.

DATAFILES A drawback of this model is that, each time we need to modify

DATAFILES A drawback of this model is that, each time we need to modify the amount of ingredients we have, we need to modify the constraints Solution: set the value of these parameters in a separate data file, with extension. dzn • $ mzn-g 12 fd cakes 2. mzn pantry. dzn no. of banana cakes = 3 no. of chocolate cakes = 8

ASSERTIONS Defensive programming suggests that we should check that the values in the data

ASSERTIONS Defensive programming suggests that we should check that the values in the data file are reasonable In case of our example, to check that the quantity of all ingredients is non-negative and generate a run-time error if this is not true Mini. Zinc provides a built-in boolean operator • The form is assert(b, s) • constraint assert(flour >= 0. 0, "Amount of flour is negative");

REAL NUMBER SOLVING Mini. Zinc also supports “real number” constraint solving using floating point

REAL NUMBER SOLVING Mini. Zinc also supports “real number” constraint solving using floating point solving Note that we declare a float variable f using var float: f • f in a fixed range l to u with var l. . u: f, where l and u are floating point expressions Addition (+), subtraction (-), multiplication (*) and floating point division (/). The built-in function int 2 float can be used to coerce integers to floating point numbers Floating point functions for • absolute value (abs), square root (sqrt), natural logarithm (ln), logarithm base 2 (log 2), logarithm base 10 (log 10), exponentiation of e (exp), sine (sin), cosine (cos), tangent (tan), arcsine (asin), arccosine (acos), arctangent (atan), and unary power (pow). The syntax for arithmetic literals is standard. Example float literals are 1. 05, 1. 3 e-5 and 1. 3+E 5

DIFFERENT SOLVERS Since we wish to use real number solving we need to use

DIFFERENT SOLVERS Since we wish to use real number solving we need to use a different solver than the finite domain solver used by mzn-g 12 fd A suitable solver would be one that supports mixed integer linear programming • $ mzn-g 12 mip cake. mzn When you download Mini. Zinc 2 you also get some solvers • mzn-g 12 fd uses G 12 finite domain solver • mzn-gecode, which will invoke mzn 2 fzn with the correct library paths and directly pass the resulting Flat. Zinc on to the Gecode Flat. Zinc interpreter • mzn-g 12 mip uses a mixed-integer programming solver • mzn-g 12 lazy G 12 Lazy Clause Generation solver Other tools • mzn 2 fzn model. mzn to transform Mini. Zinc to Flat. Zinc and use such file with another solver

SO, A MODEL IS… Inclusion of items allow the contents of another file to

SO, A MODEL IS… Inclusion of items allow the contents of another file to be inserted into the model • include 〈filename〉; Variable declarations: parameters which are assigned a (fixed) value in the model or in a data file and decision variables whose (unfixed) value is found only when the model is solved • 〈type inst expr〉: 〈variable〉 [ = 〈expression〉]; Assignment items assign a value to a variable • 〈variable〉 = 〈expression〉; Constraint items form the heart of the model • constraint 〈Boolean expression〉; Solve items specify what kind of solution is being looked for • solve satisfy; • solve maximize 〈arithmetic expression〉; • solve minimize 〈arithmetic expression〉; Output items are for nicely presenting the results • output [ 〈string expression〉, · · · , 〈string expression〉 ];