Update MiniZinc background example
This commit is contained in:
parent
651abd0b53
commit
893a8d9d5f
@ -1,15 +0,0 @@
|
|||||||
% Problem parameters
|
|
||||||
enum TOYS = {football, tennisball, stuffed_lama, stuffed_elephant};
|
|
||||||
array[TOYS] of int: toy_joy = [63, 12, 50, 100];
|
|
||||||
array[TOYS] of int: toy_space = [32, 8, 16, 40];
|
|
||||||
int: space_left = 64;
|
|
||||||
|
|
||||||
% Decision variables
|
|
||||||
var set of TOYS: selection;
|
|
||||||
var int: total_joy = sum(toy in selection)(toy_joy[toy]);
|
|
||||||
|
|
||||||
% Constraints
|
|
||||||
constraint sum(toy in selection)(toy_space[toy]) < space_left;
|
|
||||||
|
|
||||||
% Goal
|
|
||||||
solve maximize total_joy;
|
|
15
assets/mzn/back_knapsack.mzn
Normal file
15
assets/mzn/back_knapsack.mzn
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
% Problem parameters
|
||||||
|
enum TOYS;@\Vlabel{line:back-knap-toys}@
|
||||||
|
array[TOYS] of int: toy_joy;@\Vlabel{line:back-knap-joy}@
|
||||||
|
array[TOYS] of int: toy_space;@\Vlabel{line:back-knap-space}@
|
||||||
|
int: space_left;@\Vlabel{line:back-knap-left}@
|
||||||
|
|
||||||
|
% Decision variables
|
||||||
|
var set of TOYS: selection;@\Vlabel{line:back-knap-sel}@
|
||||||
|
var int: total_joy = sum(toy in selection)(toy_joy[toy]);@\Vlabel{line:back-knap-tj}@
|
||||||
|
|
||||||
|
% Constraints
|
||||||
|
constraint sum(toy in selection)(toy_space[toy]) < space_left;@\Vlabel{line:back-knap-con}@
|
||||||
|
|
||||||
|
% Goal
|
||||||
|
solve maximize total_joy;@\Vlabel{line:back-knap-obj}@
|
@ -40,7 +40,8 @@ the model.
|
|||||||
problem using dynamic programming}
|
problem using dynamic programming}
|
||||||
\end{listing}
|
\end{listing}
|
||||||
|
|
||||||
\begin{example}
|
\begin{example}%
|
||||||
|
\label{ex:back-knapsack}
|
||||||
|
|
||||||
Let us consider the following scenario: Packing for a weekend trip, I have to
|
Let us consider the following scenario: Packing for a weekend trip, I have to
|
||||||
decide which toys to bring for my dog, Audrey. We only have a small amount of
|
decide which toys to bring for my dog, Audrey. We only have a small amount of
|
||||||
@ -118,42 +119,51 @@ discrete satisfiability and optimisation problems
|
|||||||
\autocite{nethercote-2007-minizinc}. Its expressive language and extensive
|
\autocite{nethercote-2007-minizinc}. Its expressive language and extensive
|
||||||
library of constraints allow users to easily model complex problems.
|
library of constraints allow users to easily model complex problems.
|
||||||
|
|
||||||
Let us introduce the language by modelling the well-known \emph{Latin squares}
|
Let us introduce the language by modelling the problem from
|
||||||
problem \autocite{wallis-2011-combinatorics}: Given an integer \(n\), find an
|
\cref{ex:back-knapsack}. A \minizinc\ model encoding this problem is shown in
|
||||||
\(n \times n\) matrix, such that each row and column is a permutation of values
|
\cref{lst:back-mzn-knapsack}.
|
||||||
\(1 \ldots n\). A \minizinc\ model encoding this problem could look as follows:
|
|
||||||
|
|
||||||
\begin{listing}
|
\begin{listing}
|
||||||
\mznfile{assets/mzn/2_knapsack.mzn}
|
\mznfile{assets/mzn/back_knapsack.mzn}
|
||||||
\caption{\label{lst:2-mzn-knapsack} A \minizinc\ model describing a 0-1 knapsack
|
\caption{\label{lst:back-mzn-knapsack} A \minizinc\ model describing a 0-1 knapsack
|
||||||
problem}
|
problem}
|
||||||
\end{listing}
|
\end{listing}
|
||||||
|
|
||||||
\begin{mzn}
|
The model starts with the declaration of the \glspl{parameter}.
|
||||||
int: n;
|
\Lref{line:back-knap-toys} declares an enumerated type that represents all
|
||||||
array [1..n, 1..n] of var 1..n: x;
|
possible toys, \(T\) in the mathematical model in the example.
|
||||||
|
\Lref{line:back-knap-joy,line:back-knap-space} declare arrays mapping from toys
|
||||||
|
to integer values, these represent the functional mappings \(joy\) and
|
||||||
|
\(space\). Finally, \lref{line:back-knap-left} declares an integer
|
||||||
|
\gls{parameter} to represent the car capacity as an equivalent to \(C\).
|
||||||
|
|
||||||
constraint forall (r in 1..n) (
|
The model then declares its \glspl{variable}. \Lref{line:back-knap-sel} declares
|
||||||
all_different([x[r, c] | c in 1..n])
|
the main \gls{variable} \mzninline{selection}, which represents the selection of
|
||||||
);
|
toys to be packed. \(S\) in our earlier model. We also declare the variable
|
||||||
constraint forall (c in 1..n) (
|
\mzninline{total_joy}, on \lref{line:back-knap-tj}, which is functionally
|
||||||
all_different([x[r, c] | r in 1..n])
|
defined to be the summation of all the joy for the toy picked in our selection.
|
||||||
);
|
|
||||||
\end{mzn}
|
|
||||||
|
|
||||||
The model introduces a \gls{parameter} \mzninline{n}, and a two-dimensional
|
Finally, the model contains a constraint, on \lref{line:back-knap-con}, to
|
||||||
array of \glspl{variable} (marked by the \mzninline{var} keyword) \mzninline{x}.
|
ensure we do not exceed the given capacity and states the goal for the solver:
|
||||||
Each variable in \mzninline{x} is restricted to the set of integers
|
to maximise the value of the variable \mzninline{total_joy}.
|
||||||
\mzninline{1..n}, which is called the variable's \gls{domain}. The constraints
|
|
||||||
specify the requirements of the problem: for each row \mzninline{r}, the
|
One might note that, although more textual and explicit, the \minizinc\ model
|
||||||
\mzninline{x} variables of all columns must take pairwise different values (and
|
definition is very similar to our earlier mathematical definition.
|
||||||
the same for each column \mzninline{c}). This is modelled using the
|
|
||||||
\mzninline{all_different} function, one of hundreds of pre-defined constraints
|
|
||||||
in \minizinc's library.
|
|
||||||
|
|
||||||
Given ground assignments to input \glspl{parameter}, a \minizinc\ model is
|
Given ground assignments to input \glspl{parameter}, a \minizinc\ model is
|
||||||
translated (via a process called \emph{flattening}) into a set of variables and
|
translated (via a process called \emph{flattening}) into a set of variables and
|
||||||
primitive constraints. Here is the result of flattening for \mzninline{n=2}:
|
primitive constraints.
|
||||||
|
|
||||||
|
Given the assignments
|
||||||
|
|
||||||
|
\begin{mzn}
|
||||||
|
TOYS = {football, tennisball, stuffed_lama, stuffed_elephant};
|
||||||
|
toy_joy = [63, 12, 50, 100];
|
||||||
|
toy_space = [32, 8, 16, 40];
|
||||||
|
space_left = 64;
|
||||||
|
\end{mzn}
|
||||||
|
|
||||||
|
is the result of flattening for \mzninline{n=2}:
|
||||||
|
|
||||||
\begin{mzn}
|
\begin{mzn}
|
||||||
var 1..2: x_1_1;
|
var 1..2: x_1_1;
|
||||||
|
Reference in New Issue
Block a user