Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

Solving the Max2Sat problem

Sonia Cafieri
LIX, École Polytechnique
cafieri@lix.polytechnique.fr

Nov 18th 2008

S. Cafieri (LIX) max2sat problem 18/11/08 1 / 58


Outline

1 Basic concepts and definitions


Some definition
Problem statement
Alternative formulations

2 Graphical representation
Implication graph

3 Solving the problem


Solution methods
Solving MAX2SAT using AMPL

S. Cafieri (LIX) max2sat problem 18/11/08 2 / 58


Outline

1 Basic concepts and definitions


Some definition
Problem statement
Alternative formulations

2 Graphical representation
Implication graph

3 Solving the problem


Solution methods
Solving MAX2SAT using AMPL

S. Cafieri (LIX) max2sat problem 18/11/08 3 / 58


Boolean variables

Definition
A boolean variable x is a variable that can assume only two values 0 and 1.
0 is for false, 1 for true.
The negation x̄ of a boolean variable x is the variable that assumes the value 1 − x.

Note that ¯x̄ = x.

S. Cafieri (LIX) max2sat problem 18/11/08 4 / 58


Literals and Clauses

Definition
A literal l is a variable x or its negation x̄.

Let X be a set of boolean variables. For every x ∈ X there are 2 literals over x, namely
x itself and x̄.

Definition
A clause over a set of boolean variables is a disjunction of literals.

Example:
suppose we have 2 variables x1 , x2
we create a clause by using the or operator: x1 ∨x̄2 .

S. Cafieri (LIX) max2sat problem 18/11/08 5 / 58


Literals and Clauses

Definition
The length of a clause is the number of its literals.

Example:
x1 ∨ x̄2 ∨ x̄3
is a clause of length 3
its literals are x1 , x̄2 and x̄3
if x1 = 0, x2 = 0, x3 = 1, then its value is 1.

S. Cafieri (LIX) max2sat problem 18/11/08 6 / 58


CNF formula
Definition
A boolean formula in Conjunctive Normal Form (CNF) Φ is a conjunction of clauses:
^
m
Φ= Ci , where Ci are clauses, ∀i ∈ {1, .., m}.
i=1

Example:
suppose we have 3 variables: x1 , x2 , x3
we can create some clauses by using the or operator:

(x1 ∨x̄2 ), (x̄1 ∨x2 ), (x̄2 ∨x̄3 ), (x1 ∨x3 ), (x̄1 ∨x̄2 ), (x̄1 ∨x2 )

then we can join the clauses by using the and operator:

(x1 ∨x̄2 )∧(x̄1 ∨x2 )∧(x̄2 ∨x̄3 )∧(x1 ∨x3 )∧(x̄1 ∨x̄2 )∧(x̄1 ∨x2 )

Definition
The size of a CNF formula is the sum of the length of all its clauses.
S. Cafieri (LIX) max2sat problem 18/11/08 7 / 58
Assigment of values

Definition
An assigment of values to the set X of variables of a boolean formula is called a truth
assignment.

An assignment of truth values to the propositional variables:


satisfies a literal x if x takes the value 1
satisfies a literal x̄ if x takes the value 0
satisfies a clause if it satisfies at least one literal of the clause
satisfies a CNF formula if it satisfies all the clauses of the formula.

An assigmnent for a CNF formula Φ is complete if all the variables occurring in Φ have
been assigned, otherwise it is partial.

S. Cafieri (LIX) max2sat problem 18/11/08 8 / 58


Outline

1 Basic concepts and definitions


Some definition
Problem statement
Alternative formulations

2 Graphical representation
Implication graph

3 Solving the problem


Solution methods
Solving MAX2SAT using AMPL

S. Cafieri (LIX) max2sat problem 18/11/08 9 / 58


The 2-SAT problem

Definition
Given a boolean formula in Conjunctive Normal Form, consisting of a conjunction of
m clauses Ci , each of which with 2 literals, the 2-Satisfiability Problem, also called
2-SAT, is the problem of deciding if there is a truth assignment to the literals such that
each clause is satisfied (i.e. its value is 1).

