56
Arc consistency AC5, AC2001, MAC

Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Embed Size (px)

Citation preview

Page 1: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Arc consistency

AC5, AC2001, MAC

Page 2: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

AC5

A generic arc-consistency algorithm and its specializationsAIJ 57 (2-3) October 1992

P. Van Hentenryck, Y. Deville, and C.M. Teng

Page 3: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

ac5

Ac5 is a “generic” ac algorithm and can be specialised forspecial constraints (i.e. made more efficient when we knowsomething about the constraints)

Ac5 is at the heart of constraint programming

It can be “tweaked” to become ac3, or “tweaked” to become ac4But, it can exploit the semantics of constraints, and there’s the win

Page 4: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

ac5

ac5 processes a queue of tuples (i,j,w)• w has been removed from Dj• we now need to reconsider constraint Cij• to determine the unsupported values in Di

A crucial difference between ac3 and ac5• ac3

• revise(i,j) because Dj has lost some values• seek support for each and every value in Di

• ac5• process(i,j,w) because Dj has lost the value w (very specific)• seek support, possibly, for select few (1?) value in Di

Therefore, by having a queue of (i,j,w) we might be able to do things much more efficiently

Page 5: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

ac5, the intuition

• take an OOP approach• constraint is a class that can then be specialised• have a method to revise a constraint object• allow specialisation• have basic methods such as

• revise when lwb increases• revise when upb decreases• revise when a value is lost• revise when variable instantiated• revise initially

• methods take as arguments• the variable in the constraint that has changed• possibly, what values have been lost

Example: IntDomainVar x = pb.makeBoundIntVar(“x”,1,10);IntDomainVar y = pb.makeBoundIntVar(“y”,1,10);Constraint c = pb.lt(x,y);

Page 6: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

[ac5() : boolean -> let Q := {} <1> in for (i,j) {(i,j) | Cij in C} <2> do let delta := arcCons(i,j) <3> in d[i] := d[i] \ delta <4> Q := enqueue(i,delta,Q) <5> while Q ≠ {} <6> do let (i,j,w) := dequeue(Q) <7> delta := localArcCons(i,j,w) <8> in Q := enqueue(i,delta,Q) <9> d[i] := d[i] \ delta <10> return allDomainsNonEmpty(d)] <11>

ac5, a sketch

ac5 has 2 phasesPhase 1, lines <1> to <5>- revise all the constraints- build up the Q of tuples (i,j,w)

Phase 2, lines <6> to <10>-process the queue - propagate effect of deleted values - exploit knowledge of semantics of constraints - add new deletion tuples to Q

Page 7: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

ac5, the algorithm

This is just like revise, but delivers the set s ofunsupported values in d[i] over the constraint Cij

s is the set of disallowed values in d[i]remember check(i,x,j,y) is shorthand for Cij(x,y)

[arcCons(i,j) : set -> let s := {} in for x d[i] do let supported := false in for y d[j] while ¬supported do supported := check(i,x,j,y) if ¬supported then s := s {x} return s]

Can be specialised!

Page 8: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

ac5, the algorithm

[localArcCons(i,j,w) : set -> arcCons(i,j)]

Ignores the value w and becomes revise!

We can specialise this method if we know something about the semantics of Cij

We might then do much better that arcCons in termsof complexity

What we have above is the dummy’s version, or when we know nothing about the constraint (and we get AC3!)

Revise the constraint Cij because d[j] lost the value w

Page 9: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

