30 november 2005 foundations of logic and constraint programming 1 constraint (logic) programming an...

62
30 November 2005 Foundations of Logic and Constrain t Programming 1 Constraint (Logic) Programming An overview •Optimising Arc Consistency •Path Consistency •Other forms of consistency •Consistency and Constraint Trees [Dech04] Rina Dechter, Constraint Processing,

Post on 21-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 1

Constraint (Logic) Programming

­ An­overview

• Optimising­Arc­Consistency

• Path­Consistency

• Other­forms­of­consistency

• Consistency­and­Constraint­Trees

[Dech04]­Rina­Dechter,­Constraint­Processing,­

Page 2: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 2

Maintaining Arc Consistency: From AC-3 to AC-4

Inefficiency­of­AC-3

Every­time­a­value­vi­is­removed­from­the­domain­of­some­variable­Xi,­by­

predicate­ revise_dom(aij,V,D,C), all­ arcs­ leading­ to­ that­ variable­ are­

reexamined.

Nevertheless,­only­some­of­these­arcs­aki­(for­k ­i­and­­k j­)­should­be­

examined.

Although­the­removal­of­vi­may­eliminate­one­support­for­some­value­­vk­

of­ another­ variable­ Xk­ for­ which­ there­ is­ a­ constraint­ Cki­ (or­ Cik),­ other­

values­in­the­domain­of­Xi­may­maintain­support­in­variable­Xi for­the­pair­­

Xk-vk!

This­idea­is­exploited­in­algorithm­AC-4.

Page 3: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 3

Algorithm­AC-4­(counters):

­ Algorithm­AC-4­maintains­data­structures­to­account­for­the­number­of­values­

in­the­domain­of­variable­­Xi­that­support­some­value­vk­from­another­variable­

Xk,­for­which­there­is­a­constraint­Cik.

­ For­ example,­ in­ the­ 4­ queens­ problem,­ the­ counters­ that­ account­ for­ the­

support­of­value­X1= 2­are­initialised­as­follows

c(2,X1,X2) = 1 % X2-4 does not attack X1-2

c(2,X1,X3) = 2 % X3-1 and X3-3 do not attack X1-2

c(2,X1,X4) = 3 % X4-1, X4-3 and X4-4 do not attack X1-2

Maintaining Arc Consistency: AC-4

Page 4: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 4

Algorithm­AC-4­(supporting sets)

To­update­the­counters­when­a­value­is­eliminated,­it­is­useful­to­maintain­

the­ set­ of­ Variable-Value­ pairs­ that­ are­ supported­ by­ each­ value­ of­ a­

variable.

AC-4­ thus­maintain­ for­each­Value-Variable­pair­ the­set­of­all­Variable-

Value­pairs­supported­by­the­former­pair.

sup(1,X1) = [X2-3, X2-4 , X3-2, X3-4, X4-2, X4-3] sup(3,X1) = [X2-1, X3-2 , X3-4, X4-1, X4-2, X4-4] sup(4,X1) = [X2-1, X2-2 , X3-1, X3-3, X4-2, X4-3]

Maintaining Arc Consistency: AC-4

sup(2,X1) = [X2-4, X3-1, X3-3, X4-1, X4-3, X4-4]% X1-2 supports (does not attack)

X2-4, X3-1,X3-3,

...

Page 5: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 5

Algorithm­AC-4­(propagation):

Every­ time­ that­ it­ is­detected­ that­a­value­v­ from­a­variable­X­does­not­

have­support­in­another­variable­Y,­­v­is­removed­from­the­domain­of­the­

variable­X­and­is­addded­to­a­list­for­subsequent­propagation.­

However,­ although­ the­ value­ may­ loose­ support­ several­ times,­ its­

removal­may­only­be­propagated,­in a useful way,­­once.­

To­account­for­this­situation,­AC-4­maintains­a­Boolean­matrix­M.­The­1/0­

value­of­element­M[X,v]­ represents­whether­value­v­ is/is­not­present­ in­

the­domain­of­variable­­X.

Maintaining Arc Consistency: AC-4

Page 6: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 6

Algorithm­AC-4­(Overall Functioning)

AC-4­makes­use­of­the­data­structures­presented­in­the­predictable­way.

In­a­first­phase,­initialisation,­which­is­executed­only­once,­the­algorithm­

initialises­ the­data­structures­ (counters,­ supporting­sets,­boolean­matrix­

and­removal­list).

In­ the­second­phase,­propagation,­which­ is­executed­not­only­after­ the­

first­phase,­but­also­after­each­enumeration­step,­the­algorithm­performs­

the­ actual­ constraint­ propagation,­ updating­ the­ data­ structures­ as­

required.

The­two­phases­are­presented­next

Maintaining Arc Consistency: AC-4

Page 7: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 7

procedure initialise_AC-4(V,D,C); M <- 1; sup <- ; List = ; for Cij in C do for vi in dom(Xi) do ct <- 0; for vj in dom(Xj) do if satisfies({Xi-vi, Xj-vj}, Cij) then ct <- ct+1; sup(vj,Xj)<- sup(vj,Xj) Xi-vi} end if; end for if ct = 0 then M[Xi,vi] <- 0; List <- List {Xi-vi} dom(Xi) <- dom(Xi)\{vi} else c(vi, Xi, Xj) <- ct; end if end for end forend procedure

AC-4 (Initialisation)

Page 8: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 8

procedure propagate_AC-4(V,D,R);

while List do List <- List\{Xi-vi} % remove element from List

for Xj-vj in sup(vi,Xi) do

c(vj,Xj,Xi) <- c(vj,Xj,Xi) – 1

if c(vj,Xj,Xi) = 0 M[Xj,vj] = 1 then List = List {Xj-vj}; M[Xj,vj] <- 0;

dom(Xj) <- dom(Xj) \ {vj}

end if

end for

end while

end procedure

AC-4 (Propagation)

Page 9: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 9

Time Complexity of AC-4

Time Complexity of AC-4 (Initialisation): O(ad2).

Analysing­the­cycles­executed­in­the­procedure­initialise_AC-4,­

for Cij in C do

for vi in dom(Xi) do

for vj in dom(Xj) do

and­assuming­that­

• the­number­of­constraints­(arcs)­is­a,­

• the­variables­have­all­d­values­in­their­domains,­

then­the­inner­cycle­of­the­procedure­is­executed­­2ad2­times,­which­sets­