Depending on whether this is possible or not, we say that the formula is satisfiable or
unsatisfiable.

Each instance of the 2-SAT problem is a 2CNF formula.

To decide if there exists a truth assignment to the literals such that each clause is satisfied is
equivalent to ask whether or not the boolean expression C1 × · · · × Cm can take value 1, where
’×’ is the boolean multiplication:
1 × 1 = 1, 1 × 0 = 0, 0 × 0 = 0.

S. Cafieri (LIX) max2sat problem 18/11/08 10 / 58


The MAX2SAT problem

Two versions:

decision version
optimization version.

A decision problem is a question, in some formal system, with a answer YES/NO.

An optimization problem is concerned to finding the best answer to a particular prob-


lem.

S. Cafieri (LIX) max2sat problem 18/11/08 11 / 58


The MAX2SAT problem

Definition
decision version

Given a boolean formula in Conjunctive Normal Form, consisting of a conjunction of


m clauses Ci , each of which with 2 literals, and given an integer k, the Maximum 2-
Satisfiability problem, also called MAX2SAT, is the problem of deciding if there is a
truth assignment to the literals such that satisfies at least k clauses.

Definition
optimization version

Given a boolean formula in Conjunctive Normal Form, consisting of a conjunction


of m clauses Ci , each of which with 2 literals, the Maximum 2-Satisfiability problem,
also called MAX2SAT, is to find a truth assignment to the literals that maximizes the
number of satisfied clauses.

S. Cafieri (LIX) max2sat problem 18/11/08 12 / 58


The MAX2SAT problem

Definition
decision version

Given a boolean formula in Conjunctive Normal Form, consisting of a conjunction of


m clauses Ci , each of which with 2 literals, and given an integer k, the Maximum 2-
Satisfiability problem, also called MAX2SAT, is the problem of deciding if there is a
truth assignment to the literals such that satisfies at least k clauses.

Definition
optimization version

Given a boolean formula in Conjunctive Normal Form, consisting of a conjunction


of m clauses Ci , each of which with 2 literals, the Maximum 2-Satisfiability problem,
also called MAX2SAT, is to find a truth assignment to the literals that maximizes the
number of satisfied clauses.

S. Cafieri (LIX) max2sat problem 18/11/08 12 / 58


The MAX2SAT problem

Note that:

MAX2SAT – decision version –


if k = m then MAX2SAT is the same as 2SAT.

MAX2SAT – optimization version –


asks for the maximum number of satisfied clauses.
To maximize the number of satisfied clauses is equivalent to minimize the number
of unsatisfied clauses.

S. Cafieri (LIX) max2sat problem 18/11/08 13 / 58


The MAXSAT problem

The natural generalization of MAX2SAT is the MAXSAT problem.

Definition
The Maximum Satisfiability problem (MAXSAT) asks for the maximum number of
clauses which can be satisfied by any assignment.
The clauses have not a limit on the number of literals.

There are several extensions to MAXSAT:


The weighted maximum satisfiability problem (Weighted MAXSAT) asks for
the maximum weight which can be satisfied by any assignment, given a set of
weighted clauses.
The partial maximum satisfiability problem (PMAXSAT) asks for the maximum
number of clauses which can be satisfied by any assignment of a given subset of
clauses. The rest of the clauses must be satisfied.

S. Cafieri (LIX) max2sat problem 18/11/08 14 / 58


Applications

SAT and MAXSAT are central problems in Artificial Intelligence, Logic and Compu-
tational Complexity.
Many important real-world applications:

scheduling
electronic design automation
computer architecture design
pattern recognition
inference in Bayesian networks

S. Cafieri (LIX) max2sat problem 18/11/08 15 / 58


Outline

1 Basic concepts and definitions


Some definition
Problem statement
Alternative formulations

2 Graphical representation
Implication graph

3 Solving the problem


Solution methods
Solving MAX2SAT using AMPL

S. Cafieri (LIX) max2sat problem 18/11/08 16 / 58


