Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Writing a SAT solver as a hobby project

698 views

Published on

My talk slides at PFN Thursday Seminar 2017-10-12
Recorded video: https://www.youtube.com/watch?v=BR_nxNRXn5g

Published in: Technology
  • Be the first to comment

Writing a SAT solver as a hobby project

  1. 1. Thursday Seminar 2017-10-12 Masahiro Sakai (酒井 政裕) Writing a SAT solver as a hobby project
  2. 2. Outline • Decision procedures and Me • My hobby project • About SAT and SMT • Some results • Conclusion
  3. 3. About me • Masahiro Sakai (酒井 政裕) • joined PFN this May as an Engineer • My technical interest includes: – Functional Programming (esp. Haskell) – Program Verification and Decision Procedures for it – and Machine Learning (of course!)
  4. 4. Decision procedures • Decision problem: yes/no answer problems – e.g. Is intersection of given two regular language empty? • Model Checking – Does a finite state system M satisfy a temporal logic specification φ ? – it is just a language emptiness of M ∩ ¬φ, if we represent both M and φ as automaton – and can be decided using automata algorithm • Likewise, many program verification task can be reduced to simple decision problems, • Fast decision procedures have accelerated program verification. All possible behavior of M Behaviors that violate φ Counter example
  5. 5. Decision procedures and Me • When I was a grad student, ~10 years ago, I listened to talks about model checking tools. • Later, I was working on program verification, using decision procedures as off-the-shelf tools • I was impressed by their strength and usefulness, and got interested in their algorithms and implementations • Therefore I started writing toy level implementations …
  6. 6. My hobby project
  7. 7. My hobby project : toysolver • Several years ago – I started writing some code snippet just to understand those algorithms • Over the years, – I have added more and more algorithms, and the codebase grew • Now – it contains not a few algorithms and it’s ~30 kloc
  8. 8. Some of problems and algorithms • Presburger Arithmetic – Omega Test – Cooper’s Algorithm • Real Arithmetic – Fourier-Motzkin variable elimination – Simplex method – Gröbner basis (Buchberger) – Quantifier Elimination • (Mixed) Integer Programming – Branch-and-bound – Cutting plane (Gomory’s Cut) – Conti-Traverso • SAT / MaxSAT / Pseudo Boolean – DPLL / CDCL • SMT – Uninterpreted function (Congruence Closure) – Bitvectors Most of them are toy-level, but the SAT solver is modestly fast.
  9. 9. https://github.com/msakai/toysolver http://hackage.haskell.org/package/toysolver It’s available online
  10. 10. Wait !? http://hackage.haskell.org/package/toysolver ……
  11. 11. Why Haskell? 1. Because it’s the language I used most, and it’s handy for me if usable from Haskell easily 2. It’s suitable for symbolic computation like formula manipulation. 3. I have two questions: – Haskell has a good compiler and runtime suitable for usual application domain, But… – “How about computation intensive application like SAT?” – “Is good functional interface possible?”
  12. 12. Why I’m writing it? • Developing a state-of-the-art implementation • Learning algorithms • Familiarizing other libraries and tools • Handy implementation for my self
  13. 13. Reason: Learning algorithms • Learning cleaver algorithm is itself fun • Implementation is the best way to understand it • In addition to that, • the knowledge of algorithms was useful when I use other (state-of-the-art) implementation as a off-the- shelf tool
  14. 14. Reason: Familiarizing other libraries and tools • Writing and maintaining such project is a good opportunity to try new libraries, frameworks and services • For example, – I have learned several Haskell libraries, – and also integration of GitHub, Traivs CI, AppVeyor, Coveralls.io, etc.
  15. 15. Reason: Handy implementation for myself • Having handy implementation for myself is useful for – experimenting new algorithms, – solving puzzles (e.g. Sudoku, Number Link, etc.) – using in a programming competition • In particular, there are less such library available for Haskell
  16. 16. About SAT and SMT
  17. 17. What is SAT? • SAT = Boolean SATisfiability problem – “Is there an assignment that makes given formula true?” • Examples: – (P∨Q)∧(P∨¬Q)∧(¬P∨¬Q) is satisfiable with {P ↦ True, Q ↦ False} – (P∨Q)∧(P∨¬Q)∧(¬P∨¬Q)∧(¬P∨Q) is unsatisfiable • SAT is NP complete, but state-of-the-art SAT-solver can often solve problems with millions of variables / constraints. • Has many applications
  18. 18. Some Applications of SAT (and SMT) • Software/Hardware verification – Model checking, Test-case generation, … • Theorem proving • Puzzles: Sudoku, Number link, Nonogram, etc. • Program Synthesis • and more
  19. 19. What is SMT? • Weakness of SAT: Really low-level representation – Encoding problems into SAT sometimes blows-up – SAT solver cannot leverage high-level knowledge • SMT = Satisfiability Modulo Theories – An approach to overcome the weakness of SAT – Problem Example: Is there array a, function f, integers i, j such that “0 ≤ i ∧ i < 10 ∧ (2i+1=j ∨ read(a,i)=0) ∧ f(read(write(a,i,3), j-2)) ≠ f(j-i+1)”?
  20. 20. SMT = SAT solver + Theory solvers • SAT solver is responsible for Boolean reasoning • Theory solvers are responsible for handling specific functions/relations etc. SAT Solver Arithmetic Solver: +, ×, ≤ BitVector Solver Uninterpreted Function Solver: f, g, = Array Solver read, write …
  21. 21. What is interesting about SAT and SMT? (for me) • Theory of mathematical logic is connected to practical application • For example, – In SAT, its CDCL algorithm can be seen as doing concurrently • search satisfying assignment (model theoretic) • constraint propagation and conflict learning (proof theoretic deduction) – In SMT, theory combination closely related to Craig interpolation theorem
  22. 22. My SAT solver: toysat p cnf 250 1065 -159 -234 197 0 -71 13 194 0 45 -218 38 0 191 -129 -88 0 117 -164 -29 0 107 53 115 0 167 111 -57 0 -115 94 98 0 25 -51 -165 0 247 31 -64 0 156 228 11 0 64 199 -162 0 1 173 -54 0 $ toysat UF250.1065.100/uf250-01.cnf …… c #vars 250 c #constraints 1065 c Solving starts ... c ============================[ Search Statistics ]================ c Time | Restart | Decision | Conflict | LEARNT | Fixed | Removed c | | | | Limit GC | Var | Constra c ========================================================== c 0.0s | 0 | 0 | 0 | 438 0 | 0 | 0 …… c 4.0s | 11 | 23520 | 19665 | 1259 37 | 0 | 0 c #cpu_time = 3.872s c #wall_clock_time = 3.957s c #decision = 23520 c #random_decision = 105 c #conflict = 19665 c #restart = 11 s SATISFIABLE v -1 -2 3 -4 5 -6 7 -8 9 -10 v -11 -12 -13 14 15 16 17 -18 19 20 v 21 22 -23 -24 25 26 -27 28 29 -30 v 31 32 -33 34 -35 36 37 -38 39 -40 … ¬x159∨¬x234∨x197
  23. 23. My SMT solver: toysmt (set-option :produce-models true) (set-logic QF_UFLRA) (declare-sort U 0) (declare-fun x () Real) (declare-fun f (U) Real) (declare-fun P (U) Bool) (declare-fun g (U) U) (declare-fun c () U) (declare-fun d () U) (assert (= (P c) (= (g c) c))) (assert (ite (P c) (> x (f d)) (< x (f d)))) (check-sat) (get-model) (exit) $ toysmt QF_UFLRA.smt2 success … sat ((define-fun P ((x!1 U)) Bool (ite (= x!1 (as @3 U)) true false)) (define-fun c () U (as @3 U)) (define-fun d () U (as @4 U)) (define-fun f ((x!1 U)) Real (ite (= x!1 (as @4 U)) 0 (/ 555555 1))) (define-fun g ((x!1 U)) U (ite (= x!1 (as @3 U)) (as @3 U) (as @-1 U))) (define-fun x () Real (/ 1 10)))
  24. 24. Some results?
  25. 25. Some results? • Several talks • Solver competitions – Pseudo Boolean Competition 2012 – Max-SAT Evaluation 2013 – SMT-COMP 2016 – … • How was writing SAT solver in Haskell?
  26. 26. I have had talks based on what I learned
  27. 27. Pseudo Boolean Competition 2012 (PB'12) competition result: • Ranked high in some minor divisions: – placed 2nd in PARTIAL-BIGINT-LIN and SOFT- BIGINT-LIN categories – placed 4th in PARTIAL-SMALLINT-LIN and SOFT- SMALLINT-LIN categories – placed 8th in OPT-BIGINT-LIN category
  28. 28. SMT-COMP 2016 QF_LRA (Main Track) http://smtcomp.sourceforge.net/2016/results-QF_LRA.shtml?v=1467876482 ‘toysmt’ ended up dead last. But without wrong results! (Thanks to QuickCheck!)
  29. 29. How was writing SAT solver in Haskell? • How about writing computation intensive application like SAT? – Not so bad, but with some frustration: – e.g. Haskell allocate memory like breathing, but I want to avoid memory allocation in tight loop. It’s possible if we write carefully, but … • Is good functional interface possible? – Not tried yet. My SAT solver is based on imperative API and imperative implementation.
  30. 30. Conclusion • As a hobby project, I have been writing toy-level implementation of various algorithms • In particular, algorithms for SAT and related problems • It’s mainly for fun, but I have gained good experience from the activity
  31. 31. Thank you! And any questions or comments?

×