74
1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

Embed Size (px)

Citation preview

Page 1: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

1

Decision Procedures for Linear Arithmetic

Presented By Omer Katz

01/04/14

Based on slides by Ofer Strichman

Page 2: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

3

Agenda

DPLL-T (a very short reminder) What is Linear Arithmetic and why is it needed Decision procedures for Decision procedures for A few preprocessing improvement steps

Given enough time: Difference logic Delayed Theory Combination

Improvement on Nelson-Oppen Not related to Linear Arithmetic

Page 3: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

4

Reminder: DPLL-T

Our goal is to solve SMT problems with formulas from a theory T

DPLL-T is the most common approach Based on the DPLL algorithm for SAT problems Combines a decision procedure for the theory T

Page 4: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

5

Reminder: DPLL-T

Page 5: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

6

Linear Arithmetic

Theory grammar:

Can be defined over Rational () or Integers ()

Example:

Page 6: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

7

Why is it needed?

Given the following C code:

The following assembly can be generated:

Page 7: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

8

Why is it needed?

A possible optimization: Read the value of a[j] only once

Need to verify that loop won’t change a[j] Can be encoded as Linear Arithmetic problem

If no solution is found, optimization is safe

Page 8: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

9

Decision Procedures for

Gaussian’s elimination Fourier-Motzkin Simplex

Page 9: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

10

Gaussian’s elimination

A simple method for solving a set of equalities Less suitable for inequalities

Given a linear system Ax = b

A x = b

Page 10: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

11

Gaussian’s elimination

Manipulate A|b to an upper-triangular form

Then, solve backwards from the k’s row according to:

Page 11: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

12

Gaussian elimination - example

And now… x3 = -1, x2 = 3, x1 = 1 problem solved.

Page 12: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

13

Fourier-Motzkin Elimination

Earliest method for solving linear inequalities

Given linear non-strict inequalities:

Pick a variable and eliminate it Continue until all variables but one are eliminated

If problem included equalities, eliminate by assignment

Page 13: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

14

Fourier-Motzkin Elimination

nnmnm

n

b

b

b

x

x

x

aa

aa

aaa

:

:

:

:

......

::

::

:

....

2

1

2

1

1

2221

11211

bIA A system of conjoined linear inequalities

m constraints

n variables

Page 14: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

15

Fourier-Motzkin Elimination

1. When eliminating xn, partition the constraints according to the coefficient ain: ain > 0: upper bound

ain < 0: lower bound

assume ai,n >0

Page 15: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

16

Fourier-Motzkin Elimination

Example:(1) x1 – x2 ≤ 0

(2) x1 – x3 ≤ 0

(3) -x1 + x2 + 2x3 ≤ 0

(4) -x3 ≤ -1

Assume we eliminate x1.Upper bound

Upper bound

Lower bound

Category?

Page 16: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

17

Fourier-Motzkin Elimination

2. For each pair of a lower bound aln<0 andupper bound aun>0, we have

3. For each such pair, add a constraint

Page 17: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

18

Fourier-Motzkin Elimination

Example:(1) x1 – x2 ≤ 0

(2) x1 – x3 ≤ 0

(3) -x1 + x2 + 2x3 ≤ 0

(4) -x3 ≤ -1

(5) 2x3 ≤ 0 (from 1 and 3)

(6) x2 + x3 ≤ 0 (from 2 and 3)

We eliminate x1.

Page 18: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

19

Fourier-Motzkin Elimination

Example:(1) x1 – x2 ≤ 0

(2) x1 – x3 ≤ 0

(3) -x1 + x2 + 2x3 ≤ 0

(4) -x3 ≤ -1

(5) 2x3 ≤ 0 (from 1 and 3)

(6) x2 + x3 ≤ 0 (from 2 and 3)

(7) 0 ≤ -1 (from 4 and 5)Contradiction (the system is unsatisfiable)!

We eliminate x3.

Page 19: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

20

Complexity of Fourier-Motzkin

Worst-case complexity: m2n

Popular in compilers Because of simplicity

Popular when the problems are small – then it can be the fastest. Not suitable for large problems

Need another solution for big problems

Page 20: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

21

Simplex

Simplex was originally designed for solving optimization problems: Given coefficient matrix and bound vector find a

satisfying assignment that maximizes a goal function described by

Also known as Linear Programming

We are only interested in the feasibility problem If problem is feasible than it is satisfiable

Page 21: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

22

General form

Given same input as Fourier-Motzkin, convert input to general form

General form:

A combination of: Linear equalities of the form Lower and upper bounds on variables.

Page 22: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

23

Converting to General Form

Replace (where )

with

and

s1,..., sm are called the additional variables.

Page 23: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

24

Example 1

Convert

