diff --git a/assets/mzn/2_knapsack.mzn b/assets/mzn/2_knapsack.mzn deleted file mode 100644 index 01f4abb..0000000 --- a/assets/mzn/2_knapsack.mzn +++ /dev/null @@ -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; diff --git a/assets/mzn/back_knapsack.mzn b/assets/mzn/back_knapsack.mzn new file mode 100644 index 0000000..235f113 --- /dev/null +++ b/assets/mzn/back_knapsack.mzn @@ -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}@ diff --git a/chapters/2_background.tex b/chapters/2_background.tex index 72698f8..0f81ecf 100644 --- a/chapters/2_background.tex +++ b/chapters/2_background.tex @@ -40,7 +40,8 @@ the model. problem using dynamic programming} \end{listing} -\begin{example} +\begin{example}% +\label{ex:back-knapsack} 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 @@ -118,42 +119,51 @@ discrete satisfiability and optimisation problems \autocite{nethercote-2007-minizinc}. Its expressive language and extensive library of constraints allow users to easily model complex problems. -Let us introduce the language by modelling the well-known \emph{Latin squares} -problem \autocite{wallis-2011-combinatorics}: Given an integer \(n\), find an -\(n \times n\) matrix, such that each row and column is a permutation of values -\(1 \ldots n\). A \minizinc\ model encoding this problem could look as follows: +Let us introduce the language by modelling the problem from +\cref{ex:back-knapsack}. A \minizinc\ model encoding this problem is shown in +\cref{lst:back-mzn-knapsack}. \begin{listing} - \mznfile{assets/mzn/2_knapsack.mzn} - \caption{\label{lst:2-mzn-knapsack} A \minizinc\ model describing a 0-1 knapsack + \mznfile{assets/mzn/back_knapsack.mzn} + \caption{\label{lst:back-mzn-knapsack} A \minizinc\ model describing a 0-1 knapsack problem} \end{listing} -\begin{mzn} -int: n; -array [1..n, 1..n] of var 1..n: x; +The model starts with the declaration of the \glspl{parameter}. +\Lref{line:back-knap-toys} declares an enumerated type that represents all +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) ( - all_different([x[r, c] | c in 1..n]) -); -constraint forall (c in 1..n) ( - all_different([x[r, c] | r in 1..n]) -); -\end{mzn} +The model then declares its \glspl{variable}. \Lref{line:back-knap-sel} declares +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 +\mzninline{total_joy}, on \lref{line:back-knap-tj}, which is functionally +defined to be the summation of all the joy for the toy picked in our selection. -The model introduces a \gls{parameter} \mzninline{n}, and a two-dimensional -array of \glspl{variable} (marked by the \mzninline{var} keyword) \mzninline{x}. -Each variable in \mzninline{x} is restricted to the set of integers -\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 -\mzninline{x} variables of all columns must take pairwise different values (and -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. +Finally, the model contains a constraint, on \lref{line:back-knap-con}, to +ensure we do not exceed the given capacity and states the goal for the solver: +to maximise the value of the variable \mzninline{total_joy}. + +One might note that, although more textual and explicit, the \minizinc\ model +definition is very similar to our earlier mathematical definition. Given ground assignments to input \glspl{parameter}, a \minizinc\ model is 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} var 1..2: x_1_1;