[enqueue(i,delta,q) : set -> for (k,i) {(k,i) | Cki in C} do for w delta do q := q {(k,i,w} return q]

ac5, the algorithm

The domain d[i] has lost values deltaAdd to the queue of “things to do” the tuple (k,i,w)- For all w is in delta- For all Cki in C

Page 10: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

[ac5() : boolean -> let Q := {} in for (i,j) {(i,j) | Cij in C} phase 1 do let delta := arcCons(i,j) in d[i] := d[i] \ delta Q := enqueue(i,delta,Q) while Q ≠ {} phase 2 do let (i,j,w) := dequeue(Q) delta := localArcCons(i,j,w) in Q := enqueue(i,delta,Q) d[i] := d[i] \ delta return allDomainsNonEmpty(d)]

ac5, the algorithm

ac5 has 2 phases- revise all the constraints and build up the Q using arcCons- process the queue and propagate using localArcCons, exploiting knowledge on deleted values and the semantics of constraints

Page 11: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Complexity of ac5

If arcCons is O(d2) and localArcCons is O(d) then ac5 is O(e.d2)

If arcCons is O(d) and localArcCons is O() then ac5 is O(e.d)

See AIJ 57 (2-3) page 299

Remember - ac3 is O(e.d3) - ac4 is O(e.d2)

Page 12: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 13: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 14: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 15: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Functional constraints

Page 16: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

If c is functional

We can specialise arcCons and localArcCons for functional constraints

A constraint C_i,j is functional, with respect to a domain D, if and only if

• for all x in D[i] • there exists at most one value y in D[j] such that• Cij(x,y) holds

and

• for all y in D[j]• there exists at most one value in D[i] such that• Cji(y,x) holds The relation is a bijection

5

4

2

1

3

5

4

2

1

3

iV jV

An example: 3.x = y

Page 17: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

If c is functional

Let f(x) = y and f’(y) = x (f’ is the inverse of f)

[arcCons(i,j) : set -> let s := {} f := Cij

in for x d[i] do if f(x) d[j] then s := s {x} return s]

5

4

2

1

3

5

4

2

1

3

iV jV

Page 18: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

If c is functional

Let inverse(f) deliver the f’, the inverse of function f

[localArcCons(i,j,w) : set -> let f := Cij

g := Cji

in if g(w) d[i]) then return {g(w)} else return {}]

5

4

2

1

3

5

4

2

1

3

iV jV

In our picture,• assume d[j] looses the value 2 • g(2) = 3• localArcCons(i,j,2) = {3}

Page 19: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Anti-functional constraints

Page 20: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

If c is anti-functional

The value w in d[j] conflicts with only one value in d[i]

an example: x + 4 ≠ y

[localArcCons(i,j,w) : set -> let f := Cij

g := Cji

in if card(d[i]) = 1 & {g(w)} = d[i] then return d[i] else return {}]

5

4

2

1

3

5

4

2

1

3

iV jV

If d[i] has more than one value, no problem!

Page 21: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Other kinds of constraints that can be specialised

• monotonic• • localArcCons tops and tails domains

• but domains must be ordered• piecewise functional• piecewise anti-functional• piecewise monotonic

See: AIJ 57 & AR33 section 7

Page 22: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 23: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

The AIJ57 paper by van Hentenryck, Deville, and Teng is a remarkable paper

• they tell us how to build a constraint programming language• they tell us all the data structures we need

• for representing variables, their domains, etc• they introduce efficient algorithms for arc-consistency• they show us how to recognise special constraints and how we should process them• they show us how to incorporate ac5 into the search process• they explain all of this in easy to understand, well written English

It is a classic paper, in my book!

AIJ57

Page 24: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

A choco example

NOTE: old version of choco

Page 25: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 26: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 27: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 28: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

Page 29: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

The lower bound of vars[idx] has increasedvars[0] cannot take a value less than thatTherefore vars[0].setInf(vars[idx].getInf())

Page 30: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

The upper bound of vars[idx] has decreased

Page 31: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

idx > 0Find the largest upper bound of variables vars[1] to vars[n-1]

Page 32: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

idx > 0Find the largest upper bound of variables vars[1] to vars[n-1]Reduce the upper bound of vars[idx] to be the largest upper bound

Page 33: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

idx = 0 and upper bound of vars[idx] has decreased

Page 34: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

No variable vars[1] to vars[n-1] can take a value greater than upper bound of vars[0]

Page 35: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

On first posting the constraint, at top of search

Page 36: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

x[0] = max(x[1],x[2],…,x[n-1])

Variable vars[idx] is instantiated

Page 37: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 38: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 39: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Challenge/Homework

Using only toolkit constraints such as leq, geq, …model x = max(y,z)

where x, y, and z are constrained integer variables

