Constraint Systems
An Introduction to CP
Constraint Programming
CP = A technique to solve CSPs and COPs
- CSP = Constraint Satisfaction Problem
- COP = Constraint Optimization Problem
- It. Problema di Soddisfacimento/Ottimizzazione di/con Vincoli
A declarative approach
- Model & then solve (a bit like MIP)
- Model = variables + constraints
- Rich set of constraints (much more than linear in-equalities)
Is it worth learning?
A Practical Application
- Netherlands railways: 5,500 trains per day
- In 2009: timetable complete redesign (OR and CP)
- Less delay, more trains, profit increase: ~\$75M
Another Practical Application
- Port of Singapore: > 200 shipping lines, > 600 connected ports
- Problem: yard location assignment, loading plans
- For years, the Yard planning system (CP based) assisted the job
A third Practical Application
- Rosetta-Philae mission
- In 2014, first (partially successful) probe landing on a Comet
- Probe-spacecraft communication pre-scheduled via CP
CP - Quick Facts
A few reasons for using CP:
- Rich modeling language
- Fast prototyping
- Easy to maintain (modifications are simple)
- Extensible (new constraints, customizable search...)
- Very good framework for hybrid approaches
Overall:
- It's a good solution technique!
- Especially for messy, real-world, problems
Step by step
Before we tackle complex stuff
Let's take our first steps...
Map Coloring
4 available colors, different colors for contiguous regions
How would you solve it?
Map Coloring
4 available colors, different colors for contiguous regions
- Pick and color a region
- Pick another region, choose a compatible color
- Rinse & repeat
Map Coloring
4 available colors, different colors for contiguous regions
Eventually we find something like this
Partial Latin Square
- Think of that as "Poor's man sudoku"
- Different numbers (1 to 4) on rows and columns
How would you solve it?
Partial Latin Square
- Think of that as "Poor's man sudoku"
- Different numbers (1 to 4) on rows and columns
- Pick a cell, insert compatible value
- Rinse & repeat
Partial Latin Square
- Think of that as "Poor's man sudoku"
- Different numbers (1 to 4) on rows and columns
What now?
Partial Latin Square
- Think of that as "Poor's man sudoku"
- Different numbers on rows and columns
- Clear one of more moves
- Restart to insert numbers
Partial Latin Square
- Think of that as "Poor's man sudoku"
- Different numbers on rows and columns
Eventually, we find something like this
CP in a Nutshell
You see? There is a pattern.
- We reason on the constraints to narrow down the possible choice
- We make (and unmake) some choices
The core ideas in Constraint Programming are the same!
To the point that for many people:
CP = constraint reasoning + search
CP in a Nutshell
You see? There is a pattern.
- We reason on the constraints to narrow down the possible choice
- We make (and unmake) some choices
The core ideas in Constraint Programming are the same!
But I am not many people! I think this formula is much better:
CP = model + constraint reasoning + search
- CP is a declarative approach, remember?
- So it all starts with a declarative model
In the next slides we will focus on each of the three aspects
Constraint Systems
CP = model + ...
Constraint Satisfaction Problem
- First, we need to define the kind of problem we are interested in:
CSP=⟨X,D,C⟩
where:
X
is a set of variables
xi
is a single variable
- In principle: any kind of variable!
D
is a set of domains
C
is a set of constraints
More on Constraints
Constraint scope (Italian: ambito)
- A constraint
cj
is defined over a subset of X(cj)
of the variables
X(cj)⊆X
is called the scope of the constraint
Tuple
- A tuple
τ
or arity n
is just a sequence of n
values
Relation
- Let
S
be a sequence of sets S0,S1,...
- A relation
R
over S
is a subset of the Cartesian product. I.e.:
R⊆S0×S1×...
More on Constraints
Constraint
- A constraint
cj
is a relation over the domains of X(cj)
. I.e.:
cj⊆∏xi∈X(cj)Di
Here's the main idea:
- A tuple in the Cartesian product = an assignment of the variables
- A constraint is just a list of feasible assignments
A bit abstract, so let's make an example
An example CSP (definition-like)
Variables: X={x0,x1}
Domains: D(x0)={0..2}
, D(x1)={0..2}
Constraints:
c0=((0,0)(0,1)(0,2)(1,1)(1,2)(2,2))c1=((0,0)(0,1)(0,2)(1,0)(1,1)(2,0))
Solution of a CSP
A solution for a CSP is an assignment of all the variables that is feasible for all constraints.
Formally:
τ∈∏xi∈XD(xi) : τ[X(cj)]∈cj,∀cj∈C
where:
τ[X(cj)]
is the projection of τ
over X(cj)
τ[X(cj)]
= sequence of values assigned by τ
to variables in X(cj)
Solution of a CSP
A solution for a CSP is an assignment of all the variables that is feasible for all constraints.
Formally:
τ∈∏xi∈XD(xi) : τ[X(cj)]∈cj,∀cj∈C
The solutions in our example: (0,0)
, (0,1)
, (0,2)
, (1,1)
A CSP with no solution is called infeasible.
Down to Earth
Any kind of domain?
Down to Earth
Any kind of domain?
In practice:
- Integer variables
- Real variables
- Set variables! (e.g.
xi∈{{0,1},{0}}
)
- Graph variables!
- ...
In this course:
- Strong emphasis on finite, integer domains
- The most studied case
Down to Earth
Any kind of constraints?
Down to Earth
Any kind of constraints?
- With finite domain variables, yes!
- We can actually list all the feasible assignments
This is called extensional representation
- Very general
- Possibly inconvenient and inefficient (large domains)
We may prefer a symbolic or intensional representation:
- More compact, more clear, less general
Constraint Libraries
Intensional forms are made possible by Constraint libraries
- Constraint library = collection of constraint types
Our first constraint library:
Equality:
- Notation:
x=y
- Semantic: satisfied if
x
is equal to y
Disequality:
- Notation:
x≠y
- Semantic: satisfied if
x
is not equal to y
Map Coloring as a CSP
Variables and domains:
- xi∈{0..3}, for each region
Constraints:
- xi≠xj if region i and j are contiguous
Partial Latin Square as a CSP
Variables and domains:
- xi,j∈{1..4}, for each cell i,j
- i∈{0..3} = row index, j∈{0..3} = column index
Constraints:
- xi,j≠xi,h∀i,∀j<h
- xi,j≠xh,j∀j,∀i<h
Partial Latin Square as a CSP
Variables and domains:
- xi,j∈{1..4}, for each cell i,j
- i∈{0..3} = row index, j∈{0..3} = column index
Constraints:
- xi,j=v is cell i,j is pre-filled with value v
Constraint Systems
CP = model + constraint reasoning + ...
One Missing Piece
- We would never put a 3 there (not compatible)
- Can we formalize this deduction?
Main idea: reason on the domains
Filtering
- x3,1≠x3,2⇒x3,2≠4
- x2,2≠x3,2⇒x3,2≠3
- x0,2≠x3,2⇒x3,2≠2
Filtering
- The only possible value for x3,2 is 1
Filtering
Filtering for cj = removing provably infeasible values from the domains of the variables in the constraint scope
- Alternative terms: pruning, propagation
- My convention:
- Pruning = the act of removing a single value
- Filtering = the process that guides pruning
- Propagation = later!
- Italian: propagation = propagazione
Filtering
Filtering for cj = removing provably infeasible values from the domains of the variables in the constraint scope
- For constraints in extensional form:
- General methods (we will see them later)
- For constraints in intensional form:
- Specialized filtering algorithms
- Those are specified in the constraint library
- Alternative names: filtering rules, propagators
Filtering for our First Constraint Library
Equality constraint: x=y
- Rule 1: v∈D(y)∧v∉D(x)⟶v∉D(y)
- Rule 2: v∈D(x)∧v∉D(y)⟶v∉D(x)
Examples:
- Before filtering: x∈{0,1},y∈{1,2}
- After filtering: x∈{1},y∈{1}
Filtering for our First Constraint Library
Disequality constraint: x≠y
- Rule 1: D(x)={v}⟶v∉D(y)
- Rule 2: D(y)={v}⟶v∉D(x)
Examples:
- Before filtering: x∈{1},y∈{1,2}
- After filtering: x∈{1},y∈{2}
An Example on the PLS
- All original domains are {1..4}
- Let's filter all the constraints in order
- By row and then column
An Example on the PLS
- Constraint x0,0≠x0,2 prunes 2 from D(x0,0)
An Example on the PLS
- Constraint x0,1≠x0,2 prunes 2 from D(x0,1)
An Example on the PLS
- Constraints x0,2≠x0,j and x0,2≠xi,2 prune a lot
An Example on the PLS
- By filtering all constraints in order, we get this
Can we do more?
An Example on the PLS
- Yep:
D(x3,2)
and D(x3,3)
are now singletons
- Hence, our filtering rules for the
≠
constraints can be triggered
- If we do, we can filter more
Propagation
Propagation = the process by which filtering from one constraint may enable filtering from another constraint
- Propagation is controlled by a propagation algorithm
Our First Propagation Algorithm
Algorithm: AC1
- dirty = true
- while dirty:
- dirty = false
- for
cj∈C
:
- D′=filtercj(D)
- if D′≠D: dirty = true
Where filtercj(D)
is a procedure that:
- Given the current variable domains...
- ...Applies the filtering algorithm of
cj
...
- ...And then returns the updated domains
Fix Point Theorem
AC1 always converges to a fix point, which is independent on the constraint processing order.
Proof: We will prove the result in two steps:
- First, we show that AC1 always terminates...
- ...When no more pruning can be done
- Second, we show that the processing order does not matter
The proof relies on some properties of filterc(D)
Fix Point Theorem
The function filter(c) is inflationary, i.e.:
filterc(D)⊆D
- True because a filtering algorithm can only remove domains values
- Caveat:
- Technically,
filterc(D)
is inflationary w.r.t. the order ⊇
- Hence, the term may sound a bit misleading
Consequence: AC1 always terminates
- In the worst case, when
D
becomes empty
Fix Point Theorem
The function filterc(D)
is monotone, i.e.:
if D′⊆D,filterc(D′)⊆filterc(D)
- True because removing a value from
Di∈D
:
- can only reduce the number of feasible assignments
- and hence increase the number of values that can be pruned
Consequence: the processing order of cj
does not matter
- Whenever a filtering algorithms prunes something...
- ...the other algorithms just become stronger
Fix Point Theorem
Fix point: what's the point?
To relax!
Fix Point Theorem
Fix point: what's the point?
To relax!
Thanks to the fix point theorem:
- We can focus on filtering
- and let the propagation algorithm handle the rest
Fix Point Theorem
Warning: the constraint processing order
- does not affect the fix point
- does affect the number of AC1 iterations
Some orders may be more efficient
Can we determine the optimal order?
- Interesting (and challenging) research question
- NP-hard in general
AC1 on the PLS
- If we apply AC1 to our PLS
AC1 on the PLS
- If we apply AC1 to our PLS
- We get this (we pruned a lot!)...
- ...and still we don't have a solution :-(
Constraint Systems
CP = model + constraint reasoning + search
Complexity of a CSP
In general, filtering and propagation are not enough to solve a CSP
We still need to search for a solution
- In fact solving a CSP is NP-hard
- No surprise, since the definition is so general...
Don't forget that:
Most of the interesting problems out there are NP-hard!
(Basic) Search in CP
The simplest search approach in CP is Depth First Search:
- function DFS(CSP):
- if a solution has been found: return true
- if the CSP is infeasible: return false
- for d in decisions(CSP):
- if DFS(apply(d,CSP)): return true
- return false
(Basic) Search in CP
The simplest search approach in CP is Depth First Search:
- If the problem is infeasible: return false
- If a solution has been found: return true
- Otherwise, build a set of "decisions":
- Try to apply a decision
- If successful return true
- Othewise, backtrack
- Un-make the last decision
- Try with the next one
- If no decision is successful, return false (the problem is infeasible)
(Basic) Search in CP
The simplest search approach in CP is Depth First Search:
- function DFS(CSP):
- if a solution has been found: return true
- if the CSP is infeasible: return false
- for d in decisions(CSP):
- if DFS(apply(d,CSP)): return true
- return false
In our pseudo-code, backtracks are handled via recursion.
- In practice, this may be quite inefficient...
- ...We'll discuss this point again much later in the course
(Basic) Search in CP
The simplest search approach in CP is Depth First Search:
- function DFS(CSP):
- if a solution has been found: return true
- if the CSP is infeasible: return false
- for d in decisions(CSP):
- if DFS(apply(d,CSP)): return true
- return false
Unclear points:
- How to detect if a problem is infeasible?
- What are the decisions returned by
decisions(CSP)
?
- What does it mean to "apply a decision"?
DFS in CP
Q1: How to detect if a problem is infeasible?
A problem is infeasible if we have a domain wipeout
- Domain wipeout = empty domain, because of propagation
- This is a sufficient condition
- This is not a necessary condition
- I.e. if there is wipeout, then the problem is infeasible
- But infeasibility may hold even if there is not wipeout
- More on this later!
DFS in CP
Q2: What are the decisions returned by decisions(CSP)
?
They are constraints! For example:
- We pick the first unbound variable
xi
- Unbound variable = variable with non-singleton domain
- We pick the smallest value
v
in D(xi)
- Our decisions are
xi=v
and xi≠v
There are many more options, but for now let us stick to this.
DFS in CP
Q3: What does it mean to "apply a decision"?
It means:
- Adding (posting) the constraint to the problem
- Triggering its filtering algorithm for the first time
Remember: on backtrack, the constraint must be removed.
DFS on our PLS
- First unbound variable = x0,0
- Minimum value = 1
- We post x0,0=1, and the propagate
DFS on our PLS
- First unbound variable = x0,0
- Minimum value = 1
- We post x0,0=1, and the propagate
DFS on our PLS
And we have a solution!
- With just one search decision!
- This result is enabled by the use of constraint reasoning
Constraint Systems
A Modeling Excercise
Il Cartello delle Chincaglierie
Giani, Piero, Marisa e Luisanna, sono quattro (stagionati) abitanti di un piccolo paesello del centro Italia. I quattro sono legati da distanti (ma indiscutibili) legami di parentela e pertanto, come vuole il buon costume, di tanto in tanto per le occasioni si scambiano regali. I quattro non è che vadano proprio d’amore e d’accordo e così, un po’ per scherzo ed un po’ per dispetto, da qualche anno continuano a regalarsi le stesse chincaglierie
Nella fattispecie, le chincaglierie sono: una riproduzione in vetro di una conchiglia tropicale, una vecchia clessidra, una lampada in fibre ottiche (di quelle pelose) ed una statuetta a forma di cavalluccio marino.
Il Cartello delle Chincaglierie
Siamo al terzo anno in cui le cianfrusaglie devono essere scambiate e finora non ci sono stati “incidenti diplomatici”. All’indomani dell’inizio del quarto anno, Gianni deve ancora fare il suo regalo, torturato dal timore essere il primo a scatenare uno scandalo. Il poveretto ripensa con preoccupazione agli scambi a cui ha partecipato:
- Il primo anno, Gianni aveva in casa la conghiglia, che ha regalato a Marisa. Lo stesso anno, Marisa ha regalato a Gianni la lampada.
- Il secondo anno, Gianni ha regalato la lampada a Piero ed ha ricevuto in dono da Luisanna la clessidra.
Il Cartello delle Chincaglierie
Fortunatamente, Gianni è venuto a conoscenza tramite suo nipote di una nuova metodologia che si chiama Programmazione a Vincoli, che pare possa aiutarlo a risolvere i suoi problemi...
Si modelli il problema dello scambio dei regali tramite Programmazione a Vincoli e si indichi a quale persona Gianni potrà regalare la clessidra senza scatenare una bagarre.
Il Cartello delle Chincaglierie (Soluzione)
Si tratta di un Partial Latin Square problem un po’ “camuffato”. Una possibile soluzione consiste nell’introdurre una variabile per ogni persona e per ogni anno (incluso il quarto!):
xG,0,xG,1,xG,2,xG,3∈{conc,cless,lamp,stat}
xP,0,xP,1,xP,2,xP,3∈{conc,cless,lamp,stat}
xM,0,xM,1,xM,2,xM,3∈{conc,cless,lamp,stat}
xL,0,xL,1,xL,2,xL,3∈{conc,cless,lamp,stat}
Il Cartello delle Chincaglierie (Soluzione)
Ogni anno, ogni persona possiede esattamente un oggetto:
Xp,i≠Xq,i,p,q={G,P,M,L},i∈{0..3}
Nessuno può possedere lo stesso oggetto due volte:
Xp,i≠Xp,j,p={G,P,M,L},i,j∈{0..3}
Alcuni scambi sono noti:
xG,0=conc
xM,0=lamp,xM,1=conc,xG,1=lamp
xL,1=cless,xP,2=lamp,xG,2=cless
Il Cartello delle Chincaglierie (Soluzione)
Esiste una sola soluzione feasible:
Gianni: conchiglia lampada clessidra statuetta
Piero: clessidra statuetta lampada conchiglia
Marisa: lampada conchiglia statuetta clessidra
Luisanna: statuetta clessidra conchiglia lampada
Pertanto, Gianni deve regalare la clessidra a Marisa se vuole evitare lo scandalo