the­time­complexity­of­the­initialisation­phase­of­AC-4­to

O(ad2).

Page 10: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 10

Time Complexity of AC-4

Time Complexity of AC-4 (Propagation): O(ad2).

In­the­inner­cycle­of­the­propagation­procedure­a­counter­for­pair­Xj-vj is­

decremented

c(vj,Xj,Xi) <- c(vj,Xj,Xi) - 1

Since­ there­ are­ 2a­ arcs­ and­ each­ variable­ has d­ values­ in­ its­ domain,­

there­are­2ad­counters.­Each­counter­is­initialised­at­most­to­d,­as­each­

pair­ Xj-vj­ may­ only­ have­ d­ supporting­ values­ in­ the­ domain­ of­ another­

variable­­Xi.­

Hence,­the­inner­cycle­is­executed­at­most­2ad2­times,­which­determines­

the­time­complexity­of­the­propagation­phase­of­­AC-4­to­be­

O(ad2)

Page 11: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 11

Time Complexity of AC-4

Time Complexity of AC-4 (Overall): O(ad2).

Therefore,­the­overall­time­complexity­­of­­AC-4­is

O(ad2)

not­only­ in­ the­beginning­ (inicialisation + propagation)­but­also­on­ its­

subsequent­use­after­each­enumeration­(propagation­alone).

­ Such­assymptotic­worst-case­complexity­is­better­then­that­of­algorithm­AC-3,­

O(ad3).

­ Unfortunatelly,­ this­ improvement­ on­ the­ time­ complexity­ of­ AC-4­ is­ obtained­

with­a­much­less­favourable­space­complexity­than­that­of­AC-3.

Page 12: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 12

Space Complexity of AC-4

Space Complexity of AC-4: O(ad2).

As­a­whole­algorithm­­AC-4­maintains

o Counters:­As­discussed,­a­total­of­2ad

o Suporting Sets:­ In­ the­worst­case,­ for­each­constraint­Rij,­each­of­

the­d Xi-vi­pairs­supports­d­values­vj­ from­Xj­(and­vice-versa).­The­

space­to­maintain­the­supporting­sets­is­thus­­O(ad2).

o List:­Contains­at­most­2a­arcs

o Matrix M:­Maintains­nd­Boolean­values.

The­ space­ required­ to­ maintain­ the­ supporting­ sets­ dominates.­

Compared­ with­ AC-3,­ where­ a­ space­ of­ size­ O(a)­ was­ required­ to­

maintain­the­queue,­AC-4­has­the­much­worse­space­complexity­of­

O(ad2) Nevertheless,­ this­ space­ complexity­ is­ of­ the­ same­ order­ required­ to­

maintain­the­representation­of­constraints­by­extension.­

Page 13: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 13

Is AC-4 optimal?

­ The­assymptotic­complexity­of­AC-4,­cannot­be­improved­by­any­algorithm!­

­ In­fact,­to­check­whether­a­network­is­arc­consistent­it­is­necessary­to­test,­for­

each­constraint­Cij,­ that­ the­d­pairs Xi-vi­have­support­ in­Xj,­ for­which­d­ tests­

might­be­required.

­ Since­each­of­the­a­constraints­must­be­considered­twice,­­then­2ad2­tests­are­

required,­with­assymptotic­complexity­O(ad2)­similar­to­that­of­AC-4.

­ However,­ one­ should­ bear­ in­ mind­ that­ the­ worst­ case­ complexity­ is­

assymptotic.­ For­ “small”­ values­ of­ the­ parameters,­ the­ constants­ involved­

may­have­a­non­negligeable­effect.

­ Moreover,­ in­ typical­ cases,­ most­ of­ the­ data­ structures­ used­ might­ be­

unnecessary­(or­at­least­optimised).­

­ All­things­considered,­AC-3­outperforms­AC-4­almost­always,­in­practice.

Page 14: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 14

Maintaining Arc Consistency: From AC-4 to AC-6

­ Changes­to­AC-4­-­Algorithm­AC-6

Algorithm­AC-6­avoids­the­outlined­inefficiency­of­AC-4­with­a­basic­idea:­

instead­of­keeping­(counting)­all­values­vi­from­variable­Xi­that­support­a­

pair­Xj-vj,­it­simply­maintains­the­lowest­such­vi­that­supports­the­pair.

The­ initialisation­of­ the­algorithm­becomes­ “lighter”,­since­whenever­ the­

first­ value­ vi­ is­ found,­ no­ more­ supporting­ values­ are­ seeked­ and­ no­

counting­is­required.

AC-6­ also­ does­ not­ require­ the­ initialisation­ of­ supporting­ sets,­ i.e.­ to­

keep­the­set­of­all­pairs­Xi-vi­supported­by­a­pair­Xj-vj,­but­only­the­lowest­

such­supported­vi.

Of­ course,­ if­ these­ values­ are­ not­ initialised,­ they­ must­ be­ determined­

during­the­propagation­phase!­

Page 15: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 15

Complexity of AC-6

Typical complexity of AC-6

Algorithm­AC-6­has­a­space­complexity­of­O(ad), between­O(a))­of­AC-3­and­

O(ad2)­of­AC-4.­Like­in­AC-4,­the­time­complexity­is­O(ad2),­which­is­optimal­

assymptotically.

The­worst­case­time­complexity­that­can­be­inferred­from­the­algorithms­do­

not­give­a­precise­idea­of­their­average­behaviour­in­typical­situations.

For­such­study,­one­usually­tests­the­algorithms­in­a­set­of­“benchmarks”,­i.e.­

problems­­that­are­supposedly­representative­of­everyday­situations.

For­ these­ algorithms,­ either­ the­ benchmarks­ are­ instances­ of­ well­ known­

problems­ (e.g.­ N-queens),­ or­ one­ relies­ on­ randomly­ generated­ instances­

parameterised­by­

o their­size­(number­of­variables­and­cardinality­of­the­domains)­;­and­

o their­difficulty­(density­and­tightness­of­the­constraint­network).­

Page 16: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 16

Complexity of AC-6

Typical Complexity­of­algorithms­AC-3,­AC-4­e­AC-6­(N-queens)

0

2000

4000

6000

8000

10000

12000

14000

16000

4 5 6 7 8 9 10 11

# t

est

s an

d o

pera

tion

s

AC-3

AC-4

AC-6

Page 17: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 17

