Professional Documents
Culture Documents
Solving The Max2Sat Problem: Sonia Cafieri
Solving The Max2Sat Problem: Sonia Cafieri
Sonia Cafieri
LIX, École Polytechnique
cafieri@lix.polytechnique.fr
2 Graphical representation
Implication graph
2 Graphical representation
Implication graph
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.
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 .
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.
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 )
(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 assigmnent for a CNF formula Φ is complete if all the variables occurring in Φ have
been assigned, otherwise it is partial.
2 Graphical representation
Implication graph
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.
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.
Two versions:
decision version
optimization version.
Definition
decision version
Definition
optimization version
Definition
decision version
Definition
optimization version
Note that:
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.
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
2 Graphical representation
Implication graph
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.
MAXSAT problems are difficult problems: the time required to solve the problem
increases very quickly as the size of the problem grows.
2 Graphical representation
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".
a∨b
is equivalent to:
ā ⇒ b
b̄ ⇒ a
Note: for 1 clause in the 2CNF formula, we have 2 arcs in the graph.
Example 1:
(x̄1 ∨ x2 ) ∧ (x1 ∨ x̄2 ) ∧ (x̄2 ∨ x3 ) ∧ (x̄1 ∨ x̄3 )
Example 2:
Example 3:
a more complicated instance
2 Graphical representation
Implication graph
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.
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).
...
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.
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.
Basic algorithm:
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.
Example
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.
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!
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.
Some observation:
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.
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].
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
2 Graphical representation
Implication graph
Can we use AMPL for solving the MAX2SAT problem by means of the Branch and
Bound algorithm?
Of course we can!
## 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];
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
;
# max2satBB.run
option solver_msg 0;
# model
model max2sat.mod;
# data file
data max2sat.dat;
display xstar;
printf "max clauses = %d\n", fstar;
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.
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].
if: evaluates a condition and executes some specified command if the condition
is true.
Example:
if (i <> 4) then {
let v[i] := i;
}
# solve LP relaxation
option solver cplex;
solve > /dev/null;
} 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:
.....
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.
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.
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 {
.....
We have learned:
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.