MAXSAT as an Integer Programming problem
A logical variable vj (TRUE or FALSE) is replaced by a corresponding integer variable
xj , that takes values 1 or 0. So:
- an unnegated literal is simply replaced by xj ,
- a negated literal is replaced by the expression 1 − xj .
A clause is satisfied if and only if at least one of its nl literals is TRUE. For the integer
problem, we sum the corresponding nl expressions. The clause is satisfied if and only
if the sum is 1 or more.
Example:
the clause v̄1 ∨ v2 ∨ v5 ∨ v̄7
is satisfied if: (1 − x1 ) + x2 + x5 + (1 − x7 ) ≥ 1.
We must have some way of handling the maximization of satisfied clauses.
We do this by adding variables to the problem. A clause is either satisfied or it isn’t:
original clause satisfied ∨ original clause not satisfied.
We use a new variable for each clause:
yi = 1 if clause ϕi is satisfied
yi = 0 if clause ϕi is not satisfied.
S. Cafieri (LIX) max2sat problem 18/11/08 17 / 58
MAXSAT as an Integer Programming problem
A logical variable vj (TRUE or FALSE) is replaced by a corresponding integer variable
xj , that takes values 1 or 0. So:
- an unnegated literal is simply replaced by xj ,
- a negated literal is replaced by the expression 1 − xj .
A clause is satisfied if and only if at least one of its nl literals is TRUE. For the integer
problem, we sum the corresponding nl expressions. The clause is satisfied if and only
if the sum is 1 or more.
Example:
the clause v̄1 ∨ v2 ∨ v5 ∨ v̄7
is satisfied if: (1 − x1 ) + x2 + x5 + (1 − x7 ) ≥ 1.
We must have some way of handling the maximization of satisfied clauses.
We do this by adding variables to the problem. A clause is either satisfied or it isn’t:
original clause satisfied ∨ original clause not satisfied.
We use a new variable for each clause:
yi = 1 if clause ϕi is satisfied
yi = 0 if clause ϕi is not satisfied.
S. Cafieri (LIX) max2sat problem 18/11/08 17 / 58
MAXSAT as an Integer Programming problem
A logical variable vj (TRUE or FALSE) is replaced by a corresponding integer variable
xj , that takes values 1 or 0. So:
- an unnegated literal is simply replaced by xj ,
- a negated literal is replaced by the expression 1 − xj .
A clause is satisfied if and only if at least one of its nl literals is TRUE. For the integer
problem, we sum the corresponding nl expressions. The clause is satisfied if and only
if the sum is 1 or more.
Example:
the clause v̄1 ∨ v2 ∨ v5 ∨ v̄7
is satisfied if: (1 − x1 ) + x2 + x5 + (1 − x7 ) ≥ 1.
We must have some way of handling the maximization of satisfied clauses.
We do this by adding variables to the problem. A clause is either satisfied or it isn’t:
original clause satisfied ∨ original clause not satisfied.
We use a new variable for each clause:
yi = 1 if clause ϕi is satisfied
yi = 0 if clause ϕi is not satisfied.
S. Cafieri (LIX) max2sat problem 18/11/08 17 / 58
IP formulation

Let Φ be a CNF formula: Φ = ϕ1 ∧ · · · ∧ ϕm .


For all i ≤ m, let Yi = {j ≤ n | xj ∈ ϕi } and Ȳi = {j ≤ n | x̄j ∈ ϕi } and
x ∈ {0, 1}n, y ∈ {0, 1}m be binary decision variables.

Integer Programming formulation for MAXSAT:


X
max yi
X
i≤m X
s.t. ∀i ≤ m xj + (1 − xj ) ≥ yi
j∈Yi j∈Ȳi
n
x ∈ {0, 1}
y ∈ {0, 1}m

S. Cafieri (LIX) max2sat problem 18/11/08 18 / 58


Another formulation

Another formulation for the pure satisfiability SAT problem is the following.

X
min xj (1 − xj )
X
j≤n X
s.t. ∀i ≤ m xj + (1 − xj ) ≥ 1
j∈Yi j∈Ȳi
n
x ∈ [0, 1]

The SAT problem has a solution if and only if the optimal objective function value is
0, which implies that either xj = 0 or xj = 1 for all j ≤ n.

