The rules are as follows:
- Each (white) cell must be filled with a value \(1,..,9\).
- Each (white) cell belongs to a horizontal and/or a vertical block of contiguous cells.
- The values of the cells in each block add up to a given constant. These constants are to the left (or top) of the block. A notation a\b is used: a is the sum for the block below and b is the sum for the block to the right.
- The values in each block must be unique (i.e. no duplicates).
Model
The actual model is rather simple. Similar to (2) and (3) I define a binary decision variable:
\[x_{i,j,k} = \begin{cases}1 & \text{if cell $(i,j)$ has the value $k$} \\ 0& \text{otherwise}\end{cases}\] |
where \(k=\{1,..,9\}\). Of course, we can select only one value in a cell, i.e.:
\[ \sum_k x_{i,j,k} = 1 \>\>\forall i,j|IJ(i,j)\] |
where \(IJ(i,j)\) is the set of white cells we need to fill in. To make the model simpler we also define an integer variable \(v_{i,j}\) with the actual value:
\[v_{i,j} = \sum_k k\cdot x_{i,j,k}\] |
The values in each block need to add up the given constant. So:
\[\sum_{i,j|B(b,i,j)} v_{i,j} = C_b \>\>\forall b\] |
where the set \(B(b,i,j)\) is the mapping between block number and cells \((i,j)\). The uniqueness constraint is not very difficult at all:
\[\sum_{i,j|B(b,i,j)} x_{i,j,k} \le 1 \>\>\forall b,k\] |
Note that (1) uses a different numbering scheme for the cells, but otherwise the models are very similar. My version of the model is smaller in terms of nonzero elements in the matrix, due to my use of intermediate variables \(v_{i,j}\).
Data entry
A compact way to enter the data for the example problem in GAMS is:
parameter |
The sets \(IJ(i,j)\) and parameter \(C_b\) can be easily extracted from this.
Results
After adding a dummy objective, we can solve the model as a MIP (Mixed Integer Programming) Model. The output looks like:
---- 75 VARIABLE v.L c1 c2 c3 c4 c5 c6 c7 r1 9 7 8 7 9 |
It is noted that although I used the declaration \(x_{i,j}\), only the subset of cells that we actually need to fill in is used in the equations. GAMS will only actually generate the variables that appear in the equations, so all the “black” cells are not part of the model.
Cplex can solve the model in zero iterations and zero nodes: it solves the model completely in the presolve.
Interestingly in (1) somewhat disappointing performance results are reported. They use CBC and a solver called eplex (never heard of that one). I checked the above small instance with CBC and it can solve it in zero nodes, just like Cplex.
A large, difficult example
This example is taken from (4):
For this problem Cplex has to do a little bit of work. The log looks like:
--- 3,665 rows 4,921 columns 19,189 non-zeroes . . . Dual simplex solved model. Root relaxation solution time = 0.44 sec. (102.53 ticks) Nodes Cuts/ 0 0 0.0000 325 0.0000 1727 Proven optimal solution. MIP Solution: 0.000000 (4423 iterations, 0 nodes) |
To be complete, CBC takes a little more time:
Search completed - best objective 0, took 55117 iterations and 140 nodes (42.00 seconds) |
This model does not solve completely in the presolve. However the computation times look quite good to me, especially when compared to what is reported in (1).
The solution looks like:
Proving uniqueness
We can prove the uniqueness of this solution by adding the constraint:
\[\sum_{i,j,k|IJ(i,j)} x^*_{i,j,k} x_{i,j,k} \le |IJ| –1 \] |
where \(x^*\) is the previously found optimal solution, and \(|IJ|\) is the cardinality of the set \(IJ(i,j)\) (i.e. the number of “white” cells). Note that \(\sum x^*_{i,j,k}=\sum x_{i,j,k} = |IJ|\). With this constraint we should either find a different solution (so the solution was not unique), or the model should be declared “infeasible”.
References
- Helmut Simonis, Kakuro as a Constraint Problem, Cork Constraint Computation Centre (4C), University College Cork, 2008.
- MIP Modeling: From Sukodu to Kenken via Logarithms, http://yetanothermathprogrammingconsultant.blogspot.com/2016/10/mip-modeling-from-sudoku-to-kenken.html
- Solving Takuzu puzzles as a MIP using xor, http://yetanothermathprogrammingconsultant.blogspot.com/2017/01/solving-takuzu-puzzles-as-mip-using-xor.html
- http://pagesperso.g-scop.grenoble-inp.fr/~cambazah/page1/page7/assets/data_generated_hard.ecl has some larger data sets.
- http://www.gecode.org/doc-latest/MPG.pdf, Chapter 21 is about solving Kakuro using Gecode, a constraint programming solver.
- José Barahona da Fonseca, A novel linear MILP model to solve Kakuro puzzles, 2012. This has a (partial) GAMS model (stylistically I would write things differently; the model can be written more cleanly).
i don't understand Proving uniqueness. what is x*?
ReplyDeleteThe text says *the previously found optimal solution*. That is correct.
Delete