solving sudoku puzzles with sat solversblog.tsurumi-u.ac.jp/doc/files/sudokuintint.pdfsolving sudoku...

45
Solving Sudoku Puzzles with SAT Solvers 解決數獨與可滿足性求解器 Jul. 5, 2017 Tanabe Yoshinori Tsurumi University

Upload: others

Post on 20-Oct-2020

6 views

Category:

Documents


2 download

TRANSCRIPT

  • Solving Sudoku Puzzles with SAT Solvers解決數獨與可滿足性求解器

    Jul. 5, 2017Tanabe Yoshinori

    Tsurumi University

  • About Our University

    • Tsurumi University (鶴見大學)– School of Literature (文學系)– School of Dental Medicine (牙科學校)

    – Dept of Early Childhood Care and Education

    – Dept of Dental Hygiene• The number of students = 2,100

    – SoL = 1,400• Located in Yokohama City (橫濱市)

    – The 2nd largest city in Japan– Half an hour from/to Tokyo by

    train• The university is run by Soji-ji (總持

    寺) Temple (Buddhism)

  • About Our Department

    • Department of Library, Archival and Information Studies (But its Japanese name is ドキュメンテーション学科 - Department of Documentation)

    • Three "courses" in the department– Library Study (圖書館學)– Bibliography (目錄學)– Information Study (信息學)

    • About 60 students in a grade, 240 total. Seven professors.

  • About Me

    • Tanabe Yoshinori (田邊良則)• Information Study Course• Research Area

    – Software Engineering (軟件工程)– Computer Science (計算機科學)

  • Sudoku Puzzle

    8 5 7 61 6 9 7

    4 9 34 3 8 2

    3 1 47 5 2 13 6 12 6 9 4

    9 7 8 5

    9 8 5 7 2 3 4 6 11 2 3 4 6 5 9 8 76 4 7 1 8 9 5 2 35 1 4 3 7 8 6 9 28 3 2 9 1 6 7 4 57 6 9 5 4 2 1 3 83 7 8 6 5 4 2 1 92 5 6 8 9 1 3 7 44 9 1 2 3 7 8 5 6

    Problem Solution

    A "pencil puzzle"

  • Sudoku Puzzle

    9 8 5 7 2 3 4 6 11 2 3 4 6 5 9 8 76 4 7 1 8 9 5 2 35 1 4 3 7 8 6 9 28 3 2 9 1 6 7 4 57 6 9 5 4 2 1 3 83 7 8 6 5 4 2 1 92 5 6 8 9 1 3 7 44 9 1 2 3 7 8 5 6

    Rules• Each row should have all

    digits from 1 to 9• Each column should have all

    digits from 1 to 9• Each block (3x3) should have

    all digits from 1 to 9

    • Name "sudoku" comes from 数字は独身に限る(Numbers should be single.)

  • A Small Start ...

    A B C D

    a 2 1b 3c 1d 3 4

    1 1

    22 2

    33

    44 4

  • Ordinary Size

    2 9 64 1 87 4 2 3

    5 31 6 53 6

    1 5 7 46 9 2

    2 8 1

  • 2 9 64 1 87 4 2 3

    5 31 6 53 6

    1 5 7 46 9 2

    2 8 1

    Tactics

    • Tactic 1: Block

    Number "1" should be somewhere in the block.

    1

  • 2 9 64 1 87 4 2 3

    5 31 6 53 6

    1 5 7 46 9 2

    2 8 1

    Tactics

    • Tactic 2: Row or Column

    Number "6" should be somewhere in the column.

    6

  • Tactics

    81

    2

    4 3 65

    9

    • Tactic 3: Cell

    This cell should have one of digits from 1 to 9. All except 7 cannot be there.

    7

  • 17 3 8 4

    52 1

    6

    34 5 6 7 8

    A Quiz

    • Where should "1" go in the block?

    1

    In either case, this row is

    shaded for "1".

    "1" should go either of these

    two cells

  • 17 3 8 4

    52 1

    6

    34 5 6 7 8

    Tactics

    • Tactic 4: In-either-case Theory

    In either case, this row is

    shaded for "1".

    "1" should go either of these

    two cells

  • Other Tactics

    • Tactic 5: Reservation• Tactic 6: Hash-form Theory• ...• ...

  • Sudoku Solver

    • How can we make a Sudoku solver?– A computer program that solves Sudoku puzzles

    • Implementing tactics– We do not know if a given set of tactics is

    sufficient to solve all possible Sudoku puzzles

  • Have you learned ...

    Classical Propositional Logic (經典命題邏輯) ?• Truth Value Table• Tautology (!p || q) → (p → q)• De Morgan's Law

    !(p ∧ q) ↔ !p||!q• Negation/Conjunctive/Disjunctive Normal Form• Natural Deduction• Proof figure

  • SAT

    • SAT (Boolean SATisfiability Problem 布爾可滿足性問題) is another "game"

    • Overview– Two truth values (真值):

    TRUE (真) or FALSE (假)– Three operators (邏輯運算符): ! (NOT), || (OR), && (AND)

    – Task: Find a truth value assignment that makes a given expression TRUE

  • Operators

    p !p

    T FF T

    p q p || q

    T T TT F TF T TF F F

    p q p && q

    T T TT F FF T FF F F

    T ... TrueF ... False

    Black letters are inputRed letters are output

    NOT OR AND

  • Quiz

    TRUE or FALSE?

    1. T || F2. (!F) && T3. (T && F) || ((!T) || T)4. (F || T) &&

    (F || F || T) &&(F || F || F || T) &&(F || F || F || F || T)

  • Variable and Formula

    • A variable (変数) stands for either TRUE or FALSE– p, q, r, ...., p_a, p_b, ...

    • A formula (公式) is a combination of variables and operators. Examples:(1) p_A || !p_B(2) (p_a1 || !p_b) &&

    (p_a1 || p_a2 || !p_b) &&(p_a1 || p_a2 || p_a3 || !p_b)

  • Satisfiability Problem

    • Can you make a given formula TRUE by assigning truth values to variables?

    • Yes, sometimes:– Q1: (!p) && (p || q)– A1: Yes. Let p = FALSE and q = TRUE.

    • but not always.– Q2: p && !p– A2: No, I cannot make the formula TRUE.

    • Boolean Satisfiability Problem (SAT = 可滿足性問題): "For a given formula, judge whether there exists a truth value assignment that makes the formula TRUE, and if yes, give such an assignment."

  • Quiz

    (1) (p || q) && !(p && q)(2) p_A && p_B && !(p_A || p_B)(3)

    (p_Aa_1 || p_Aa_2 || p_Aa_3 || p_Aa_4) && (!(p_Aa_1 && p_Aa_2)) && (!(p_Aa_1 && p_Aa_3)) && (!(p_Aa_1 && p_Aa_4)) && (!(p_Aa_2 && p_Aa_3)) && (!(p_Aa_2 && p_Aa_4)) && (!(p_Aa_3 && p_Aa_4)) && (p_Ab_1 || p_Ab_2 || p_Ab_3 || p_Ab_4) && (!(p_Ab_1 && p_Ab_2)) && (!(p_Ab_1 && p_Ab_3)) && (!(p_Ab_1 && p_Ab_4)) && (!(p_Ab_2 && p_Ab_3)) && (!(p_Ab_2 && p_Ab_4)) && (!(p_Ab_3 && p_Ab_4)) && (p_Ac_1 || p_Ac_2 || p_Ac_3 || p_Ac_4) && (!(p_Ac_1 && p_Ac_2)) && (!(p_Ac_1 && p_Ac_3)) && (!(p_Ac_1 && p_Ac_4)) && (!(p_Ac_2 && p_Ac_3)) && (!(p_Ac_2 && p_Ac_4)) && (!(p_Ac_3 && p_Ac_4)) && (p_Ad_1 || p_Ad_2 || p_Ad_3 || p_Ad_4) && (!(p_Ad_1 && p_Ad_2)) && (!(p_Ad_1 && p_Ad_3)) && (!(p_Ad_1 && p_Ad_4)) && (!(p_Ad_2 && p_Ad_3)) && (!(p_Ad_2 && p_Ad_4)) && (!(p_Ad_3 && p_Ad_4)) && (p_Ba_1 || p_Ba_2 || p_Ba_3 || p_Ba_4) && (!(p_Ba_1 && p_Ba_2)) && (!(p_Ba_1 && p_Ba_3)) && (!(p_Ba_1 && p_Ba_4)) && (!(p_Ba_2 && p_Ba_3)) && (!(p_Ba_2 && p_Ba_4)) && (!(p_Ba_3 && p_Ba_4)) && (p_Bb_1 || p_Bb_2 || p_Bb_3 || p_Bb_4) && (!(p_Bb_1 && p_Bb_2)) && (!(p_Bb_1 && p_Bb_3)) && (!(p_Bb_1 && p_Bb_4)) && (!(p_Bb_2 && p_Bb_3)) && (!(p_Bb_2 && p_Bb_4)) && (!(p_Bb_3 && p_Bb_4)) && (p_Bc_1 || p_Bc_2 || p_Bc_3 || p_Bc_4) && (!(p_Bc_1 && p_Bc_2)) && (!(p_Bc_1 && p_Bc_3)) && (!(p_Bc_1 && p_Bc_4)) && (!(p_Bc_2 && p_Bc_3)) && (!(p_Bc_2 && p_Bc_4)) && (!(p_Bc_3 && p_Bc_4)) && (p_Bd_1 || p_Bd_2 || p_Bd_3 || p_Bd_4) && (!(p_Bd_1 && p_Bd_2)) && (!(p_Bd_1 && p_Bd_3)) && (!(p_Bd_1 && p_Bd_4)) && (!(p_Bd_2 && p_Bd_3)) && (!(p_Bd_2 && p_Bd_4)) && (!(p_Bd_3 && p_Bd_4)) && (p_Ca_1 || p_Ca_2 || p_Ca_3 || p_Ca_4) && (!(p_Ca_1 && p_Ca_2)) && (!(p_Ca_1 && p_Ca_3)) && (!(p_Ca_1 && p_Ca_4)) && (!(p_Ca_2 && p_Ca_3)) && (!(p_Ca_2 && p_Ca_4)) && (!(p_Ca_3 && p_Ca_4)) && (p_Cb_1 || p_Cb_2 || p_Cb_3 || p_Cb_4) && (!(p_Cb_1 && p_Cb_2)) && (!(p_Cb_1 && p_Cb_3)) && (!(p_Cb_1 && p_Cb_4)) && (!(p_Cb_2 && p_Cb_3)) && (!(p_Cb_2 && p_Cb_4)) && (!(p_Cb_3 && p_Cb_4)) && (p_Cc_1 || p_Cc_2 || p_Cc_3 || p_Cc_4) && (!(p_Cc_1 && p_Cc_2)) && (!(p_Cc_1 && p_Cc_3)) && (!(p_Cc_1 && p_Cc_4)) && (!(p_Cc_2 && p_Cc_3)) && (!(p_Cc_2 && p_Cc_4)) && (!(p_Cc_3 && p_Cc_4)) && (p_Cd_1 || p_Cd_2 || p_Cd_3 || p_Cd_4) && (!(p_Cd_1 && p_Cd_2)) && (!(p_Cd_1 && p_Cd_3)) && (!(p_Cd_1 && p_Cd_4)) && (!(p_Cd_2 && p_Cd_3)) && (!(p_Cd_2 && p_Cd_4)) && (!(p_Cd_3 && p_Cd_4)) && (p_Da_1 || p_Da_2 || p_Da_3 || p_Da_4) && (!(p_Da_1 && p_Da_2)) && (!(p_Da_1 && p_Da_3)) && (!(p_Da_1 && p_Da_4)) && (!(p_Da_2 && p_Da_3)) && (!(p_Da_2 && p_Da_4)) && (!(p_Da_3 && p_Da_4)) && (p_Db_1 || p_Db_2 || p_Db_3 || p_Db_4) && (!(p_Db_1 && p_Db_2)) && (!(p_Db_1 && p_Db_3)) && (!(p_Db_1 && p_Db_4)) && (!(p_Db_2 && p_Db_3)) && (!(p_Db_2 && p_Db_4)) && (!(p_Db_3 && p_Db_4)) && (p_Dc_1 || p_Dc_2 || p_Dc_3 || p_Dc_4) && (!(p_Dc_1 && p_Dc_2)) && (!(p_Dc_1 && p_Dc_3)) && (!(p_Dc_1 && p_Dc_4)) && (!(p_Dc_2 && p_Dc_3)) && (!(p_Dc_2 && p_Dc_4)) && (!(p_Dc_3 && p_Dc_4)) && (p_Dd_1 || p_Dd_2 || p_Dd_3 || p_Dd_4) && (!(p_Dd_1 && p_Dd_2)) && (!(p_Dd_1 && p_Dd_3)) && (!(p_Dd_1 && p_Dd_4)) && (!(p_Dd_2 && p_Dd_3)) && (!(p_Dd_2 && p_Dd_4)) && (!(p_Dd_3 && p_Dd_4)) && (p_Aa_1 || p_Ba_1 || p_Ca_1 || p_Da_1) && (p_Aa_2 || p_Ba_2 || p_Ca_2 || p_Da_2) && (p_Aa_3 || p_Ba_3 || p_Ca_3 || p_Da_3) && (p_Aa_4 || p_Ba_4 || p_Ca_4 || p_Da_4) && (p_Ab_1 || p_Bb_1 || p_Cb_1 || p_Db_1) && (p_Ab_2 || p_Bb_2 || p_Cb_2 || p_Db_2) && (p_Ab_3 || p_Bb_3 || p_Cb_3 || p_Db_3) && (p_Ab_4 || p_Bb_4 || p_Cb_4 || p_Db_4) && (p_Ac_1 || p_Bc_1 || p_Cc_1 || p_Dc_1) && (p_Ac_2 || p_Bc_2 || p_Cc_2 || p_Dc_2) && (p_Ac_3 || p_Bc_3 || p_Cc_3 || p_Dc_3) && (p_Ac_4 || p_Bc_4 || p_Cc_4 || p_Dc_4) && (p_Ad_1 || p_Bd_1 || p_Cd_1 || p_Dd_1) && (p_Ad_2 || p_Bd_2 || p_Cd_2 || p_Dd_2) && (p_Ad_3 || p_Bd_3 || p_Cd_3 || p_Dd_3) && (p_Ad_4 || p_Bd_4 || p_Cd_4 || p_Dd_4) && (p_Aa_1 || p_Ab_1 || p_Ac_1 || p_Ad_1) && (p_Aa_2 || p_Ab_2 || p_Ac_2 || p_Ad_2) && (p_Aa_3 || p_Ab_3 || p_Ac_3 || p_Ad_3) && (p_Aa_4 || p_Ab_4 || p_Ac_4 || p_Ad_4) && (p_Ba_1 || p_Bb_1 || p_Bc_1 || p_Bd_1) && (p_Ba_2 || p_Bb_2 || p_Bc_2 || p_Bd_2) && (p_Ba_3 || p_Bb_3 || p_Bc_3 || p_Bd_3) && (p_Ba_4 || p_Bb_4 || p_Bc_4 || p_Bd_4) && (p_Ca_1 || p_Cb_1 || p_Cc_1 || p_Cd_1) && (p_Ca_2 || p_Cb_2 || p_Cc_2 || p_Cd_2) && (p_Ca_3 || p_Cb_3 || p_Cc_3 || p_Cd_3) && (p_Ca_4 || p_Cb_4 || p_Cc_4 || p_Cd_4) && (p_Da_1 || p_Db_1 || p_Dc_1 || p_Dd_1) && (p_Da_2 || p_Db_2 || p_Dc_2 || p_Dd_2) && (p_Da_3 || p_Db_3 || p_Dc_3 || p_Dd_3) && (p_Da_4 || p_Db_4 || p_Dc_4 || p_Dd_4) && (p_Aa_1 || p_Ab_1 || p_Ba_1 || p_Bb_1) && (p_Aa_2 || p_Ab_2 || p_Ba_2 || p_Bb_2) && (p_Aa_3 || p_Ab_3 || p_Ba_3 || p_Bb_3) && (p_Aa_4 || p_Ab_4 || p_Ba_4 || p_Bb_4) && (p_Ac_1 || p_Ad_1 || p_Bc_1 || p_Bd_1) && (p_Ac_2 || p_Ad_2 || p_Bc_2 || p_Bd_2) && (p_Ac_3 || p_Ad_3 || p_Bc_3 || p_Bd_3) && (p_Ac_4 || p_Ad_4 || p_Bc_4 || p_Bd_4) && (p_Ca_1 || p_Cb_1 || p_Da_1 || p_Db_1) && (p_Ca_2 || p_Cb_2 || p_Da_2 || p_Db_2) && (p_Ca_3 || p_Cb_3 || p_Da_3 || p_Db_3) && (p_Ca_4 || p_Cb_4 || p_Da_4 || p_Db_4) && (p_Cc_1 || p_Cd_1 || p_Dc_1 || p_Dd_1) && (p_Cc_2 || p_Cd_2 || p_Dc_2 || p_Dd_2) && (p_Cc_3 || p_Cd_3 || p_Dc_3 || p_Dd_3) && (p_Cc_4 || p_Cd_4 || p_Dc_4 || p_Dd_4) && p_Ca_3 && p_Da_4 && p_Ab_4 && p_Bc_2 && p_Dd_1

  • SAT Solver

    • http://xxx-xxx.xxx/minisat/itl.htmlA great SAT solver (可滿足性求解器) called "Minisat" (http://minisat.se/) is running as the back-end solver.

    • If the outermost operators are &&, they can be omitted by giving newlines.

    p_A && (p_B || !p_A) && (p_C || (p_A && !p_C))

    p_Ap_B || !p_Ap_C || (p_A && !p_C)

  • SAT and SAT solver

    • SAT is known to be "NP-complete". (I.e., it takes very long time to solve when a formula is big.)

    • In these 15-20 years, the performance of Boolean SAT solvers has been improved drastically.

  • SAT Solver Performance Improvement

  • Solving Sudoku by SAT solver

    Sudoku Problem Formula

    Truth ValueAssignmentSudoku Solution

    translation

    SAT solver

    translation

    A B C D

    a 3 4b 4c 2d 1

    A B C D

    a 2 1 3 4b 4 3 1 2c 1 2 4 3d 3 4 2 1

  • Name of Cells

    A B C D

    a

    b

    c

    d

    Bd

    Dc

  • Variables

    p_Aa_1, p_Aa_2, p_Aa_3, p_Aa_4p_Ab_1, p_Ab_2, p_Ab_3, p_Ab_4p_Ac_1, p_Ac_2, p_Ac_3, p_Ac_4p_Ad_1, p_Ad_2, p_Ad_3, p_Ad_4p_Ba_1, p_Ba_2, p_Ba_3, p_Ba_4p_Bb_1, p_Bb_2, p_Bb_3, p_Bb_4p_Bc_1, p_Bc_2, p_Bc_3, p_Bc_4p_Bd_1, p_Bd_2, p_Bd_3, p_Bd_4p_Ca_1, p_Ca_2, p_Ca_3, p_Ca_4p_Cb_1, p_Cb_2, p_Cb_3, p_Cb_4p_Cc_1, p_Cc_2, p_Cc_3, p_Cc_4p_Cd_1, p_Cd_2, p_Cd_3, p_Cd_4p_Da_1, p_Da_2, p_Da_3, p_Da_4p_Db_1, p_Db_2, p_Db_3, p_Db_4p_Dc_1, p_Dc_2, p_Dc_3, p_Dc_4p_Dd_1, p_Dd_2, p_Dd_3, p_Dd_4

    We use the following 64 variables:

    The value of cell Ba is 3

    The value of cell Dc is 2

    A B C D

    a 3b

    c

    d

    A B C D

    a

    b

    c 2d

  • Solution and Variable Values

    A B C D

    a 2 1 3 4b 4 3 1 2c 1 2 4 3d 3 4 2 1

    p_Aa_1, p_Aa_2, p_Aa_3, p_Aa_4p_Ab_1, p_Ab_2, p_Ab_3, p_Ab_4p_Ac_1, p_Ac_2, p_Ac_3, p_Ac_4p_Ad_1, p_Ad_2, p_Ad_3, p_Ad_4p_Ba_1, p_Ba_2, p_Ba_3, p_Ba_4p_Bb_1, p_Bb_2, p_Bb_3, p_Bb_4p_Bc_1, p_Bc_2, p_Bc_3, p_Bc_4p_Bd_1, p_Bd_2, p_Bd_3, p_Bd_4p_Ca_1, p_Ca_2, p_Ca_3, p_Ca_4p_Cb_1, p_Cb_2, p_Cb_3, p_Cb_4p_Cc_1, p_Cc_2, p_Cc_3, p_Cc_4p_Cd_1, p_Cd_2, p_Cd_3, p_Cd_4p_Da_1, p_Da_2, p_Da_3, p_Da_4p_Db_1, p_Db_2, p_Db_3, p_Db_4p_Dc_1, p_Dc_2, p_Dc_3, p_Dc_4p_Dd_1, p_Dd_2, p_Dd_3, p_Dd_4

    TRUE FALSE

  • Solving Sudoku by SAT solver

    Sudoku Problem Formula

    Truth ValueAssignmentSudoku Solution

    translation

    SAT solver

    translation

    A B C D

    a 3 4b 4c 2d 1

    A B C D

    a 2 1 3 4b 4 3 1 2c 1 2 4 3d 3 4 2 1

  • Initial Configuration

    p_Ca_3p_Da_4p_Ab_4p_Bc_2p_Dd_1

    A B C D

    a 3 4b 4c 2d 1

  • Translating Sudoku Rules

    1. Each row has all digits.

    2. Each column has all digits.

    3. Each block has all digits.

  • Translation

    1. Each row has all digits.E.g. Row "a" (Aa - Ba - Ca - Da) has all digits.i.e., it has digit 1:

    p_Aa_1 || p_Ba_1 || p_Ca_1 || p_Da_1

    and it has digit 2:p_Aa_2 || p_Ba_2 || p_Ca_2 || p_Da_2

    and 3:p_Aa_3 || p_Ba_3 || p_Ca_3 || p_Da_3

    and 4:p_Aa_4 || p_Ba_4 || p_Ca_4 || p_Da_4

    We can build similar formulae for rows "b", "c" and "d".

    A B C D

    a

    b

    c

    d

  • Translation

    • Similar formulae for columns, e.g.p_Aa_1 || p_Ab_1 || p_Ac_1 || p_Ad_1

    • ... and blocks, e.g.p_Aa_1 || p_Ab_1 || p_Ba_1 || p_Bb_1

  • Translation• We have translated the problem and all the Sudoku rules

    into formulae. • Complete? ..., not yet.• The formula set defined so far would yield:

    p_Aa_1, p_Aa_2, p_Aa_3, p_Aa_4p_Ab_1, p_Ab_2, p_Ab_3, p_Ab_4p_Ac_1, p_Ac_2, p_Ac_3, p_Ac_4p_Ad_1, p_Ad_2, p_Ad_3, p_Ad_4p_Ba_1, p_Ba_2, p_Ba_3, p_Ba_4p_Bb_1, p_Bb_2, p_Bb_3, p_Bb_4p_Bc_1, p_Bc_2, p_Bc_3, p_Bc_4p_Bd_1, p_Bd_2, p_Bd_3, p_Bd_4p_Ca_1, p_Ca_2, p_Ca_3, p_Ca_4p_Cb_1, p_Cb_2, p_Cb_3, p_Cb_4p_Cc_1, p_Cc_2, p_Cc_3, p_Cc_4p_Cd_1, p_Cd_2, p_Cd_3, p_Cd_4p_Da_1, p_Da_2, p_Da_3, p_Da_4p_Db_1, p_Db_2, p_Db_3, p_Db_4p_Dc_1, p_Dc_2, p_Dc_3, p_Dc_4p_Dd_1, p_Dd_2, p_Dd_3, p_Dd_4

    TRUE FALSE

  • Implicit Condition

    Each cell has one and only one digit.E.g. Variables for cell Aa:

    p_Aa_1 p_Aa_2 p_Aa_3 p_Aa_4

    One of them must be true:p_Aa_1 || p_Aa_2 || p_Aa_3 || p_Aa_4

    ... and only one of them can be true.!(p_Aa_1 && p_Aa_2)!(p_Aa_1 && p_Aa_3)!(p_Aa_1 && p_Aa_4)!(p_Aa_2 && p_Aa_3)!(p_Aa_2 && p_Aa_4)!(p_Aa_3 && p_Aa_4)

    We can build similar formulae for cells Ab, Ac, ... and Dd.

  • The Formula(p_Aa_1 || p_Aa_2 || p_Aa_3 || p_Aa_4)

    && (!(p_Aa_1 && p_Aa_2))&& (!(p_Aa_1 && p_Aa_3))&& (!(p_Aa_1 && p_Aa_4))&& (!(p_Aa_2 && p_Aa_3))&& (!(p_Aa_2 && p_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_4))&& (!(p_Bc_3 && p_Bc_4))&& (p_Bd_1 || p_Bd_2 || p_Bd_3 || p_Bd_4)&& (!(p_Bd_1 && p_Bd_2))&& (!(p_Bd_1 && p_Bd_3))&& (!(p_Bd_1 && p_Bd_4))&& (!(p_Bd_2 && p_Bd_3))&& (!(p_Bd_2 && p_Bd_4))&& (!(p_Bd_3 && p_Bd_4))&& (p_Ca_1 || p_Ca_2 || p_Ca_3 || p_Ca_4)&& (!(p_Ca_1 && p_Ca_2))&& (!(p_Ca_1 && p_Ca_3))&& (!(p_Ca_1 && p_Ca_4))&& (!(p_Ca_2 && p_Ca_3))&& (!(p_Ca_2 && p_Ca_4))&& (!(p_Ca_3 && p_Ca_4))&& (p_Cb_1 || p_Cb_2 || p_Cb_3 || p_Cb_4)&& (!(p_Cb_1 && p_Cb_2))&& (!(p_Cb_1 && p_Cb_3))&& (!(p_Cb_1 && p_Cb_4))&& (!(p_Cb_2 && p_Cb_3))&& (!(p_Cb_2 && p_Cb_4))&& (!(p_Cb_3 && p_Cb_4))&& (p_Cc_1 || p_Cc_2 || p_Cc_3 || p_Cc_4)&& (!(p_Cc_1 && p_Cc_2))&& (!(p_Cc_1 && p_Cc_3))&& (!(p_Cc_1 && p_Cc_4))&& (!(p_Cc_2 && p_Cc_3))&& (!(p_Cc_2 && p_Cc_4))&& (!(p_Cc_3 && p_Cc_4))&& (p_Cd_1 || p_Cd_2 || p_Cd_3 || p_Cd_4)&& (!(p_Cd_1 && p_Cd_2))&& (!(p_Cd_1 && p_Cd_3))&& (!(p_Cd_1 && p_Cd_4))&& (!(p_Cd_2 && p_Cd_3))&& (!(p_Cd_2 && p_Cd_4))&& (!(p_Cd_3 && p_Cd_4))

    && (p_Da_1 || p_Da_2 || p_Da_3 || p_Da_4)&& (!(p_Da_1 && p_Da_2))&& (!(p_Da_1 && p_Da_3))&& (!(p_Da_1 && p_Da_4))&& (!(p_Da_2 && p_Da_3))&& (!(p_Da_2 && p_Da_4))&& (!(p_Da_3 && p_Da_4))&& (p_Db_1 || p_Db_2 || p_Db_3 || p_Db_4)&& (!(p_Db_1 && p_Db_2))&& (!(p_Db_1 && p_Db_3))&& (!(p_Db_1 && p_Db_4))&& (!(p_Db_2 && p_Db_3))&& (!(p_Db_2 && p_Db_4))&& (!(p_Db_3 && p_Db_4))&& (p_Dc_1 || p_Dc_2 || p_Dc_3 || p_Dc_4)&& (!(p_Dc_1 && p_Dc_2))&& (!(p_Dc_1 && p_Dc_3))&& (!(p_Dc_1 && p_Dc_4))&& (!(p_Dc_2 && p_Dc_3))&& (!(p_Dc_2 && p_Dc_4))&& (!(p_Dc_3 && p_Dc_4))&& (p_Dd_1 || p_Dd_2 || p_Dd_3 || p_Dd_4)&& (!(p_Dd_1 && p_Dd_2))&& (!(p_Dd_1 && p_Dd_3))&& (!(p_Dd_1 && p_Dd_4))&& (!(p_Dd_2 && p_Dd_3))&& (!(p_Dd_2 && p_Dd_4))&& (!(p_Dd_3 && p_Dd_4))&& (p_Aa_1 || p_Ba_1 || p_Ca_1 || p_Da_1)&& (p_Aa_2 || p_Ba_2 || p_Ca_2 || p_Da_2)&& (p_Aa_3 || p_Ba_3 || p_Ca_3 || p_Da_3)&& (p_Aa_4 || p_Ba_4 || p_Ca_4 || p_Da_4)&& (p_Ab_1 || p_Bb_1 || p_Cb_1 || p_Db_1)&& (p_Ab_2 || p_Bb_2 || p_Cb_2 || p_Db_2)&& (p_Ab_3 || p_Bb_3 || p_Cb_3 || p_Db_3)&& (p_Ab_4 || p_Bb_4 || p_Cb_4 || p_Db_4)&& (p_Ac_1 || p_Bc_1 || p_Cc_1 || p_Dc_1)&& (p_Ac_2 || p_Bc_2 || p_Cc_2 || p_Dc_2)&& (p_Ac_3 || p_Bc_3 || p_Cc_3 || p_Dc_3)&& (p_Ac_4 || p_Bc_4 || p_Cc_4 || p_Dc_4)&& (p_Ad_1 || p_Bd_1 || p_Cd_1 || p_Dd_1)&& (p_Ad_2 || p_Bd_2 || p_Cd_2 || p_Dd_2)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ca_2 || p_Cb_2 || p_Da_2 || p_Db_2)&& (p_Ca_3 || p_Cb_3 || p_Da_3 || p_Db_3)&& (p_Ca_4 || p_Cb_4 || p_Da_4 || p_Db_4)&& (p_Cc_1 || p_Cd_1 || p_Dc_1 || p_Dd_1)&& (p_Cc_2 || p_Cd_2 || p_Dc_2 || p_Dd_2)&& (p_Cc_3 || p_Cd_3 || p_Dc_3 || p_Dd_3)&& (p_Cc_4 || p_Cd_4 || p_Dc_4 || p_Dd_4)&& p_Ca_3&& p_Da_4&& p_Ab_4&& p_Bc_2&& p_Dd_1

    downloadable from http://dcm-trm.net/minisat/

    Initial Configuration&& p_Ca_3&& p_Da_4&& p_Ab_4&& p_Bc_2&& p_Dd_1

    Row "a" has all four digits&& (p_Aa_1 || p_Ba_1 || p_Ca_1 || p_Da_1)&& (p_Aa_2 || p_Ba_2 || p_Ca_2 || p_Da_2)&& (p_Aa_3 || p_Ba_3 || p_Ca_3 || p_Da_3)&& (p_Aa_4 || p_Ba_4 || p_Ca_4 || p_Da_4)

    Cell "Ab" has one and only one digit.&& (p_Ab_1 || p_Ab_2 || p_Ab_3 || p_Ab_4)&& (!(p_Ab_1 && p_Ab_2))&& (!(p_Ab_1 && p_Ab_3))&& (!(p_Ab_1 && p_Ab_4))&& (!(p_Ab_2 && p_Ab_3))&& (!(p_Ab_2 && p_Ab_4))&& (!(p_Ab_3 && p_Ab_4))

    Cell "Aa" has one and only one digit.(p_Aa_1 || p_Aa_2 || p_Aa_3 || p_Aa_4)

    && (!(p_Aa_1 && p_Aa_2))&& (!(p_Aa_1 && p_Aa_3))&& (!(p_Aa_1 && p_Aa_4))&& (!(p_Aa_2 && p_Aa_3))&& (!(p_Aa_2 && p_Aa_4))&& (!(p_Aa_3 && p_Aa_4))

  • Results

    (See Demo)

  • A Program to Generate (the Fixed Part of) the Formulaimport Data.List

    size :: Intsize = 2

    allConstraints :: [String]allConstraints =

    [oneOf [i] [j] vRange | i

  • Application of SAT Solvers

    Can be used to solve CSPs (= Constraint Satisfaction Problems)• Planning• Scheduling• Resource Allocation• Time Table in SchoolsAlso used in many other tools as a backend engine• Software Verification• Analysis of Genetic Network• Design Automation• Crypt-analysis

  • Summary

    • The Boolean SAT problem is to decide whether there is an assignment to make a formula to be TRUE.

    • The performance of SAT solvers have been much improved. They are now used to solve complex problems.

    • Sudoku puzzles can be solved by using SAT solvers.

  • If you are interested in SAT solvers, ...

    and want to know them more deeply:• Learn Classical Propositional Logic, in the first place

    – Truth values, tautologies, syntax, semantics, negation/conjunctive/disjunctive normal forms

    • Maybe that is sufficient. You do NOT need further logical background such as the predicate logic, proof figure, completeness theorem, etc.

    • There is one big reference book (about 1000 pages) for SAT problem/solver, that covers from theory and algorithms to applications to extensions:– Biere et.al. (eds): Handbook of Satisfiability. IOS Press,

    2009.

  • If you are interested in puzzles, ...

    and want to apply a SAT solver to them:• Logic Puzzles page of Wikipedia (English).

    – A lot of puzzle types are listed.• Nikoli (http://www.nikoli.com/)

    – A Japanese company specialized in "pencil puzzles"

    – It has popularized the name of Sudoku.– Rules and strategies are explained in English on

    the web site.

  • ExercisesSolve the following, only if you are interested, of course :-)1. Try to solve another 4x4 mini sudoku puzzle. You will find

    sudoku problems by conducting a web search with keywords "sudoku" and "4x4".

    2. Try to solve a 9x9 sudoku puzzle. Again, there are lots of sudoku problems on the web.

    3. How can you use a SAT solver to check whether a given sudoku problem has two or more solutions?

    4. (If you are a programmer,) write an automatic sudoku solver. You can download Minisat from the web, and invoke it from your program. You may need to learn what CNF is to understand the input for Minisat (and most of other Boolean SAT solvers).

    5. (If you are a puzzle fan,) express other puzzles as Boolean SAT problems. Try Nonogram (easy) and Slitherlink (difficult) for example.

  • Questions?

    Solving Sudoku Puzzles with SAT Solvers�解決數獨與可滿足性求解器About Our UniversityAbout Our DepartmentAbout MeSudoku PuzzleSudoku PuzzleA Small Start ...Ordinary SizeTacticsTacticsTacticsA QuizTacticsOther TacticsSudoku SolverHave you learned ...SATOperatorsQuizVariable and FormulaSatisfiability ProblemQuizSAT SolverSAT and SAT solverSAT Solver Performance ImprovementSolving Sudoku by SAT solverName of CellsVariablesSolution and Variable ValuesSolving Sudoku by SAT solverInitial ConfigurationTranslating Sudoku RulesTranslationTranslationTranslationImplicit ConditionThe FormulaResultsA Program to Generate (the Fixed Part of) the FormulaApplication of SAT SolversSummaryIf you are interested in SAT solvers, ...If you are interested in puzzles, ...Exercisesスライド番号 46