Modeling Sudoku puzzles with Python Sean Davis Matthew Henderson - - PowerPoint PPT Presentation

modeling sudoku puzzles with python
SMART_READER_LITE
LIVE PREVIEW

Modeling Sudoku puzzles with Python Sean Davis Matthew Henderson - - PowerPoint PPT Presentation

Modeling Sudoku puzzles with Python Sean Davis Matthew Henderson Andrew Smith June 30, 2010 SciPy 2010 Python for Scientific Computing Conference June 28 to July 3, 2010 Background - The OKlibrary http://www.ok-sat-library.org/


slide-1
SLIDE 1

Modeling Sudoku puzzles with Python

Sean Davis Matthew Henderson Andrew Smith June 30, 2010

SciPy 2010 Python for Scientific Computing Conference June 28 to July 3, 2010

slide-2
SLIDE 2

Background - The OKlibrary

◮ http://www.ok-sat-library.org/

slide-3
SLIDE 3

Background - The OKlibrary

◮ http://www.ok-sat-library.org/ ◮ Open-source research platform and generative library for

generalised SAT solving

slide-4
SLIDE 4

Background - The OKlibrary

◮ http://www.ok-sat-library.org/ ◮ Open-source research platform and generative library for

generalised SAT solving

◮ Developed by Oliver Kullmann at the University of

Swansea since 2005.

slide-5
SLIDE 5

Background - The OKlibrary

◮ http://www.ok-sat-library.org/ ◮ Open-source research platform and generative library for

generalised SAT solving

◮ Developed by Oliver Kullmann at the University of

Swansea since 2005.

◮ Modeling of combinatorial puzzles via SAT (Latin

squares/Sudoku)

slide-6
SLIDE 6

Background - The OKlibrary

◮ http://www.ok-sat-library.org/ ◮ Open-source research platform and generative library for

generalised SAT solving

◮ Developed by Oliver Kullmann at the University of

Swansea since 2005.

◮ Modeling of combinatorial puzzles via SAT (Latin

squares/Sudoku)

◮ C++/Lisp/Bash

slide-7
SLIDE 7

Background - sudoku.py

◮ http://bitbucket.org/matthew/scipy2010

slide-8
SLIDE 8

Background - sudoku.py

◮ http://bitbucket.org/matthew/scipy2010 ◮ Open-source

slide-9
SLIDE 9

Background - sudoku.py

◮ http://bitbucket.org/matthew/scipy2010 ◮ Open-source ◮ Developed by authors at Berea College since 2010

slide-10
SLIDE 10

Background - sudoku.py

◮ http://bitbucket.org/matthew/scipy2010 ◮ Open-source ◮ Developed by authors at Berea College since 2010 ◮ Modeling of Sudoku puzzles in a variety of domains

slide-11
SLIDE 11

Background - sudoku.py

◮ http://bitbucket.org/matthew/scipy2010 ◮ Open-source ◮ Developed by authors at Berea College since 2010 ◮ Modeling of Sudoku puzzles in a variety of domains ◮ Python

slide-12
SLIDE 12

Overview

◮ Modeling Sudoku in Python

slide-13
SLIDE 13

Overview

◮ Modeling Sudoku in Python

◮ Constraint models

slide-14
SLIDE 14

Overview

◮ Modeling Sudoku in Python

◮ Constraint models ◮ Graph models

slide-15
SLIDE 15

Overview

◮ Modeling Sudoku in Python

◮ Constraint models ◮ Graph models ◮ Integer programming models

slide-16
SLIDE 16

Overview

◮ Modeling Sudoku in Python

◮ Constraint models ◮ Graph models ◮ Integer programming models ◮ Polynomial models

slide-17
SLIDE 17

Overview

◮ Modeling Sudoku in Python

◮ Constraint models ◮ Graph models ◮ Integer programming models ◮ Polynomial models

◮ Using sudoku.py

slide-18
SLIDE 18

Overview

◮ Modeling Sudoku in Python

◮ Constraint models ◮ Graph models ◮ Integer programming models ◮ Polynomial models

◮ Using sudoku.py

◮ Creating puzzles

slide-19
SLIDE 19