to:

Page 24: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

25

Example 2

Convert

to:

Page 25: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

26

Same problem, geometrically

Page 26: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

27

Matrix form

x y s1 s2 s3

Due to the additional variables: now A is an m x (n + m) matrix.

Page 27: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

28

The tableau

The diagonal part is inherent to the general form

Marked section will always be there

We can instead write:

x y s1 s2 s3

x y

s1

s2

s3

Page 28: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

29

The tableau

The tableau changes throughout the algorithm, but maintains its m x n structure

Distinguish between basic and nonbasic variables Initially, basic variables = the additional variables.

x y

s1

s2

s3

Page 29: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

30

The tableau

Denote by B – Basic variables N – Nonbasic variables

The tableau is simply a rewrite of the system:

The basic variables are also called the dependent variables. Their value is determined by the values of the other variables

Page 30: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

31

The general simplex algorithm

Simplex maintains and updates: The tableau, an assignment to all variables The bounds

Two invariants:

All nonbasic variables satisfy their bounds

Page 31: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

32

Invariants

Initially, B = additional variables N = problem variables (xi) = 0 for i {1,...,n+m}

Trivial to see that initial state satisfies invariants

Page 32: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

33

The simplex algorithm

The initial assignment satisfies If the bounds of all basic variables are satisfied by ,

