17
How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven Leyffer , Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting style)

How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Embed Size (px)

Citation preview

Page 1: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

How to Play Sudoku & Win

Integer Programming Formulation of a Popular Game

Sven Leyffer, Argonne, Feb. 15, 2005

(windoze powerpoint sumi painting style)

Page 2: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Sudoku RulesGiven initial dataFill digits 1-9 into boxes such thatEvery digit 1-9 appears in every row, column, and 3x3 box.Works for letters, names etc.

1 6 3 8

2 3 9

7 1 6

7 8 9 4 2

4 9

9 2 5 1 4

6 2 9

4 7 6

5 7 6 3

Page 3: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Solving Sudoku

Eliminate rows & cols from 3x3 boxE.g. eliminate 2 from 3x3 box:

1 6 3 8

2 3 9

7 1 6

7 8 9 4 2

4 9

9 2 5 1 4

6 2 9

4 7 6

5 7 6 3

Page 4: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Solving Sudoku

Eliminate rows & cols from 3x3 boxE.g. eliminate 2 from 3x3 box:Gives new 2

1 6 3 8

2 3 9

7 1 6

7 8 9 4 2

2 4 9

9 2 5 1 4

6 2 9

4 7 6

5 7 6 3

Page 5: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Solving Sudoku

Eliminate rows & cols from 3x3 boxE.g. eliminate 2 from 3x3 box:Gives new 2Continue …

1 6 3 2 8

2 3 9

7 1 6

7 8 9 4 2

2 4 9

9 2 5 1 4

6 2 9

4 7 6

5 7 6 3

Page 6: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Solving Sudoku

Eliminate rows & cols from 3x3 boxE.g. eliminate 2 from 3x3 box:Gives new 2Continue …… fill other digits

1 6 3 2 8

2 3 9

2 7 1 6

7 8 9 4 2

2 4 9

9 2 5 1 4

6 2 9

4 2 7 6

5 7 6 2 3

Page 7: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Solving Sudoku

Eliminate rows & cols from 3x3 boxE.g. eliminate 2 from 3x3 box:Gives new 2Continue …… fill other digitsFill 3x3 boxes

1 6 3 2 8

2 3 4 9 5

2 7 1 6

7 8 9 4 2

2 4 9

9 2 5 1 4

6 2 9

4 2 7 6

5 7 6 2 3

Page 8: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Solving Sudoku by Integer Optimization

0-1 variables model digit assignment& linear equations model rules… gives integer linear program (ILP)NP-complete (graph coloring) problem (6,670,903,752,021,072,936,960 sols)Solve using ILP solvers: CPLEX, MINTOSee ~leyffer/sudoku/ for AMPL models

Page 9: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Branch-and-Bound Solver

Solve continuous relaxation: [0,1]

Branch on non-integral variable:

Two new probs: Set y=1 or y=0

Continue until all nodes explored

Page 10: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Solution to Simple Example

Solver: CPLEXSimple Problem:

Time: 0 Nodes: 0 … AMPL presolve

Challenging Problem

Time: 0.03 seconds Nodes: 0

… integral LP soln

1 7 5 4 9 6 3 2 8

8 6 2 3 7 1 4 9 5

4 9 3 8 5 2 7 1 6

7 1 8 9 4 3 5 6 2

2 5 4 1 6 8 9 3 7

9 3 6 7 2 5 1 8 4

6 2 9 5 3 4 8 7 1

3 4 1 2 8 7 6 5 9

5 8 7 6 1 9 2 4 3

Page 11: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Sudokus Without Solutions

No solutionAMPL/CPLEX:

Infeasible constraints by presolve

Proof of non-existence of solution

1 6 3 8

2 3 8 9

7 1 6

7 8 9 4 2

4 9

9 2 5 1 4

6 2 9

4 7 6

5 7 6 3

Page 12: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Sudokus With Multiple Solutions

Many solutionAMPL/MINTO:

Find first solutionInteger cuts to remove solutionResolve to find new solutionStop when infeasible

Soln at root nodewhat’s the secret?

1 6 3

2

6

7 9 2

9 5 1 4

6 9

7 6

7 3

Page 13: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

AMPL’s Primal Presolve

Transform problem to smaller/easier equivalent problemRemove fixed variables Remove constraints that express variable boundsIteratively tighten bounds on variables, using Gauss-SeidelTodd has experience with NCP presolve!

Page 14: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

MINTO’s Primal HeuristicsFind solution early … reduce tree-searchProbing: tentatively set binaries to 0 or 1Rounding & feasibility pump

LP relaxation: solution x … round to r(x)Solve LP min dist(y,r(x)) subject to Ax=bLP solution y … round to r(y) & repeat

Heuristics: genetic algorithms, et al.

Page 15: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Integer Optimization Applications

Crew schedulingMachine schedulingReactor core reload:

Max fuel efficiencySubject to safety & diffusion PDEFind reloading schedule… integer & nonlinear

Page 16: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

Constrained Logic Programming

CLP origins: AI & logic CLP: declarative language to formulate logical problems, scheduling etcCLP: combines constraints with logic & constraint satisfactionCLP allows tailored solutions/rulesCHIP, ECLiPSe, GNU Prolog, IF/Prolog, ILOG, Koalog, MOzart, and SICStus

Page 17: How to Play Sudoku & Win Integer Programming Formulation of a Popular Game Sven LeyfferSven Leyffer, Argonne, Feb. 15, 2005 (windoze powerpoint sumi painting

References:Wikipedia: en.wikipedia.org/wiki/SudokuTh. Koch: How to Solve Sudoku …M.J. Chlond: IP Modeling: Su Doku (05)W. Shortz: Sudoku – 100 wordless puzzlesFourer & Gay: AMPL’s Primal Presolve (93)Sudoku Programmers Forum