writing a sat solver as a hobby project

31
Thursday Seminar 2017-10-12 Masahiro Sakai (酒井 政裕) Writing a SAT solver as a hobby project

Upload: masahiro-sakai

Post on 29-Jan-2018

129 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Writing a SAT solver as a hobby project

Thursday Seminar 2017-10-12Masahiro Sakai (酒井政裕)

Writing a SAT solveras a hobby project

Page 2: Writing a SAT solver as a hobby project

Outline

• Decision procedures and Me• My hobby project• About SAT and SMT• Some results• Conclusion

Page 3: Writing a SAT solver as a hobby project

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!)

Page 4: Writing a SAT solver as a hobby project

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 satisfya 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 φ

Counterexample

Page 5: Writing a SAT solver as a hobby project

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, andgot interested in their algorithms and implementations

• Therefore I started writing toy level implementations …

Page 6: Writing a SAT solver as a hobby project

My hobby project

Page 7: Writing a SAT solver as a hobby project

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

Page 8: Writing a SAT solver as a hobby project

Some of problems and algorithms

• Presburger Arithmetic– Omega Test– Cooper’s Algorithm

• Real Arithmetic– Fourier-Motzkin variable

elimination – Simplex method – Grob̈ner 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.

Page 9: Writing a SAT solver as a hobby project

https://github.com/msakai/toysolver http://hackage.haskell.org/package/toysolver

It’s available online

Page 10: Writing a SAT solver as a hobby project

Wait !?

http://hackage.haskell.org/package/toysolver

……

Page 11: Writing a SAT solver as a hobby project

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?”

Page 12: Writing a SAT solver as a hobby project

Why I’m writing it?

• Developing a state-of-the-art implementation• Learning algorithms• Familiarizing other libraries and tools• Handy implementation for my self

Page 13: Writing a SAT solver as a hobby project

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

Page 14: Writing a SAT solver as a hobby project

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.

Page 15: Writing a SAT solver as a hobby project

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

Page 16: Writing a SAT solver as a hobby project

About SAT and SMT

Page 17: Writing a SAT solver as a hobby project

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

Page 18: Writing a SAT solver as a hobby project

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

Page 19: Writing a SAT solver as a hobby project

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)”?

Page 20: Writing a SAT solver as a hobby project

SMT = SAT solver + Theory solvers

• SAT solver is responsible for Boolean reasoning• Theory solvers are responsible for handling specific

functions/relations etc.

SATSolver

Arithmetic Solver:+, ×, ≤

BitVector Solver

Uninterpreted Function Solver:

f, g, =Array Solverread, write

Page 21: Writing a SAT solver as a hobby project

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

Page 22: Writing a SAT solver as a hobby project

My SAT solver: toysat

p cnf 250 1065-159 -234 197 0-71 13 194 045 -218 38 0191 -129 -88 0117 -164 -29 0107 53 115 0167 111 -57 0-115 94 98 025 -51 -165 0247 31 -64 0156 228 11 064 199 -162 01 173 -54 0136 -98 -215 0

$ toysat UF250.1065.100/uf250-01.cnf……c #vars 250c #constraints 1065c Solving starts ...c ============================[ Search Statistics ]============================c Time | Restart | Decision | Conflict | LEARNT | Fixed | Removedc | | | | Limit GC | Var | Constrac =============================================================================c 0.0s | 0 | 0 | 0 | 438 0 | 0 | 0……c 4.0s | 11 | 23520 | 19665 | 1259 37 | 0 | 0c #cpu_time = 3.872sc #wall_clock_time = 3.957sc #decision = 23520c #random_decision = 105c #conflict = 19665c #restart = 11s SATISFIABLEv -1 -2 3 -4 5 -6 7 -8 9 -10v -11 -12 -13 14 15 16 17 -18 19 20v 21 22 -23 -24 25 26 -27 28 29 -30v 31 32 -33 34 -35 36 37 -38 39 -40…

¬x159∨¬x234∨x197

Page 23: Writing a SAT solver as a hobby project

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.smt2success…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)))

Page 24: Writing a SAT solver as a hobby project

Some results?

Page 25: Writing a SAT solver as a hobby project

Some results?

• Several talks• Solver competitions

– Pseudo Boolean Competition 2012– Max-SAT Evaluation 2013– SMT-COMP 2016– …

• How was writing SAT solver in Haskell?

Page 26: Writing a SAT solver as a hobby project

I have had talks based on what I learned

Page 27: Writing a SAT solver as a hobby project

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

Page 28: Writing a SAT solver as a hobby project

SMT-COMP 2016QF_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!)

Page 29: Writing a SAT solver as a hobby project

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.

Page 30: Writing a SAT solver as a hobby project

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

Page 31: Writing a SAT solver as a hobby project

Thank you!And any questions or comments?