return `Satisfiable’

Otherwise... choose a variable and pivot Pivoting is the basic step of the algorithm

Page 33: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

34

Pivoting

Find a basic variable xi that violates its bounds.

Suppose that (xi) < li We need to fix the value of xi

Find a nonbasic variable xj such that aij > 0 and (xj) < uj, or

aij < 0 and (xj) > lj

Such a variable xj is called suitable.

If there is no suitable variable – return ‘Unsatisfiable’

Page 34: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

35

Pivoting xi with xj

Solve equation i for xj:

From:

To:

Swap xi and xj, and update the i-th row accordingly.

From

To:

ai1 ... aij ... ain

-ai1

aij

... 1

aij

... -ain

aij

Page 35: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

36

Pivoting xi with xj

Update all other rows: Replace xj with its equivalent obtained from row i:

Page 36: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

37

Pivoting

Update as follows: Increase (xj) by

Now xj is a basic variable: it can violate its bounds.

Update (xi) to li

Update for all other basic (dependent) variables.

Page 37: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

38

Example

Recall the tableau and constraints in our example:

Initially assigns 0 to all variables Bounds of s1 and s3 are violated

Page 38: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

39

Example

Recall the tableau and constraints in our example:

We will pivot on s1

x is a suitable nonbasic variable for pivoting It has no upper bound

So now we pivot s1 with x

Page 39: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

40

Example

Recall the tableau and constraints in our example:

Solve 1st row for x: Replace x with s1 in other rows:

Page 40: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

41

Example

The new state:

Solve 1st row for x: Replace x with s1 in other rows:

Page 41: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

42

Example

The new state:

We should increase x by Hence, (x) = 0 + 2 = 2

Now s1 is equal to its lower bound: (s1) = 2 Update all the others

Page 42: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

43

Example

The new state:

Now s3 violates its lower bound

y is a suitable nonbasic variable for pivoting

Page 43: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

44

Example

The new state:

We should increase y by

Page 44: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

45

Example

The final state:

All constraints are now satisfied

Page 45: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

46

A few observations

The additional variables: Only additional variables have bounds. These bounds are permanent. Additional variables exit the base only on extreme points

(their lower or upper bounds). When entering the base, they shift towards the other bound

and possibly cross it (violate it).

Page 46: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

47

A few observations

Can it be that we pivot(xi,xj) and then pivot(xj,xi) and enter a (local) cycle ? No.

Is termination guaranteed ? No. Perhaps there are bigger cycles.

Page 47: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

48

A few observations

In order to avoid circles, we use Bland’s rule: determine a total order on the variables. Choose the first basic variable that violates its bounds, and

first nonbasic suitable variable for pivoting. It can be proven that this guarantees that no base is

repeated, which implies termination. We won’t prove this

Page 48: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

49

A few observations

Simplex is exponential in the worst case However, considered very efficient on most real

practical problems Need for exponential number of steps is rare

Page 49: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

50

Decision Procedures for

Decision problem for is NP-hard Unlike

Branch & Bound Cuts

Omega test Based on Fourier-Motzkin elimination Will not be discussed today

Page 50: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

51

Decision Procedures for

Both Branch & Bound and Cuts rely on a solver to provide a (possibly non-integer) solution For example: Simplex

All variables in the final solution must be integers In the case of simplex, this does not includes the additional

variables The additional variables do not have to be a part of the outputted

solution

Page 51: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

52

x1

x2

Non-Integral Solution

Is there a difference?

52

Previous methods found non-integral solutions

Rounding will not suffice

Page 52: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

53

Is there a difference?

Previous methods found non-integral solutions

Rounding will not suffice No guarantee that integral

solution exists

53

x1

x2

Page 53: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

54

Assume that all variables are finite. Enumerate all solutions with a tree

Guaranteed to find a feasible solution if it exists But, exponential growth in the size of the tree /

computation time

A naïve solution

x1=0

x2=0 x2=2x2=1

x1=1 x1=2

x2=0 x2=2x2=1x2=0 x2=2x2=1

Page 54: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

55

Branch and Bound

The main idea: Solve the ‘relaxed’ problem, i.e. no integrality constraints. If the relaxed problem is infeasible – backtrack (there is no integer

solution in this branch) If the solution is integral – terminate (‘feasible’). Otherwise split on a variable for which the assignment is non-integral,

and repeat for each case.

Branch & Bound is guaranteed to find an integer solution if one exists

Branch & Bound can extended to handle the case where some of the variables are integers and some remain rational

Page 55: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

56

x2≤1

Splitting on non-integral solutions.

x1

x2

x1

x2

Solve Relaxation to get Non-Integral solutions If relaxation is infeasible, prune branch

Create two sub-branches by adding constraints

x2≥2

Non-integral solution

Page 56: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

57

Example

Suppose our system A has variables x1… x4, and that the solver returned a solution (1, 0.7, 2.5, 3).

Choose one of x2, x3. Suppose we choose x2.

Solve two new problems: A1 = A {x2 0}

A2 = A {x2 1}

Clearly A1 or A2 are satisfiable iff A is.

Page 57: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

58

The branch and bound tree

A

A2 A1

A12 A11

(1,0 .7,2.5,3)

(1,-1.5,1.5,4.1)

x2 · 0 x2 ¸ 1

x3 ¸ 1

(1,3,0.5,2)

(1,3,0.5,2)

x3 · 0

(1,3,4,1) x

Each leaf is a feasible solution.

Pruned due to infeasibility

Page 58: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

59

Termination

x

y

Does B & B guaranteed to terminate ? No.

For example: Consider a constraint like This constraint won’t terminate

Page 59: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

60

Termination

Does B & B guaranteed to terminate ? No.

For example: Consider a constraint like This constraint won’t terminate

It is always possible to find tighter bounds on variable values Will not be discussed

Page 60: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

62

Improvement - Cutting Planes

Eliminate non-integer solutions by adding constraints (i.e. improve tightness of relaxation).

All feasible integer solutions remain feasible

Last non-integer solution is not feasible

x1

x2

Added Cut

Page 61: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

63

Cutting Planes

Cutting planes might never find a solution Will get exponentially closer to a solution

Usually applied in conjunction with Branch & Bound

x1

x2

Added Cut

Page 62: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

64

Cutting planes

Adding valid inequalities

Examples: From 2x 11

we can conclude x 5

What can be learned from the bounds , the constraint and

the assignment ?

Page 63: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

65

Cutting planes

Examples: What can be learned from the bounds , the constraint and

the assignment ?

We know that the following currently holds:

We need to make both sides of the equation integers

Page 64: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

66

Cutting planes

Examples: We need to make both sides of the equation integers

1, 0.5 are lower bounds therefore right side of equation is positive

Therefore:

In case of upper bounds replace “” with “”

New constraint eliminates current assignment

Page 65: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

67

Gomory cuts

If basic variable as non-integer value and all nonbasic variables are at bounds => we can apply cut

Given constraint of the formand assignment

Split nonbasic variables into 2 groups:

Page 66: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

68

Gomory cuts

Substract bounds from constraint

And make left side integer

Page 67: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

69

Gomory cuts

Further split J and K:

Page 68: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

70

Gomory cuts

If right side is positive:

Page 69: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

71

Gomory cuts

If right side is negative:

Page 70: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

72

New constraint:

Gomory cuts

Could probably be 2

Page 71: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

73

Preprocessing

Constraints can be removed Example:

x1 + x2 2, x1 1, x2 1 First constraint is redundant.

In general, for a set:

is redundant if

Page 72: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

74

Preprocessing

Bounds can be tightened Example:

2x1 + x2 2, x2 4, x1 3 From 1st and 2nd constraints: x1 -1

In general, if a0 > 0

And, if a0 < 0

Page 73: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

75

Preprocessing (Only for integers)

Convert strict inequalities to non-strict inequalities Example:

Given inequality

Rewrite as Or

Page 74: 1 Decision Procedures for Linear Arithmetic Presented By Omer Katz 01/04/14 Based on slides by Ofer Strichman

77

Questions?