SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
Sudoku solving with CVXOPT




                        Sudoku solving with CVXOPT

                                     Ben Moran
                             http://transnite.wordpress.com

                                 09 December 2009
Sudoku solving with CVXOPT
  Background




      1 Background



      2 Sudoku as matrix and vectors



      3 Sparsity and solving



      4 Conclusion
Sudoku solving with CVXOPT
  Background
Sudoku solving with CVXOPT
  Background



Based on. . .


              a paper by Babu, Pelckmans, Stoica:
                   Linear Systems, Sparse Solutions, and Sudoku   1

              Solve Sudoku by turning it into a linear programming problem,
              inspired by new signal processing paradigm: compressed
              sensing 2
              I thought it was interesting and reimplemented it in Python
              with CVXOPT



         1
             http://www.it.uu.se/katalog/praba420/Sudoku.pdf
         2
             http://nuit-blanche.blogspot.com/
Sudoku solving with CVXOPT
  Background



Why?




            Ben Laurie - Sudoku is a   denial of service attack on human

            intellect

            It's a solved problem!
Sudoku solving with CVXOPT
  Background



Existing solutions


             Knuth's Dancing Links (DLX)     3

             Norvig's constraint propagation (also Python)    4

             Sinkhorn method 5
             And many others




         3
           http://en.wikipedia.org/wiki/Dancing_Links
         4
           http://norvig.com/sudoku.html
         5
           http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?isnumber=4802290arnumber=4804
Sudoku solving with CVXOPT
  Background



But. . .


            This is still an interesting way into the brand new eld of
            compressed sensing
            And what have Sudoku got to do with. . .
                  signal processing
                  sensor networks
                  single-pixel cameras
                  satellite imaging?
            Our solution will be interesting, as the solver won't know the
            rules of Sudoku at all!
Sudoku solving with CVXOPT
  Sudoku as matrix and vectors



What are linear systems?

             Matrix - Vector multiplication

                                 1 3           2         11
                                                    =
                                 4 −1          3          5
                                          Ax   =b
             In Python:
             import numpy
             numpy . m a t r i x ( ' 1 3 ; 4 −1 ' ) ∗ 
                   numpy . m a t r i x ( ' 2 ; 3 ' )
            matrix ( [ [ 1 1 ] ,
                       [ 5]])
Sudoku solving with CVXOPT
  Sudoku as matrix and vectors



Solving a linear system


             Given
                    1 3                          11
             A   =                and   b   =          , what is x?
                    4 −1                          5
             In Python:
             import numpy
             numpy . l i n a l g . s o l v e ( numpy . m a t r i x ( ' 1 3 ; 4 −1 ' ) ,
                                                  numpy . m a t r i x ( ' 1 1 ; 5 ' ) )
            matrix ( [ [ 2 . ] ,
                       [ 3.]])
Sudoku solving with CVXOPT
  Sudoku as matrix and vectors



Sudoku board as an indicator vector
             We can turn a 9x9 Sudoku board into a single vector with
             9x9x9 = 729 elements
             Each 9 entries corresponds to one cell, most are zero
             Put a 1 in the rst place for 1, in the second place for 2. . .
             p = Problem (  1 2 . .  ,N=2)
             numpy . m a t r i x ( p . t o _ i n d i c a t o r _ v e c t o r ( ) , ' i ' )
            matrix ( [ [ 1 ] ,
                       [ 0 ],
                       [ 0 ],
                       [ 1 ],
                       [ 0 ],
                       [ 0 ],
                       [ 0 ],
                       [ 0 ]])
Sudoku solving with CVXOPT
  Sudoku as matrix and vectors



