View
235
Download
0
Embed Size (px)
Citation preview
21 November 2006 Foundations of Logic and Constraint Programming 1
Constraint (Logic) Programming
Anoverview
• BooleanConstraintPropagation
• FiniteDomainsandConstraintNetworks
• ConsistencyCriteria
• MaintaningNodeConsistency
• MaintainingArcConsistency
[Dech04]RinaDechter,ConstraintProcessing,
21 November 2006 Foundations of Logic and Constraint Programming 2
Boolean Satisfiability
InmostBooleanproblems, theuseofa completesolver isquite innefficient,
sinceitmaintainsthesetofallsolutionsinasolvedform.
Alternatively, a Boolean satisfiability problem may be solved, usually much
moreefficiently,bysearchingforasolution.
In this case, two alternatives are available, with respect to the role of
constraints:
Constraints are used passively. Once the variable appearing in the
constraintareinstantiated,theconstraintischecked.Ifitisnotsatisfied,
thesearchbacktracks.
Constraintsareusedactively,toreducethedomainofthevariablesstill
toinstantiate.
Thesealternativesareillustratedbymodellingallconstraintsasclauses.
21 November 2006 Foundations of Logic and Constraint Programming 3
Boolean Clauses
Syntatically,
• A literal:variableoritsnegation
Variablesaredenotedbyuppercaseidentifiers,andnegationwith.
• Aclause:setofliterals.
Clausesarerepresentedwiththedisjunctionoperator,e.g.A B C
• ABoolean Satisfiabilty Problem:setofclauses.
Semantically,
• BooleanvariablesmaytakevaluesTrue / False(orT / For1 / 0).
• Anegation““reverses,ofcourse,thisvalue.
• A clause is interpreted as adisjunction of literals. Hence, a clause issatisfiedifat least oneofitsliteralstakesvalueTrue.
• Solvingaproblemcorresponds to findvalues to thevariablessuch thatallclausesaresatisfied.
21 November 2006 Foundations of Logic and Constraint Programming 4
Search Tree
• Everyproblemmayberepresentedasa(search) tree,whereeachnodehas
twochildren,correspondingtoavariableanditsnegationnotappearinginthe
pathfromtheroottothenode.
• Asolutionoftheproblem,acompleteasignmentofT/Fvaluestoallvariables,
correspondstoapathfromtheroottoaleaf,thatsatisfiesallconstraints.
• Thesearchprocessmaybeimplementedbytraversingthesearchtree.
• Acompletesearchwillvisit(ifnecessary)allleafs.
CC CC
B B
BB BB
C C
A A
21 November 2006 Foundations of Logic and Constraint Programming 5
Search Tree
• Agenerate and testapproachtoconstraintsolvingwillvisitallleafs(generate
potential solutions) and test the satisfiability of all clauses for the
correspondingassignmentsofvaluestovariables.
• Searchmay be improved bybacktracking - at each node (not only in the
leafs)theclauseswithvariablesdefinedaretestedforsatisfiability.
• In this form, by testing incomplete solutions, backtracking avoids the
“completion”ofmanyimpossiblepartialsolutions.
c1: A B
c2: A C
c3: B C
CC CC
B B
BB BB
C C
A A
c3 c2
c1
c3
21 November 2006 Foundations of Logic and Constraint Programming 6
Search Tree
• Still,backtrackinghandlesconstraintspassively.First,valuesareassignedto
variables;then,constraints(clauses)aretested.
• Alternatively,constraintsarehandledactivelywith“constraintpropagation”.
• IntheBooleanConstraintPropagation(BCP):
whenall literalsbut oneofaclauseareassignedFalse,thentheclause
becomesactiveandpropagatestheremaining literalisassignedTrue.
c1: A B
c2: A C
c3: B C
CC CC
B B
BB BB
C C
A AA = 1 B 1 (c1) C 1(c2) ? (c3)
C = 1 B 1 (c3)
21 November 2006 Foundations of Logic and Constraint Programming 7
• This propagation can be noticed in the 4-queens problem. Here are some
examples:
1. Propagate a 0-assignment
2. Propagate a 1
Boolean Constraint Propagation
1 1 0
X11 X12 X12 0
X21 X22 X23 X24 X24 1
1 0 000 0 0
000
1 0 000 0 0
000
1
21 November 2006 Foundations of Logic and Constraint Programming 8
• Hereisthecompletesearchprocesswithconstraintpropagation
Assign X11 Propagate X23 & Propagate
X24 & Propagate & Propagate
X12 & Propagate & Propagate & Propagate & Propagate
Boolean Constraint Propagation
1 1 0 0 0
00
0
0
0
0
1 0 0 0
00
0
0
0
0
100
0
0
1 0 0 0
0
0
0
0
0
0
1
0
0
0
1 0 0 0
0
0
0
0
0
0
1
0
0
0
1
0
1 000
0 0 0
00
0
1 000
0 0 0
00
0
1
0
0
1 000
0 0 0
00
0
1
0
01
0
1 000
0 0 0
00
0
1
0
01
0 1
Fail !X31 X32 X33 X34
Backtrack X23
Fail !X41 X42 X43 X44
Backtrack X24
21 November 2006 Foundations of Logic and Constraint Programming 9
Boolean Clauses in SICStus
• SICStusdoesnotprovideanyconstraintsolverforthesatisfiabilityofBoolean
clauses(SAT).
• Nevertheless,itcanbeeasilyimplementedasaninstanceofafinitedomains
solver,wherethedomainofthevariablesare{0,1}.
• A clause is thus converted into an inequality constraint over an arithmetic
expression.Anexampleillustratesthismodelling
• ClauseX Y Z
X + (1-Y) + Z 1 X – Y + Z 0
• Propagation 1:X = 0, Y = 1 0-1+Z 0 Z 1 Z 1
• Propagation 2:X = 0, Z = 0 0-Y+0 0 Y 0 Y 0
(seedetailsinqueens4_sat_fd.pl)
21 November 2006 Foundations of Logic and Constraint Programming 10
Why Finite Domains ?
Inmostproblems,andnotwithstandingthetheoreticalandpracticalinterestin
SAT,problemsaremorenaturallyspecifiedwithvariableswhosedomainisnot
restrictedto0/1values,butmaytakeafinitesetofvalues.
In particular, the N queens problem is more “easily” programmed with n
variables,oneforeachrow,whosevaluedenotesthecolumnthatthequeenof
that row must be in. The values allowed are, of course, the number of the
column,i.e.integersfrom1 to n.
Such specification, not only is more natural, but it also may lead to a
substantialincreaseintheefficiencyofthesolvingprocess.
FiniteDomain encodings of problemswheren variables can takek different
values implement a search spacewith a substantially different size from the
correspondingBooleancodingwhereeachvalue is representedbyadistinct
Booleanvariable.
21 November 2006 Foundations of Logic and Constraint Programming 11
Why Finite Domains ?
To compare the search spacesofBooleanandFiniteDomain encodings forproblemswherenvariablescantakekdifferentvalueswenotethat:
BooleanEncodings-k*nbooleanvariables(0/1).
SearchSpace=2kn.
FiniteDomains-nvariableseachwithkpossiblevalues
SearchSpace=kn
RatiooftheSearchSpaces
r=2kn/2log2k*n=2(k-log2(k))*n
Forexample,intheN-queensproblem,wehave
n=8k=8,andr=2(8-3)*8=2401012
n=16k=16,andr=2(16-4)*16=21921058
21 November 2006 Foundations of Logic and Constraint Programming 12
Finite Domains
Ofcourse, theratioof thesearchspacesthatareactuallysearchedisnotso
large,namelyduetoconstraintpropagation.Still,thedifferencesmaybequite
significant.
TheefficiencyoftheCLPprogramsoverfinitedomains(includingtheBoolean
case)maystillbeimprovedbymakinguseof
AdequatePropagation Algorithms
Appropriate Heuristics for selection of the Variable to label and the
Valuetoassign
Beforeinvestigatingtheseissues,anumberofconceptsareintroduced
ConstraintsandConstraint Network
Partial andTotal Solutions
SatisfactionandConsistency
21 November 2006 Foundations of Logic and Constraint Programming 13
Basic Concepts: Domains
Definition(Domain of a Variable):
Thedomainofavariableisthe(finite)setofvaluesthatcanbeassignedtothatvariable.
Given some variableX, its domainwill be usually referred to asdom(X) or,simply,Dx.
Example:TheNqueensproblemmaybemodelledbymeansofNvariables,X1 to Xn,allwiththedomainfrom1ton.
Dom(Xi) = {1,2, ..., n} or Xi :: 1..n.
21 November 2006 Foundations of Logic and Constraint Programming 14
Basic Concepts: Labels
Toformalisethenotionofthestateofavariable(i.e.itsassignmentwithoneof
thevaluesinitsdomain)wehavethefollowing
Definition(Label):
AlabelisaVariable-Valuepair,wheretheValueisoneoftheelements
ofthedomainoftheVariable.
Thenotionofapartialsolution,inwhichsomeofthevariablesoftheproblem
havealreadyassignedvalues,iscapturedbythefollowing
Definition (Compound Label):
Acompound labelisasetoflabelswithdistinctvariables.
21 November 2006 Foundations of Logic and Constraint Programming 15
Basic Concepts: Constraints
Wecomenowtotheformaldefinitionofaconstraint
Definition(Constraint):
Given a set of variables, aconstraint is a set of compound labels on
thesevariables.
Alternatively,aconstraintmaybedefinedsimplyasarelation,i.e.asubsetof
the cartesian product of the domains of the variables involved in that
constraint.
Forexample,givenaconstraintRijkinvolvingvariablesXi, XjandXk,then
Rijk dom(Xi) x dom(Xj) x dom(Xk)
21 November 2006 Foundations of Logic and Constraint Programming 16
Basic Concepts: Constraints
GivenaconstraintC,thesetofvariablesinvolvedinthatconstraintisdenoted
byvars(C).
Simetrically, thesetofconstraints inwhichvariableXparticipates isdenoted
bycons(X).
Noticethataconstraintisarelation,notafunction,sothatitisalwaysCij = Cji.
Inpractice,constraintsmaybespecifiedby
• Extension: through an explicit enumeration of the allowed compound
labels;
• Intension: through some predicate (or procedure) that determines the
allowedcompoundlabels.
21 November 2006 Foundations of Logic and Constraint Programming 17
Basic Concepts: Constraint Definition
Forexample, theconstraintC13 involvingX1and X3 in the4-queensproblem,maybespecified
By extension(labelform):
C13 = {{X1-1,X3-2},{X1-1,X3-4},{X1-2,X3-1},{X1-2,X3-3},
{X1-3,X3-2},{X1-3,X3-4},{X1-4,X3-1},{X1-4,X3-3}}.
or,intuple(relational)form,omittingthevariables
C13 = {<1,2>,<1,4>,<2,1>,<2,3>,<3,2>,<3,4>,<4,1>,<4,3>}.
By intension:
C13 = (X1 X3) (1+X1 3+X3) (3+X1 1+X3).
21 November 2006 Foundations of Logic and Constraint Programming 18
Basic Concepts: Constraint Arity
Definition(Constraint Arity):
The constraint arity of some constraint C is the number of variables
overwhichtheconstraintisdefined,i.e.thecardinalityofthesetVars(C).
Despite the fact that constraints may have an arbitrary arity, an important
subsetoftheconstraintsisthesetofbinary constraints.
Theimportanceofsuchconstraintsistwofold
Allconstraintsmaybeconvertedintobinaryconstraints
A number of concepts and algorithms are appropriate for these
constraints.
21 November 2006 Foundations of Logic and Constraint Programming 19
Basic Concepts: Binary Constraints
Conversion to Binary Constraints
Ann-aryconstraint,C,definedbyk compoundlabelsonitsvariablesX1 to Xn,
isequivalenttonbinaryconstraints,C0i,throughtheadditionofanewvariable,
X0,whosedomainistheset1 to k.
Inpractice,
The kn-arylabelsmaybesortedinsomearbitraryorder.
Each of the n binary constraints C0i relates the new variable X0 with the
variableXi.
Thecompoundlabel{Xi-vi, X0-j}belongstoconstraintC0iiffXi-vibelongstothe
j-thcompoundlabelthatdefinesC.
21 November 2006 Foundations of Logic and Constraint Programming 20
Basic Concepts: Binary Constraints
Example:
GivenvariablesX1 to X3,withdomains1..3,thefollowingternaryconstraintCiscomposedof6labels..
C(X1, X2, X3) = {<1,2,3>,<1,3,2>,<2,1,3>,<2,3,1> ,<3,1,2>,<3,2,1>}
Eachofthelabelsmaybeassociatedtoavaluefrom1to6
1 : <1,2,3>, 2 : <1,3,2>, ... , 6: <3,2,1>
Now, the following binaryconstraints,C01 toC03, areequivalent to the initialternaryconstraintC
C01(X0, X1) = {<1,1>, <2,1>, <3,2>, <4,2>, <5,3>, <6,3>}
C02(X0, X2) = {<1,2>, <2,3>, <3,1>, <4,3>, <5,1>, <6,2>}
C03(X0, X3) = {<1,3>, <2,2>, <3,3>, <4,1>, <5,2>, <6,1>}
21 November 2006 Foundations of Logic and Constraint Programming 21
Basic Concepts: Constraint Satisfaction
Definition(Constraint Satisfaction 1):
A compound label satisfies a constraint if their variables are the same
andifthecompoundlabelisamemberoftheconstraint.
Inpractice, it is convenient togeneraliseconstraint satisfaction tocompound
labelsthatstrictlycontaintheconstraintvariables.
Definition(Constraint Satisfaction 2):
A compound label satisfies a constraint if its variables contain the
constraint variables and the projection of the compound label to these
variablesisamemberoftheconstraint.
21 November 2006 Foundations of Logic and Constraint Programming 22
Basic Concepts: Constraint Satisfaction Problem
Definition(Constraint Satisfaction Problem):
Aconstraint satisfaction problemisatriple<V, D, C>where
Visthesetofvariablesoftheproblem
Disthedomain(s)ofitsvariables
Cisthesetofconstraintsoftheproblem
Definition(Problem Solution):
AsolutiontoaConstraintSatisfactionProblemP:<V, D, C>, isacompound
labeloverthevariablesVoftheproblem,whichsatisfiesallconstraintsinC.
21 November 2006 Foundations of Logic and Constraint Programming 23
Basic Concepts: Constraint Graph
Forconvenience,theconstraintsofaproblemmaybeconsideredasforminga
specialgraph.
Definition(Constraint Graph or Constraint Network):
The Constraint Graph or Constraint Network of a binary constraint
satisfactionproblemisdefinedasfollows
Thereisanodeforeachofthevariablesoftheproblem.
For each non-trivial constraint of the problem, involving one or two
variables,thegraphcontainsanarclinkingthecorrespondingnodes.
When the problems include constraints with arbitrary arity, the Constraint
Network may be formed after converting these constraints on its binary
equivalent.
21 November 2006 Foundations of Logic and Constraint Programming 24
Basic Concepts: Constraint Hyper-Graph
Whenitisnotconvenienttoconvertn-aryconstraintstotheirbinaryequivalent,
theproblemmayberepresentedbyanhiper-graph.
Definition(Constraint Hyper-Graph):
Any constraint satisfaction problem with arbitrary n-ary constraints may be
representedbyaConstraints Hiper-Graphformedasfollows:
Thereisanodeforeachofthevariablesoftheproblem.
For each constraint of the problem, the graph contains an hiper-arc
linkingthecorrespondingnodes.
Of course, when the problem has only binary constraints, the hiper-graph
degeneratesintothesimplergraph.
21 November 2006 Foundations of Logic and Constraint Programming 25
Example: 4-Queens
Example:
The4queensproblemmaybespecifiedbythefollowingconstraint network:
X1in1..4
X4in1..4
X3in1..4X2in1..4
C12
R23
R14
C24
C34
C13
C13:<1,2>, <1,4>, <2,1>, <2,3>,<3,2>, <3,4>, <4,1>, <4,3>
21 November 2006 Foundations of Logic and Constraint Programming 26
Basic Definitions – Graph Density
Definition(Complete Constraint Network):
A constraint netwok is complete, when there is an arc connecting any two
nodesofthegraph(i.e.whenthereisaconstraintoveranypairofvariables).
The4queensproblem(infact,anynqueensproblem)hasacompletegraph.
However,thisisoftennotthecase:mostgraphsaresparse.Inthiscase,itis
importanttomeasurethedensityoftheconstraintnetwork.
Definition(Density of a Constraint Graph):
Thedensityofaconstraintgraph is the ratiobetween thenumberof itsarcs
andthenumberofarcsofacompletegraphonthesamenumberofnodes
21 November 2006 Foundations of Logic and Constraint Programming 27
Basic Definitions – Problem Difficulty
Inprinciple,the“difficulty”ofaconstraintproblemisrelatedtothedensityofits
graph.
Intuitively, the denser the graph is, themore difficult the problem becomes,
sincetherearemoreopportunitiestoinvalidateacompoundlabel.
It is important to distinguish between the difficulty of a problem and the
difficultyofsolvingtheproblem.
Inparticular,adifficultproblemmaybesodifficultthatitistrivialtofindit
tobeimpossible!
The“difficulty”ofaproblemisalsorelatedtothedifficultyofsatisfyingeachof
itsconstraints.Suchdifficultymaybemeasuredthroughits“tightness”.
21 November 2006 Foundations of Logic and Constraint Programming 28
Basic Definitions – Constraint Tightness
Definition(Tightness of a Constraint):
GivenaconstraintC onvariables X1 ... Xn,withdomainsD1 to Dn, the
tightnessofC isdefinedas theratiobetweenthenumberof labels that
define the constraint and the size (i.e. the cardinality) of the cartesian
productD1 x D2 x .. Dn.
For example, the tightness of constraint C13 of the 4 queens problem, on
variablesX1andX3withdomains1..4
C13 = {<1,2>, <1,4>, <2,1>, <2,3>, <3,2>, <3,4>, <4,1>, <4,3>}
is½,sincethereare8tuplesintherelationoutofthepossible16(4*4).
Thenotionoftightnessmaybegeneralisedforthewholeproblem.
21 November 2006 Foundations of Logic and Constraint Programming 29
Basic Definitions – Problem Tightness
Definition(Tightness of a Problem):
ThetightnessofaconstraintsatisfactionproblemwithvariablesX1 ... Xn,
istheratiobetweenthenumberofitssolutionsandthecardinalityofthe
cartesianproduct
D1 x D2 x .. Dn.
Forexample,the4queensproblem,thathasonlytwosolutions<2,4,1,3>and
<3,1,4,2>
hastightness2/(4*4*4*4) = 1/128.
21 November 2006 Foundations of Logic and Constraint Programming 30
Basic Definitions – Problem Solving Difficulty
Thedifficultyinsolvingaconstraintsatisfactionproblemisrelatedbothwiththe
Thedensityofitsgraphand
Itstightness.
Assuch,whentestingalgorithmstosolvethis typeofproblems, it isusual to
generaterandom problem instances,parameterisednotonlybythenumber
ofvariablesandthesizeoftheirdomains,butalsobythedensityofitsgraph
andthetightnessofitsconstraints.
Thestudyoftheseissueshasledtotheconclusionthatconstraintsatisfaction
problemsoftenexhibitaphase transition,whichshouldbetakenintoaccount
inthestudyofthealgorithms.
This phase transition typically contains the most difficult instances of the
problem,andseparatestheinstancesthataretriviallysatisfiedfromthosethat
aretriviallyinsatisfiable.
21 November 2006 Foundations of Logic and Constraint Programming 31
Basic Definitions – Problem Solving Difficulty
Forexample,inSAT,ithasbeenfoundthatthephasetransitionoccurswhen
therationofclausestovariablesisaround4.3.
0 5 10 15
# clauses / # variables
difficulty
4.3
21 November 2006 Foundations of Logic and Constraint Programming 32
Basic Definitions – Redundancy
Another issue to consider in the difficulty of solving a constraint satisfaction
problem is the potential existence of redundant values and labels in its
constraints.
Definition(Redundant Value):
A value inthedomainofavariableisredundant,ifitdoesnotappearin
anysolutionoftheproblem.
Definition(Redundant Label):
Acompound labelofaconstraintisredundantifitisnottheprojection
totheconstraintvariablesofasolutiontothewholeproblem.
Redundantvaluesand labels increase thesearchspaceuseless,andshould
thusbeavoided.There isnopoint in testingavalue thatdoesnotappear in
anysolution!
21 November 2006 Foundations of Logic and Constraint Programming 33
Basic Definitions – Redundancy
Example:
The4queensproblemonlyadmitstwosolutions:
<2,4,1,3> and <3,1,4,2>.
Hence,values1and4areredundantinthedomainofvariablesX1andX4,and
values2and3areredundantinthedomainofvariablesX2andX3.
Regardingredundantlabels, labels<2,3>(showninthefigure)and<3,2>are
redundantinconstraintC13.
21 November 2006 Foundations of Logic and Constraint Programming 34
Basic Definitions – Redundancy
Example:
The 4 queens problem, which only admits the two solutions <2,4,1,3> and
<3,1,4,2> may be “simplified” by elimination of the redundant values and
labels.
C13:<1,2>, <1,4>, <2,1>, <2,3>,<3,2>, <3,4>, <4,1>, <4,3>
X1in1,2,3,4
X4in1,2,3,4
X3in1,2,3,4X2in1,2,3,4
C12
R23
R14
C24
C34
C13
21 November 2006 Foundations of Logic and Constraint Programming 35
Basic Definitions – Equivalent Problems
Ofcourse,anyproblemshouldbeequivalenttoanyofitssimplifiedversions.
Formally,
Definition(Equivalent Problems):
TwoproblemsP1 = <V1, D1, C1>andP2 = <V2, D2, C2>areequivalent iff
theyhavethesamevariables(i.e.V1 = V2)andthesamesetofsolutions.
The“simplification”ofaproblemmayalsobeformalised
Definition(Reduced Problem):
AproblemP=<V,D,C>isreducedtoP’=<V’, D’, C’>if
PandP’areequivalent;
ThedomainsD’areincludedinD;and
TheconstraintsC’areatleastasrestrictiveasthosein C.
21 November 2006 Foundations of Logic and Constraint Programming 36
Constraint Solving Methods
As shown before, and independently of any problem reduction, a generate
and testproceduretosolveaConstraintSatisfactionProblemisusuallyvery
inneficient.
Nevertheless, this is the approach taken in local searchmethods (simulated
annealingorgeneticalgorithms)–althoughmostlyinanoptimisationcontext!
It is thus often preferable to use a solvingmethod that isconstructive and
incremental,whereby a compound label is being completed (constructive),
onevariableatatime(incremental),untilasolutionisreached.
However,onemustcheck thatateverystep in theconstructionofasolution
theresultinglabelhasstillthepotentialtoreachacompletesolution.
21 November 2006 Foundations of Logic and Constraint Programming 37
Constraint Solving Methods
Ideally,acompoundlabelshouldbetheprojectionofsomeproblemsolution.
Unfortunately,intheprocessofsolvingaproblem,itssolutionsarenotknown
yet!
Hence, one must use a notion weaker than that of a (complete) solution,
namely
Definition(k-Partial Solution):
Ak-partial solution of a constraint solvingproblemP = <V,D,C>, is a
compoundlabelonasubsetofk ofitsvariables,Vk,thatsatisfiesallthe
constraintsinCwhosevariablesareincludedinVk.
21 November 2006 Foundations of Logic and Constraint Programming 38
Constructive Solving Methods
This is of course, the basis of the solving methods that use some form of
backtracking. If conveniently performed, backtrackingmay be regarded as a
treesearch,wherethepartialsolutionscorrespondtotheinternalnodesofthe
treeandcompletesolutionstoitsleaves.
2
4
1
3
31 4
1
4
2
21 November 2006 Foundations of Logic and Constraint Programming 39
Problem Search Space
Clearly,themoreaproblemisreduced,theeasieritis,inprinciple,tosolveit.
Given a problem P=<V,D,C> with n variables X1,..,Xn the potential search
space where solutions can be found (i.e. the leaves of the search tree with
compoundlabels{<X1-v1>, ..., <Xn-vn>})hascardinality
#S = #D1 * #D2 * ... * #Dn
Assuming identicalcardinality (orsomekindofaverageof thedomainssize)
forallthevariabledomains,(#Di = d)thesearchspacehascardinality
#S = dn
whichisexponentialonthe“size”noftheproblem.
21 November 2006 Foundations of Logic and Constraint Programming 40
If instead of the cardinality d of the initial problem, one solves a reduced
problemwhosedomainshavelowercardinalityd’(<d)thesizeofthepotential
searchspacealsodecreasesexponentially!
S’/S=d’n/dn=(d’/d)n
Such exponential decrease may be very significant for “reasonably” large
valuesofn,asshowninthetable.
10 20 30 40 50 60 70 80 90 100
7 6 4.6716 21.824 101.95 476.29 2225 10395 48560 226852 1E+06 5E+06
6 5 6.1917 38.338 237.38 1469.8 9100.4 56348 348889 2E+06 1E+07 8E+07
5 4 9.3132 86.736 807.79 7523.2 70065 652530 6E+06 6E+07 5E+08 5E+09
4 3 17.758 315.34 5599.7 99437 2E+06 3E+07 6E+08 1E+10 2E+11 3E+12
3 2 57.665 3325.3 191751 1E+07 6E+08 4E+10 2E+12 1E+14 7E+15 4E+17
d d'
S/S'
n
Problem Search Space
21 November 2006 Foundations of Logic and Constraint Programming 41
Reduction of the Search Space
Inpractice,thispotentialnarrowingofthesearchspacehasacostinvolvedin
findingtheredundantvalues(andlabels).
Adetailedanalysisof thecostsandbenefits inthegeneralcaseisextremely
complex,sincetheprocessdependshighlyontheinstancesoftheproblemto
besolved.
However, it is reasonable to assume that the computational effort spent on
problemreductionisnotproportionaltothereductionachieved,becomingless
andlessefficient.
Aftersomepoint,thegainobtainedbythereductionofthesearchspacedoes
notcompensatetheextraeffortrequiredtoachievesuchreduction.
21 November 2006 Foundations of Logic and Constraint Programming 42
Reduction of the Search Space
• Qualitatively,thisprocessmayberepresentedbymeansofthefollowinggraph
Com
p uta
ti ona
l Co s
t
R - Reduction Cost
S- Search Cost
R+S Combined Cost
Effort spent in solving the problem
Amount of Reduction Achieved
21 November 2006 Foundations of Logic and Constraint Programming 43
Reduction of the Search Space
The effort in reducing the domains must be considered within the general
schemetosolvetheproblem.
InConstraint Logic Programming, the specification of the constraints usually
precedestheenumerationofthevariables.
Problem(Vars):-
Declaration of Variables and Domains,
Specification of Constraints,
Labelling of the Variables.
Ingeneral,searchisperformedexclusivelyonthelabellingofthevariables.
The execution model alternates enumeration with propagation, making it
possibletoreducetheproblematvariousstagesofthesolvingprocess.
21 November 2006 Foundations of Logic and Constraint Programming 44
Reduction of the Search Space
Hence,givenaproblemwithnvariablesX1toXntheexecuctionmodelfollows
thefollowingpattern:
Declaration of Variables and Domains,
Specification of Constraints,
indomain(X1), % value selection with backtraking
propagation, % reduction of problem X2 to Xn
indomain(X2),
propagation, % reduction of problem X3 to Xn
...
indomain(Xn-1)
propagation, % reduction of problem Xn
indomain(Xn)
21 November 2006 Foundations of Logic and Constraint Programming 45
Domain Reduction – Consistency Criteria
Once formallydefined thenotionofproblemreduction,onemustdiscuss the
actualprocedurethatmaybeusedtoachieveit.
First of all, onemust ensure thatwhatever procedure is used, the reduction
keepstheproblemequivalenttotheinitialone.
Herewehaveasmallproblem,sincethedefinitionofequivalencerequiresthe
solutionstobethesameandwedonotknowingeneralwhatthesolutionsare!
Nevertheless, thesolutionswillbethesameif in theprocessofreductionwe
have theguarantee that “nosolutionsare lost”.Suchguaranteesaremetby
severalcriteria.
21 November 2006 Foundations of Logic and Constraint Programming 46
Domain Reduction – Consistency Criteria
Consistency criteria enable to establish redundant values in the variables
domains in an indirect form, i.e. requiring no prior knowledge on the set of
problemsolutions.
Hence, procedures that maintain these criteria during the “propagation”
phases,willeliminateredundantvaluesandsodecreasethesearchspaceon
thevariablesyettobeenumerated.
For constraint satisfaction problems with binary constraints, the most usual
criteriaare,inincreasinglycomplexityorder,
Node Consistency
Arc Consistency
Path Consistency
21 November 2006 Foundations of Logic and Constraint Programming 47
Domain Reduction – Node Consistency
Definition(Node Consistency):
Aconstraint satisfactionproblem isnode-consistent ifnovalueon the
domainofitsvariablesviolatestheunaryconstraints.
Thiscriterionmayseembothobviousanduseless.Afterall,whowouldspecify
adomainthatviolatestheunaryconstraints?!
However, this criterionmust be regardedwithin the context of the execution
modelthatincrementallycompletespartialsolutions.Constraintsthatwerenot
unary in the initial problem become so when one (or more) variables are
enumerated.
21 November 2006 Foundations of Logic and Constraint Programming 48
Domain Reduction – Node Consistency
Example:
Aftertheinitialpostingofthe
constraints, the constraint
network model at the right
represents the 4-queens
problem.
Afterenumerationofvariable
X1, i.e.X1=1,constraintsC12,
C13andC14becomeunary!!
X1in1..4
X4in1..4
X3in1..4X2in1..4
C12
C23
R14
C24
C34
R13
X4in1..4
X3in1..4X2in1..4C23
C24
C34
X21,2
X31,3
X41,4
21 November 2006 Foundations of Logic and Constraint Programming 49
Domain Reduction – Node Consistency
Analgoriththatmaintainsnodeconsistencyshouldremovefromthedomains
ofthe“future”variablestheappropriatevalues.
Maintainingnodeconsistencyachievesadomainreductionsimilartowhatwas
achievedinpropagationwiththebooleanformulation.
X4 in 2,3
X3 in 2,4X2 in 3,4C23
C24
C34
X2 1,2 X3 1,3
X4 1,4
X4 1,4
1 1
1 1
1 1
X2 1,2X3 1,3
21 November 2006 Foundations of Logic and Constraint Programming 50
Maintaining Node Consistency
Giventhesimplicityofthenodeconsistencycriterion,analgorithmtomaintainitisverysimpleandwithlowcomplexity.
ApossiblealgorithmisNC-1,below.
procedure NC-1(V, D, R); for X in V for v in Dx do for Cx in {Cons(X): Vars(Cx) = {X}} do if not satisfy(X-v, Cx) then Dx <- Dx \ {v} end for end for end forend procedure
21 November 2006 Foundations of Logic and Constraint Programming 51
Maintaining Node Consistency
Space Complexity of NC-1: O(nd)
Assumingn variables in the problem, eachwithd values in its domain, and
assuming that thevariable’sdomainsare representedbyextension,aspace
ndisrequiredtokeepexplicitelythedomainsofthevariables.
Forexample,theinitialdomain1..4ofvariablesXiinthe4queensproblemis
representedbyabooleanvector(where1meansthevalueisinthedomain)
Xi = [1,1,1,1] or 1111.
After enumeration X1=1, node consistency prunes the domain of other
variablestoX1 = 1000,X2 = 0011,X3 = 0101andX4 = 0110
AlgorithmNC-1doesnotrequireadditionalspace,soitsspacecomplexityis
O(nd).
21 November 2006 Foundations of Logic and Constraint Programming 52
Maintaining Node Consistency
Time Complexity of NC-1: O(nd)
Assumingnvariables in theproblem,eachwithdvalues in itsdomain,
andtakingintoaccountthateachvalueisevaluatedonesingletime,itis
easytoconcludethatalgorithmNC-1hastimecomplexityO(nd).
The low complexity, both temporal and spatial, of algorithm NC-1, makes it
suitabletobeusedinvirtualallsituationsbyasolver.
However,nodeconsistencyisratherincomplete,notbeingabletodetectmany
possiblereductions.
21 November 2006 Foundations of Logic and Constraint Programming 53
Domain Reduction – Arc Consistency
A more demanding and complex criterion of consistency is that of arc-
consistency
Definition(Arc Consistency):
Aconstraintsatisfactionproblemisarc-consistentif,
• Itisnode-consistent;and
• ForeverylabelXi-viofeveryvariableXi,andforallconstraintsCij,defined
overvariablesXiandXj, theremustexistavaluevj thatsupportsvi, i.e.
suchthatthecompoundlabel{Xi-vi,Xj-vj}satisfiesconstraintCij.
21 November 2006 Foundations of Logic and Constraint Programming 54
Domain Reduction – Arc Consistency
Example:
AfterenumerationofvariableX1=1,andmaking thenetworknode-consistent,
the4queensproblemhasthefollowingconstraintnetwork:
However, label X2-3 has no support in variable X3, since neither compound
label{X2-3,X3-2}nor{X2-3,X3-4}satisfyconstraintC23.
Therefore,value3canbesafelyremovedfromthedomainofX2.
X4 in 2,3
X3 in 2,4X2 in 3,4C23
C24
C34
X2 1,2 X3 1,3
X4 1,4X4 1,4
1 1
1 1
1 1
X2 1,2X3 1,3
21 November 2006 Foundations of Logic and Constraint Programming 55
Domain Reduction – Arc Consistency
Example(cont.):
In fact, none (!) of the values ofX3 has support in variables X2 andX4., as
shownbelow:
label X3-4 has no support in variable X2, since none of the compound
labels{X2-3,X3-4}and{X2-4,X3-4}satisfyconstraintC23.
label X3-2 has no support in variable X4, since none of the compound
labels{X3-2,X4-2}and{X3-2,X4-3}satisfyconstraintC34.
X4 1,4
1 1
1 1
1 1
X2 1,2X3 1,3
21 November 2006 Foundations of Logic and Constraint Programming 56
Domain Reduction – Arc Consistency
Example(cont.):
Sincenoneof thevalues from thedomainofX3hassupport invariables X2
andX4,maintenanceofarc-consistencyemptiesthedomainofX3!
Hence, maintenance of arc-consistency not only prunes the domain of the
variablesbutalsoantecipatesthedetectionofunsatisfiabilityinvariableX3!In
thiscase,backtrackingofX1=1maybestartedevenbeforetheenumerationof
variableX2.
Given the good trade-of between pruning power and simplicity of arc-
consistency,anumberofalgorithmshavebeenproposedtomaintainit.
X4 1,4
1 1
1 1
1 1
X2 1,2X3 1,3
21 November 2006 Foundations of Logic and Constraint Programming 57
Maintaining Arc Consistency: AC-1
AC-1,shownbelow,isaverysimplealgorithmtomaintainArcConsistency´
procedure AC-1(V, D, C);
NC-1(V,D,C); % node consistency
Q = {aij | Cij C Cji C }; % see note repeat
changed <- false;
for aij in Q do
changed <- changed or revise_dom(aij,V,D,C)
end for
until not change
end procedure
Note:forconstraintCij twodirectedarcs,aijandaji,areconsidered
21 November 2006 Foundations of Logic and Constraint Programming 58
Maintaining Arc Consistency: AC-1
Predicate rev_dom(aij,V,D,R) succeeds iff it prunes values from thedomainofXi
predicate revise_dom(aij,V,D,C): Boolean;
success <- false;
for vi in dom(Xi) do
if there is no vj in dom(Xj) such that
satisfies({Xi-vi,Xj-vj},Cij) then
dom(Xi) <- dom(Xi) \ {vi};
success <- true;
end if
end for
revise_dom <- success;
end predicate
21 November 2006 Foundations of Logic and Constraint Programming 59
Time Complexity of AC-1
Time Complexity of AC-1 : O(nad3)
Assuming nvariablesintheproblem,eachwithdvaluesinitsdomain,anda
total of a arcs, in the worst case, predicate revise_dom, checks d2 pairs of
values.
The number of arcs aij in queue Q is 2a (2 directed arcs aij and aji are
consideredforeachconstraintCij).Foreachvalueremovedfromonedomain,
revise_domiscalled2atimes.
Intheworstcase,onlyonevaluefromonevariableisremovedineachcycle,
andthecycleisexecutedndtimes.
Therefore,theworst-casetimecomplexityofAC-1isO(d22a*nd),i.e.
O(nad3)
21 November 2006 Foundations of Logic and Constraint Programming 60
Space Complexity of AC-1
Space Complexity of AC-1 : O(ad2) = O(n2d2)
AC-1mustmaintain a queueQ,withmaximum size2a. Hence the inherent
spacialcomplexityofAC-1isO(a).
To this space, one has to add the space required to represent the domains
O(nd)andtheconstraintsoftheproblem.Assumingaconstraintsanddvalues
in each variable domain the space required is O(ad2), and a total space
requirementof
O(nd + ad2)
whichdominatesO(a).
For“dense”constraintnetworks”,a n2/2.Thisisthenthedominantterm,and
thespacecomplexitybecomes
O(ad2) = O(n2d2)
21 November 2006 Foundations of Logic and Constraint Programming 61
Maintaining Arc Consistency: From AC-1 to AC-3
InefficiencyofAC-1
Every time a value vi is removed from the domain of variable Xi by
predicaterevise_dom(aij,V,D,R), allarcsarereexamined.
However,onlythearcsaki(fork iandk j)shouldbereexamined.
This isbecause theremovalofvimayeliminate thesupport fromsome
valuevkofsomevariableXkforwhichthereisaconstraintCki(orCik).
SuchinefficiencyiseliminatedinalgorithmAC-3.
21 November 2006 Foundations of Logic and Constraint Programming 62
Maintaining Arc Consistency: AC-3
AC-3onlyrevisitsarcsforwhichthedomainofthe“leading”variablehasbeen
“revised”(narrowed).
procedure AC-3(V, D, C);
NC-1(V,D,R); % node consistency
Q = {aij | Rij C Cji C }; while Q do Q = Q \ {aij} % removes an element from Q
if revise_dom(aij,V,D,C) then % revised Xi
Q = Q {aki | Cki C k i k j} end if
end while
end procedure
Intuitively,AC-3musthavenotonlybetterworst-case,butalsoabettertypical-
casecomplexitythanAC-1.
21 November 2006 Foundations of Logic and Constraint Programming 63
Time and Space Complexity of AC-3
Time Complexity of AC-3: O(ad3)
Each arc aki is only added to Q when some value vi is removed from the
domainofXi.
Intotal,eachofthe2aarcsmaybeaddedtoQ(andremovedfromQ)d times.
Everytimethatanarcisremoved,predicaterevise_domiscalled,tocheckat
mostd2pairsofvalues.
All thingsconsidered,andincontrastwithAC-1,withcomplexityO(nad3), the
timecomplexityofAC-3,intheworstcase,isO(2ad * d2),i.e.O(ad3).
Space Complexity of AC-3: O(ad2)
As tospacecomplexityAC-3hasthesamerequirements thanAC-1,andthe
same worst-case space complexity of O(ad2) O(n2d2), due to the
representationofconstraintsbyextension.