Overview

◮ Modeling Sudoku in Python

◮ Constraint models ◮ Graph models ◮ Integer programming models ◮ Polynomial models

◮ Using sudoku.py

◮ Creating puzzles ◮ Solving puzzles

slide-20
SLIDE 20

A traditional Sudoku puzzle is a partial assignment of 1, . . . , 9 to the cells of a 9 × 9 grid with the latin property on rows, columns and boxes. 2 5 3 9 1 1 4 4 7 2 8 5 2 9 8 1 4 3 3 6 7 2 7 3 9 3 6 4

slide-21
SLIDE 21

A solution of a Sudoku puzzle is a total assignment which extends the original partial assignment and satisfies the same latin properties. 2 5 8 7 3 6 9 4 1 6 1 9 8 2 4 3 5 7 4 3 7 9 1 5 2 6 8 3 9 5 2 7 1 4 8 6 7 6 2 4 9 8 1 3 5 8 4 1 6 5 3 7 2 9 1 8 4 3 6 9 5 7 2 5 7 6 1 4 2 8 9 3 9 2 3 5 8 7 6 1 4

slide-22
SLIDE 22

A (generalized) Sudoku puzzle

  • f boxsize n is a partial

assignment of 1, . . . , n2 to the cells of an n2 × n2 grid with the latin property on rows, columns and boxes. 2 5 3 9 1 1 4 4 7 2 8 5 2 9 8 1 4 3 3 6 7 2 7 3 9 3 6 4

slide-23
SLIDE 23

Constraint Satisfaction Problems

A constraint satisfaction problem (CSP) is a collection of constraints.

slide-24
SLIDE 24

Constraint Satisfaction Problems

A constraint satisfaction problem (CSP) is a collection of constraints. A constraint restricts the values assigned to certain variables.

slide-25
SLIDE 25

Constraint Satisfaction Problems

A constraint satisfaction problem (CSP) is a collection of constraints. A constraint restricts the values assigned to certain variables. A variable v has an associated domain D(v).

slide-26
SLIDE 26

Constraint Satisfaction Problems

A constraint satisfaction problem (CSP) is a collection of constraints. A constraint restricts the values assigned to certain variables. A variable v has an associated domain D(v). A solution of a CSP is an assignment to the variables which satisfies all the constraints.

slide-27
SLIDE 27

Modeling Sudoku – Variables

For a Sudoku puzzle of boxsize n we have variables xi 1 ≤ i ≤ n4 The domain D(xi) = {1, . . . , n2}. xi = j means that cell i is assigned value j.

slide-28
SLIDE 28

Modeling Sudoku – The AllDifferent constraint

The AllDifferent constraint forces a set of variables to have mutually different values.

slide-29
SLIDE 29

Modeling Sudoku – The AllDifferent constraint

The AllDifferent constraint forces a set of variables to have mutually different values.

slide-30
SLIDE 30

Modeling Sudoku – The AllDifferent constraint

The AllDifferent constraint forces a set of variables to have mutually different values. For example, if n = 2:

slide-31
SLIDE 31

Modeling Sudoku – The AllDifferent constraint

The AllDifferent constraint forces a set of variables to have mutually different values. For example, if n = 2:

◮ Row 1: AllDifferent(x1, x2, x3, x4)

slide-32
SLIDE 32

Modeling Sudoku – The AllDifferent constraint

The AllDifferent constraint forces a set of variables to have mutually different values. For example, if n = 2:

◮ Row 1: AllDifferent(x1, x2, x3, x4) ◮ Column 1: AllDifferent(x1, x5, x9, x13)

slide-33
SLIDE 33

Modeling Sudoku – The AllDifferent constraint

The AllDifferent constraint forces a set of variables to have mutually different values. For example, if n = 2:

◮ Row 1: AllDifferent(x1, x2, x3, x4) ◮ Column 1: AllDifferent(x1, x5, x9, x13) ◮ Box 1: AllDifferent(x1, x2, x5, x6)

slide-34
SLIDE 34

Modeling Sudoku – The ExactSum constraint

