Chapter 7 Constraint Programming
Chapter 7 Constraint Programming
OPTIMIZATION
Constraint Programming
CONTENT
• Constraint Satisfaction Optimization Problems
• Constraint Propagation
• Branching and Backtracking Search
• Examples
Constraint Satisfaction Problems
• Variables
• X = {X0, X1, X2, X3, X4}
• Domain
• X0, X1, X2, X3, X4 {1,2,3,4,5}
• Constraints
• C1: X2 + 3 ≠ X1
• C2: X3 ≤ X4
• C3: X2 + X3 = X0 + 1
• C4: X4 ≤ 3
• C5: X1 + X4 = 7
• C6: X2 = 1 X4 ≠ 2
Constraint Satisfaction Problems
• CSP = (X,D,C), in which:
• X = {X1,...,XN} – set of variables
• D = {D(X1),…, D(XN)} – domains of variables
• C = {C1,..,CK} – set of constraints over variables
• Denote X(c) – set of variables appearing in the constraint c
Constraint Satisfaction Problems
• COP = (X,D,C,f), in which:
• X = {X1,...,XN} – set of variables
• D = {D(X1),…, D(XN)} – domains of variables
• C = {C1,..,CK} – set of constraints over variables
• Denote X(c) – set of variables appearing in the constraint c
• f: objective function to be optimized
Constraint Programming
• A computation paradigm for solving CSP, COP combining
• Constraint Propagation: narrow the search space by pruning
redundant values from the domains of variables
• Branching (backtracking search): split the problem into
equivalent sub-problems by
• Instantiating some variables with values of its domain
• Split the domain of a selected variable into sub-domains
Constraint Programming
Constraint Propagation
• Domain consistency (DC)
• Given a CSP = (X,D,C), a constraint c C is called domain
consistent if for each variable Xi X(c) and each value vD(Xi),
there exists values for variables of X(c) \ {Xi} such that c is satisfied
• A CSP is called domain consistent if c is domain consistent for all c
C
Constraint Propagation
• DC algorithms aim at pruning redundant values from the domains of
variables so that the obtained equivalent CSP is domain consistent
Constraint Propagation
• Example: CSP = (X,D,C) in which:
• X = {X1, X2, X3, X4}
• D(X1) = {1,2,3,4}, D(X2) ={1,2,3,4,5,6,7}, D(X3) = {2,3,4,5}, D(X4) =
{1,2,3,4,5,6}
• C = {c1,c2,c3} với
• c1 X1 + X2 5
• c2 X1 + X3 X4
• c3 X1 + 3 X3
→ CSP is domain consistent
• When branching, consider X1 = 1, a DC algorithm will transform the
given CSP to an equivalent domain consistent CSP1 having : D1(X1)
= {1}, D1(X2) = {4,5,6,7}, D1(X3) = {2,3,4}, D1(X4) = {1,2,3,4,5}
Constraint Propagation
• A domain consistent CSP does not ensure to have feasible solutions
• Example:
• X = {X1,X2,X3}
• D(X1) = D(X2) = D(X3) = {0,1}
• c1 X1 ≠ X2, c2 X1 ≠ X3, c3 X2 ≠ X3
→ The CSP is domain consistent but does not have any feasible
solution
Constraint Propagation
X1 1
X2 2
X3 3
X4 4
Constraint Propagation
• X = {X1, X2, X3, X4}
• D(X1) = {1,2,4}, D(X2) = {1}, D(X3) = {4}, D(X4) = {3,4}
X1 1
X2 2 Edge (X1, 1) X2 2
X3 3 X3 3
X4 4 X4 4
X1 1 X1 1
X2 2 Edge (X4, 4) X2 2
X3 3 X3 3
X4 4
X1 1 1
X2 2 Edge (X1, 4) X2 2
X3 3 X3 3
X4 4 X4
X1 1
X2 2
X3 3
X4 4
Feasible solution!
Branching & Backtracking Search
• Constraint propagation is not enough for finding feasible solutions
• Combine constraint propagation with branching and backtracking search
• Split the original CSP P0 into sub-problems CSP P1,…,PM
• Set of solutions of P0 is equivalent to the union of sets of
solutions to P1,…,PM
• Domain of each variable in P1,…,PM is not greater than the
domain of that variable in P0
• Search Tree
• Root is the original CSP P0
• Each node of the tree is a CSP
• If P1,…,PM are children of P0 then the set of solutions of P0 is
equivalent to the union of sets of solutions to P1,…,PM
• Leaves
• A feasible solution
• Failure (a variable has an empty domain)
Branching & Backtracking Search
Branching & Backtracking Search
Branching & Backtracking Search
• Search strategies
• Variable selection
• dom heuristic: select a variable having the smallest domain
• deg heuristic: select a variable participating in most of the
constraints
• dom+deg heuristic: first apply dom, then use deg when tie
break (when there are more than one variable with the same
smallest domain size)
• dom/deg: select a variable having the smallest dom/deg
• Value selection
• Increasing order
• Decreasing order
Example
• Variables
• X = {X0, X1, X2, X3, X4}
• Domain
• X0, X1, X2, X3, X4 {1,2,3,4,5}
• Constraints
• C1: X2 + 3 ≠ X1
• C2: X3 ≤ X4
• C3: X2 + X3 = X0 + 1
• C4: X4 ≤ 3
• C5: X1 + X4 = 7
• C6: X2 = 1 X4 ≠ 2
Example
'‘‘
If-Then-Else expression
if x[2] = 1 then x[4] != 2
'''
from ortools.sat.python import cp_model
class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
#print intermediate solution
def __init__(self,variables):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__variables = variables
self.__solution_count = 0
def on_solution_callback(self):
self.__solution_count += 1
for v in self.__variables:
print('%s = %i'% (v,self.Value(v)), end = ' ')
print()
def solution_count():
return self.__solution_count
Example
model = cp_model.CpModel()
x = {}
for i in range(5):
x[i] = model.NewIntVar(1,5,'x[' + str(i) + ']')
c1 = model.Add(x[2] + 3 != x[1])
c2 = model.Add(x[3] <= x[4])
c3 = model.Add(x[2] + x[3] == x[0] + 1)
c4 = model.Add(x[4] <= 3)
c5 = model.Add(x[1] + x[4] == 7)
b = model.NewBoolVar('b')
#constraints
model.Add(x[2] == 1).OnlyEnforceIf(b)
model.Add(x[2] != 1).OnlyEnforceIf(b.Not())
model.Add(x[4] != 2).OnlyEnforceIf(b)
Example
solver = cp_model.CpSolver()
solution_printer = VarArraySolutionPrinter(vars)
solver.SearchForAllSolutions(model,solution_printer)
Thank you
for your
attentions!