Page 40: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

AC2001

Taken from IJCAI01

“Making AC-3 an Optimal Algorithm” by

Y Zhang and R. Yap

and

“Refining the basic constraint propagation algorithm”by

Christian Bessiere & Jean-Charles Regin

Page 41: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 42: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

The complexity of ac3 A beautiful proof

• A constraint Cij is revised iff it enters the Q• Cij enters Q iff some value in d[j] is deleted• Cij can enter Q at most d times (the size of domain d[j])• A constraint can be revised at most d times• There are e constraints in C (the set of constraints)• revise is executed at most e.d times• the complexity of revise is O(d2)• the complexity of ac3 is then O(e.d3)

Page 43: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

[revise(d:array<set>,c:tuple,i:integer,j:integer) : boolean ->let revised := false in (for x in copy(d[i]) (let supported := false in (for y in d[j] // this is naive (if check(c,x,y) (supported := true, return())), if not(supported) (delete(d[i],x), revised := true))), CONSISTENT := d[i] != {}, revised)]

When searching for a support for x in d[j] we always start from scratchThis is naïve.

Page 44: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

• Assume domains are totally ordered• Let resumePoint[i,j,x] = y

• where y is the first value in d[j] that supports x in d[i]• i.e. C_i,j is satisfied by the pair (x,y)

• Assume succ(y,d) delivers the successor of y in domain d • (and nil if no successor exists)

When we revise constraint C_i,j, we look for support for x in d[j]• get the resumePoint for x in domain d[j] over the constraint• call this value y• if y is in d[j] then x is supported! Do nothing!• If y is not in d[j] then

• we search through the remainder of d[j] looking for support• we use a successor function to get next y in d[j]• if we find a y that satisfies the constraint set resumePoint[i,j,x] = y

Page 45: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

[succ(x:integer,l:list) : integer -> if (l = nil) -1 else if (l[1] > x) l[1] else succ(x,cdr(l))]//// find the successor of x in ordered list l// NOTE: in the ac2001 algorithms we assume // we can do this in O(1)//

Page 46: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

[existsY(d:array<set>,c:tuple,i:integer,j:integer,x:integer) : boolean -> let y := RP[i][j][x] in (if (y % d[j]) true else let supported := false in (y := succ(y,list!(d[j])), while (y > 0 & not(supported)) (if check(c,x,y) (RP[i][j][x] := y, supported := true) else y := succ(y,list!(d[j]))), supported))]//// find the first value y in d[j] that supports x in d[i]// if found return true otherwise false//

Page 47: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

[revise(d:array<set>,c:tuple,i:integer,j:integer) : boolean -> let revised := false in (for x in copy(d[i]) (if not(existsY(d,c,i,j,x)) (delete(d[i],x), revised := true)), revised)]

Page 48: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

In the two papers, the algorithm is shown to be optimal,for arbitrary binary csp’s

).( 2deO

Page 49: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

• arc-consistency is at the heart of constraint programming• it is the inferencing step used inside search• it has to be efficient• data structures and algorithms are crucial to success

• ac is established possibly millions of times when solving• it has to be efficient

• we have had an optimal algorithm many times• ac4, ac6, ac7, ac2001

• ease of implementation is an issue• we like simple things

• but we might still resort to empirical study!

Page 50: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

MAC

What’s that then

Page 51: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Maintain arc-consistency

• Instantiate a variable v[i] := x• impose unary constraint d[i] = {x}• make future problem ac• if domain wipe out

• backtrack and impose constraint d[i] x• make future ac

• and so on

pb.solve(false)

Page 52: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Maintain arc-consistency

• why use instantiation?• Domain splitting?• resolve disjunctions first

• for example (V1 < V2 OR V2 < V1)

Page 53: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

You now know enough to go out andbuild a reasonably efficient and useful CP toolkit

Page 54: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,

Now its time to start using a CP toolkit

Page 55: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,
Page 56: Arc consistency AC5, AC2001, MAC. AC5 A generic arc-consistency algorithm and its specializations AIJ 57 (2-3) October 1992 P. Van Hentenryck, Y. Deville,