The ExactSum constraint restricts the values of variables to have a given sum. So, if x4 = 3, we can use the constraint ExactSum(x4, 3)

slide-35
SLIDE 35

Modeling Sudoku – python-constraint

http://labix.org/python-constraint Developed by Gustavo Niemeyer.

slide-36
SLIDE 36

Modeling Sudoku – python-constraint

http://labix.org/python-constraint Developed by Gustavo Niemeyer. > > > from constraint import Problem > > > from sudoku import cells , symbols

slide-37
SLIDE 37

Modeling Sudoku – python-constraint

http://labix.org/python-constraint Developed by Gustavo Niemeyer. > > > from constraint import Problem > > > from sudoku import cells , symbols > > > cp = Problem() > > > cp. addVariables ( cells (2) , symbols(2))

slide-38
SLIDE 38

Modeling Sudoku – The empty board

> > > from sudoku import \ cells_by_row , cells_by_col , cells_by_box

slide-39
SLIDE 39

Modeling Sudoku – The empty board

> > > from sudoku import \ cells_by_row , cells_by_col , cells_by_box > > > sudoku. cells_by_row (2) [[1 , 2, 3, 4] , [5 , 6, 7, 8] , [9 , 10, 11, 12], [13, 14, 15, 16]]

slide-40
SLIDE 40

Modeling Sudoku – The empty board

> > > from sudoku import \ cells_by_row , cells_by_col , cells_by_box > > > sudoku. cells_by_row (2) [[1 , 2, 3, 4] , [5 , 6, 7, 8] , [9 , 10, 11, 12], [13, 14, 15, 16]] > > > sudoku. cells_by_col (2) [[1 , 5, 9, 13], [2 , 6, 10, 14], [3 , 7, 11, 15], [4 , 8, 12, 16]]

slide-41
SLIDE 41

Modeling Sudoku – The empty board

> > > from sudoku import \ cells_by_row , cells_by_col , cells_by_box > > > sudoku. cells_by_row (2) [[1 , 2, 3, 4] , [5 , 6, 7, 8] , [9 , 10, 11, 12], [13, 14, 15, 16]] > > > sudoku. cells_by_col (2) [[1 , 5, 9, 13], [2 , 6, 10, 14], [3 , 7, 11, 15], [4 , 8, 12, 16]] > > > sudoku. cells_by_box (2) [[1 , 2, 5, 6] , [3 , 4, 7, 8] , [9 , 10, 13, 14], [11, 12, 15, 16]]

slide-42
SLIDE 42

Modeling Sudoku – The empty board

> > > for row in cells_by_row (2): . . .

  • cp. addConstraint ( AllDifferentConstraint () , row)

. . .

slide-43
SLIDE 43

Modeling Sudoku – The empty board

> > > for row in cells_by_row (2): . . .

  • cp. addConstraint ( AllDifferentConstraint () , row)

. . . > > > for col in cells_by_col (2): . . .

  • cp. addConstraint ( AllDifferentConstraint () ,

col ) . . .

slide-44
SLIDE 44

Modeling Sudoku – The empty board

> > > for row in cells_by_row (2): . . .

  • cp. addConstraint ( AllDifferentConstraint () , row)

. . . > > > for col in cells_by_col (2): . . .

  • cp. addConstraint ( AllDifferentConstraint () ,

col ) . . . > > > for box in cells_by_box (2): . . .

  • cp. addConstraint ( AllDifferentConstraint () , box)
slide-45
SLIDE 45

Modeling Sudoku – Puzzles

> > > d = {3: 2, 5: 2, 6: 1, 7: 4, \ 8: 3, 10: 4, 12: 2, 13: 1}

slide-46
SLIDE 46

Modeling Sudoku – Puzzles

> > > d = {3: 2, 5: 2, 6: 1, 7: 4, \ 8: 3, 10: 4, 12: 2, 13: 1} > > > from constraint import ExactSumConstraint as Exact > > > for cell in d: . . .

  • cp. addConstraint (Exact(d[ cell ]) ,

cell )

slide-47
SLIDE 47

Modeling Sudoku – Solving