S. Cafieri (LIX) max2sat problem 18/11/08 19 / 58


Complexity

SAT was the first known NP-complete problem [Cook, 1971].


(NP standing for Nondeterministic Polynomial time – any given solution of the problem p
can be verified in polynomial time and every other problem in NP can be reduced to p).

MAX2SAT is shown to be NP-complete by reduction from 3SAT.

The more general MAXSAT is also NP-complete.

2SAT is polynomially solvable.

MAXSAT problems are difficult problems: the time required to solve the problem
increases very quickly as the size of the problem grows.

S. Cafieri (LIX) max2sat problem 18/11/08 20 / 58


Outline

1 Basic concepts and definitions


Some definition
Problem statement
Alternative formulations

2 Graphical representation
Implication graph

3 Solving the problem


Solution methods
Solving MAX2SAT using AMPL

S. Cafieri (LIX) max2sat problem 18/11/08 21 / 58


Implication graph

Definition
An implication graph is a directed graph G(V, A) (V is the set of verteces and A the set
of directed arcs), where each vertex u ∈ V represents the truth status of a boolean lit-
eral, and each directed arc a(u, v) ∈ A, which connects vertex u and vertex v, represents
the implication "if the literal u is true then the literal v is also true".

Implication graphs can be used for analyzing complex boolean expressions.


An instance of the MAX2SAT problem can be represented using an implication
graph.

S. Cafieri (LIX) max2sat problem 18/11/08 22 / 58


Implication graph

A 2CNF formula (i.e. an instance of MAX2SAT or 2SAT) can be transformed into an


implication graph by replacing each of its disjunctions by a pair of implications and
then representing these implications graphically as directed arcs in a graph.

Given two literals a and b, the disjunction

a∨b

is equivalent to:

ā ⇒ b
b̄ ⇒ a

Note: for 1 clause in the 2CNF formula, we have 2 arcs in the graph.

S. Cafieri (LIX) max2sat problem 18/11/08 23 / 58


Implication graph

Example 1:
(x̄1 ∨ x2 ) ∧ (x1 ∨ x̄2 ) ∧ (x̄2 ∨ x3 ) ∧ (x̄1 ∨ x̄3 )

- clause 1 x̄1 ∨ x2 gives: x1 ⇒ x2 and x̄2 ⇒ x̄1


- clause 2 x1 ∨ x̄2 gives: x̄1 ⇒ x̄2 and x2 ⇒ x1
- clause 3 x̄2 ∨ x3 gives: x2 ⇒ x3 and x̄3 ⇒ x̄2
- clause 4 x̄1 ∨ x̄3 gives: x1 ⇒ x̄3 and x3 ⇒ x̄1

S. Cafieri (LIX) max2sat problem 18/11/08 24 / 58


Implication graph

Example 2:

(x1 ∨ x̄2 ) ∧ (x̄1 ∨ x2 ) ∧ (x̄2 ∨ x̄3 ) ∧ (x1 ∨ x3 ) ∧


(x̄1 ∨ x̄2 ) ∧ (x1 ∨ x2 ) ∧ (x2 ∨ x3 ) ∧ (x2 ∨ x̄3 )

S. Cafieri (LIX) max2sat problem 18/11/08 25 / 58


Implication graph

Example 3:
a more complicated instance

2CNF formula with


m = 200 clauses
n = 60 variables

S. Cafieri (LIX) max2sat problem 18/11/08 26 / 58


Outline

1 Basic concepts and definitions


Some definition
Problem statement
Alternative formulations

2 Graphical representation
Implication graph

3 Solving the problem


Solution methods
Solving MAX2SAT using AMPL

S. Cafieri (LIX) max2sat problem 18/11/08 27 / 58


How can we solve the problem?

A very simple algorithm: exhaustive search.


Enumerate all possible 1 and 0 configurations, and verify each one.

As the number of variables becomes large, enumerating all possible combinations


becomes impossible.
For n variables, this search can require 2n tests.
Ex.: n = 5, there are 25 = 32 possible configurations:
00000, 00001, 00010, 00100, . . . , 11111.