Sudoku rules as a matrix system
             Now we can set up a special matrix to enforce the rules of
             Sudoku
             It will have one row for each constraint, and 9x9x9 = 729
             columns
             When you multiply this with the indicator vector, you

             get all 1's if the board is valid

             Here are some of the rules for 2x2 Sudoku:
             p = Problem (  1 2 . .  ,N=2)
             numpy . m a t r i x ( p . m a t r i x ( ) )
            matrix ( [ [ 1 , 1 , 0 , 0 , 0 ,                  0,   0,   0.]   ,
                       [ 0, 0, 1, 1, 0,                       0,   0,   0.]   ,
                       [ 0, 0, 0, 0, 1,                       1,   0,   0.]   ,
                       [ 0, 0, 0, 0, 0,                       0,   1,   1.]   ,
                       [ 1, 0, 1, 0, 0,                       0,   0,   0.]   ,
                                                  ...
                       [ 0, 0, 0, 1, 0,                       0,   0,   0.]])
Sudoku solving with CVXOPT
  Sudoku as matrix and vectors



Rules for 2x2 Sudoku

                                 1 2
                                 _ _
Sudoku solving with CVXOPT
  Sudoku as matrix and vectors



                             _   _   _   1   5   _    _   7    _
                                                                  
                            1   _   6   _   _   _    8   2    _   
                             3   _   _   8   6   _    _   4    _
                                                                  
                                                                  
                             9   _   _   4   _   _    5   6    7
                                                                  
                                                                  
                             _   _   4   7   _   8    3   _    _
                                                                  
                                                                  
                             7   3   2   _   _   6    _   _    4
                                                                  
                                                                  
                             _   4   _   _   8   1    _   _    9
                                                                  
                                                                  
                                                                  
                            _   1   7   _   _   _    2   _    8   
                             _   5   _   _   3   7    _   _    _
                                 Figure: Full size 9x9 board
Sudoku solving with CVXOPT
  Sudoku as matrix and vectors




                                 Figure: Full size 9x9 rules matrix
Sudoku solving with CVXOPT
  Sparsity and solving



What do we gain by writing Sudoku like that?


            We can check solutions with a matrix-vector multiply
                  Turn the proposed solution into an indicator vector
                  Multiply it with the the rules matrix
                  If the solution is valid, the answer will be all ones
            Solve Ax = [1 1 1 1 1] to nd the answer?
            . . . No - there is an innite number of answers, most won't
            give valid boards
            Need something else to nd our true correct answer
Sudoku solving with CVXOPT
  Sparsity and solving



Sparsity

            The true solution if it exists will be the   sparsest

            (Sparse vector == mostly zeros)
                                        0                        3
                                                                   
                                       1                      1    
                                        0                       0.1
                                                                   
                                                                   
                             Sparse    0    vs non-sparse     4
                                                                   
                                                                      
                                        0                       −2
                                                                   
                                                                   
                                        1                        1
                                                                   
                                                                   
                                        0                       .5
            That still doesn't help. . . no ecient method to solve that
            directly
Sudoku solving with CVXOPT
  Sparsity and solving



