Constraint (Logic) Programming
-
Upload
jesse-bush -
Category
Documents
-
view
54 -
download
2
description
Transcript of Constraint (Logic) Programming
11 December 2007 Foundations of Logic and Constraint Programming 1
Constraint (Logic) Programming
Anoverview
• OptimisingArcConsistency
• PathConsistency
• Otherformsofconsistency
• ConsistencyandConstraintTrees
[Dech04]RinaDechter,ConstraintProcessing,
11 December 2007 Foundations of Logic and Constraint Programming 2
Maintaining Arc Consistency: From AC-3 to AC-4
InefficiencyofAC-3
EverytimeavalueviisremovedfromthedomainofsomevariableXi,by
predicate revise_dom(aij,V,D,C), all arcs leading to that variable are
reexamined.
Nevertheless,onlysomeofthesearcsaki(fork iandk j)shouldbe
examined.
Althoughtheremovalofvimayeliminateonesupportforsomevaluevk
of another variable Xk for which there is a constraint Cki (or Cik), other
valuesinthedomainofXimaymaintainsupportinvariableXi forthepair
Xk-vk!
ThisideaisexploitedinalgorithmAC-4.
11 December 2007 Foundations of Logic and Constraint Programming 3
AlgorithmAC-4(counters):
AlgorithmAC-4maintainsdatastructurestoaccountforthenumberofvalues
inthedomainofvariableXithatsupportsomevaluevkfromanothervariable
Xk,forwhichthereisaconstraintCik.
For example, in the 4 queens problem, the counters that account for the
supportofvalueX1= 2areinitialisedasfollows
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
11 December 2007 Foundations of Logic and Constraint Programming 4
AlgorithmAC-4(supporting sets)
Toupdatethecounterswhenavalueiseliminated,itisusefultomaintain
the set of Variable-Value pairs that are supported by each value of a
variable.
AC-4 thusmaintain foreachValue-Variablepair thesetofallVariable-
Valuepairssupportedbytheformerpair.
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,
...
11 December 2007 Foundations of Logic and Constraint Programming 5
AlgorithmAC-4(propagation):
Every time that it isdetected thatavaluev fromavariableXdoesnot
havesupportinanothervariableY,visremovedfromthedomainofthe
variableXandisadddedtoalistforsubsequentpropagation.
However, although the value may loose support several times, its
removalmayonlybepropagated,in a useful way,once.
Toaccountforthissituation,AC-4maintainsaBooleanmatrixM.The1/0
valueofelementM[X,v] representswhethervaluev is/isnotpresent in
thedomainofvariableX.
Maintaining Arc Consistency: AC-4
11 December 2007 Foundations of Logic and Constraint Programming 6
AlgorithmAC-4(Overall Functioning)
AC-4makesuseofthedatastructurespresentedinthepredictableway.
Inafirstphase,initialisation,whichisexecutedonlyonce,thealgorithm
initialises thedatastructures (counters, supportingsets,booleanmatrix
andremovallist).
In thesecondphase,propagation,which isexecutednotonlyafter the
firstphase,butalsoaftereachenumerationstep,thealgorithmperforms
the actual constraint propagation, updating the data structures as
required.
Thetwophasesarepresentednext
Maintaining Arc Consistency: AC-4
11 December 2007 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)
11 December 2007 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)
11 December 2007 Foundations of Logic and Constraint Programming 9
Time Complexity of AC-4
Time Complexity of AC-4 (Initialisation): O(ad2).
Analysingthecyclesexecutedintheprocedureinitialise_AC-4,
for Cij in C do
for vi in dom(Xi) do
for vj in dom(Xj) do
andassumingthat
• thenumberofconstraints(arcs)isa,
• thevariableshavealldvaluesintheirdomains,
thentheinnercycleoftheprocedureisexecuted2ad2times,whichsets
thetimecomplexityoftheinitialisationphaseofAC-4to
O(ad2).
11 December 2007 Foundations of Logic and Constraint Programming 10
Time Complexity of AC-4
Time Complexity of AC-4 (Propagation): O(ad2).
IntheinnercycleofthepropagationprocedureacounterforpairXj-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,
thereare2adcounters.Eachcounterisinitialisedatmosttod,aseach
pair Xj-vj may only have d supporting values in the domain of another
variableXi.
Hence,theinnercycleisexecutedatmost2ad2times,whichdetermines
thetimecomplexityofthepropagationphaseofAC-4tobe
O(ad2)
11 December 2007 Foundations of Logic and Constraint Programming 11
Time Complexity of AC-4
Time Complexity of AC-4 (Overall): O(ad2).
Therefore,theoveralltimecomplexityofAC-4is
O(ad2)
notonly in thebeginning (inicialisation + propagation)butalsoon its
subsequentuseaftereachenumeration(propagationalone).
Suchassymptoticworst-casecomplexityisbetterthenthatofalgorithmAC-3,
O(ad3).
Unfortunatelly, this improvement on the time complexity of AC-4 is obtained
withamuchlessfavourablespacecomplexitythanthatofAC-3.
11 December 2007 Foundations of Logic and Constraint Programming 12
Space Complexity of AC-4
Space Complexity of AC-4: O(ad2).
AsawholealgorithmAC-4maintains
o Counters:Asdiscussed,atotalof2ad
o Suporting Sets: In theworstcase, foreachconstraintRij,eachof
thed Xi-vipairssupportsdvaluesvj fromXj(andvice-versa).The
spacetomaintainthesupportingsetsisthusO(ad2).
o List:Containsatmost2aarcs
o Matrix M:MaintainsndBooleanvalues.
The space required to maintain the supporting sets dominates.
Compared with AC-3, where a space of size O(a) was required to
maintainthequeue,AC-4hasthemuchworsespacecomplexityof
O(ad2) Nevertheless, this space complexity is of the same order required to
maintaintherepresentationofconstraintsbyextension.
11 December 2007 Foundations of Logic and Constraint Programming 13
Is AC-4 optimal?
TheassymptoticcomplexityofAC-4,cannotbeimprovedbyanyalgorithm!
Infact,tocheckwhetheranetworkisarcconsistentitisnecessarytotest,for
eachconstraintCij, that thedpairs Xi-vihavesupport inXj, forwhichd tests
mightberequired.
Sinceeachoftheaconstraintsmustbeconsideredtwice,then2ad2testsare
required,withassymptoticcomplexityO(ad2)similartothatofAC-4.
However, one should bear in mind that the worst case complexity is
assymptotic. For “small” values of the parameters, the constants involved
mayhaveanonnegligeableeffect.
Moreover, in typical cases, most of the data structures used might be
unnecessary(oratleastoptimised).
Allthingsconsidered,AC-3outperformsAC-4almostalways,inpractice.
11 December 2007 Foundations of Logic and Constraint Programming 14
Maintaining Arc Consistency: From AC-4 to AC-6
ChangestoAC-4-AlgorithmAC-6
AlgorithmAC-6avoidstheoutlinedinefficiencyofAC-4withabasicidea:
insteadofkeeping(counting)allvaluesvifromvariableXithatsupporta
pairXj-vj,itsimplymaintainsthelowestsuchvithatsupportsthepair.
The initialisationof thealgorithmbecomes “lighter”,sincewhenever the
first value vi is found, no more supporting values are seeked and no
countingisrequired.
AC-6 also does not require the initialisation of supporting sets, i.e. to
keepthesetofallpairsXi-visupportedbyapairXj-vj,butonlythelowest
suchsupportedvi.
Of course, if these values are not initialised, they must be determined
duringthepropagationphase!
11 December 2007 Foundations of Logic and Constraint Programming 15
Complexity of AC-6
Typical complexity of AC-6
AlgorithmAC-6hasaspacecomplexityofO(ad), betweenO(a))ofAC-3and
O(ad2)ofAC-4.LikeinAC-4,thetimecomplexityisO(ad2),whichisoptimal
assymptotically.
Theworstcasetimecomplexitythatcanbeinferredfromthealgorithmsdo
notgiveapreciseideaoftheiraveragebehaviourintypicalsituations.
Forsuchstudy,oneusuallyteststhealgorithmsinasetof“benchmarks”,i.e.
problemsthataresupposedlyrepresentativeofeverydaysituations.
For these algorithms, either the benchmarks are instances of well known
problems (e.g. N-queens), or one relies on randomly generated instances
parameterisedby
o theirsize(numberofvariablesandcardinalityofthedomains);and
o theirdifficulty(densityandtightnessoftheconstraintnetwork).
11 December 2007 Foundations of Logic and Constraint Programming 16
Complexity of AC-6
Typical ComplexityofalgorithmsAC-3,AC-4eAC-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
11 December 2007 Foundations of Logic and Constraint Programming 17
Complexity of AC-6
Typical ComplexityofalgorithmsAC-3,AC-4andAC-6(randomlygeneratedproblemswithvariabletightness)
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
11 December 2007 Foundations of Logic and Constraint Programming 18
Maintaining Arc Consistency: AC-3d
Bidirectionality of support was also exploited in an adaptation, of both
algorithmsAC-6andAC-3,resultinginalgorithmsA-7andA-3d.
ThemaindifferencebetweenalgorithmsAC-3and AC-3dconsistsofthefact
thatwheneverarcaijisremovedfromqueueQ,thearc ajiisalsoremoved,in
caseitisthere.
In this case, both domains of Xi and Xj are revised, which avoids much
duplicatedwork.
Although itdoesnot improve theworst-casecomplexityofAC-3, the typical
complexity of AC-3d seems quite interesting (namely in some problems for
whichextensivetestswereperformed).
11 December 2007 Foundations of Logic and Constraint Programming 19
Maintaining Arc Consistency: AC-3d
ComparisonofTypicalComplexity(# of checks)
(previousramdomlygeneratedproblems)
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
11 December 2007 Foundations of Logic and Constraint Programming 20
Maintaining Arc Consistency: AC-3d
ComparisonofTypicalComplexity
(previousramdomlygeneratedproblems)
(equivalent CPU time,inms,inaPentiumat200MHz)
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
11 December 2007 Foundations of Logic and Constraint Programming 21
Maintaining Arc Consistency: AC-3d
ComparisonofTypicalComplexity
(previousramdomlygeneratedproblems)
(equivalent CPU time,inms,inaPentiumat200MHz)
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
11 December 2007 Foundations of Logic and Constraint Programming 22
Maintaining Arc Consistency: AC-3d
ResultsseemparticularlyinterestingforproblemsinwhichAC-7wasproved
muchsuperiortoAC-6,bothinnumberoftestsandinCPUtime.
ThisisthecaseofproblemRLFAP (Radio Link Frequency Assignment
Problem)-
Thisproblemconsistsofassigningradiofrequenciesinasafeway(noriskof
scrambling),forwhichinstanceswith3,5,8e11antenaewerestudied.
Thecode(aswellasthatofotherbenchmarkproblems)maybefoundina
benchmarkarchive,availablefromtheinternet,inURL
http://ftp.cs.unh.edu/pub/csp/archive/code/benchmarks
Togetherwithmanyotherproblems,itcomposesthebenchmarkweblibrary
http://www.csplib.org
11 December 2007 Foundations of Logic and Constraint Programming 23
Maintaining Arc Consistency: AC-3d
ComparisonofTypicalComplexity(#ofchecks)
(RFLAPproblems)
0
200000
400000
600000
800000
1000000
1200000
#3 #5 #8 #11
AC-3AC-6AC-7AC-3d
11 December 2007 Foundations of Logic and Constraint Programming 24
Maintaining Arc Consistency: AC-3d
ComparisonofTypicalComplexity
(RFLAPproblems)
(equivalent CPU time,inms,inaPentiumat200MHz)
0
500
1000
1500
2000
2500
# 3 # 5 # 8 # 11
AC-3AC-6AC-7AC-3d
11 December 2007 Foundations of Logic and Constraint Programming 25
Path Consistency
Inadditiontoarcconsistency,othertypesofconsistencymaybedefinedfor
binaryconstraintnetworks.
Path consistency is a “classical” type of consistency, stronger than arc
consistency, and requiring some “more global view” of the problem, not
centeredineachconstraintinisolation.
Thebasicideaofpathconsistencyisthat,inadditiontochecksupportinthe
arcs of the constraint network between variables Xi and Xj, further support
mustbecheckedinthevariablesXk1, Xk2
... Xkmthatformapathbetween Xi
andXj,i.e.wheneverthereareconstraintsCi,k1, Ck1,k2
, ..., Ckm-1, km and Ckm,j.
Infact,itispossibletoshowthisisequivalenttoseeksupportinanyvariable
Xk,connectedtobothXiandXj.
11 December 2007 Foundations of Logic and Constraint Programming 26
Path Consistency
Definition(Path Consistency):
Aconstraintsatisfactionproblemispath-consistentif,
o Itisarc-consistent;and
o Foreverypairofvariables XiandXj,andpathsXi-Xi1- ... - Xik
–Xj,
thedirectconstraintCi,jistighterthanthecompositionofthe
constraintsinthepathCi,i1, Ci1,i2
, ... , Cin,j.
Inpractice,everyvalueviinvariableXimusthavesupport,notonlyinsome
valuevj fromvariableXjbutalsoinvaluesvi1, vi2
,...,vinfromthedomainof
thevariablesinthepath.
11 December 2007 Foundations of Logic and Constraint Programming 27
Path Consistency
Maintainingthistypeofconsistencyhasnaturallyahighercomputationalcost
thanmaintainingasimplercriterionsuchasarcconsistency.
Inordertodoso,itisconvenienttomaintainarepresentationbyextensionof
thebinaryconstraints,intheformofabooleanmatrix.
Assumingthatvariables XiandXjhave,respectively,di anddj valuesintheir
domain,constraintCijismaintainedasabooleanmatrixMijofsizedi*dj.
Thevalue1/0ofelementMij[k,l]indicateswhetherthepair{Xi-vk, Xj-vl}
satisfies/ornotconstraintCij.
11 December 2007 Foundations of Logic and Constraint Programming 28
Path Consistency
Example: 4 queens
Boolean Matrix M12, regarding constraint C12 between variables X1 e X2 (or anyvariablesinconsecutiverows)
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
BooleanMatrixM13,regardingconstraintC12betweenvariablesX1andX2(oranyvariablesrowsseparedbyonerow)
M12[3,4] = M12[4,3] = 0
M13[1,2] = M13[2,1] = 1
M13[2,4] = M13[4,2] = 0
11 December 2007 Foundations of Logic and Constraint Programming 29
Checking Path Consistency
Checking Path Consistency
ToeliminatefrommatrixMij(i.e.tozeroanelement)thosevaluesthatdonot
satisfypathconsistencythroughathirdvariable,Xk,onemayuseoperations
similartomatrixmultiplicationandsum.
MIJ <- MIJ MIK MKJ
where the operation operates like in a matrix “sum”, but with arithmetic
additionreplacedbyboolean conjunction,andtheoperationcorresponds
to matrix multiplication in which arithmetic multiplication and addition are
replacedbybooleanconjunctionanddisjunction.
One must still consider the diagonal matrix Mkk to represent the domain of
variable Xk.
11 December 2007 Foundations of Logic and Constraint Programming 30
Path Consistency: 4 Queens
Example (4 queens):
Checkingifcompoundlabel{X1-1,X3-4}ispathinconsistent,throughX2.
M13[1,4]<-M13[1,4]M12[1,X]M23[X,4]
Infact,M12[1,X]M23[X,4]=0since
M12[1,1]M23[1,4]%X2-1doesnotsupport{X1-1,X3-4}
M12[1,2]M23[2,4]%X2-2doesnotsupport{X1-1,X3-4}
M12[1,3]M23[3,4]%X2-3doesnotsupport{X1-1,X3-4}
M12[1,4]M23[4,4]%X2-4doesnotsupport{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
11 December 2007 Foundations of Logic and Constraint Programming 31
Path Consistency: 4 Queens
Path consistency is stronger than arc consistency, in the sense that its
maintenacewillallow,ingeneral,toeliminatemoreredundantvaluesfromthe
domainsofthevariablesthansimplerarcconsistencyisabletodo
Inparticular,forthe4queensproblem,themaintenanceofpathconsistency
performstheeliminationfromthedomainofthevariablesofalltheredundant
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-freeenumeration.
Notice, that the enumeration and its backtracking is the cause of the
exponentialcomplexityofsolvingnontrivialproblems.
11 December 2007 Foundations of Logic and Constraint Programming 32
Path Consistency: 4 Queens
Pathconsistencyinthe4queensproblem
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
11 December 2007 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
11 December 2007 Foundations of Logic and Constraint Programming 34
Maintaining Path Consistency
Of course, this increase in the pruning power of path consistency does not
comeforfree.Thecomputationalcomplexityofachievingandmaintainingitis
(much)greaterthanthecostsassociatedwitharcconsistency.
The algorithms to maintain path consistency, PC-x, have therefore higher
complexitythanthoseoftheAC-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
thatavoidrepetitionoftests,muchinthesamewaythatthehighermembers
of the AC-y family do. We will simply outline the optimisations and present
theircomplexity.
11 December 2007 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
11 December 2007 Foundations of Logic and Constraint Programming 36
Maintaining Path Consistency: PC-1
Algorithm PC-1:Time Complexity
Themainprocedureperformsacycle
repeat
...
until Rn = R0
When there are n2 constraints Cij (dense graph), since each of the
correspondingmatrixhasd2elements,intheworstcaseonlyoneelementis
zero-edineachcycle.Hence,thecyclecanbeexecutedupton2d2times.
Ineachcycle,wehaven3nestedcyclesoftheform
for k from 1 to n do
for i from 1 to n do
for j from 1 to n do
11 December 2007 Foundations of Logic and Constraint Programming 37
Maintaining Path Consistency: PC-1
Algorithm PC-1:Time Complexity (cont.)
Eachoperation
Mijk <- Mijk-1 Mikk-1 Mkkk-1 Mkjk-1
requiresO(d3)binaryoperations,sinceeachof thed2elements iscomputed
through d ’s (boolean conjunction) and d-1 operations ’s (boolean
conjunction).
Combiningallthesefactors,thetimecomplexityforPC-1becomes
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
whicha n2).
11 December 2007 Foundations of Logic and Constraint Programming 38
Maintaining Path Consistency: PC-1
Algorithm PC-1:Space Complexity
ThespacecomplexityofPC-1derivesfrommaintenanceof
n3matricesMijk, forallsetsof constraintsCij,andthepaths througha
differentvariableXk.
Thesize,d2elements,ofeachsuchmatrix.
Hence,thespacecomplexityofalgorithmPC-1is
O(n3d2)
Thiscomplexity,againmuchhigherthanintheACcase(AC-4hascomplexity
O(ad2) O(n2d2)),isduetotheexplicitrepresentationoftheconstraints,and
thepaths,nomoredatastructuresbeingmaintained.
11 December 2007 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
becauseofthezero-ingofvaluesinthematricesMij,(thesameprincipleof
AC-3)suchthatonlyrelevantconsistencytestaresubsequentlyperformed.
IncontrastwithcomplexityO(n5d5)ofPC-1,thetimecomplexityofPC-2is
O(n3d5)
The space complexity is also better than with PC-1, O(n3d2). For PC-2 the
spacecomplexityis
O(n3+n2d2)
11 December 2007 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
elementimpliesthereevaluationofthepaths.
In contrast with the time complexity of PC-1, O(n5d5), and that of PC-3,
O(n3d5),thetimecomplexityofPC-4is
O(n3d3)
As expected, the space complexity of PC-4 is worse than that of PC-2,
O(n3+n2d2),beingsimilartothatofPC-1,i.e.
O(n3d2)
11 December 2007 Foundations of Logic and Constraint Programming 41
K-Consistency
Node,arcandpathconsistency,areallinstancesofthemoregeneralcaseof
k-consistency.
Informally, a constraint network is k-consistent when for a group of k
variables,Xi1,... , Xik
thevaluesineachdomainhavesupport inthoseofthe
othervariables,consideringthissupportinaglobalform.
The following examples shows a classical example of the advantages of
keepingaglobalviewonconstraints
Anodeconsistentnetwork,thatisnotarcconsistent
0 0
11 December 2007 Foundations of Logic and Constraint Programming 42
K-Consistency
Anarcconsistentnetwork,thatisnotpathconsistent
0,1 0,1
0,1
Apath-consistentnetwork,thatisnot4-consistent
0..2 0..2
0..2
0..2
11 December 2007 Foundations of Logic and Constraint Programming 43
(Strong) k-Consistency
Definition (k-Consistency):
Aconstraintsatisfactionproblem(orconstraintnetwork)is1-consistent
if the values in the domain of its variables satisfy all the unary
constraints.
Anetworkisk-consistentiffallits(k-1)-compound labels(i.e.formed
byk-1 pairsX-v) that satisfy the relevant constraints canbeextended
withsomelabelonanyothervariable,toformsomek-compoundlabels
thatmakethenetworkk-1consistent(satisfytherelevantconstraints).
Definition (Strong k-Consistency):
Aconstraintproblemisstronglyk-consistent,ifandonlyifitisi-
consistent,foralli 1 .. k.
11 December 2007 Foundations of Logic and Constraint Programming 44
(Strong) k-Consistency
• Aconstraintnetworkmaybek-consistencybutnotm-consistent(form<k).
Forexample,thenetworkbelowis3-consistent,butnot2-consistent.Hence
itisnotstrongly3-consistent.
Theonly2-compoundlabels,thatsatisfytheconstraints
{A-0,B-1},{A-0,C-0},and{B-1,C-0}
maybeextendedtotheremainingvariable
{A-0,B-1,C-0}.
However,the1-compoundlabel{B-0}cannotbeextended
tovariablesAorC{A-0,B-0}!
0 0
0,1
B
CA
As can be seen from the definitions, node, arc and path consistency are all
instancesofstrong k-consistency).Infact,aconstraintnetworkis
node consistentiffitisstrongly 1-consistent.
arc consistentiffitisstrongly2-consistent.
path consistent iffitisstrongly3-consistent.
11 December 2007 Foundations of Logic and Constraint Programming 45
(i,j) -Consistency
The k-consistencycriterionmaystillbeextendedtoamoregeneralcriterion
of(i,j)-consistency.
Aproblemis(i,j)-consistentiffallitsi-compoundlabelsthatsatisfythe
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
relevantconstraints.
Ofcourse,k-consistencyisequivalentto(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
algorithmKS-1[Tsan93]).
11 December 2007 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,
sincetheyonlyregardunaryconstraints.
The generalisation of arc consistency for general n-ary constraint networks
(foranyn > 2)isquitesimple.
Theconceptofanarcmustbereplacedbythatofanhyper-arc,anditmust
beguaranteedthateachvalueinthedomainofavariablemustbesupported
byvaluesintheothervariablesthatsharethesamehyper-arc(constraint).
Although less intuitive (what is a path in a hyper-graph?), path consistency
maystillbedefinedbymeansofstrong3-consistencyforhyper-graphs.
11 December 2007 Foundations of Logic and Constraint Programming 47
Maintaining Generalised Arc-Consistency: GAC-3
Inpractice,onlygeneralisedarc-consistency ismaintained,byadaptationof
theAC-xalgorithms,asisthecaseofthealgorithmGAC-3below.
To each k-ary constraint one has to consider k directed hyper-arcs to
representthesupport.
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
11 December 2007 Foundations of Logic and Constraint Programming 48
Maintaining Generalised Arc-Consistency: GAC-3
Algorithm GAC-3: Time Complexity
BycomparisonwithAC-3,predicate revise_dom,checksatmostdk(d2)k-
tuplesofvaluesink-aryconstraints.
Whenevera valuevi is removed from thedomainsofXi,k-1 hyper-arcs
areinsertedinqueueQ foreachk-ary constraintinvolvingthatvariable.
Asawhole,eachofthekahyper-arcsmaybeinserted(andsubsequently
removed) (k-1)d times from queue Q. All factors considered, the worst
casetimecomplexityofalgorithmGAC-3isO(k(k-1)2ad * dk),i.e.
O(k2adk+1)
Ofcourse,AC-3,withO(ad3),isthespecialcaseofGAC-3 (fork=2).
11 December 2007 Foundations of Logic and Constraint Programming 49
Constraint Dependent Consistency
Thealgorithmsandconsistencycriteriaconsideredsofartakeintonoaccount
thesemanticsofspecificconstraints,handlingthemallinauniformway.
In practice, such approach is not very useful, since important gains may be
achievedbyusingspecialisedcriteriaandalgorithms.
For example, regardless of more general schemes, it is not useful, for
inequalityconstraints(),when considered separately,togobeyondsimple
nodeconsistency.
In fact, for Xi Xj,onemayonlyeliminateavalue fromavariabledomain,
whenthedomainoftheotherisreducedtoasingleton.
11 December 2007 Foundations of Logic and Constraint Programming 50
Constraint Dependent Consistency
Other more specific types of consistency may be used and enforced. For
exampleinthenqueensproblem,theconstraint
no_attack(Xi, Xj)
shouldonlybeconsideredwhenthesizeofthedomainofoneofthevariables
becomesreducedto3orlessthan3values.
Infact,if Ximaintains3valuesinthedomain,allvaluesinthedomainofXjwill
have,atleast,onesupportingvalueinthedomainofXi.
Thiisbecausetheconstraintissimmetricalandanyqueenmayonlyattack3
queensinadifferentrow.
11 December 2007 Foundations of Logic and Constraint Programming 51
Bounds Consistency
Anotherspecifictypeofconsistencythatisveryusefulinnumericalconstraints
is “bounds-consistency”, often used with constraints such as =<, <, = in
ordereddomains.
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}
Whereasarcconsistencywouldreducethedomainsofthevariablesto{4, 8}
with some computational cost, enforcing simpler bounds consistency would
resultinthedomains
A:: {4, 5, 8} and B :: {4, 6, 8}
Althoughthesedomainskeepredundantvalues,boundsconsistencyismuch
easiertomaintain.
11 December 2007 Foundations of Logic and Constraint Programming 52
Bounds Consistency
Bounds consistency is specially interesting when dealing with very large
domains,andwithn-aryconstraints,asiscommoninnumericalproblems.
ConsiderforexampletheternaryconstraintA + B =< C,wherevariableshave
domains1..1000.Arcconsistencywould requirechecking10003 = 109 triples
<vA, vB, vC>!!!
Boundsconsistencyismucheasiertoachieve,bysimplyenforcing
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)
11 December 2007 Foundations of Logic and Constraint Programming 53
Constraint Trees
• Beforeaddressing theneed for heuristic search (even in reducednetworks),
namely the importance of the order in which variables are selected for
enumeration,letusconsiderthespecialcaseofconstraintgraphsthattakethe
formofatree.
If variableA isenumerated 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
11 December 2007 Foundations of Logic and Constraint Programming 54
Constraint Trees
• Toguaranteethattheenumerationdoesnotleadimmediatelytounsatisfiability,
valueafromvariableAmusthavesupportinvariablesB,CandD.
• Thisreasoningmayproceedrecursively,foreachofthesubtreeswithrootsB,
C and D. For example, enumeration of B (say, B = b) does not lead to an
immediate“deadend”ifvaluebhassupportinEandF.
• In general, if enumeration of the variables in the tree is made from root to
leaves,suchenumerationwillnotleadtounsatisfiabilityifthevalueskeptinany
nodehavesupportintheirchildrennodes.
• Hence, a good heuristic to select the variable to enumerate is choosing
variablesintherootofthetree(orsubtree).
E
CB
GF
D
H I
11 December 2007 Foundations of Logic and Constraint Programming 55
Constraint Trees
Although this is not the case in general constraint graphs, as seen before,
constrainttreesthatarearcconsistentarealsosatisfiable.
Infact,usinganyheuristicsthatselectstherootsofthetreeanditssubtrees,
Anarbitraryvalueinthedomainoftherootvariablemaybechosen
Thischoicenever leadstocontradiction,sinceallvalues inthedomainof
therootvariableshavesupportintheirchildrenvariables,
Suchsupportisrecursivelyguaranteeduntiltheleaves.
Moreover, this direccionality in the enumeration (from root to leaves) enables
theconsiderationofdirected arc consistency,aweakercriterionthanfullarc
consistency,butsufficienttohandleconstrainttrees.
11 December 2007 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
andDhavesupportinthedomainofA.
Theonlyrequirementisthatall values
in the domain of A have support in
thedomainsofB,CandD.
When a value is chosen for A,
constraints on A and its descendents
becomeunary,andthevaluesfromthe
domainsofB,CandDareremovedby
simplenodeconsistency.
A
E
CB
GF
D
H I
E
CB
GF
D
H I
11 December 2007 Foundations of Logic and Constraint Programming 57
Directed Arc Consistency
Ingeneral,onemaydefineacriterionofdirectedarcconsistencyif,incontrastto
what has been considered, we consider a directed graph to represent the
constraintnetwork(assumingsomedirectiontoeachconstraintoftheproblem).
Definition (Directed Arc Consistency):
Aconstraintproblemisdirected arcconsistentiff
Itisnodeconsistent;and
ForeverylabelXi-viofanyvariableXi,andforanydirected arcaij(fromXi
toXj)correspondingtoaconstraintCij,thereisasupportingvaluevj inthe
domainofXj,i.e.thecompoundlabel{Xi-vi, Xj-vj}satisfiesconstraint Rij.
11 December 2007 Foundations of Logic and Constraint Programming 58
Maintaining Directed Arc Consistency: DAC
Maintaining Directed Arc Consistency
Thealgorithmbelowassumesthatvariablesare“sorted”from1ton,andthere
are only arcs from the “lowest” to the “highest” variable. It simply enforces
consistency in all directed arcs, starting with the highest variables, without
consideringanyfurtherreexaminationofthearcs.
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)isthatusedinAC-1andAC-3.
11 December 2007 Foundations of Logic and Constraint Programming 59
Maintaining Directed Arc Consistency: DAC
Algorithm DAC: Time Complexity
Asusual,letusassumeaarcsandthatthenvariableshavedvaluesintheir
domain.
The algorithm visits each arc exactly once. For each arc aij, each of the d
valuesofvariable XiistestedwithdvaluesfromvariableXj,intheworstcase.
Allfactorsconsidered,andlikeinAC-4/6/7,theworstcasetimecomplexityof
algorithmDACisO(ad2).
However,inatreea = n-1(nota n2)sothecomplexityofDACis
O(nd2)
Moreover, the algorithm has no requirements regarding memory use (space
complexity).
11 December 2007 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 thegeneralcase thismightbe innadequate, this isnotaproblem in the
case of trees, if the arcs, which are directed in descending order of the
variables (the tree roothas lowestnumber)are revised indescendingorder,
i.e.arcsclosertotheleavesarerevisedfirst.
Thenallthevaluesofanodehaveguaranteedsupportinallthenodesdown
totheleavesofatree,althoughnotnecessarilyonthoseuptothetreeroot!
Asshownbefore, ifenumerationstarts fromtheroot, theunsupportedvalues
areeliminatedbynodeconsistency.
11 December 2007 Foundations of Logic and Constraint Programming 61
Maintaining Directed Arc Consistency: DAC
Example:Taketheconstrainttreebelow
1
5
32
76
4
8 9
> >
>> > >>
>X1in{1,3,5,7,9},
X2in{2,4,6},X5in{4,8},X6in{3,9},
X3in{1,5,7},X7in{3,9},
X4in{1,5,8},X8in{3,7},X9in{2,9}
Afterrevisingarc X9->X4 X4in{1,5,8} %X9>=2
X8->X4 X4in{1,5,8} %X8>=3
X7->X3 X3in{1,5,7} %X7>=3
X6->X2 X2in{2,4,6} %X6>=3
X5->X2 X2in{2,4,6} %X5>=4
X4->X1 X1in{1,3,5,7,9} %X4>=5
X3->X1 X1in{1,3,5,7,9} %X3>=5
X2->X1 X1in{1,3,5,7,9} %X2>=6
11 December 2007 Foundations of Logic and Constraint Programming 62
Maintaining Directed Arc Consistency: DAC
Example (cont):Theenumerationbecamebacktrack free!
X1in{7,9},
X2in{6},X5in{4,8},X6in{3,9},
X3in{5,7},X7in{3,9},
X4in{5,8},X8in{3,7},X9in{2,9}
X1=7NCenforcesX2in{6},X3in{5},X4in{5}
X2=6NCenforcesX5in{4},X6in{3},
X3=5NCenforcesX7in{3},
X4=4NCenforcesX8in{3},X9in{2},
X1=9NCenforcesX2in{6},X3in{5,7},X4in{4,8}
X2=6....,X3in{5,7},enforcesX7=3
X4=4NCenforcesX8in{3},X9in{2}
orX4=8NCenforcesX8in{3,7},X9in{2}
1
5
32
76
4
8 9
> >
>> > >>
>