Complexity of AC-6

Typical Complexity­of­algorithms­AC-3,­AC-4­and­AC-6­(randomly­generated­problems­with­variable­tightness)

n = 12 variables, d= 16 values, density = 50%

0

2000

4000

6000

8000

10000

12000

14000

16000

18000

20000

220005 10 15 20

25

30

35

40

45

50

60

70

80

# t

ests

and

ope

rati

ons

AC-3

AC-4

AC-6

Page 18: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 18

Maintaining Arc Consistency: AC-3d

­ Bidirectionality­ of­ support­ was­ also­ exploited­ in­ an­ adaptation,­ of­ both­

algorithms­AC-6­and­AC-3,­resulting­in­algorithms­A-7­and­A-3d.

­ The­main­difference­between­algorithms­­AC-3­and AC-3d­consists­of­the­fact­

that­whenever­arc­aij­is­removed­from­queue­Q,­the­arc aji­is­also­removed,­in­

case­it­is­there.

­ In­ this­ case,­ both­ domains­ of­ Xi­ and­ Xj are­ revised,­ which­ avoids­ much­

duplicated­work.

­ Although­ it­does­not­ improve­ the­worst-case­complexity­of­AC-3,­ the­ typical­

complexity­ of­ AC-3d­ seems­ quite­ interesting­ (namely­ in­ some­ problems­ for­

which­­extensive­tests­were­performed).

Page 19: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 19

Maintaining Arc Consistency: AC-3d

