Jan 2025
Constraint Programming: an inroduction
Dr Ing. Houndji V. Ratheil
https://ratheil.info
Lecturer-Researcher at UAC
Houndji Vinasetan Ratheil
https://ratheil.info
Teaching experience (since my PhD thesis)
- Arti cial Intelligence since 2014
- Optimisation since 2015
- Object Oriented Modelling/Programming
- Reliability of Information System
- etc.
Head of Software Engineering department at IFRI, UAC
2
fi
Outline
General Introduction
Global Constraints
How CP works concretely ?
Take Away Message
3
General Introduction
Constraint Programming (CP) is a paradigm derived from artificial
intelligence, operational research, and algorithmic that can be used to
solve combinatorial optimization problems.
CP solves problems by interleaving search (assigning a value to an
unassigned variable) and propagation (removing inconsistent values).
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.
Other declarative approaches : Logic Programming, Operations Research
Find a formal representation of
Find a mathematical formulation of the problem
the knowledge with logical rules
6
General Introduction
In CP we would like to describe the problem in natural “english” language.
7
General Introduction
In CP we would like to describe the problem in natural “english” language.
In practice it looks like
8
CP Applications
Agricultural land allocation
Production Planning
Rail Applications
Vehicule Routing
Scheduling
Tra c Engineering
and much more… 9
fi
A library of problems
A live demo with OscaR
10
A library of problems
CSPLib: A problem library for constraints
h ps://www.csplib.org/
This library presents a list of 92 problems
in 14 categories proposed by 70 authors.
11
tt
General Introduction
CSP = (X, D, C)
Variables Xi Domains Constraints
D(Xi )
Solution of CSP: Model
- An assignment of all variables +
Resolution
- All constraints are satis able
COP (Constraint Optimisation Problem):
CSP + objectif function(s)
12
fi
General Introduction
CSP = (X, D, C)
Variables Xi Domains Constraints
D(Xi )
Example: X1 2 {1, 2} X2 2 {1, 2} X1 6= X2
A solution ?
13
Modeling : an example 1
A farmer sends his son to the market with XOF100.000. With
this XOF100.000, the son must buy a hundred animals. He has
to come back to the farm with the 100 animals and he has to
spend the full XOF100.000. A chick costs XOF500, a pig
XOF5.000, and an ox XOF20.000. The son must buy at least
one animal of each species.
How many chicks, pigs, and oxen will the son have?
To do : propose a CP model for this problem.
14
Modeling : an example 2
Coin return problem
We are interested in a beverage vending machine.
The user inserts coins for a total of T fcfa, then selects a drink,
the price of which is P fcfa. It is necessary to calculate the
change to be returned, knowing that the distributor has in
reserve E2 coins of 200f, E1 coins of 100f, C50 coins of 50f,
C25 coins of 25f, and C10 coins of 10f.
To do : propose a CP model for this problem.
What should be added if we want to minimize the number of
coins to be returned.
15
Modeling : an example 3
The steelworks
A small steel mill (steel manufacturing plant) must decide on
the allocation for the next week (40 hours in total) of its rolling
mill (machine used to reduce the thickness of materials). This
processes un nished steel plates and can produce two types of
semi- nished products, strips and rolls. The rolling mill can
produce 200 strips or 140 rolls per hour, knowing that a strip
brings in $25 and a roll $30. Finally, it is not necessary to
produce more than the total of the programmed orders, namely
6000 strips and 4000 rolls.
Determine the respective quantities to be produced to
maximize pro t.
To do : propose a CP model for this problem. 16
fi
fi
fi
General Introduction
Model
+
Resolution
17
General Introduction
CSP = (X, D, C)
Variables Xi Domains Constraints
D(Xi )
Model
+
Resolution
18
General Introduction
CSP = (X, D, C)
Variables Xi Domains Constraints
D(Xi )
Example X1 2 {1, 2} X2 2 {1, 2} X1 6= X2
Model
+
Resolution
19
General Introduction
CSP = (X, D, C)
Variables Xi Domains Constraints
D(Xi )
Model
+
Resolution
Filtering + Search
20
General Introduction
CSP = (X, D, C)
Variables Xi Domains Constraints
D(Xi )
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
Example: X1 2 {1, 2} X2 2 {1, 2} X1 6= X2
X1 = 1 X1 = 2
Search
X2 = 1 X2 = 2 X2 = 2
X2 = 1
(1, 2) (2, 1)
25
General Introduction
Example : X1 2 {1, 2} X2 2 {1, 2} X1 6= X2
X1 = 1 X1 = 2
Filtering
X2 = 1 X2 = 2 X2 = 2
X2 = 1
(1, 2) (2, 1)
26
General
CP in Introduction
One slide
Filtering removes inconsistent values
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
Filtering removes inconsistent values
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
How CP works concretely ?
Take Away Message
Global Constraints
Global constraint :
- A constraint reasoning on many variables at the same time
- Specialized, powerful ltering
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
E ec ve for op miza on problem
Mainly based on:
an optimal solution of a related (relaxed) problem
the optimal value of this solution is used to filter the objective variable
an op mis c evalua on of the cost increase if a value is assigned to a variable
39
ff
ti
ti
ti
ti
ti
fi
ti
ti
ti
Global Constraints: Catalog
A catalog of more than 400 global constraints.
h p://sofdem.github.io/gccat/
This catalog presents a list of 423 global constraints
issued from the literature in constraint programming and from popular constraint systems.
40
tt
Global Constraints
Some examples
allDifferent
globalCardinalityConstraint
minimumAssignment
regular
binPacking
stockingCost
minimumArborescence
41
Outline
General Introduction
Global Constraints
How CP works concretely ?
Take Away Message
42
How CP works concretely?
Model
CP solver: A library of constraints +
Resolution
CP Modelling = Putting together the different constraints of the problem.
43
CP Solvers Catalog
Many CP-solvers exist to ease the implementation of model.
http://openjvm.jvmhost.net/CPSolvers/
This catalog presents a list of 35 CP based solvers (August 13, 2018).
44
How CP works concretely?
A live demo with the solver OscaR.
45
How CP works concretely?
A vehicule routing model
Put together the different constraints.
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?
allDifferent: A stronger Filtering Algorithm
• Remove any impossible values
x1= x3= x5=
x6= x8= x9=
{1,2,3, x2={2} {1,2,3, x4= {1} {1,2,3, x7= {3}
{6,7,8} {6,7,8,9} {6,7,8}
4,5} 4,5} 4,5,6}
48
How CP works concretely?
allDifferent: A stronger Filtering Algorithm
• Remove any impossible values
1,2,3, 1,2,3, 1,2,3,
2 1 6,7,8 3 6,7,8,9 6,7,8
4,5 4,5 4,5,6
after pruning
2 1 6 7,8 3 9 7,8
4,5 4,5
49
How CP works concretely?
allDifferent: A stronger Filtering Algorithm
• Remove any impossible values
1,2,3, 1,2,3, 1,2,3,
2 1 6,7,8 3 6,7,8,9 6,7,8
4,5 4,5 4,5,6
after pruning
4,5 2 4,5 1 6 7,8 3 9 7,8
A more global reasoning
50
How CP works concretely?
allDifferent: Basic (fast) Filtering Algorithm
• As soon as a variable xi has a single value v
Remove v from the domain of every other variable
allDifferent
1,2,3, 1,2,3, 1,2,3,
2 1 6,7,8 3 6,7,8,9 6,7,8
4,5 4,5 4,5,6
after pruning
4,5 2 4,5 1 4,5,6 6,7,8 3 6,7,8,9 6,7,8
Weak pruning for allDifferent is fast and easy to implement but
does not remove all inconsistent values
Pierre Schaus 51
Global Filtering for allDifferent (J-C. Régin Algorithm, 95)
• Build the variable-value graph
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
Requires to compute Strongly
connected components in
residual graph
How CP works concretely?
Each constraint must remove impossible values.
Yes but the pruning of one constraint might trigger
new deductions for other constraints…
C1(X1, C2(X2,X4, Constraint Store
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?
Model Example: n-Queens in OscaR CP
object Queens extends App with CPModel {
val n = 12 //number of queens
val Queens = 0 until n
//variables
val queens = for (i <- Queens) yield CPIntVar(1 to n) variables and
domains
onSolution { nSol += 1 }
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?
Large Neighborhood Search
Search Algo: « No gold here!
I should not wait too long before moving at
another place »
It seems there
is no good solution here!
I should not wait too long
before moving at another
place 70
How CP works concretely?
Large Neighborhood Search
Search Algo: « I change my strategy,
I’ll visit randomly other places for limited
amount of time »
relax S ⇤
relax S ⇤
relax S ⇤
Outline
General Introduction
Global Constraints
How CP works concretely ?
Take Away Message
72
Take away message
You must experiment CP !!
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
Pierre Schaus, Constraint Programming: A tool inspired by gold seekers.
Grascomp, 2015. [Some slides are from this reference]
CSPLib: A problem library for constraints. csplib.org
A catalog of global constraints. h p://sofdem.github.io/gccat/
CP Solvers Catalog. h p://openjvm.jvmhost.net/CPSolvers/
Mini-CP: A lightweight Constraint Programming Solver h p://www.minicp.org/
OscaR is a Scala toolkit for solving Operations Research problems.
https://bitbucket.org/oscarlib/oscar/wiki/Home
76
tt
tt
tt