> > > cp. getSolution () {1: 4, 2: 3, 3: 2, 4: 1, 5: 2, 6: 1, 7: 4, 8: 3, 9: 3, 10: 4, 11: 1, 12: 2, 13: 1, 14: 2, 15: 3, 16: 4}

slide-48
SLIDE 48

Modeling Sudoku – Puzzle objects

> > > from sudoku import Puzzle

slide-49
SLIDE 49

Modeling Sudoku – Puzzle objects

> > > from sudoku import Puzzle > > > Puzzle (cp. getSolution () , 2) +

− − − − −+ − − − − −+

| 4 3 | 2 1 | | 2 1 | 4 3 | +

− − − − −+ − − − − −+

| 3 4 | 1 2 | | 1 2 | 3 4 | +

− − − − −+ − − − − −+

slide-50
SLIDE 50

Modeling Sudoku – The solve function

> > > p = Puzzle (d, 2)

slide-51
SLIDE 51

Modeling Sudoku – The solve function

> > > p = Puzzle (d, 2) > > > from sudoku import solve > > > solve (p) +

− − − − −+ − − − − −+

| 4 3 | 2 1 | | 2 1 | 4 3 | +

− − − − −+ − − − − −+

| 3 4 | 1 2 | | 1 2 | 3 4 | +

− − − − −+ − − − − −+

slide-52
SLIDE 52

Modeling Sudoku – Graph models

◮ J. Gago-Vargas, I. Hartillo-Hermosa, J. Martin-Morales, J. M.

Ucha- Enriquez, Sudokus and Groebner Bases: not only a Divertimento, In: Lecture Notes in Computer Science, vol.

  • 4194. pp. 155-165. 2005
slide-53
SLIDE 53

Modeling Sudoku – Graph models

◮ J. Gago-Vargas, I. Hartillo-Hermosa, J. Martin-Morales, J. M.

Ucha- Enriquez, Sudokus and Groebner Bases: not only a Divertimento, In: Lecture Notes in Computer Science, vol.

  • 4194. pp. 155-165. 2005

◮ The graph model has a node for every cell. T

wo nodes are adjacent in the graph model if they represent dependent cells.

slide-54
SLIDE 54

Modeling Sudoku – Graph models

11 10 13 12 15 14 16 1 3 2 5 4 7 6 9 8

Figure: The Shidoku graph

slide-55
SLIDE 55

Modeling Sudoku – Graph models

Networkx : http://networkx.lanl.gov/ > > > from networkx import Graph > > > g = Graph() > > > g. add_nodes_from( cells (2))

slide-56
SLIDE 56

Modeling Sudoku – Graph models

Networkx : http://networkx.lanl.gov/ > > > from networkx import Graph > > > g = Graph() > > > g. add_nodes_from( cells (2)) > > > from sudoku import dependent_cells > > > g. add_edges_from( dependent_cells (2))

slide-57
SLIDE 57

Modeling Sudoku – Node coloring

> > > for cell in d: . . . g.node[ cell ][ ’ color ’ ] = d[ cell ]

slide-58
SLIDE 58

Modeling Sudoku – Node coloring

> > > for cell in d: . . . g.node[ cell ][ ’ color ’ ] = d[ cell ] > > > from sudoku import node_coloring , n_colors > > > cg = node_coloring (g) > > > n_colors (cg) 6

slide-59
SLIDE 59

Modeling Sudoku – Node coloring

> > > for cell in d: . . . g.node[ cell ][ ’ color ’ ] = d[ cell ] > > > from sudoku import node_coloring , n_colors > > > cg = node_coloring (g) > > > n_colors (cg) 6 > > > from sudoku import graph_to_dict > > > s = Puzzle ( graph_to_dict (cg) , 2) > > > s +

− − − − −+ − − − − −+

| 3 5 | 2 6 | | 2 1 | 4 3 | +

− − − − −+ − − − − −+

| 5 4 | 3 2 | | 1 2 | 5 4 | +

− − − − −+ − − − − −+

slide-60
SLIDE 60

Modeling Sudoku – Further models

Modeling domain Python library Integer Programming pyglpk v0.3 http://tfinley.net/software/pyglpk/ Polynomials sympy http://code.google.com/p/sympy/