0% found this document useful (0 votes)
6 views

Chapter 7 Constraint Programming

Uploaded by

hai.nguyen29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter 7 Constraint Programming

Uploaded by

hai.nguyen29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

FUNDAMENTALS OF

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 vD(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

Algorithm AC3(X,D,C){ Algorithm ReviseAC3(x,c){


Q = {(x,c) | c  C  x X(c)}; CHANGE = false;
while(Q not empty){ for v  D(x) do{
select and remove (x,c) from Q; if there does not exists other values
if ReviseAC3(x,c) then{ of X(c) \ {x} such that c
is satisfied then{
if D(x) = {} then
remove v from D(x);
return false;
CHANGE = true;
else
}
Q = Q {(x’,c’) | c’C\{c}  x,x’ X(c’)  x≠ x’}
}
}
return CHANGE;
} }
return true;
}
Constraint Propagation

• Some constraints, e.g., binary constraints (related 2 variables) → have


efficient DC algorithm
• Constraint AllDifferent(X1,X2,…,XN), the DC algorithm is efficient based
on the matching (Max-Matching) algorithm on bipartite graphs
• Nodes on the right-hand side are variables and nodes on the left-
hand side are values
• For each edge (Xi, v), (với v D(Xi)), if there does not exist a
matching of size N containing (Xi, v), then v is removed from D(Xi)
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

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

No matching of size 3 → remove 1 from


D(X1)
Constraint Propagation
• X = {X1, X2, X3, X4}
• D(X1) = {2,4}, D(X2) = {1}, D(X3) = {4}, D(X4) = {3,4}

X1 1 X1 1

X2 2 Edge (X4, 4) X2 2

X3 3 X3 3

X4 4

No matching of size 3 → removed 4 from


D(X4)
Constraint Propagation
• X = {X1, X2, X3, X4}
• D(X1) = {2,4}, D(X2) = {1}, D(X3) = {4}, D(X4) = {3}

X1 1 1

X2 2 Edge (X1, 4) X2 2

X3 3 X3 3

X4 4 X4

No matching of size 3 → removed 4 from


D(X1)
Constraint Propagation
• X = {X1, X2, X3, X4}
• D(X1) = {2}, D(X2) = {1}, D(X3) = {4}, D(X4) = {3}

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()

#Force the solver to follow the decision strategy exactly


solver.parameters.search_branching = cp_model.FIXED_SEARCH

vars = [x[i] for i in range(5)]

solution_printer = VarArraySolutionPrinter(vars)
solver.SearchForAllSolutions(model,solution_printer)
Thank you
for your
attentions!

You might also like