S. Cafieri (LIX) max2sat problem 18/11/08 28 / 58


Solution methods
Two classes of methods are used to solve SAT and MAXSAT problems:
exact methods and approximate methods.
Exact methods
guarantee finding an optimal solution for an instance or, if there is no solution, proving its
unsatisfiability
perform exhaustive searches
do not check every possible configuration (they usually use strategies to speed up the
search)
their performance can be limited for large instances
are generally based on the exploration of a binary search tree, building incrementally truth
assignments.

Approximate methods
do not guarantee finding an optimal solution
are quickly in finding a solution (if they find it)
are mainly based on local search and evolutionary algorithms.

S. Cafieri (LIX) max2sat problem 18/11/08 29 / 58


Solution methods
Exact methods

variants of Davis-Putnam-Logemann-Loveland (DPLL) procedure


(designed for SAT - deterministic tree-search with backtracking) [Davis et al., 1962]
Branch and Bound (BB) algorithms [Borchers and Furman, 1999].

Approximate methods

GSAT
(greedy local search – starts with a randomly generated assignment, then repeatedly changes
(’flips’) the assignment of the variable that leads to the largest decrease in the total number
of unsatisfied clauses).
WalkSAT
(starts with randomly generated assignment, then repeatdly picks a clause which is unsat-
isfied by the current assignment and flips a variable within that clause).
...

From now on, we will focus on BB algorithms.


S. Cafieri (LIX) max2sat problem 18/11/08 30 / 58
Branch and Bound algorithm

Branch and Bound algorithms are usually applied for solving general Mixed Integer
Linear Programming (MILP) problems.
They work by recursively partitioning the search space and computing a lower and an
upper bound to the optimal value of the objective function in each subregion.
For a minimization problem, the lower bound is usually computed by constructing and
solving a linear relaxation, and the upper bound can simply be a local optimum found
by a local MILP solver.

S. Cafieri (LIX) max2sat problem 18/11/08 31 / 58


Branch and Bound algorithm

Let’s try to use a BB algorithm for solving a MAX2SAT problem.

Basic concepts:

The space of all possible assignments for a CNF formula can be represented as
a search tree, where internal nodes represent partial assignments and leaf nodes
represent complete assignments.

A Branch and Bound algorithm for MAXSAT explores the search tree in a depth-
first manner.

Let’s examine these concepts in details.

S. Cafieri (LIX) max2sat problem 18/11/08 32 / 58


Branch and Bound algorithm
SAT problem

First consider the SAT problem.

Basic algorithm:

Select a variable and set its value to 0 or 1.


Note: different 0/1 assignments create branches of the search tree.

Replace the selected variable with the selected value in the formula.

If the selected value satisfies all the clauses, then assign a value to the next vari-
able, and so on.

Else, if the selected value fails to satisfy one of the clauses, prune the current
branch and backtrack the search.

Keep performing this process until all clauses are satisfied.

S. Cafieri (LIX) max2sat problem 18/11/08 33 / 58


Search tree

Example of binary search tree

S. Cafieri (LIX) max2sat problem 18/11/08 34 / 58


Branch and Bound algorithm
SAT problem

Example

(x̄2 ∨ x̄5 ) ∧ (x1 ∨ x̄3 ) ∧ (x4 ∨ x̄5 ) ∧ (x1 ∨ x2 )

Start setting x1 = 0 and substitute this value into the formula.


We get a 0 in two clauses, but we don’t know yet if the formula is satisfied or not.
Go to the next variable in the tree, x2 . Set x2 = 0.
The 4-th clause is unsatisfied, because has both x1 and x2 as 0.
Therefore, we don’t continue exploring that branch, since we cannot satisfy the
formula.
Try the other branch for x2 : set x2 to 1. The 4-th clauses is now satisfied.
Set x3 = 0. The 2-nd clauses is satisfied.
Set x4 = 0.
Set x5 = 0. Then all clauses are satisfied: we solved the SAT problem.

S. Cafieri (LIX) max2sat problem 18/11/08 35 / 58


Branch and Bound algorithm
SAT problem

Observation
Notice that we didn’t need to go through every branch of the tree to find the solution.

