Quantcast
Channel: Yet Another Math Programming Consultant
Viewing all articles
Browse latest Browse all 809

Evil Sudoku

$
0
0

 On sudoku.com[1] there is now an "evil" difficulty level. I found these very difficult to solve by hand (I usually give up).



 

Of course, a good MIP presolver/preprocessor devours this in no time. It should solve it using zero iterations, and zero nodes in less than 0.1 seconds.


Version identifier: 20.1.0.1 | 2021-04-07 | 3a818710c
CPXPARAM_Advance 0
CPXPARAM_Threads 1
CPXPARAM_MIP_Display 4
CPXPARAM_MIP_Pool_Capacity 0
CPXPARAM_MIP_Tolerances_AbsMIPGap 0
Generic callback 0x50
Tried aggregator 2 times.
MIP Presolve eliminated 168 rows and580 columns.
Aggregator did 28 substitutions.
Reduced MIP has 128 rows, 122 columns, and501 nonzeros.
Reduced MIP has 122 binaries, 0 generals, 0 SOSs, and0 indicators.
Presolve time = 0.06 sec. (0.93 ticks)
Found incumbent of value 0.000000 after 0.06 sec. (1.66 ticks)

Root node processing (before b&c):
Real time = 0.06 sec. (1.67 ticks)
Sequential b&c:
Real time = 0.00 sec. (0.00 ticks)
------------
Total (root+branch&cut) = 0.06 sec. (1.67 ticks)

--- MIP status (101): integer optimal solution.
--- Cplex Time: 0.06sec (det. 1.67 ticks)

Proven optimal solution
MIP Solution: 0.000000 (0 iterations, 0 nodes)
Final Solve: 0.000000 (0 iterations)

Best possible: 0.000000
Absolute gap: 0.000000
Relative gap: 0.000000


CBC is a bit terser (not always the case):


COIN-OR CBC      38.2.196226ea8 Feb 19, 2022          WEI x86 64bit/MS Window

COIN-OR Branch and Cut (CBC Library 2.10)
written by J. Forrest

Calling CBC main solution routine...
No integer variables - nothing to do

Solved to optimality (within gap tolerances optca and optcr).
MIP solution: 0.000000e+00 (0 nodes, 0.019 seconds)

Best possible: 0.000000e+00
Absolute gap: 0.000000e+00 (absolute tolerance optca: 0)
Relative gap: 0.000000e+00 (relative tolerance optcr: 0.0001)



References


  1. https://sudoku.com/evil/

Appendix: GAMS model

$ontext

  
Evil Sudoku from sudoku.com

  
Reference:
  
http://yetanothermathprogrammingconsultant.blogspot.com/2016/10/mip-modeling-from-sudoku-to-kenken.html


$offtext

set
  a     
'all areas (rows,columns,squares)'/r1*r9,c1*c9,s1*s9/
  i(a)  
'rows'/r1*r9/
  j(a)  
'columns'/c1*c9/
  s(a)  
'squares'/s1*s9/
  u(a,i,j)
'areas with unique values'
  k     
'values'/1*9/;
;

*
* all the areas (rows, columns, squares) where we need to impose unique values
*
u(i,i,j) =
yes;
u(j,i,j) =
yes;
u(s,i,j)$(
ord(s)=3*ceil(ord(i)/3)+ceil(ord(j)/3)-3) = yes;

*
* given values
*
table v0(i,j)
  
c1 c2 c3 c4 c5 c6 c7 c8 c9
r1                 9     5
r2  7                    1
r3        8  2        7     9
r4  6              4  9     8
r5     4     1
r6                          5
r7        2     3
r8  8              1  6     4
r9                       7
;

*
* MIP model
*
binaryvariable x(i,j,k);
x.fx(i,j,k)$(v0(i,j)=k.val) = 1;
variable z 'dummy objective';

equations
   dummy
'objective'
   unique(a,k)
'all-different'
   values(i,j)
'one value per cell'
;

dummy.. z =e= 0;
unique(a,k)..
sum(u(a,i,j), x(i,j,k)) =e= 1;
values(i,j)..
sum(k, x(i,j,k)) =e= 1;

model sudoku /all/;

*
* solve
*
solve sudoku minimizing z using mip;

*
* display solution
*
parameter v(i,j) 'solution';
v(i,j) =
sum(k, k.val*x.l(i,j,k));
option v:0;
display v;


----     73 PARAMETER v  solution

c1 c2 c3 c4 c5 c6 c7 c8 c9

r1 324719856
r2 759863412
r3 168245739
r4 671354928
r5 945182367
r6 283976145
r7 492637581
r8 837521694
r9 516498273

Viewing all articles
Browse latest Browse all 809

Trending Articles