Comparison­of­Typical­Complexity­(# of checks)­

(previous­ramdomly­generated­problems)

0

1000000

2000000

3000000

4000000

5000000

6000000

7000000

8000000

9000000

Prob 1 Prob 2 Prob 3a Prob 3b Prob 4a Prob 4b

AC-3AC-4AC-6AC-7AC-3d

Page 20: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 20

Maintaining Arc Consistency: AC-3d

Comparison­of­Typical­Complexity­

(previous­ramdomly­generated­problems)

(equivalent CPU time,­in­ms,­in­a­Pentium­at­200­MHz)

0

1000

2000

3000

4000

5000

6000

Prob 1 Prob 2 Prob 3a Prob 3b Prob 4a Prob 4b

AC-3AC-4AC-6AC-7AC-3d

Page 21: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 21

Maintaining Arc Consistency: AC-3d

Comparison­of­Typical­Complexity­

(previous­ramdomly­generated­problems)

(equivalent CPU time,­in­ms,­in­a­Pentium­at­200­MHz)

0

1000

2000

3000

4000

5000

6000

Prob 1 Prob 2 Prob 3a Prob 3b Prob 4a Prob 4b

AC-3AC-4AC-6AC-7AC-3d

Page 22: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 22

Maintaining Arc Consistency: AC-3d

­ Results­seem­particularly­interesting­for­problems­in­which­AC-7­was­proved­

much­superior­to­AC-6,­both­in­number­of­tests­and­in­CPU­time.

­ This­is­the­case­of­problem­RLFAP (Radio Link Frequency Assignment

Problem)-

­ This­­problem­consists­of­assigning­radio­frequencies­in­a­safe­way­(no­risk­of­

scrambling),­for­which­instances­with­3,­5,­8­e­11­antenae­were­studied.

­ The­code­(­as­well­as­that­of­other­benchmark­problems)­may­be­found­in­a­

benchmark­archive,­available­from­the­internet,­in­URL

­ http://ftp.cs.unh.edu/pub/csp/archive/code/benchmarks

­ Together­with­many­other­problems,­it­composes­the­benchmark­web­library

­http://www.csplib.org

Page 23: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 23

Maintaining Arc Consistency: AC-3d

Comparison­of­Typical­Complexity­(#­of­checks)­

(RFLAP­problems)

0

200000

400000

600000

800000

1000000

1200000

#3 #5 #8 #11

AC-3AC-6AC-7AC-3d

Page 24: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 24

Maintaining Arc Consistency: AC-3d

Comparison­of­Typical­Complexity­

(RFLAP­problems)

(equivalent CPU time,­in­ms,­in­a­Pentium­at­200­MHz)

0

500

1000

1500

2000

2500

# 3 # 5 # 8 # 11

AC-3AC-6AC-7AC-3d

Page 25: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 25

Path Consistency

­ In­addition­to­arc­consistency,­other­types­of­consistency­may­be­defined­for­

binary­constraint­networks.

­ Path­ consistency­ is­ a­ “classical”­ type­ of­ consistency,­ stronger­ than­ arc­

consistency,­ and­ requiring­ some­ “more­ global­ view”­ of­ the­ problem,­ not­

centered­in­each­constraint­in­isolation.

­ The­basic­idea­of­path­consistency­is­that,­in­addition­to­check­support­in­the­

arcs­ of­ the­ constraint­ network­ between­ variables­ Xi­ and­ Xj,­ further­ support­

must­be­checked­in­the­variables­Xk1, Xk2

... Xkm­that­form­a­path­between­ Xi­

and­Xj,­i.e.­whenever­there­are­constraints­­Ci,k1, Ck1,k2

, ..., Ckm-1, km and Ckm,j.

­ In­fact,­it­is­possible­to­show­this­is­equivalent­to­seek­support­in­any­variable­

Xk,connected­to­both­Xi­and­Xj.

Page 26: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 26

Path Consistency

Definition­(Path Consistency):

A­constraint­satisfaction­problem­is­path-consistent­if,­

o It­is­arc-consistent;­and

o For­every­pair­of­variables Xi­and­Xj,­and­paths­Xi-Xi1- ... - Xik

–Xj,­

the­direct­constraint­Ci,j­is­tighter­than­the­composition­of­the­

constraints­in­the­path­Ci,i1, Ci1,i2

, ... , Cin,j.­

­ In­practice,­every­value­vi­in­variable­Xi­must­have­support,­not­only­in­some­

value­vj from­variable­Xj­but­also­in­values­vi1, vi2

­,­...­,­vin­from­the­domain­of­

the­variables­in­the­path.

Page 27: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 27

Path Consistency

­ Maintaining­this­type­of­consistency­has­naturally­a­higher­computational­cost­

than­maintaining­a­simpler­criterion­such­as­arc­consistency.

­ In­order­to­do­so,­it­is­convenient­to­maintain­a­representation­by­extension­of­

the­binary­constraints,­in­the­form­of­a­boolean­matrix.

­ Assuming­that­variables­ Xi­and­Xj­have,­respectively,­di and­dj values­in­their­

domain,­constraint­Cij­is­maintained­as­a­boolean­matrix­­Mij­of­size­di*dj.

­ The­value­1/0­of­element­Mij[k,l]­indicates­whether­the­pair­{Xi-vk, Xj-vl}­

satisfies/or­not­constraint­Cij.

Page 28: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 28

Path Consistency

Example: 4 queens

Boolean­ Matrix­ M12,­ regarding­ constraint­ C12­ between­ variables­ X1­ e­ X2­ (or­ any­variables­in­consecutive­rows)­

1\ 2 1 2 3 4

1 0 0 1 1

2 0 0 0 1

3 1 0 0 0

4 1 1 0 0

M12[1,3] = M12[3,1] = 1

1\ 3 1 2 3 4

1 0 1 0 1

2 1 0 1 0

3 0 1 0 1

4 1 0 1 0

Boolean­Matrix­M13,­regarding­constraint­C12­between­variables­X1­and­X2­(or­any­variables­rows­separed­by­one­row)­

M12[3,4] = M12[4,3] = 0

M13[1,2] = M13[2,1] = 1

M13[2,4] = M13[4,2] = 0

Page 29: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 29

Checking Path Consistency

Checking Path Consistency

­ To­eliminate­from­matrix­Mij­(i.e.­to­zero­an­element)­those­values­­that­do­not­

satisfy­path­consistency­through­a­third­variable,­Xk,­one­may­use­operations­

similar­to­matrix­multiplication­and­sum.

MIJ <- MIJ MIK MKJ­­

where­ the­ ­ operation­ operates­ like­ in­ a­ matrix­ “sum”,­ but­ with­ arithmetic­

addition­replaced­by­boolean conjunction,­and­the­­operation­corresponds­

to­ matrix­ multiplication­ in­ which­ arithmetic­ multiplication­ and­ addition­ are­

replaced­by­boolean­conjunction­and­disjunction.

­ One­ must­ still­ consider­ the­ diagonal­ matrix­ Mkk­ to­ represent­ the­ domain­ of­

variable­ Xk.

Page 30: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 30

Path Consistency: 4 Queens

Example (4 queens):

­ Checking­if­compound­label­{X1-1,­X3-4}­is­path­inconsistent,­through­X2.

M13[1,4]­<-­M13[1,4]­­M12[1,X]­­M23[X,4]­­

­ In­fact,­­M12[1,X]­­M23[X,4]­=­0­since

­­­M12[1,1]­­M23[1,4]­­­%­X2-1­does­not­support­{X1-1,X3-4}

­M12[1,2]­­M23[2,4]­­­%­X2-2­does­not­support­{X1-1,X3-4}

­M12[1,3]­­M23[3,4]­­­%­X2-3­does­not­support­{X1-1,X3-4}

­M12[1,4]­­M23[4,4]­­­%­X2-4­does­not­support­{X1-1,X3-4}

1\ 2 1 2 3 4 2\ 3 1 2 3 4 1\ 3 1 2 3 4 1\ 3 1 2 3 4

1 0 0 1 1 1 0 0 1 1 1 0 1 0 1 1 0 1 0 0

2 0 0 0 1 2 0 0 0 1 2 1 0 1 0 2 1 0 0 0

3 1 0 0 0 3 1 0 0 0 3 0 1 0 1 3 0 0 0 1

4 1 1 0 0 4 1 1 0 0 4 1 0 1 0 4 0 0 1 0

Page 31: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 31

Path Consistency: 4 Queens

­ Path­ consistency­ is­ stronger­ than­ arc­ consistency,­ in­ the­ sense­ that­ its­

maintenace­will­allow,­in­general,­to­eliminate­more­redundant­values­from­the­

domains­of­the­variables­than­simpler­arc­consistency­is­able­to­do

­ In­particular,­for­the­4­queens­problem,­the­maintenance­of­path­consistency­

performs­the­elimination­from­the­­domain­of­the­variables­of­all­the­redundant­

values,­ that­ do­ not­ belong­ to­ any­ solution,­ even before any enumeration

takes place!

­ The­ problem­ reduced­ by­ path­ consistency­ may­ thus­ be­ solved­ with­ a­

backtracking-free­enumeration.­

­ Notice,­ that­ the­ enumeration­ and­ its­ backtracking­ is­ the­ cause­ of­ the­

exponential­complexity­of­solving­non­trivial­problems.

Page 32: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 32

Path Consistency: 4 Queens

Path­consistency­in­the­4­queens­problem

1\ 2 1 2 3 4 2\ 3 1 2 3 4 1\ 3 1 2 3 4 1\ 3 1 2 3 4

1 0 0 1 1 1 0 0 1 1 1 0 1 0 1 1 0 1 0 0

2 0 0 0 1 2 0 0 0 1 2 1 0 1 0 2 1 0 0 0

3 1 0 0 0 3 1 0 0 0 3 0 1 0 1 3 0 0 0 1

4 1 1 0 0 4 1 1 0 0 4 1 0 1 0 4 0 0 1 0

1\ 3 1 2 3 4 3\ 4 1 2 3 4 1\ 4 1 2 3 4 1\ 4 1 2 3 4

1 0 1 0 0 1 0 0 1 1 1 0 1 1 0 1 0 0 0 0

2 1 0 0 0 2 0 0 0 1 2 1 0 1 1 2 0 0 1 1

3 0 0 0 1 3 1 0 0 0 3 1 1 0 1 3 1 1 0 0

4 0 0 1 0 4 1 1 0 0 4 0 1 1 0 4 0 0 0 0

Page 33: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 33

Path Consistency: 4 Queens

1\ 4 1 2 3 4 4\ 2 1 2 3 4 1\ 2 1 2 3 4 1\ 2 1 2 3 4

1 0 0 0 0 1 0 1 0 1 1 0 0 1 1 1 0 0 0 0

2 0 0 1 1 2 1 0 1 0 2 0 0 0 1 2 0 0 0 1

3 1 1 0 0 3 0 1 0 1 3 1 0 0 0 3 1 0 0 0

4 0 0 0 0 4 1 0 1 0 4 1 1 0 0 4 0 0 0 0

1\ 2 1 2 3 4 2\ 3 1 2 3 4 1\ 3 1 2 3 4 1\ 3 1 2 3 4

1 0 0 0 0 1 0 0 1 1 1 0 1 0 0 1 0 0 0 0

2 0 0 0 1 2 0 0 0 1 2 1 0 0 0 2 1 0 0 0

3 1 0 0 0 3 1 0 0 0 3 0 0 0 1 3 0 0 0 1

4 0 0 0 0 4 1 1 0 0 4 0 0 1 0 4 0 0 0 0

1\ 2 1 2 3 4 2\ 4 1 2 3 4 1\ 4 1 2 3 4 1\ 4 1 2 3 4

1 0 0 0 0 1 0 1 0 1 1 0 0 0 0 1 0 0 0 0

2 0 0 0 1 2 1 0 1 0 2 0 0 1 1 2 0 0 1 0

3 1 0 0 0 3 0 1 0 1 3 1 1 0 0 3 0 1 0 0

4 0 0 0 0 4 1 0 1 0 4 0 0 0 0 4 0 0 0 0

Page 34: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 34

Maintaining Path Consistency

­ Of­ course,­ this­ increase­ in­ the­ pruning­ power­ of­ path­ consistency­ does­ not­

come­for­free.­The­computational­complexity­of­achieving­and­maintaining­it­is­

(much)­greater­than­the­costs­associated­­with­arc­consistency.

­ The­ algorithms­ to­ maintain­ path­ consistency,­ PC-x,­ have­ therefore­ higher­

complexity­than­those­of­the­AC-y family.

­ As­ an­ example,­ algorithm­ PC-1­ (quite­ simple,­ with­ no­ optimisatiosn)­ is­

presented.

­ ­ The­ more­ sophisticated­ algorithms,­ PC-2­ and­ PC-4,­ include­ optimisations­

that­avoid­repetition­of­tests,­much­in­the­same­way­that­the­higher­members­

of­ the­ AC-y­ family­ do.­ We­ will­ simply­ outline­ the­ optimisations­ and­ present­

their­complexity.

Page 35: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 35

Maintaining Path Consistency: PC-1

Algorithm PC-1

procedure PC-1(V,D,C);

n <- #Z; Mn <- C;

repeat

M0 <- Mn;

for k from 1 to n do

for i from 1 to n do

for j from 1 to n do

Mijk <- Mijk-1 Mikk-1 Mkkk-1 Mkjk-1

end for

end for

end for

until Mn = M0

C <- Mn

end procedure

Page 36: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 36

Maintaining Path Consistency: PC-1

­ Algorithm PC-1:­Time Complexity

­ The­main­procedure­performs­a­cycle

repeat

...

until Rn = R0

When­ there­ are­ n2­ constraints­ Cij­ (dense­ graph),­ since­ each­ of­ the­

corresponding­matrix­has­d2­elements,­in­the­worst­case­only­one­element­is­

zero-ed­in­each­cycle.­Hence,­the­cycle­can­be­executed­up­to­n2d2­times.

­ In­each­cycle,­we­have­­n3­nested­cycles­of­the­form

for k from 1 to n do

for i from 1 to n do

for j from 1 to n do

Page 37: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 37

Maintaining Path Consistency: PC-1

Algorithm PC-1:­Time Complexity (cont.)

­ Each­operation

Mijk <- Mijk-1 Mikk-1 Mkkk-1 Mkjk-1­

requires­O(d3)­binary­operations,­since­each­of­ the­d2­elements­ is­computed­

through­ d­ ’s (boolean­ conjunction)­ and­ d-1­ operations­ ’s (boolean­

conjunction)­. ­

­ Combining­all­these­factors,­the­time­complexity­for­PC-1­becomes­

O(n2d2 *n3 * d3) ­­­­­­i.­e.­­O(n5d5)

much­ higher­ than­ those­ obtained­ with­ AC­ algorithms­ (even­ AC-1,­ with­

complexity­ O(nad3),­ presents­ complexity­ O(n3d3)­ for­ dense­ networks,­ for­

which­a n2).

Page 38: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 38

Maintaining Path Consistency: PC-1

Algorithm PC-1:­Space Complexity

­ The­space­complexity­of­PC-1­derives­from­maintenance­of

n3­matrices­Mijk,­ for­all­sets­of­ ­constraints­Cij,­and­the­paths­ through­a­

different­variable­­Xk.­

The­size,­d2­elements,­of­each­such­matrix.

­ Hence,­the­space­complexity­­of­algorithm­PC-1­is­­

O(n3d2)

­ This­complexity,­again­much­higher­than­in­the­AC­case­(AC-4­has­complexity­

O(ad2) O(n2d2)­),­is­due­to­the­explicit­representation­of­the­constraints,­and­

the­paths,­no­more­data­structures­being­maintained.

Page 39: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 39

Maintaining Path Consistency: PC-2

Algorithm PC-2: Complexity­

­ This­ algorithm­ maintains­ a­ list­ of­ the­ paths­ that­ have­ to­ be­ reexamined­

because­of­­the­zero-ing­of­values­in­the­matrices­­Mij,­(the­same­principle­of­

AC-3)­such­that­only­relevant­consistency­test­are­subsequently­performed.

­ In­contrast­with­complexity­­O(n5d5)­of­PC-1,­the­time­complexity­of­PC-2­is

O(n3d5)

­ The­ space­ complexity­ is­ also­ better­ than­ with­ PC-1,­ O(n3d2).­ For PC-2­ the­

space­complexity­is­­

O(n3+n2d2)

Page 40: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 40

Maintaining Path Consistency: PC-4

­ Algorithm PC-4: Complexity

­ By­ analogy­ with­ AC-4,­ algorithm­ PC-4,­ maintains­ a­ set­ of­ counters­ and­

pointers­ to­ improve­ the­ evaluation­ of­ the­ cases­ where­ the­ removal­ of­ an­

element­implies­the­reevaluation­of­the­paths.

­ In­ contrast­ with­ the­ time­ complexity­ of­ PC-1,­ ­ O(n5d5),­ and­ that­ of­ PC-3,­

O(n3d5),­the­time­complexity­of­­PC-4­is

O(n3d3)

­ As­ expected,­ the­ space­ complexity­ of­ PC-4­ is­ worse­ than­ that­ of­ PC-2,­

O(n3+n2d2),­being­similar­to­that­of­PC-1,­i.e.

­O(n3d2)

Page 41: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 41

K-Consistency

­ Node,­arc­and­path­consistency,­are­all­instances­of­the­more­general­case­of­

k-consistency.

­ Informally,­ a­ constraint­ network­ is­ k-consistent­ when­ for­ a­ group­ of­ k

variables,­Xi1,... , Xik

­ the­values­in­each­domain­have­support­ in­those­of­the­

other­variables,­considering­this­support­in­a­global­form.

­ The­ following­ examples­ shows­ a­ classical­ example­ of­ the­ advantages­ of­

keeping­a­global­view­on­constraints

A­node­consistent­network,­that­is­not­arc­consistent

0 0

Page 42: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 42

K-Consistency

An­arc­consistent­network,­that­is­not­path­consistent

0,1 0,1

0,1

A­path-consistent­network,­that­is­not­4-consistent

0..2 0..2

0..2

0..2

Page 43: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 43

(Strong) k-Consistency

­ Definition (k-Consistency):

A­constraint­satisfaction­problem­(or­constraint­network)­is­­1-consistent­

if­ the­ values­ in­ the­ domain­ of­ its­ variables­ satisfy­ all­ the­ unary­

constraints.

A­network­is­k-consistent­iff­all­its­(k-1)-compound labels­(i.e.­formed­

by­k-1­ pairs­X-v)­ that­ satisfy­ the­ relevant­ constraints­ can­be­extended­

with­some­label­on­any­other­variable,­to­form­some­k-compound­labels­

that­make­the­network­k-1­consistent­(satisfy­the­relevant­constraints)­.

­ Definition (Strong k-Consistency):

A­constraint­problem­is­strongly­k-consistent,­if­and­only­if­it­is­­­­­­­­­­­­i-

consistent,­for­all­i 1 .. k.­

Page 44: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 44

(Strong) k-Consistency

• A­constraint­network­may­be­k-consistency­but­not­m-consistent­(for­m­<­k).­

For­example,­the­network­below­is­­3-consistent,­but­not­­2-consistent.­Hence­

it­is­not­strongly­3-consistent.

The­only­2-compound­labels,­that­satisfy­the­constraints

{A-0,B-1},­­{A-0,C-0},­and­{B-1,­C-0}

may­be­extended­to­the­remaining­variable

{A-0,B-1,C-0}.

However,­the­1-compound­label­{B-0}­cannot­be­extended­

to­variables­A­or­C­{A-0,B-0}­!­

0 0

0,1

B

CA

As­ can­ be­ seen­ from­ the­ definitions,­ node,­ arc­ and­ path­ consistency­ are­ all­

instances­of­strong k-consistency).­In­fact,­a­constraint­network­is

node consistent­­iff­it­is­strongly 1-consistent.

arc consistent­­iff­it­is­strongly­2-consistent.

path consistent ­iff­it­is­strongly­3-consistent.

Page 45: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 45

(i,j) -Consistency

­ The k-consistency­criterion­may­still­be­extended­to­a­more­general­criterion­­

of­(i,j)-consistency.

A­problem­is­(i,j)-consistent­iff­all­its­i-compound­labels­that­satisfy­the­

relevant­ constraints­ may­ be­ extended­ with­ the­ addition­ of­ any­ j­ new­

labels­ to­ form­ a­ k-compound­ label­ (where­ k=i+j)­ that­ still­ satisfy­ the­

relevant­constraints­.

­ Of­course,­k-consistency­is­equivalent­to­­(k-1,1)-consistency

­ Although­ interesting,­ from­ a­ theoretical­ viewpoint,­ criteria­ such­ as­ k-

consistency­ and­ (i,j)-consistency­ are­ not­ maintained­ in­ practice­ (but­ see­

algorithm­KS-1­[Tsan93]).­

Page 46: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 46

Generalised Arc-Consistency

­ In­ the­ general­ case­ of­ non-binary­ constraints­ and­ constraint­ networks,­ the­

notion­ of­ node­ consistency­ and­ its­ enforcing­ algorithm­ NC-1­ may­ be­ kept,­

since­they­only­regard­unary­constraints.­

­ The­ generalisation­ of­ arc­ consistency­ for­ general n-ary­ constraint­ networks­

(for­any­n > 2)­is­quite­simple.

­ The­concept­of­an­arc­must­be­replaced­by­that­of­an­hyper-arc,­and­it­must­

be­guaranteed­that­each­value­in­the­domain­of­a­variable­must­be­supported­

by­values­in­the­other­variables­that­share­the­same­hyper-arc­(constraint).

­ Although­ less­ intuitive­ (what­ is­ a­ path­ in­ a­ hyper-graph?),­ path­ consistency­

may­still­be­defined­by­means­of­strong­3-consistency­for­hyper-graphs.

Page 47: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 47

Maintaining Generalised Arc-Consistency: GAC-3

­ In­practice,­only­generalised­arc-consistency­ is­maintained,­by­adaptation­of­

the­AC-x­algorithms,­as­is­the­case­of­the­algorithm­GAC-3­below.­

­ To­ each­ k-ary constraint­ one­ has­ to­ consider­ k­ directed­ hyper-arcs­ to­

represent­the­support.­

procedure GAC-3(V, D, C);

NC-1(V,D,C); % node consistency

Q = {hai..j|Ci..j C ... Ci..j C}; %k hyper-arcs

while Q do Q = Q \ {hai..j } % removes an element

from Q

if revise_dom(hai..j,V,D,C) then % Xi revised

Q = Q {hak1..kn | Xi vars(Ck2..kn) k1..kn i .. j }

end while

end procedure

Page 48: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 48

Maintaining Generalised Arc-Consistency: GAC-3

Algorithm GAC-3: Time Complexity

By­comparison­with­AC-3,­predicate revise_dom,­checks­at­most­dk(d2)­k-

tuples­of­values­in­k-ary­constraints.

Whenever­a­ value­vi­ is­ removed­ from­ the­domains­of­Xi,­k-1­ hyper-arcs­

are­inserted­in­queue­Q for­each­k-ary constraint­involving­that­variable.­

As­a­whole,­each­of­the­ka­hyper-arcs­may­be­inserted­(and­subsequently­

removed)­ (k-1)d times­ from­ queue­ Q.­ All­ factors­ considered,­ the­ worst­

case­time­complexity­of­algorithm­GAC-3­is­O(k(k-1)2ad * dk),­i.e.­

O(k2adk+1)

Of­course,­AC-3,­with­O(ad3),­is­the­special­case­of­GAC-3 (for­k=2).

Page 49: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 49

Constraint Dependent Consistency

­ The­algorithms­and­consistency­criteria­considered­so­far­take­into­no­account­

the­semantics­of­specific­constraints,­handling­them­all­in­a­uniform­way.

­ In­ practice,­ such­ approach­ is­ not­ very­ useful,­ since­ important­ gains­ may­ be­

achieved­by­using­specialised­criteria­and­algorithms.

­ For­ example,­ regardless­ of­ more­ general­ schemes,­ it­ is­ not­ useful,­ for­

inequality­constraints­(),­when considered separately,­to­go­beyond­simple­

node­consistency.

­ In­ fact,­ for­ ­Xi Xj,­one­may­only­eliminate­a­value­ from­a­variable­domain,­

when­the­domain­of­the­other­is­reduced­to­a­singleton.

Page 50: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 50

Constraint Dependent Consistency

­ Other­ more­ specific­ types­ of­ consistency­ may­ be­ used­ and­ enforced.­ For­

example­in­the­n­queens­problem,­­the­constraint

no_attack(Xi, Xj)

­ should­only­be­considered­­when­the­size­of­the­domain­of­one­of­the­variables­

becomes­reduced­to­3­or­less­than­3­values.

­ In­fact,­if Xi­maintains­3­values­in­the­domain,­all­values­in­the­domain­of­­Xj­will­

have,­at­least,­one­supporting­value­in­the­domain­of­­Xi.

­ Thi­is­because­the­constraint­is­simmetrical­and­any­queen­may­only­attack­3­

queens­in­a­different­row.

Page 51: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 51

Bounds Consistency

­ Another­specific­type­of­consistency­that­is­very­useful­in­numerical­constraints­

is­ “bounds-consistency”,­ often­ used­ with­ ­ constraints­ such­ as­ =<, <, =­ ­ in­

ordered­domains.

­ For­ example,­ consider­ constraint­ A = B,­ when­ variables­ A­ and­ B,­ have­

domains

A:: {1, 2, 3, 4, 5, 8, 10} and B :: {4, 6, 8, 12, 15, 20}

­ Whereas­arc­consistency­would­reduce­the­domains­of­the­variables­to­{4, 8}­

with­ some­ computational­ cost,­ enforcing­ simpler­ bounds­ consistency­ would­

result­in­the­domains

A:: {4, 5, 8} and B :: {4, 6, 8}

­ Although­these­domains­keep­redundant­values,­bounds­consistency­is­much­

easier­to­maintain.

Page 52: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 52

Bounds Consistency

­ Bounds­ consistency­ is­ specially­ interesting­ when­ dealing­ with­ very­ large­

domains,­and­with­n-ary­constraints,­as­is­common­in­numerical­problems.

­ Consider­for­example­the­ternary­constraint­A + B =< C,­where­variables­have­

domains­1..1000.­Arc­consistency­would­ require­checking­10003 = 109 triples­

<vA, vB, vC>­!!!­

­ Bounds­consistency­is­much­easier­to­achieve,­by­simply­enforcing

min dom(C) >= min dom(A) + min dom(B)

max dom(A) =< max dom(C) - min dom(B)

max dom(B) =< max dom(C) - min dom(A)

Page 53: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 53

Constraint Trees

• Before­addressing­ the­need­ for­ heuristic­ search­ (even­ in­ reduced­networks),­

namely­ the­ importance­ of­ the­ order­ in­ which­ variables­ are­ selected­ for­

enumeration,­let­us­consider­the­special­case­of­constraint­graphs­that­take­the­

form­of­a­tree.­

If­ variable­A­ is­enumerated­ first,­

with­ some­ value­ a­ from­ its­

domain­ (A = a),­ the­ problem­ is­

decomposed­ into­ 3­ independent­

subproblems.

A

E

CB

GF

D

H I

E

CB

GF

D

H I

Page 54: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 54

Constraint Trees

• To­guarantee­that­the­enumeration­does­not­lead­immediately­to­unsatisfiability,­

value­a­from­variable­A­must­have­support­in­variables­­B,­C­­and­D.

• This­reasoning­may­proceed­recursively,­for­each­of­the­subtrees­with­roots­­B,­

C­ and­ D.­ For­ example,­ enumeration­ of­ B­ (say,­ B = b)­ does­ not­ lead­ to­ an­

immediate­“dead­end”­if­value­b­has­support­in­E­and­F.­

• In­ general,­ if­ enumeration­ of­ the­ variables­ in­ the­ tree­ is­ made­ from­ root­ to­

leaves,­such­enumeration­will­not­lead­to­unsatisfiability­if­the­values­kept­in­any­

node­have­support­in­their­children­nodes.

• Hence,­ a­ good­ heuristic­ to­ select­ the­ variable­ to­ enumerate­ is­ choosing­

variables­in­the­root­of­the­tree­(or­subtree).

E

CB

GF

D

H I

Page 55: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 55

Constraint Trees

­ Although­ this­ is­ not­ the­ case­ in­ general­ constraint­ graphs,­ as­ seen­ before,­

constraint­trees­that­are­arc­consistent­are­also­satisfiable.­

­ In­fact,­using­any­heuristics­that­selects­the­roots­of­the­tree­and­its­subtrees,­

An­arbitrary­value­in­the­domain­of­the­root­variable­may­be­chosen­

This­choice­never­ leads­to­contradiction,­since­all­values­ in­the­domain­of­

the­root­variables­have­support­in­their­children­variables,­

Such­support­is­recursively­guaranteed­until­the­leaves.

­ Moreover,­ this­ direccionality­ in­ the­ enumeration­ (from­ root­ to­ leaves)­ enables­

the­consideration­of­directed arc consistency,­a­weaker­criterion­than­full­arc­

consistency,­but­sufficient­to­handle­constraint­trees.

Page 56: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 56

Constraint Trees

­ In­ the­ example,­ it is not necessary­

that­ all­ values­ in­ the­ domain­ of­ B,­ C­

and­D­have­support­in­the­domain­of­A.­

­ The­only­requirement­is­that­all values

in the domain of A have support­ in­

the­domains­of­B,­C­and­D.

­ When­ a­ value­ is­ chosen­ for­ A,­

constraints­ on­ A­ and­ its­ descendents­

become­unary,­and­the­values­from­the­

domains­of­B,­C­and­D­are­removed­by­

simple­node­consistency.­

A

E

CB

GF

D

H I

E

CB

GF

D

H I

Page 57: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 57

Directed Arc Consistency

­ In­general,­one­may­define­a­criterion­of­directed­arc­consistency­if,­in­contrast­to­

what­ has­ been­ considered,­ we­ consider­ a­ directed graph­ to­ represent­ the­

constraint­network­(assuming­some­direction­to­each­constraint­of­the­problem).

Definition (Directed Arc Consistency):

A­constraint­problem­is­directed arc­consistent­iff

It­is­node­consistent;­and

For­every­label­Xi-vi­of­any­variable­Xi,­and­for­any­directed arc­aij­(from­Xi

to­Xj)­corresponding­to­a­constraint­Cij,­there­is­a­supporting­value­vj­ in­the­

domain­of­Xj,­i.e.­the­compound­label­{Xi-vi, Xj-vj}­satisfies­constraint Rij.

Page 58: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 58

Maintaining Directed Arc Consistency: DAC

Maintaining Directed Arc Consistency

­ The­algorithm­below­assumes­that­variables­are­“sorted”­from­1­to­n,­and­there­

are­ only­ arcs­ from­ the­ “lowest”­ to­ the­ “highest”­ variable.­ It­ simply­ enforces­

consistency­ in­ all­ directed­ arcs,­ starting­ with­ the­ highest­ variables,­ without­

considering­any­further­reexamination­of­the­arcs.

procedure DAC(V, D, C); for j from n downto 2 do for Cij in C do if i < j then revise_dom(aij,V,D,C) end if end for end forend procedure

Note:­revise_dom(aij,V,D,R)­is­that­used­in­AC-1­and­AC-3.

Page 59: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 59

Maintaining Directed Arc Consistency: DAC

Algorithm DAC: Time Complexity­­

­ As­usual,­let­us­assume­a­arcs­and­that­the­n­variables­have­d­values­in­their­

domain.

­ The­ algorithm­ visits­ each­ arc­ exactly­ once.­ For­ each­ arc­ aij,­ each­ of­ the­ d­

values­of­variable Xi­is­tested­with­d­values­from­variable­Xj,­in­the­worst­case.­­

­ All­factors­considered,­and­like­in­AC-4/6/7,­the­worst­case­time­complexity­of­

algorithm­DAC­is­O(ad2).­

­ However,­in­a­tree­a = n-1­(not­a n2)­so­the­complexity­of­DAC­is

O(nd2)

­ Moreover,­ the­ algorithm­ has­ no­ requirements­ regarding­ memory­ use­ (space­

complexity).

Page 60: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 60

Maintaining Directed Arc Consistency: DAC

­ As­ can­ be­ seen,­ algorithm­ DAC­ does­ not­ guarantee­ the­ elimination­ of­ all­

redundant­ values,­ since­ it­ does­ not­ reexamine­ variables­ that­ could­ loose­

support.

­ If­ in­ the­general­case­ this­might­be­ innadequate,­ this­ is­not­a­problem­ in­ the­

case­ of­ trees,­ if­ the­ arcs,­ which­ are­ directed­ in­ descending­ order­ of­ the­

variables­ (the­ tree­ root­has­ lowest­number)­are­ revised­ in­descending­order,­

i.e.­arcs­closer­to­the­leaves­are­revised­first.

­ Then­all­the­values­of­a­node­have­guaranteed­support­in­all­the­nodes­down­

to­the­leaves­of­a­tree,­although­not­necessarily­on­those­upto­the­tree­root!­

­ As­shown­before,­ if­enumeration­starts­ from­the­root,­ the­unsupported­values­

are­eliminated­by­node­consistency.

Page 61: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 61

Maintaining Directed Arc Consistency: DAC

Example:­­Take­the­constraint­tree­below

1

5

32

76

4

8 9

> >

>> > >>

>X1­in­{1,­3,­5,­7,­9},

X2­in­{2,­4,­6},­X5­in­{4,8},­X6­in­{3,­9},

X3­in­{1,­5,­7},­X7­in­{3,­9},­

X4­in­{1,­5,­8},­X8­in­{3,­7},­X9­in­{2,­9}

After­revising­arc X9­->­X4­ X4­in­{1,­5,­8}­ %­X9­>=­2­

X8­->­X4­ X4­in­{1,­5,­8} %­X8­>=­3

X7­->­X3­ X3­in­{1,­5,­7} %­X7­>=­3

X6­->­X2­ X2­in­{2,­4,­6} %­X6­>=­3

X5­->­X2­ X2­in­{2,­4,­6} %­X5­>=­4

X4­->­X1­ X1­in­{1,­3,­5,­7,­9} %­X4­>=­5

X3­->­X1­ X1­in­{1,­3,­5,­7,­9} %­X3­>=­5

X2­->­X1­ X1­in­{1,­3,­5,­7,­9} %­X2­>=­6

Page 62: 30 November 2005 Foundations of Logic and Constraint Programming 1 Constraint (Logic) Programming An overview Optimising Arc Consistency Path Consistency

30 November 2005 Foundations of Logic and Constraint Programming 62

Maintaining Directed Arc Consistency: DAC

Example (cont):­The­enumeration­became­backtrack free­!

X1­in­{7,­9},

X2­in­{6},­X5­in­{4,­8},­X6­in­{3,­9},

X3­in­{5,­7},­X7­in­{3,­9},­

X4­in­{5,­8},­X8­in­{3,­7},­X9­in­{2,­9}

X1­=­7­­NC­enforces­X2­in­{6},­X3­in­{5},­X4­in­{5}

­­­­­X2­=­6­­NC­enforces­X5­in­{4},­X6­in­{3},

­­­­­X3­=­5­­NC­enforces­X7­in­{3},

­­­­­X4­=­4­­NC­enforces­X8­in­{3},­X9­in­{2},

X1­=­9­­NC­enforces­X2­in­{6},­X3­in­{5,7},­X4­in­{4,­8}

­­­­­X2­=­6­­....,­X3­in­{5,7},­enforces­X7­=­3­

­­­­­X4­=­4­­NC­enforces­X8­in­{3},­­­­X9­in­{2}­­

or­­X4­=­8­­NC­enforces­X8­in­{3,­7},­X9­in­{2}

1

5

32

76

4

8 9

> >

>> > >>

>