Observation
If an assigned variable fails to satisfy one of the clauses, then we prune the correspond-
ing branch of the search tree.

What does it happen when we consider MAXSAT?

S. Cafieri (LIX) max2sat problem 18/11/08 36 / 58


Branch and Bound algorithm
MAXSAT problem

Consider now the MAXSAT problem.

We want to satisfy the maximum number of clauses, not all the clauses.
So, if we find an unsatisfied clause when assigning a value to a variable, then we
have not to stop at that point and go back to the other branch.
If a certain 0/1 value for a variable fails to satisfy a clauses, it could be that the
same value satisfies many other clauses: it is possible that continuing down on
that branch we get maximum satisfiability.
We have to explore more branches!

When can we stop exploring a branch?

S. Cafieri (LIX) max2sat problem 18/11/08 37 / 58


BB algorithm for MAX2SAT

Consider the IP formulation of the MAX2SAT problem.


Algorithm:

At each node of the search tree solve the Linear Programming (LP) relaxation
obtained by replacing the integrality requirements by the simple bounds:
∀j ∈ {1, . . . , n} 0 ≤ xj ≤ 1, ∀i ∈ {1, . . . , m} 0 ≤ yi ≤ 1.

If the solution to the LP is infeasible, backtrack to a higher level in the search tree.

If the solution to the LP is integral, compare it to the best integral solution found
so far. Backtrack to a higher level in the search tree.

Else (if the solution has one or more fractional variables) branch on one of the
fractional variables and repeat the procedure.

S. Cafieri (LIX) max2sat problem 18/11/08 38 / 58


BB algorithm for MAX2SAT

Some observation:

Branching allows to extend the current partial assignment by instantiating a new


variable. This leads to the creation of two new branches of the tree from the
current branch: the left branch corresponds to instantiate the new variable to 0,
the right branch corresponds to instantiate the new variable to 1.

Backtracking is used to go back to a higher level in the search tree and continue
exploring the tree along others branches.

When we find an assignment of values such that the number of satisfied clauses is
greater than the number we previously found, we store the values we have found.
The corresponding value of the objective function is the current bound and it
represents the current best value of the objective function.

S. Cafieri (LIX) max2sat problem 18/11/08 39 / 58


BB algorithm for MAX2SAT

For the sake of simplicity, we only consider the basic BB algorithm for MAX2SAT,
but usually suitable strategies are used to reduce the computational complexity.

Observation
BB algorithms for MAX2SAT usually implement methods to reduce the input formula:
the formula is simplified according to transformation rules without changing the max-
imum number of satisfiable clauses
[Gramm, PhD thesis, 1999].

S. Cafieri (LIX) max2sat problem 18/11/08 40 / 58


BB algorithm for MAX2SAT
Input a 2CNF instance
while (termination test not satisfied) do
solve the continuous relaxation
if (the solution is infeasible) then
backtracking
else
if (the solution is integer) then
if (the current value of the obj. func. is greater than
the one previously found) then
store the current solution
endif
backtracking
else
branching
endif
endif
endwhile
The output is the maximum number of satisfied clauses.
S. Cafieri (LIX) max2sat problem 18/11/08 41 / 58
BB algorithm for MAX2SAT: problem relaxation

The linear continuous relaxation is obtained by relaxing the integrality constraints on


the variables.

Let Φ be a CNF formula: Φ = ϕ1 ∧ · · · ∧ ϕm .


For all i ≤ m, let Yi = {j ≤ n | xj ∈ ϕi } and Ȳi = {j ≤ n | x̄j ∈ ϕi } and
x ∈ Rn , y ∈ Rm be decision variables.

X
max yi
X
i≤m X
s.t. ∀i ≤ m xj + (1 − xj ) ≥ yi
j∈Yi j∈Ȳi
0≤x≤1
0≤y≤1

S. Cafieri (LIX) max2sat problem 18/11/08 42 / 58


BB algorithm for MAX2SAT: branching rule

We branch if we find a solution containing still fractional values.


A lot of branching rules are possible. What rule can we use?

A very simple branching rule:


