SLIDE 1
An Introduction to CP CP = A technique to solve CSPs and COPs CSP = - - PowerPoint PPT Presentation
An Introduction to CP CP = A technique to solve CSPs and COPs CSP = - - PowerPoint PPT Presentation
An Introduction to CP 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
SLIDE 2
SLIDE 3
■ Netherlands railways: 5,500 trains per day ■ In 2009: timetable complete redesign (OR and CP) ■ Less delay, more trains, profit increase: ~\$75M
SLIDE 4
■ 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
SLIDE 5
■ Rosetta-Philae mission ■ In 2014, first (partially successful) probe landing on a Comet ■ Probe-spacecraft communication pre-scheduled via CP
SLIDE 6
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
SLIDE 7
Before we tackle complex stuff Let's take our first steps...
SLIDE 8
4 available colors, different colors for contiguous regions How would you solve it?
SLIDE 9
4 available colors, different colors for contiguous regions ■ Pick and color a region ■ Pick another region, choose a compatible color ■ Rinse & repeat
SLIDE 10
4 available colors, different colors for contiguous regions Eventually we find something like this
SLIDE 11
■ Think of that as "Poor's man sudoku" ■ Different numbers (1 to 4) on rows and columns
4 3 3 2
How would you solve it?
SLIDE 12
■ Think of that as "Poor's man sudoku" ■ Different numbers (1 to 4) on rows and columns
4 3 3 2
■ Pick a cell, insert compatible value ■ Rinse & repeat
SLIDE 13
■ Think of that as "Poor's man sudoku" ■ Different numbers (1 to 4) on rows and columns
4 3 3 2 2 1
What now?
SLIDE 14
■ Think of that as "Poor's man sudoku" ■ Different numbers on rows and columns
4 3 3 2 2 1
■ Clear one of more moves ■ Restart to insert numbers
SLIDE 15
■ Think of that as "Poor's man sudoku" ■ Different numbers on rows and columns
4 3 3 1 1 2 4 2 3 3 1 4 2 4 1 2
Eventually, we find something like this
SLIDE 16
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
SLIDE 17
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
SLIDE 18
CP = model + ...
SLIDE 19
■ First, we need to define the kind of problem we are interested in: where: ■ is a set of variables ■ is a single variable ■ In principle: any kind of variable! ■ is a set of domains ■ takes values in ■ is a set of constraints
SLIDE 20
Constraint scope (Italian: ambito) ■ A constraint is defined over a subset of
- f the variables
■ is called the scope of the constraint Tuple ■ A tuple or arity is just a sequence of values Relation ■ Let be a sequence of sets ■ A relation over is a subset of the Cartesian product. I.e.:
SLIDE 21
Constraint ■ A constraint is a relation over the domains of . I.e.: 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
SLIDE 22
Variables: Domains: , Constraints:
SLIDE 23
A solution for a CSP is an assignment of all the variables that is feasible for all constraints. Formally: where: ■ is the projection of over ■ = sequence of values assigned by to variables in
SLIDE 24
A solution for a CSP is an assignment of all the variables that is feasible for all constraints. Formally: The solutions in our example: , , , A CSP with no solution is called infeasible.
SLIDE 25
Any kind of domain?
SLIDE 26
Any kind of domain? In practice: ■ Integer variables ■ Real variables ■ Set variables! (e.g. ) ■ Graph variables! ■ ... In this course: ■ Strong emphasis on finite, integer domains ■ The most studied case
SLIDE 27
Any kind of constraints?
SLIDE 28
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
SLIDE 29
Intensional forms are made possible by Constraint libraries ■ Constraint library = collection of constraint types Our first constraint library: Equality: ■ Notation: ■ Semantic: satisfied if is equal to Disequality: ■ Notation: ■ Semantic: satisfied if is not equal to
SLIDE 30
Variables and domains: ■ , for each region Constraints: ■ if region and are contiguous
SLIDE 31
4 3 3 2
Variables and domains: ■ , for each cell ■ = row index, = column index Constraints: ■ ■
SLIDE 32
4 3 3 2
Variables and domains: ■ , for each cell ■ = row index, = column index Constraints: ■ is cell is pre-filled with value
SLIDE 33
CP = model + constraint reasoning + ...
SLIDE 34
4 3 3 2 3
■ We would never put a 3 there (not compatible) ■ Can we formalize this deduction? Main idea: reason on the domains
SLIDE 35
4 3 3 2
1 2 3 4
■ ■ ■
SLIDE 36
4 3 3 2
1
■ The only possible value for is 1
SLIDE 37
Filtering for = 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
SLIDE 38
Filtering for = 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
SLIDE 39
Equality constraint: ■ Rule 1: ■ Rule 2: Examples: ■ Before filtering: ■ After filtering:
SLIDE 40
Disequality constraint: ■ Rule 1: ■ Rule 2: Examples: ■ Before filtering: ■ After filtering:
SLIDE 41
4 3 3 2
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
■ All original domains are ■ Let's filter all the constraints in order ■ By row and then column
SLIDE 42
4 3 3 2
1 2 3 4 1 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
■ Constraint prunes 2 from
SLIDE 43
4 3 3 2
1 2 3 4 1 3 4 1 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
■ Constraint prunes 2 from
SLIDE 44
4 3 3 2
1 3 4 1 3 4 1 3 4 1 3 4 1 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
■ Constraints and prune a lot
SLIDE 45
4 3 3 2
1 1 3 4 1 3 1 3 4 1 4 1 2 1 2 4 1 2 4 1 2 1 2 4 2 1 2 3
■ By filtering all constraints in order, we get this Can we do more?
SLIDE 46
4 3 3 2
1 1 3 4 1 3 1 3 4 1 4 1 2 1 2 4 1 2 4 1 2 1 2 4 2 1 2 3
■ Yep: and are now singletons ■ Hence, our filtering rules for the constraints can be triggered ■ If we do, we can filter more
SLIDE 47
Propagation = the process by which filtering from one constraint may enable filtering from another constraint ■ Propagation is controlled by a propagation algorithm
SLIDE 48
Algorithm: AC1
dirty = true while dirty: dirty = false for : if : dirty = true
Where is a procedure that: ■ Given the current variable domains... ■ ...Applies the filtering algorithm of ... ■ ...And then returns the updated domains
SLIDE 49
AC1 always converges to a fix point, which is independent
- n 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
SLIDE 50
The function filter( ) is inflationary, i.e.: ■ True because a filtering algorithm can only remove domains values ■ Caveat: ■ Technically, is inflationary w.r.t. the order ■ Hence, the term may sound a bit misleading Consequence: AC1 always terminates ■ In the worst case, when becomes empty
SLIDE 51
The function is monotone, i.e.: ■ True because removing a value from : ■ can only reduce the number of feasible assignments ■ and hence increase the number of values that can be pruned Consequence: the processing order of does not matter ■ Whenever a filtering algorithms prunes something... ■ ...the other algorithms just become stronger
SLIDE 52
Fix point: what's the point? To relax!
SLIDE 53
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
SLIDE 54
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
SLIDE 55
4 3 3 2
1 1 3 4 1 3 1 3 4 1 4 1 2 1 2 4 1 2 4 1 2 1 2 4 2 1 2 3
■ If we apply AC1 to our PLS
SLIDE 56
4 3 3 2
1 1 3 4 1 3 1 4 1 4 1 2 1 2 4 1 2 4 1 2 1 4 2 1 2 3
■ If we apply AC1 to our PLS ■ We get this (we pruned a lot!)... ■ ...and still we don't have a solution :-(
SLIDE 57
CP = model + constraint reasoning + search
SLIDE 58
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!
SLIDE 59
The simplest search approach in CP is Depth First Search:
function : if a solution has been found: return true if the CSP is infeasible: return false for in : if : return true return false
SLIDE 60
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)
SLIDE 61
The simplest search approach in CP is Depth First Search:
function : if a solution has been found: return true if the CSP is infeasible: return false for in : if : 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
SLIDE 62
The simplest search approach in CP is Depth First Search:
function : if a solution has been found: return true if the CSP is infeasible: return false for in : if : return true return false
Unclear points: ■ How to detect if a problem is infeasible? ■ What are the decisions returned by ? ■ What does it mean to "apply a decision"?
SLIDE 63
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!
SLIDE 64
Q2: What are the decisions returned by ? They are constraints! For example: ■ We pick the first unbound variable ■ Unbound variable = variable with non-singleton domain ■ We pick the smallest value in ■ Our decisions are and There are many more options, but for now let us stick to this.
SLIDE 65
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.
SLIDE 66
■ First unbound variable = ■ Minimum value = 1 ■ We post , and the propagate
SLIDE 67
■ First unbound variable = ■ Minimum value = 1 ■ We post , and the propagate
SLIDE 68
And we have a solution! ■ With just one search decision! ■ This result is enabled by the use of constraint reasoning
SLIDE 69
A Modeling Excercise
SLIDE 70
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.
SLIDE 71
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.
SLIDE 72
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.
SLIDE 73
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!): ■ ■ ■ ■
SLIDE 74
Ogni anno, ogni persona possiede esattamente un oggetto: ■ Nessuno può possedere lo stesso oggetto due volte: ■ Alcuni scambi sono noti: ■ ■ ■
SLIDE 75