lecture 2: sat solvers - university of pennsylvaniacis189/files/lecture2.pdflecture 2: solvers &...

34
Lecture 2: Solvers & Encoding Jediah Katz [email protected] CIS 189

Upload: others

Post on 10-Mar-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Lecture 2:Solvers & EncodingJediah Katz [email protected]

CIS 189

Page 2: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Recap

Last week

● CNF-SAT problem

● Tseitin’s transformation

● Intro to SAT solvers

This week

● Using SAT solvers in Python

● Encoding problems with CNF-SAT

2

Page 3: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

MiniSAT

● MiniSat is a minimal, open source SAT solver created by Niklas Eén & Niklas Sörensson○ An Extensible SAT-solver (Eén & Sörensson, 2003)

● Silver medal in SAT Comp. 2005...

● …with only ~600 lines of C++ code!

3

Niklas SörenssonNiklas Eén

Page 4: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

PicoSAT

● PicoSAT is an open source SAT solver created by Prof. Armin Biere○ (Q)CompSAT and (Q)PicoSAT at the SAT’06 Race (Biere 2006)

● Armin Biere: Handbook of Satisfiability

● Gold medalist in SAT Competition 2007

● Written in C, but bindings to Python, JS, R, etc.○ We’ll use this: can be easily installed with pip!

4

Armin Biere

Page 5: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Notation

● Positive ( 𝑥 ) and negative ( 𝑥 ) literals

● Recall that we often consider a clause as a set of literals, and a CNF as a set of clauses

● Therefore might express CNFs in compact notation:

{𝑥𝑧, 𝑦𝑧𝑥} ≃ 𝑥 ∨ 𝑧 ∧ 𝑦 ∨ 𝑧 ∨ 𝑥

5

Page 6: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

The DIMACS CNF Format● File format (.cnf) for representing CNF formulas created by

the Center for Discrete Math and Computer Science

● File can begin with comment lines: c [comment]