branch on the first fractional variable xi that we find, for i ∈ {1, . . . , n}.
Using this criterion, branching index is:
i = min {1, . . . , n} | xi ∈
/ {0, 1}.

S. Cafieri (LIX) max2sat problem 18/11/08 43 / 58


Outline

1 Basic concepts and definitions


Some definition
Problem statement
Alternative formulations

2 Graphical representation
Implication graph

3 Solving the problem


Solution methods
Solving MAX2SAT using AMPL

S. Cafieri (LIX) max2sat problem 18/11/08 44 / 58


Using AMPL to implement the algorithm

Can we use AMPL for solving the MAX2SAT problem by means of the Branch and
Bound algorithm?
Of course we can!

We can write an AMPL model for the LP relaxation of MAX2SAT.


We can solve at each node of the search tree the LP problem using the solver
CPLEX.
We can implement the BB algorithm, incorporating the above solution, using the
AMPL language:
scripts of AMPL commands can be set up to run as programs that perform repeti-
tive actions. A script can solve a series of instances, by including a solve statement
inside a loop [http://www.ampl.com/].

S. Cafieri (LIX) max2sat problem 18/11/08 45 / 58


MAX2SAT problem (relaxation): AMPL model
## parameters
param m, integer; # number of clauses
param n, integer; # number of variables (2n is the number of literals)
set M := 1..m;
set N := 1..n;
# phi[i,j] = 1 if x_j appears in clause i
# = -1 if bar{x}_j appears in clause i
# = 0 otherwise
param phi{M,N} integer, default 0;

## variables
# x = 1 if literal x is true and 0 if false
# y = 1 if clause is satisfied

## continuous relaxation
var x{ i in N} >= 0, <=1;
var y{ i in M} >= 0, <=1;

## objective
maximize clauses: sum{i in M} y[i];

## constraints
subject to clause {i in M} :
sum{j in N : phi[i,j] = 1} x[j] +
sum{j in N : phi[i,j] = -1} (1-x[j]) >= y[i];

S. Cafieri (LIX) max2sat problem 18/11/08 46 / 58


Encoding an instance: an AMPL data file
param m := 4; # number of clauses
param n := 3; # number of variables

# phi[i,j] = 1 if x_j appears in clause i


# = -1 if bar{x}_j appears in clause i
# = 0 otherwise

param phi :=
1 1 -1
1 2 1
2 1 1
2 2 -1
3 2 -1
3 3 1
4 1 -1
4 3 -1
;

This corresponds to the following instance:

(x̄1 ∨ x2 ) ∧ (x1 ∨ x̄2 ) ∧ (x̄2 ∨ x3 ) ∧ (x̄1 ∨ x̄3 )

S. Cafieri (LIX) max2sat problem 18/11/08 47 / 58


Solving MAX2SAT: an AMPL .run file

# max2satBB.run
option solver_msg 0;

# model
model max2sat.mod;

# data file
data max2sat.dat;

# branch and bound


include BB.run;

display xstar;
printf "max clauses = %d\n", fstar;

S. Cafieri (LIX) max2sat problem 18/11/08 48 / 58


Solving MAX2SAT: an AMPL .run file
Observations

Our .run file use an "include" command.


The AMPL command
include filename

cause input to be taken from a file named filename: the command is replaced by the
content of filename.

We include BB.run, containing the implementation of the BB algorithm. All the ampl
commands contained in this file will be executed.
BB.run is an AMPL script.

S. Cafieri (LIX) max2sat problem 18/11/08 49 / 58


AMPL scripts

AMPL scripts can contain any AMPL command and may include programming lan-
guage constructs like for, repeat and if to repeat statements and perform them
conditionally [Fourer, Gay, Kernighan, AMPL, 2nd edition].

Iterating over a set: the for statement.


Iterating subject to a condition: the repeat statement.
Testing a condition: the if-then-else statement.

S. Cafieri (LIX) max2sat problem 18/11/08 50 / 58


AMPL scripts
for: executes commands for each member of a set.
Example:
for {i in 1..4} {
let v[i] := i;
}

repeat: continues iterating as long as a logical condition is satisfied.


Example:
repeat while (i <= 4) {
let v[i] := i;
}

if: evaluates a condition and executes some specified command if the condition
is true.
Example:
if (i <> 4) then {
let v[i] := i;
}

S. Cafieri (LIX) max2sat problem 18/11/08 51 / 58


BB for MAX2SAT: AMPL script framework
# param definitions
...
repeat while (termination = 0) {

# retrieve x’ from list


# fix x’ (to 0 or 1)

# solve LP relaxation
option solver cplex;
solve > /dev/null;

if (solve_result = "infeasible") then {


# no branching

} else {
# check if all xi are binary (integ = 1)
if (integ = 1) then {
if (clauses > fstar) then {
let fstar := clauses;
let {i in N} xstar[i] := x[i];
}
# no branching

} else { # branching
# choice of the branching variable
# update list
}
}
# check for termination
} S. Cafieri (LIX) max2sat problem 18/11/08 52 / 58
BB for MAX2SAT: use of AMPL iteration and conditional
statements

Main loop:

repeat while (termination = 0) {

.....

The parameter termination is initially set to 0. The loop causes performing itera-
tively the commands specified therein until termination is changed to 1: this hap-
pens when all possible configurations have been tested.

S. Cafieri (LIX) max2sat problem 18/11/08 53 / 58


BB for MAX2SAT: use of AMPL iteration and conditional
statements
Another use of the repeat statement: choice of the branching variable.
Iterations are performed until the flag parameter is changed to 1, that is when the first
non-integer variable is encountered, or all the variables have been examined (so the
counter ii is qual to n.

# choice of branching variable


let ii := 1;
let flag := 0;
repeat while (ii <= n and flag = 0) {
if (x[ii] <> 0 and x[ii] <> 1) then {
let l := ii;
let flag := 1;
}
let ii := ii+1;
}

Note also the use of the if statement.

S. Cafieri (LIX) max2sat problem 18/11/08 54 / 58


BB for MAX2SAT: use of AMPL iteration and conditional
statements

Use of the for statement: in order to check integrality of the variables, we use a loop
on N and test the values of all variables.
Use of the if ...then statement: at each iteration i, the if statement evaluates the
condition xi 6= 0 and xi 6= 1 and, if it is satisfied (i.e. variable xi is not binary), then
changes the value of the parameter integ to 0.

# check if all xi are binary


let integ := 1;
for {i in N} {
if (x[i] <> 0 and x[i] <> 1) then {
let integ := 0;
}
}

S. Cafieri (LIX) max2sat problem 18/11/08 55 / 58


BB for MAX2SAT: use of AMPL iteration and conditional
statements

Nested if statements: if all variables are integer, then check if the computed value
of the objective function (clauses) is greater than the current best value and, if so,
update the current best value and the current solution.

if (integ = 1) then {
if (clauses > fstar) then {
let fstar := clauses;
let {i in N} xstar[i] := x[i];
}
let branch := 0;

} else {
.....

S. Cafieri (LIX) max2sat problem 18/11/08 56 / 58


Summary

We have learned:

what satisfiability and maximum satisfiability problems are;

how MAX2SAT can be solved by a Branch and Bound algorithm;

how MAX2SAT can be solved using AMPL.

S. Cafieri (LIX) max2sat problem 18/11/08 57 / 58


Bibliography

J. Gramm.
Exact algorithms for Max2Sat and their applications.
PhD thesis, 1999.
M. Davis, G. Logemann, D. Loveland.
A machine program for theorem-proving.
Communications of the ACM, 5:394–397, 1962.

T. Alsiniet, F. Manyá, J. Planes.


Improved Branch and Bound algorithms for Max-Sat.
Proceedings of the 6th International Conference on the Theory and Applications of
Satisfiability Testing, 2003.

S. Joy, J.E. Mitchell, B. Borchers.


Solving MAX-SAT and Weighted MAX-SAT Problems Using Branch-and-Cut.
http://www.rpi.edu/ mitchj/papers.html, 1998.

S. Cafieri (LIX) max2sat problem 18/11/08 58 / 58

You might also like