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
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

An Introduction to CP

slide-2
SLIDE 2

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?

slide-3
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
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
SLIDE 5

■ Rosetta-Philae mission ■ In 2014, first (partially successful) probe landing on a Comet ■ Probe-spacecraft communication pre-scheduled via CP

slide-6
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
SLIDE 7

Before we tackle complex stuff Let's take our first steps...

slide-8
SLIDE 8

4 available colors, different colors for contiguous regions How would you solve it?

slide-9
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
SLIDE 10

4 available colors, different colors for contiguous regions Eventually we find something like this

slide-11
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
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
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
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
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
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
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
SLIDE 18

CP = model + ...

slide-19
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
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
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
SLIDE 22

Variables: Domains: , Constraints:

slide-23
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
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
SLIDE 25

Any kind of domain?

slide-26
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
SLIDE 27

Any kind of constraints?

slide-28
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
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
SLIDE 30

Variables and domains: ■ , for each region Constraints: ■ if region and are contiguous

slide-31
SLIDE 31

4 3 3 2

Variables and domains: ■ , for each cell ■ = row index, = column index Constraints: ■ ■

slide-32
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
SLIDE 33

CP = model + constraint reasoning + ...

slide-34
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
SLIDE 35

4 3 3 2

1 2 3 4

■ ■ ■

slide-36
SLIDE 36

4 3 3 2

1

■ The only possible value for is 1

slide-37
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
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
SLIDE 39

Equality constraint: ■ Rule 1: ■ Rule 2: Examples: ■ Before filtering: ■ After filtering:

slide-40
SLIDE 40

Disequality constraint: ■ Rule 1: ■ Rule 2: Examples: ■ Before filtering: ■ After filtering:

slide-41
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
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
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
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
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
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
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
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
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
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
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
SLIDE 52

Fix point: what's the point? To relax!

slide-53
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
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
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
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
SLIDE 57

CP = model + constraint reasoning + search

slide-58
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
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
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
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
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
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
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
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
SLIDE 66

■ First unbound variable = ■ Minimum value = 1 ■ We post , and the propagate

slide-67
SLIDE 67

■ First unbound variable = ■ Minimum value = 1 ■ We post , and the propagate

slide-68
SLIDE 68

And we have a solution! ■ With just one search decision! ■ This result is enabled by the use of constraint reasoning

slide-69
SLIDE 69

A Modeling Excercise

slide-70
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
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
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
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
SLIDE 74

Ogni anno, ogni persona possiede esattamente un oggetto: ■ Nessuno può possedere lo stesso oggetto due volte: ■ Alcuni scambi sono noti: ■ ■ ■

slide-75
SLIDE 75

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