Dierent kinds of distance: L-2 norm, Pythagoras

            You measure the length of a vector with a              norm

            This is an       L-p   norm (for a 3d vector):

                                      (|x1 |p + |x2 |p + |x3 |p )1/p
            If you plug in p=2, you get normal Euclidean distance
            (Pythagoras' theorem):

                                         (|x1 |2 + |x2 |2 + |x3 |2 )
            We can choose the solution with the smallest L2-norm. . .
Sudoku solving with CVXOPT
  Sparsity and solving



Dierent kinds of distance: L-2 norm, Pythagoras
            But we don't get a valid answer
           # Least squares s o l u t i o n
            M = numpy . m a t r i x ( s a m p l e ( ) . m a t r i x ( ) )
            o n e s =numpy . o n e s ( (M. s h a p e [ 0 ] , 1 ) )
            v = numpy . l i n a l g . p i n v (M) ∗ o n e s
            (M∗ v ) [ : 4 ]
           matrix ( [ [ 1 . ] ,
                       [ 1.] ,
                       [ 1.] ,
                       [ 1.]])
            v [ : 4 ]
           matrix ([[ −0.0735114 ] ,
                       [ 0.24178992] ,
                       [ 0.00310795] ,
                       [ 0.28139045]])
Sudoku solving with CVXOPT
  Sparsity and solving



Dierent kinds of distance: L-0 norm, sparsity



            Plug in   p=0    and you get L0, sparsity (number of non-zeros):

                                     |x1 |0 + |x2 |0 + |x3 |0
            The right solution    will   have the lowest L0 norm
            But we've no way of nding it, besides solving the Sudoku!
Sudoku solving with CVXOPT
  Sparsity and solving



Dierent kinds of distance: L-1 norm
            With   p=1       you get the absolute sum, taxicab distance

                                       |x1 |1 + |x2 |1 + |x3 |1
Sudoku solving with CVXOPT
  Sparsity and solving



Why is L-1 important?



            There are ecient methods of solving it (linear programming)
            And for many matrices, the minimum L1 norm solution turns
            out to also minimize L0
            (Smallest L1 then nds the sparsest solution)
            This insight is central to the compressed sensing revolution
Sudoku solving with CVXOPT
  Sparsity and solving



Python - CVXOPT


              Now we can install    python-cvxopt
                                                    6   and solve the problem!
              CVXOPT is a GPL library for optimization
                   linear programming
                   Other kinds of convex optimization
              We can use it to nd the solution x of Ax=b, with the smallest
              L1-norm
              The result comes out as a binary vector of 0s and 1s (without
              being constrained to do so!)



         6
             http://abel.ee.ucla.edu/cvxopt/
Sudoku solving with CVXOPT
  Sparsity and solving



Python - CVXOPT
            After all that work to set up the question, the answer is one
            line!
            h e l p c v x o p t . s o l v e r s . l p
           Help on f u n c t i o n l p i n module c v x o p t . c o n e p r o g :

            l p ( c , G , h , A=None , b=None , s o l v e r=None , . . . )
                    S o l v e s a p a i r o f p r i m a l and d u a l LPs
                            minimize          c ' ∗x
                            s u b j e c t t o G∗ x + s = h
                                              A∗ x = b
                                              s = 0
            ...

           # S o l v e w i t h CVXOPT
            c v x o p t . s o l v e r s . l p ( c0 , G , hh )
Sudoku solving with CVXOPT
  Conclusion



Caveats


            This won't solve every Sudoku - it tends to solve the easier
            ones
            It fails when the L1 minimum doesn't nd the L0 minimum
            There are other methods, from the compressed sensing
            literature, like iterative reweighting, that solve more
            For Sudoku, which puzzles are hard  why is still an open
            problem at the frontiers of research
Sudoku solving with CVXOPT
  Conclusion



Compressed sensing

              Very many important, real world signals are sparse: images,
              sounds, gene expressions
              Instead of capturing the raw signal and compressing. . .
              . . . compressed sensing means you can capture them
              pre-compressed, with fewer measurements
                   (below the Nyquist rate: 44.1kHz CDs to play back 22kHz
                   frequencies)
              Less hardware needed will mean better images or cheaper kit
              Single-pixel camera   7



         7
             http://dsp.rice.edu/cscamera
Sudoku solving with CVXOPT
  Conclusion



Code, links and slides



            Code is at http://github.com/benmoran/L1-Sudoku/
            Sporadic blog: http://transnite.wordpress.com
            Find CVXOPT at
            http://abel.ee.ucla.edu/cvxopt/examples/index.html
            And the nice CVXMOD wrapper at http://cvxmod.net

Contenu connexe

Tendances

Lesson 1: Functions and their representations (slides)
Lesson 1: Functions and their representations (slides)Lesson 1: Functions and their representations (slides)
Lesson 1: Functions and their representations (slides)Matthew Leingang
 
Protein Secondary Structure Prediction using Deep Learning methods
Protein Secondary Structure Prediction using Deep Learning methodsProtein Secondary Structure Prediction using Deep Learning methods
Protein Secondary Structure Prediction using Deep Learning methodsChrysoula Kosma
 
A fusion of soft expert set and matrix models
A fusion of soft expert set and matrix modelsA fusion of soft expert set and matrix models
A fusion of soft expert set and matrix modelseSAT Journals
 
Unification and Refactoring of Clones
Unification and Refactoring of ClonesUnification and Refactoring of Clones
Unification and Refactoring of ClonesNikolaos Tsantalis
 
Int Math 2 Section 6-6 1011
Int Math 2 Section 6-6 1011Int Math 2 Section 6-6 1011
Int Math 2 Section 6-6 1011Jimbo Lamb
 
Integrated Math 2 Section 6-6
Integrated Math 2 Section 6-6Integrated Math 2 Section 6-6
Integrated Math 2 Section 6-6Jimbo Lamb
 
RNN sharing at Trend Micro
RNN sharing at Trend MicroRNN sharing at Trend Micro
RNN sharing at Trend MicroChun Hao Wang
 

Tendances (16)

Lesson 1: Functions and their representations (slides)
Lesson 1: Functions and their representations (slides)Lesson 1: Functions and their representations (slides)
Lesson 1: Functions and their representations (slides)
 
Derivadas
DerivadasDerivadas
Derivadas
 
D0111720
D0111720D0111720
D0111720
 
Protein Secondary Structure Prediction using Deep Learning methods
Protein Secondary Structure Prediction using Deep Learning methodsProtein Secondary Structure Prediction using Deep Learning methods
Protein Secondary Structure Prediction using Deep Learning methods
 
A fusion of soft expert set and matrix models
A fusion of soft expert set and matrix modelsA fusion of soft expert set and matrix models
A fusion of soft expert set and matrix models
 
Logic gates
Logic gatesLogic gates
Logic gates
 
Unification and Refactoring of Clones
Unification and Refactoring of ClonesUnification and Refactoring of Clones
Unification and Refactoring of Clones
 
Sect1 6
Sect1 6Sect1 6
Sect1 6
 
C0531519
C0531519C0531519
C0531519
 
Ch1 2 (2)
Ch1 2 (2)Ch1 2 (2)
Ch1 2 (2)
 
08 - Complexity
08 - Complexity08 - Complexity
08 - Complexity
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
 
Int Math 2 Section 6-6 1011
Int Math 2 Section 6-6 1011Int Math 2 Section 6-6 1011
Int Math 2 Section 6-6 1011
 
Integrated Math 2 Section 6-6
Integrated Math 2 Section 6-6Integrated Math 2 Section 6-6
Integrated Math 2 Section 6-6
 
RNN sharing at Trend Micro
RNN sharing at Trend MicroRNN sharing at Trend Micro
RNN sharing at Trend Micro
 
Semi-Magic Squares From Snake-Shaped Matrices
Semi-Magic Squares From Snake-Shaped MatricesSemi-Magic Squares From Snake-Shaped Matrices
Semi-Magic Squares From Snake-Shaped Matrices
 

En vedette

Sudoku powerpoint
Sudoku powerpointSudoku powerpoint
Sudoku powerpointunion40
 
Presentation - Sudoku Assignment
Presentation - Sudoku  AssignmentPresentation - Sudoku  Assignment
Presentation - Sudoku AssignmentCj Uni
 
Intelligent Sudoku Solver
Intelligent Sudoku SolverIntelligent Sudoku Solver
Intelligent Sudoku SolverAmrish Jhaveri
 
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์Thira Woratanarat
 
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9ทอง บุญยศ
 
Clinical Practice Guideline for Dementia 2008
Clinical Practice Guideline for Dementia 2008Clinical Practice Guideline for Dementia 2008
Clinical Practice Guideline for Dementia 2008Utai Sukviwatsirikul
 
ซูโดกุ สสวท
ซูโดกุ สสวทซูโดกุ สสวท
ซูโดกุ สสวทNat Krub
 
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557Utai Sukviwatsirikul
 
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพการทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพThira Woratanarat
 
Sudoku Solving with Computational Intelligence
Sudoku Solving with Computational IntelligenceSudoku Solving with Computational Intelligence
Sudoku Solving with Computational Intelligenceharaldhiss
 
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานีคู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานีtopsaby99
 
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษรซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษรPawaputanon Mahasarakham
 
กายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthmaกายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthmaSureerut Physiotherapist
 

En vedette (20)

Sudoku
SudokuSudoku
Sudoku
 
Sudoku powerpoint
Sudoku powerpointSudoku powerpoint
Sudoku powerpoint
 
Sudoku
SudokuSudoku
Sudoku
 
Sudoku ppt
Sudoku pptSudoku ppt
Sudoku ppt
 
Presentation - Sudoku Assignment
Presentation - Sudoku  AssignmentPresentation - Sudoku  Assignment
Presentation - Sudoku Assignment
 
Sudoku
SudokuSudoku
Sudoku
 
Intelligent Sudoku Solver
Intelligent Sudoku SolverIntelligent Sudoku Solver
Intelligent Sudoku Solver
 
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
บทเรียนการพัฒนาระบบเครือข่ายบริการทางการแพทย์
 
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
การดูแลสุขภาพระยะยาวเขตบริการสุขภาพที่ 9
 
Sudoku puzzles
Sudoku puzzlesSudoku puzzles
Sudoku puzzles
 
Clinical Practice Guideline for Dementia 2008
Clinical Practice Guideline for Dementia 2008Clinical Practice Guideline for Dementia 2008
Clinical Practice Guideline for Dementia 2008
 
Sudoku valencia 2
Sudoku valencia  2Sudoku valencia  2
Sudoku valencia 2
 
ซูโดกุ สสวท
ซูโดกุ สสวทซูโดกุ สสวท
ซูโดกุ สสวท
 
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
แนวทางเวชปฏิบัติภาวะสมองเสื่อม 2557
 
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพการทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
การทบทวนสถานการณ์และกลไกจัดการความแตกฉานด้านสุขภาพ
 
Sudoku Solving with Computational Intelligence
Sudoku Solving with Computational IntelligenceSudoku Solving with Computational Intelligence
Sudoku Solving with Computational Intelligence
 
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานีคู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
คู่มือ การสื่อสารกับผู้ป่วยชาวต่างชาติ โรงพยาบาลนพรัตนราชธานี
 
Cpg copd
Cpg copdCpg copd
Cpg copd
 
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษรซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
ซูโดกุขนาด 9x9 ช่อง แบบตัวอักษร
 
กายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthmaกายภาพบำบัดในผู้ป่วยAsthma
กายภาพบำบัดในผู้ป่วยAsthma
 

Similaire à L1 Sudoku

Thesis writing - week9
Thesis writing - week9Thesis writing - week9
Thesis writing - week9s1160123
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
DeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningDeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningMasahiro Sakai
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In RubyRoss Lawley
 
Hierarchical matrix techniques for maximum likelihood covariance estimation
Hierarchical matrix techniques for maximum likelihood covariance estimationHierarchical matrix techniques for maximum likelihood covariance estimation
Hierarchical matrix techniques for maximum likelihood covariance estimationAlexander Litvinenko
 
3D Math Primer: CocoaConf Atlanta
3D Math Primer: CocoaConf Atlanta3D Math Primer: CocoaConf Atlanta
3D Math Primer: CocoaConf AtlantaJanie Clayton
 
There are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docxThere are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docxrelaine1
 
Rpartii 131126003007-phpapp01
Rpartii 131126003007-phpapp01Rpartii 131126003007-phpapp01
Rpartii 131126003007-phpapp01Sunil0108
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notesInfinity Tech Solutions
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabkrishna_093
 

Similaire à L1 Sudoku (20)

19 Jo P July 08
19 Jo P July 0819 Jo P July 08
19 Jo P July 08
 
Thesis writing - week9
Thesis writing - week9Thesis writing - week9
Thesis writing - week9
 
4th Semester Computer Science and Engineering (ACU-2022) Question Paper
4th Semester Computer Science and Engineering (ACU-2022) Question Paper4th Semester Computer Science and Engineering (ACU-2022) Question Paper
4th Semester Computer Science and Engineering (ACU-2022) Question Paper
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Assignment4
Assignment4Assignment4
Assignment4
 
Enter The Matrix
Enter The MatrixEnter The Matrix
Enter The Matrix
 
DeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningDeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep Learning
 
Taller parcial 2
Taller parcial 2Taller parcial 2
Taller parcial 2
 
3rd Semester Computer Science and Engineering (ACU) Question papers
3rd Semester Computer Science and Engineering  (ACU) Question papers3rd Semester Computer Science and Engineering  (ACU) Question papers
3rd Semester Computer Science and Engineering (ACU) Question papers
 
Families of Triangular Norm Based Kernel Function and Its Application to Kern...
Families of Triangular Norm Based Kernel Function and Its Application to Kern...Families of Triangular Norm Based Kernel Function and Its Application to Kern...
Families of Triangular Norm Based Kernel Function and Its Application to Kern...
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
 
Lec3
Lec3Lec3
Lec3
 
Ch07 linearspacealignment
Ch07 linearspacealignmentCh07 linearspacealignment
Ch07 linearspacealignment
 
Hierarchical matrix techniques for maximum likelihood covariance estimation
Hierarchical matrix techniques for maximum likelihood covariance estimationHierarchical matrix techniques for maximum likelihood covariance estimation
Hierarchical matrix techniques for maximum likelihood covariance estimation
 
3D Math Primer: CocoaConf Atlanta
3D Math Primer: CocoaConf Atlanta3D Math Primer: CocoaConf Atlanta
3D Math Primer: CocoaConf Atlanta
 
There are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docxThere are two types of ciphers - Block and Stream. Block is used to .docx
There are two types of ciphers - Block and Stream. Block is used to .docx
 
R part II
R part IIR part II
R part II
 
Rpartii 131126003007-phpapp01
Rpartii 131126003007-phpapp01Rpartii 131126003007-phpapp01
Rpartii 131126003007-phpapp01
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 

Dernier

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Dernier (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

L1 Sudoku

  • 1. Sudoku solving with CVXOPT Sudoku solving with CVXOPT Ben Moran http://transnite.wordpress.com 09 December 2009
  • 2. Sudoku solving with CVXOPT Background 1 Background 2 Sudoku as matrix and vectors 3 Sparsity and solving 4 Conclusion
  • 3. Sudoku solving with CVXOPT Background
  • 4. Sudoku solving with CVXOPT Background Based on. . . a paper by Babu, Pelckmans, Stoica: Linear Systems, Sparse Solutions, and Sudoku 1 Solve Sudoku by turning it into a linear programming problem, inspired by new signal processing paradigm: compressed sensing 2 I thought it was interesting and reimplemented it in Python with CVXOPT 1 http://www.it.uu.se/katalog/praba420/Sudoku.pdf 2 http://nuit-blanche.blogspot.com/
  • 5. Sudoku solving with CVXOPT Background Why? Ben Laurie - Sudoku is a denial of service attack on human intellect It's a solved problem!
  • 6. Sudoku solving with CVXOPT Background Existing solutions Knuth's Dancing Links (DLX) 3 Norvig's constraint propagation (also Python) 4 Sinkhorn method 5 And many others 3 http://en.wikipedia.org/wiki/Dancing_Links 4 http://norvig.com/sudoku.html 5 http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?isnumber=4802290arnumber=4804
  • 7. Sudoku solving with CVXOPT Background But. . . This is still an interesting way into the brand new eld of compressed sensing And what have Sudoku got to do with. . . signal processing sensor networks single-pixel cameras satellite imaging? Our solution will be interesting, as the solver won't know the rules of Sudoku at all!
  • 8. Sudoku solving with CVXOPT Sudoku as matrix and vectors What are linear systems? Matrix - Vector multiplication 1 3 2 11 = 4 −1 3 5 Ax =b In Python: import numpy numpy . m a t r i x ( ' 1 3 ; 4 −1 ' ) ∗ numpy . m a t r i x ( ' 2 ; 3 ' ) matrix ( [ [ 1 1 ] , [ 5]])
  • 9. Sudoku solving with CVXOPT Sudoku as matrix and vectors Solving a linear system Given 1 3 11 A = and b = , what is x? 4 −1 5 In Python: import numpy numpy . l i n a l g . s o l v e ( numpy . m a t r i x ( ' 1 3 ; 4 −1 ' ) , numpy . m a t r i x ( ' 1 1 ; 5 ' ) ) matrix ( [ [ 2 . ] , [ 3.]])
  • 10. Sudoku solving with CVXOPT Sudoku as matrix and vectors Sudoku board as an indicator vector We can turn a 9x9 Sudoku board into a single vector with 9x9x9 = 729 elements Each 9 entries corresponds to one cell, most are zero Put a 1 in the rst place for 1, in the second place for 2. . . p = Problem ( 1 2 . . ,N=2) numpy . m a t r i x ( p . t o _ i n d i c a t o r _ v e c t o r ( ) , ' i ' ) matrix ( [ [ 1 ] , [ 0 ], [ 0 ], [ 1 ], [ 0 ], [ 0 ], [ 0 ], [ 0 ]])
  • 11. Sudoku solving with CVXOPT Sudoku as matrix and vectors Sudoku rules as a matrix system Now we can set up a special matrix to enforce the rules of Sudoku It will have one row for each constraint, and 9x9x9 = 729 columns When you multiply this with the indicator vector, you get all 1's if the board is valid Here are some of the rules for 2x2 Sudoku: p = Problem ( 1 2 . . ,N=2) numpy . m a t r i x ( p . m a t r i x ( ) ) matrix ( [ [ 1 , 1 , 0 , 0 , 0 , 0, 0, 0.] , [ 0, 0, 1, 1, 0, 0, 0, 0.] , [ 0, 0, 0, 0, 1, 1, 0, 0.] , [ 0, 0, 0, 0, 0, 0, 1, 1.] , [ 1, 0, 1, 0, 0, 0, 0, 0.] , ... [ 0, 0, 0, 1, 0, 0, 0, 0.]])
  • 12. Sudoku solving with CVXOPT Sudoku as matrix and vectors Rules for 2x2 Sudoku 1 2 _ _
  • 13. Sudoku solving with CVXOPT Sudoku as matrix and vectors _ _ _ 1 5 _ _ 7 _    1 _ 6 _ _ _ 8 2 _  3 _ _ 8 6 _ _ 4 _     9 _ _ 4 _ _ 5 6 7     _ _ 4 7 _ 8 3 _ _     7 3 2 _ _ 6 _ _ 4     _ 4 _ _ 8 1 _ _ 9        _ 1 7 _ _ _ 2 _ 8  _ 5 _ _ 3 7 _ _ _ Figure: Full size 9x9 board
  • 14. Sudoku solving with CVXOPT Sudoku as matrix and vectors Figure: Full size 9x9 rules matrix
  • 15. Sudoku solving with CVXOPT Sparsity and solving What do we gain by writing Sudoku like that? We can check solutions with a matrix-vector multiply Turn the proposed solution into an indicator vector Multiply it with the the rules matrix If the solution is valid, the answer will be all ones Solve Ax = [1 1 1 1 1] to nd the answer? . . . No - there is an innite number of answers, most won't give valid boards Need something else to nd our true correct answer
  • 16. Sudoku solving with CVXOPT Sparsity and solving Sparsity The true solution if it exists will be the sparsest (Sparse vector == mostly zeros) 0 3      1   1  0 0.1         Sparse  0  vs non-sparse  4      0 −2         1 1         0 .5 That still doesn't help. . . no ecient method to solve that directly
  • 17. Sudoku solving with CVXOPT Sparsity and solving Dierent kinds of distance: L-2 norm, Pythagoras You measure the length of a vector with a norm This is an L-p norm (for a 3d vector): (|x1 |p + |x2 |p + |x3 |p )1/p If you plug in p=2, you get normal Euclidean distance (Pythagoras' theorem): (|x1 |2 + |x2 |2 + |x3 |2 ) We can choose the solution with the smallest L2-norm. . .
  • 18. Sudoku solving with CVXOPT Sparsity and solving Dierent kinds of distance: L-2 norm, Pythagoras But we don't get a valid answer # Least squares s o l u t i o n M = numpy . m a t r i x ( s a m p l e ( ) . m a t r i x ( ) ) o n e s =numpy . o n e s ( (M. s h a p e [ 0 ] , 1 ) ) v = numpy . l i n a l g . p i n v (M) ∗ o n e s (M∗ v ) [ : 4 ] matrix ( [ [ 1 . ] , [ 1.] , [ 1.] , [ 1.]]) v [ : 4 ] matrix ([[ −0.0735114 ] , [ 0.24178992] , [ 0.00310795] , [ 0.28139045]])
  • 19. Sudoku solving with CVXOPT Sparsity and solving Dierent kinds of distance: L-0 norm, sparsity Plug in p=0 and you get L0, sparsity (number of non-zeros): |x1 |0 + |x2 |0 + |x3 |0 The right solution will have the lowest L0 norm But we've no way of nding it, besides solving the Sudoku!
  • 20. Sudoku solving with CVXOPT Sparsity and solving Dierent kinds of distance: L-1 norm With p=1 you get the absolute sum, taxicab distance |x1 |1 + |x2 |1 + |x3 |1
  • 21. Sudoku solving with CVXOPT Sparsity and solving Why is L-1 important? There are ecient methods of solving it (linear programming) And for many matrices, the minimum L1 norm solution turns out to also minimize L0 (Smallest L1 then nds the sparsest solution) This insight is central to the compressed sensing revolution
  • 22. Sudoku solving with CVXOPT Sparsity and solving Python - CVXOPT Now we can install python-cvxopt 6 and solve the problem! CVXOPT is a GPL library for optimization linear programming Other kinds of convex optimization We can use it to nd the solution x of Ax=b, with the smallest L1-norm The result comes out as a binary vector of 0s and 1s (without being constrained to do so!) 6 http://abel.ee.ucla.edu/cvxopt/
  • 23. Sudoku solving with CVXOPT Sparsity and solving Python - CVXOPT After all that work to set up the question, the answer is one line! h e l p c v x o p t . s o l v e r s . l p Help on f u n c t i o n l p i n module c v x o p t . c o n e p r o g : l p ( c , G , h , A=None , b=None , s o l v e r=None , . . . ) S o l v e s a p a i r o f p r i m a l and d u a l LPs minimize c ' ∗x s u b j e c t t o G∗ x + s = h A∗ x = b s = 0 ... # S o l v e w i t h CVXOPT c v x o p t . s o l v e r s . l p ( c0 , G , hh )
  • 24. Sudoku solving with CVXOPT Conclusion Caveats This won't solve every Sudoku - it tends to solve the easier ones It fails when the L1 minimum doesn't nd the L0 minimum There are other methods, from the compressed sensing literature, like iterative reweighting, that solve more For Sudoku, which puzzles are hard why is still an open problem at the frontiers of research
  • 25. Sudoku solving with CVXOPT Conclusion Compressed sensing Very many important, real world signals are sparse: images, sounds, gene expressions Instead of capturing the raw signal and compressing. . . . . . compressed sensing means you can capture them pre-compressed, with fewer measurements (below the Nyquist rate: 44.1kHz CDs to play back 22kHz frequencies) Less hardware needed will mean better images or cheaper kit Single-pixel camera 7 7 http://dsp.rice.edu/cscamera
  • 26. Sudoku solving with CVXOPT Conclusion Code, links and slides Code is at http://github.com/benmoran/L1-Sudoku/ Sporadic blog: http://transnite.wordpress.com Find CVXOPT at http://abel.ee.ucla.edu/cvxopt/examples/index.html And the nice CVXMOD wrapper at http://cvxmod.net