CPBy HVR-en
CPBy HVR-en
2
fi
Outline
General Introduction
Global Constraints
4
General Introduction
Constraint Programming allows a declarative description of the problem to solve.
5
General Introduction
Constraint Programming allows a declarative description of the problem to solve.
6
General Introduction
7
General Introduction
8
CP Applications
Agricultural land allocation
Production Planning
Rail Applications
Vehicule Routing
Scheduling
Tra c Engineering
10
A library of problems
CSP = (X, D, C)
CSP = (X, D, C)
A solution ?
13
Modeling : an example 1
14
Modeling : an example 2
Model
+
Resolution
17
General Introduction
CSP = (X, D, C)
Model
+
Resolution
18
General Introduction
CSP = (X, D, C)
19
General Introduction
CSP = (X, D, C)
Model
+
Resolution
Filtering + Search
20
General Introduction
CSP = (X, D, C)
Model
+
Resolution
Filtering + Search
21
General Introduction
Resolution in CP : an intuition
Fruit seeking
22
General Introduction
Solution = Fruit
23
General Introduction
24
General Introduction
X1 = 1 X1 = 2
Search
X2 = 1 X2 = 2 X2 = 2
X2 = 1
(1, 2) (2, 1)
25
General Introduction
X1 = 1 X1 = 2
Filtering
X2 = 1 X2 = 2 X2 = 2
X2 = 1
(1, 2) (2, 1)
26
General
CP in Introduction
One slide
Example 2:
X Y +2
X 2 {1, 2, 3, 4, 5} Y 2 {2, 3, 4, 5, 6}
27
General
CP in Introduction
One slide
Example 2:
X Y +2
X 2 {1, 2, 3, 4, 5} Y 2 {2, 3, 4, 5, 6}
28
General Introduction
CP = Search + Filtering
Too small
Too big
29
Outline
General Introduction
Global Constraints
31
fi
Global Constraints
Global constraint :
- A constraint reasoning on many variables at the same time
- Specialized, powerful ltering
Example : allDif f erent(X1 , X2 , X3 )
X2 2 {1, 2, 3}
X1 #X2 X2 #X3
X1 2 {1, 2} X3 2 {1, 2}
X1 #X3
32
fi
Global Constraints
Cost-based ltering
- Filtering for optimization constraint
- Effective for optimization problem
33
fi
Global Constraints
Cost-based ltering
- Filtering for optimization constraint
- Effective for optimization problem
Example : allDif f erent(X1 , X2 , X3 )
X2 2 {1, 2, 3}
X1 #X2 X2 #X3
X1 2 {1, 2} X3 2 {1, 2}
X1 #X3
34
fi
Global Constraints
Cost-based ltering
- Filtering for optimization constraint
- Effective for optimization problem
X
Example : allDif f erent(X1 , X2 , X3 ) with (Xi = j) · cost(Xi j) K
i
3 2 4 K 2 [0, . . . , 10]
X2 2 {1, 2, 3}
X1 #X2 X2 #X3
4 2 2 4
X1 2 {1, 2} X3 2 {1, 2}
X1 #X3
35
fi
Global Constraints
Cost-based ltering
- Filtering for optimization constraint
- Effective for optimization problem
X
Example : allDif f erent(X1 , X2 , X3 ) with (Xi = j) · cost(Xi j) K
i
3 2 4 K 2 [0, . . . , 10]
X2 2 {1, 2, 3}
X1 #X2 X2 #X3
4 2 2 4
X1 2 {1, 2} X3 2 {1, 2}
X1 #X3
36
fi
Global Constraints
Cost-based ltering
- Filtering for optimization constraint
- Effective for optimization problem
X
Example : allDif f erent(X1 , X2 , X3 ) with (Xi = j) · cost(Xi j) K
i
3 2 4 K 2 [0, . . . , 10]
X2 2 {1, 2, 3}
X1 #X2 X2 #X3
4 2 2 4
X1 2 {1, 2} X3 2 {1, 2}
X1 #X3
37
fi
Global Constraints
Cost-based ltering
- Filtering for optimization constraint
- Effective for optimization problem
X
Example : allDif f erent(X1 , X2 , X3 ) with (Xi = j) · cost(Xi j) K
i
3 2 4 K 2 [0, . . . , 10]
X2 2 {1, 2, 3}
minAssignment
X1 #X2 X2 #X3
4 2 2 4
X1 2 {1, 2} X3 2 {1, 2}
X1 #X3
38
fi
Global Constraints
Cost-based ltering
Filtering for op miza on constraint
the optimal value of this solution is used to filter the objective variable
39
ff
ti
ti
ti
ti
ti
fi
ti
ti
ti
Global Constraints: Catalog
h p://sofdem.github.io/gccat/
Some examples
allDifferent
globalCardinalityConstraint
minimumAssignment
regular
binPacking
stockingCost
minimumArborescence
41
Outline
General Introduction
Global Constraints
Model
CP solver: A library of constraints +
Resolution
43
CP Solvers Catalog
44
How CP works concretely?
45
How CP works concretely?
46
The job of a constraint !
• Given the status of the domain, do you think that you still have a
solution?
• Remove the values from the domains that you think are impossible in
a solution (= filtering or pruning)
variables and
domains
A constraint in action!
Impossible values
47
How CP works concretely?
48
How CP works concretely?
after pruning
2 1 6 7,8 3 9 7,8
4,5 4,5
49
How CP works concretely?
after pruning
50
How CP works concretely?
after pruning
X1
X2
X3
X4
5
Global Filtering for allDifferent (J-C. Régin Algorithm, 95)
1
Consistency Check:
• There is a solution to X1
allDifferent([X1,X2,X3,X4]) iff 2
there is a maximum matching
M (of size 4) in the variable X2
value graph 3
Filtering:
X3
• Remove edges that do not
belong to some maximum 4
matching
X4
Domain Store
X1 ∈ {1,3}
X4 ∈ {9} X3 ∈ {1,2,5,6} C3(X1,
C7(X1,X2,
X5 ∈ {3,4,5,8,10}
X2 ∈ {1,,4,9,11}
C6(X4, C5(X1,X2,X3,X
54
How CP works concretely?
Fix-Point Algorithm
repeat
select a constraint c
if c is OK wrt the domain store
apply filtering algorithm of c
else
return KO
until no value can be removed
55
Back to our gold seeker illustration
lgo
A
int
o
x -p
Fi P
sum( X[] ): i X(i) = 0
allDifferent( X[] ):
8i < j : X(i) 6= X(j)
regular( X[] ):
56
How CP works concretely?
Fix-Point Example
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{0,1,2,4}
x2∈{2,4}
x3∈{1,2,3,4,5,6,7,8}
57
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{0,1,2,4}
x2∈{2,4}
x3∈{1,2,3,4,5,6,7,8}
58
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{0,1,2,4}
x2∈{2,4}
x3∈{2,3,4,5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains 59
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{0,1,2,4}
x2∈{2,4}
x3∈{2,3,4,5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains 60
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{0,1,2,4}
x2∈{2,4}
x3∈{2,3,4,5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains 61
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{2,4}
x2∈{2,4}
x3∈{2,3,4,5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains 62
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{2,4}
x2∈{2,4}
x3∈{2,3,4,5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains 63
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{2,4}
x2∈{2,4}
x3∈{4,5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains 64
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{2,4}
x2∈{2,4}
x3∈{4,5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains 65
How CP works concretely?
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{2,4}
x2∈{2,4}
x3∈{5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains
66
How CP works concretely?
Fix-Point! but not a solution yet …
We need to search …
Constraint Store
allDifferent(x1,x2,x3)
x1+x2=x3
x2 <= x1
Domain Store
x1∈{2,4}
x2∈{2,4}
x3∈{5,6,7,8}
ps: We assume the sum constraint only reasons on bounds of the domains 67
How CP works concretely?
add(allDifferent(queens)) constraints
add(allDifferent(for (i <- Queens) yield queens(i) + i))
add(allDifferent(for (i <- Queens) yield queens(i) - i))
search {
binaryFirstFail(queens) default search
}
start()
68
}
How CP works concretely?
Optimization with CP
// your constraints // your constraints
cp.minimize(objVar) } search {
} search { branching(decVars)
} onSolution {
branching(decVars)
updateBound()
} start()
} start()
4 25
600
5 10 26 41
6 9 11 18 27 34 550
Objective
7 8 12 13 19 20 28 31 35 40
500
14 17 21 24 29 30 32 33 36 37
450
15 16 22 23 38 39
400
Best solution 5 10 15 20 25 30 35 40
Node Number 69
How CP works concretely?
It seems there
is no good solution here!
I should not wait too long
before moving at another
place 70
How CP works concretely?
relax S ⇤
relax S ⇤
relax S ⇤
Outline
General Introduction
Global Constraints
73
Take away message
Xi = V Xi != V
Constraint Store
Constraints
Domain store
Fix-Point Algo
repeat
select a constraint c
if c is OK wrt the domain store
apply filtering algorithm of c
else
return KO
until no value can be removed
74
Thank you for your attention
SomeOutline
references
76
tt
tt
tt