● Info line: p cnf [#vars] [#clauses]

● Rest of file: each clause on its own line○ pos/neg literals are pos/neg integers, separated by whitespace

○ variables are in the range [1, #vars]

○ clauses are usually terminated by 0

6

Page 7: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Example: DIMACS Format

Translate the following CNF into DIMACS format:

{𝑥𝑧, 𝑦𝑧𝑥}

7

Solution:c little.cnf

c this is DIMACS!

p cnf 3 2

1 -3 0

2 3 -1 0

Page 8: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Solving a CNF with PicoSAT

Let’s solve 𝜑 = 1 ∧ 2 ∨ 3 by solving the equisatisfiable CNF:

5, 41, 42, 412, 543, 54, 53

So what’s the satisfying assignment for 𝜑?

8

Page 9: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

SAT Encodings

Why do we care about SAT?

Can encode most problems as an instance of CNF-SAT. Let’s see how!

9

Page 10: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Warmup: Boolean Encodings

10

𝑥 ⇒ 𝑦

𝑥 ⇔ 𝑦

𝑥 ⊕ 𝑦

if 𝑥 then 𝑦 else 𝑧

{𝑥𝑦}

{𝑥𝑦, 𝑦𝑥}

{𝑥𝑦, 𝑥𝑦}

{𝑥𝑦, 𝑥𝑧}

Page 11: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

First-Order Logic Encodings

11

𝐴𝑙𝑙(𝑥1, … , 𝑥𝑛)

𝐴𝐿𝑂 𝑥1, … , 𝑥𝑛

𝐴𝑀𝑂(𝑥1, … , 𝑥𝑛)

𝐸𝑥𝑎𝑐𝑡𝑙𝑦𝑂𝑛𝑒(𝑥1, … , 𝑥𝑛)

{𝑥1, 𝑥2, … , 𝑥𝑛}

{𝑥1𝑥2⋯𝑥𝑛}

𝑥𝑖𝑥𝑗 0 ≤ 𝑖 < 𝑗 ≤ 𝑛

𝐴𝐿𝑂 … ∧ 𝐴𝑀𝑂(… )

Page 12: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Complexity of Encoding

12

𝐴𝑀𝑂(𝑥1, … , 𝑥𝑛) 𝑥𝑖𝑥𝑗 0 ≤ 𝑖 < 𝑗 ≤ 𝑛

● Called the binomial encoding● How many clauses in this encoding?● Other encodings that use fewer clauses, but

introduce extra variables

Page 13: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Binary AMO Encoding

13

𝐴𝑀𝑂(𝑥1, … , 𝑥𝑛)

● Retain original variables 𝑥1, … , 𝑥𝑛

● Let 𝑚 = ⌈lg 𝑛⌉, and introduce new variables 𝑦1, … , 𝑦𝑚○ 𝑦𝑗 represents 𝑗𝑡ℎ bit of 𝑖, where 𝑥𝑖 is the (≤) one True var

● Clauses:

𝑥𝑖 ⇒ ൝𝑦𝑗 if 𝑗𝑡ℎ bit of 𝑖 is 1

𝑦𝑗 if 𝑗𝑡ℎ bit of 𝑖 is 0∀𝑥𝑖 , 𝑦𝑗

● How many extra variables? How many clauses?

Page 14: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Good Encoding Matters

14

● The performance of a SAT solver can vary hugely depending on the encoding.

● Different encodings work better in different cases: no “universal best encoding.”

● Start simple and be more clever if necessary.

Page 15: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Graph Coloring

15

Can we color 𝑮 = 𝑽,𝑬 with ≤ 𝒌 colors?

● Direct encoding: one boolean variable for each assignment of variable to value○ Typically the first choice of variables to try, if possible○ Good enough for many problems, but may need to

experiment with smarter options for best results

● For this problem:○ 𝑥𝑖𝑐: if we assign 𝑣𝑖 color 𝑐

Page 16: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Graph Coloring

16

Can we color 𝑮 = 𝑽,𝑬 with ≤ 𝒌 colors?

● 𝑥𝑖𝑐: if we assign 𝑣𝑖 color 𝑐● Constraint 1: every vertex has ≥ 1 color

𝑥𝑖1𝑥𝑖2…𝑥𝑖𝑘 ∀ 𝑣𝑖 ∈ 𝑉

Page 17: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Graph Coloring

17

Can we color 𝑮 = 𝑽,𝑬 with ≤ 𝒌 colors?

● 𝑥𝑖𝑐: if we assign 𝑣𝑖 color 𝑐● C2: if 𝑢, 𝑣 ∈ 𝐸, then 𝑢, 𝑣 have different colors

𝑥𝑖𝑐𝑥𝑗𝑐 ∀ (𝑣𝑖 , 𝑣𝑗) ∈ 𝐸, 𝑐 ∈ [1. . 𝑘]

Page 18: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Graph Coloring

18

Can we color 𝑮 = 𝑽,𝑬 with ≤ 𝒌 colors?

● 𝑥𝑖𝑐: if we assign 𝑣𝑖 color 𝑐● C3: every vertex has ≤ 1 color● If we can color 𝐺 with multi-color vertices, of

course we can color 𝐺 with one-color vertices, so this constraint is unnecessary

Page 19: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

From Coloring to Min-Coloring

● What if we wanted to find the 𝜒(𝐺), the minimum number of colors to color 𝐺?

● Binary search○ Check if 𝐺 can be colored with 1, 2, 4, 8, 16, ... colors

○ Stop at smallest value 2𝑘 such that 𝐺 is 2𝑘-colorable

○ Binary search for 𝜒(𝐺) in range 2𝑘−1, 2𝑘

● Requires 𝑂 lg 𝜒(𝐺) runs of solver

19

Page 20: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

From Coloring to Min-Coloring

● UNSAT can be way harder than SAT○ Finding just one satisfying assignment vs. showing that

none exists, in a massive search space

● Instead of starting with 𝑘 = 1 and working up, can start with 𝑘 = 𝑛 and work down○ Even better: start with 𝑘 = Δ + 1○ Sometimes better to descend linearly, rather than binary

20

Page 21: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Does Size Matter?

21

● Weak correlation between number of variables/clauses and “hardness” of formula

● Just because a constraint is unnecessarydoesn’t mean it’s useless

● In fact, adding extra constraints rules out more infeasible options and is typically critical to solving quickly

Page 22: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

22

Mitchell, Selman & Levesque, 1992

● Relationship between clause-to-variable ratio and solving time on random hard 3-CNF formulas

● Underconstrained: easy to find satisfying assignment while avoiding conflicts

● Overconstrained: many conflicts quickly rule out unsatisfying assignments

● High difficulty “sweet spot”

Page 23: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Symmetry Breaking

23

● Solving UNSAT graph coloring problems takes a very long time... why?

● Must rule out every symmetric coloring

● Ex: these equivalent colorings are technically different

Page 24: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Symmetry Breaking

24

● Key idea: add constraints that rule out equivalent symmetric colorings

● Basic way to do this: pick some vertices (ideally a dense subgraph) and fix their colors

Page 25: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

PicoSAT Coloring Benchmarks

25

*Running on my Dell XPS laptop with 16GB of RAM, in a Jupyter notebook.

0

0.2

0.4

0.6

0.8

1

1.2

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

*Tim

e to s

olv

e (

s)

Number of colors k

Graph: games120 (n=120, m=1276, χ=9)

C1 & C2 only

C1, C2, & C3

Page 26: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Stable Matchings

26

We have 𝑛 men and 𝑛 women. Each man and woman submits a preference list ranking everyone of the opposite sex (descending).

Goal: find a matching of men to women.

A man and woman who both prefer each other to their matched partners are a blocking pair.

A matching is stable if it has no blocking pairs.

Page 27: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Stable Matchings

27

𝑚𝑖𝑝: if man 𝑖 is matched to 𝑝𝑡ℎ woman or later on his list

𝑤𝑖𝑝: if woman 𝑖 is matched to 𝑝𝑡ℎ man or later on her list

𝑊1 > 𝑊2 𝑀1 𝑊1 𝑀1 > 𝑀2

𝑊1 > 𝑊2 𝑀2 𝑊2 [𝑀1 > 𝑀2]

𝒎𝟏𝟏 𝒎𝟏𝟐 𝒎𝟐𝟏 𝒎𝟐𝟐 𝒘𝟏𝟏 𝒘𝟏𝟐 𝒘𝟐𝟏 𝒘𝟐𝟐

T F T T T F T T

Page 28: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Stable Matchings

28

● C1: every man is matched

𝑚𝑖1 1 ≤ 𝑖 ≤ 𝑛

(plus symmetric constraints for women for this and the following constraints)

𝑚𝑖𝑝: if man 𝑖 is matched to 𝑝𝑡ℎ woman or later on his list

𝑤𝑖𝑝: if woman 𝑖 is matched to 𝑝𝑡ℎ man or later on her list

Page 29: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Stable Matchings

29

● C2: if a man gets his 𝑝𝑡ℎ or later choice, it’s also his 𝑝 − 1 𝑡ℎ or later choice

𝑚𝑖𝑝 ⇒ 𝑚𝑖(𝑝−1) 1 ≤ 𝑖 ≤ 𝑛, 2 ≤ 𝑝 ≤ 𝑛

𝑚𝑖𝑝: if man 𝑖 is matched to 𝑝𝑡ℎ woman or later on his list

𝑤𝑖𝑝: if woman 𝑖 is matched to 𝑝𝑡ℎ man or later on her list

Page 30: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Stable Matchings

30

● C3: if man 𝑖 is matched to woman 𝑗, then she is matched to him also

𝑚𝑖𝑝 ∧ 𝑚𝑖 𝑝+1 ⇒ 𝑤𝑗𝑞 ∧ 𝑤𝑗(𝑞+1) 1 ≤ 𝑖, 𝑗 ≤ 𝑛

● 𝑝 = position of woman 𝑗 in man 𝑖’s list

● 𝑞 = position of man 𝑖 in woman 𝑗’s list

𝑚𝑖𝑝: if man 𝑖 is matched to 𝑝𝑡ℎ woman or later on his list

𝑤𝑖𝑝: if woman 𝑖 is matched to 𝑝𝑡ℎ man or later on her list

Page 31: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Encoding Stable Matchings

31

● C4: if man 𝑖 is matched to someone worse than woman 𝑗, her match must be better than him

𝑚𝑖(𝑝+1) ⇒ 𝑤𝑗𝑞 1 ≤ 𝑖, 𝑗 ≤ 𝑛

● 𝑝 = position of woman 𝑗 in man 𝑖’s list

● 𝑞 = position of man 𝑖 in woman 𝑗’s list

𝑚𝑖𝑝: if man 𝑖 is matched to 𝑝𝑡ℎ woman or later on his list

𝑤𝑖𝑝: if woman 𝑖 is matched to 𝑝𝑡ℎ man or later on her list

Page 32: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Why Stable Matchings?

● Gale-Shapley algorithm solves SM problem in linear time. Why use SAT solvers?

● SMTI: stable matching problem where preference lists may be incomplete and contain ties

● SM-C: stable matching problem with couples

● Our encoding easily generalizes to SMTI, SM-C

● Theorem: SMTI and SM-C are NP-complete.

32

Page 33: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

Next Week

● Start learning how SAT solvers work● Homework 0 due tonight at midnight!● Quiz 1 due on 9/14

● Untimed, unlimited attempts, easy● Homework 1 due on 9/28

○ Encoding Sudoku into SAT○ Extra credit challenge to win $15!

33

Page 34: Lecture 2: SAT Solvers - University of Pennsylvaniacis189/files/Lecture2.pdfLecture 2: Solvers & Encoding Jediah Katz jediahk@seas.upenn.edu CIS 189

ReferencesA. Biere, Handbook of satisfiability. Amsterdam: IOS Press, 2009.

J. Burkardt, “CNF Files,” John Burkardt's Home Page. [Online]. Available: https://people.sc.fsu.edu/~jburkardt/data/cnf/cnf.html.

34