View
229
Download
0
Tags:
Embed Size (px)
Citation preview
16 December 2005 Foundations of Logic and Constraint Programming 1
Constraint (Logic) Programming
Anoverview
• HeuristicsSearch
• NetworkCharacteristics
• StaticandDynamicHeuristics
• HeuristicsinSICStus
• AlternativestoPureBacktracking
[Dech04]RinaDechter,ConstraintProcessing,
16 December 2005 Foundations of Logic and Constraint Programming 2
Heuristic Search
Algorithmsthatmaintainsomeformofconsistency,remove(many?)redundant
valuesbut,notbeingcomplete,donoteliminatetheneedforsearch.
Evenwhenaconstraintnetworkisconsistent,enumerationissubjecttofailure.
Infact,aconsistentconstraintnetworkmaynotevenbesatisfiable(neithera
satisfiableconstraintnetworkisnecessarilyconsistent).
All that is guaranteed by maintaining some type of consistency is that the
networksareequivalent.
Solutions are not “lost” in the reduced network, that despite having less
redundantvalues,hasallthesolutionsoftheformer.
16 December 2005 Foundations of Logic and Constraint Programming 3
Heuristic Search
Hence,thedomainpruningdoesnoteliminateingeneraltheneedforsearch.
The search space is usually organised as a tree, and the search becomes
someformoftreesearch.
As usual, the various branches down from one node of the search tree
correspond to the assignment of the different values in the domain of a
variable.
Assuch,a tree leafcorresponds toacompletecompound label (includingall
theproblemvariables).
A depth first search in the tree, resorting to backtracking when a node
corresponds to a dead end (unsatisfiability), corresponds to an incremental
completionofpartialsolutionsuntilacompleteoneisfound.
16 December 2005 Foundations of Logic and Constraint Programming 4
Heuristic Search
Given theexecutionmodelofconstraint logicprogramming (oranyalgorithm
thatinterleavessearchwithconstraintpropagation)
Problem(Vars):-
Declaration of Variables and Domains,
Specification of Constraints,
Labelling of the Variables.
the enumeration of the variables determines the shape of the search tree,
sincethenodesthatarereacheddependontheorder inwhichvariablesare
enumerated.
Takeforexampletwodistinctenumerationsofvariableswhosedomainshave
differentcardinality,e.g.X in 1..2, Y in 1..3andZ in 1..4.
16 December 2005 Foundations of Logic and Constraint Programming 5
Heuristic Search
enum([X,Y,Z]):-
indomain(X)
propagation
indomain(Y),
propagation,
indomain(Z).
#ofnodes=32
(2+6+24)
1 2 3 4 1 2 3 4 1 2 3 4
1 2 3
1
1 2 3 4 1 2 3 4 1 2 3 4
1 2 3
2
16 December 2005 Foundations of Logic and Constraint Programming 6
Heuristic Search
enum([X,Y,Z]):- indomain(Z), propagation indomain(Y), propagation, indomain(X).
#ofnodes=40(4+12+24)
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
2 3 1 2 312 3 1 2 31
1 2 3 4
16 December 2005 Foundations of Logic and Constraint Programming 7
Heuristic Search
Theorder inwhichvariablesareenumeratedmayhavean important impact
ontheefficiencyofthetreesearch,since
The number of internal nodes is different, despite the same number of
leaves,orpotentialsolutions, #Di.
Failures can be detected differently, favouring some orderings of the
enumeration.
Depending on the propagation used, different orderings may lead to
differentpruningsofthetree.
Theordering of the domains has no direct influence on the search space,
althoughitmayhavegreatimportanceinfindingthefirstsolution.
16 December 2005 Foundations of Logic and Constraint Programming 8
Variable Selection Heuristics
To control the efficiency of tree search one should in principle adopt
appropriateheuristicstoselect
Thenextvariabletolabel
Thevaluetoassigntotheselectedvariable
Sinceheuristics forvalue choicewillnotaffect thesizeof thesearchtreeto
be explored, particular attention will be paid to the heuristics for variable
selection.Here,twotypesofheuristicscanbeconsidered:
Static - the ordering of the variables is set up before starting the
enumeration,nottakingintoaccountthepossibleeffectsofpropagation.
Dynamic-theselectionofthevariableisdeterminedafteranalysisofthe
problemthatresultedfrompreviousenumerations(andpropagation).
16 December 2005 Foundations of Logic and Constraint Programming 9
Width of a Node
Static heuristics are based on some properties of the underlying constraint graphs,
namelytheirwidthandbandwidth.
Firstthewidthofanodeisdefined-
Definition: Width of a node N, given ordering O):
Given some total ordering, O, of the nodes of a graph, the width of a node N,
induced by ordering OisthenumberoflowerordernodesthatareadjacenttoN.
.
As an example, given any
ordering that is increasing from
the root to the leaves of a tree,
allnodesofthetree(exceptthe
root)havewidth1.
1
5
32
76
4
8 9
16 December 2005 Foundations of Logic and Constraint Programming 10
Width of a Graph
Definition(Width of a graph G, induced by O):
Givensomeordering,O,ofthenodesofagraph,G,thewidthofGinducedby
orderingO,isthemaximumwidthofitsnodes,giventhatordering.
Definition(Width of a graph G):
ThewidthofagraphGisthelowestwidthofthegraphinducedbyanyof its
orderingsO.
1
5
32
76
4
8 9
It is apparent from these
definitions, that a tree is a
specialgraphwhosewidthis1.
16 December 2005 Foundations of Logic and Constraint Programming 11
Graph Width
Example:
Inthegraphbelow,wemayconsidervariousorderingsofitsnodes,inducing
differentwidths.Thewidthofthegraphis3(thisis,forexamplethewidth
inducedbyorderingO1dir).
2 3
5 6
1
7
4
1 2 3 4 5 6 7
<<wO1dir=3(nodes4,5,6and7)>>wO1inv=5(nd1)
<<wO2dir=5(node1)>>wO1inv=6(nd4)
4 3 2 5 6 7 1
16 December 2005 Foundations of Logic and Constraint Programming 12
Graph Width and k-Consistency
Asshownbefore,togetabacktrackfreesearchinatree,itwouldbeenough
to guarantee that, for each node N, all of its children (adjacent nodes with
higherorder)havevaluesthatsupportthevaluesinthedomainofnodeN.
Thisresultcanbegeneralisedforarbitrarygraphs,givensomeorderingofits
nodes.
If, according to some ordering, the graph has widthw, and if enumeration
follows that ordering, backtrack free search is guaranteed if the network is
stronglyk-consistent(withk > w).
Infact,likeforthecaseofthetrees,itwouldbeenoughtomaintainsomekind
of directed strong consistency, if the labelling of the nodes is done in
“increasing”order.
16 December 2005 Foundations of Logic and Constraint Programming 13
Graph Width and k-Consistency
Example:
WithorderingLo1dir,strong4-consistencyguaranteesbacktrackfreesearch.
Such consistency guarantees that any 3-compound label that satisfies the
relevant constraints, may be extended to a 4th variable that satisfies the
relevantconstraints.
Infact,anyvaluev1fromthedomainofvariable1maybeselected.Ifstrong
4-consistency ismaintained, values from thedomainsof theother variables
willpossiblyberemoved,butnovariablewillhaveitsdomainemptied.
2 3
5 6
1
7
41 2 3 4 5 6 7
16 December 2005 Foundations of Logic and Constraint Programming 14
Graph Width and k-Consistency
Example (cont.):
Sincetheorderinginduceswidth3,everyvariableXkconnectedtovariableX1is
connectedatmostwithother2variables“lower” thanXk.Forexample,variable
X6,connectedtovariableX1 isalsoconnectedto lowervariablesX3andX4 (the
sameappliestoX5-X4-X1andX4-X3-X2-X1)
Hence,ifthenetworkisstrongly4-consistent,andifthelabelX1-v1wasthere,this
meansthatany3-compoundlabel{X1-v1,X3-v3,X4-v4}couldbeextendedtoa4-
compound label {X1-v1, X3-v3, X4-v4, X6-v6} satisfying the relevant constraints.
When X1 is enumerated to v1, values v3, v4, v6 (at least) will be kept in their
variabledomains.
2 3
5 6
1
7
41 2 3 4 5 6 7
16 December 2005 Foundations of Logic and Constraint Programming 15
Graph Width and k-Consistency
Thisexampleshowsthat,aftertheenumerationofthe“lowest”variable(inan
ordering that induces a width w to the graph) of a strongly k-consistent
network(andk>w)theremainingvaluesstillhavevaluesintheirdomains(to
avoidbacktracking).
Being strongly k-consistent, all variables connected to node 1 (variableX1)
areonlyconnectedtoatmostw-1(<k)othervariableswithlowerorder.
Hence, ifsomevaluev1was in thedomainofvariableX1, thenforall these
sets of w variables, some w-compound label {X1-v1,...,Xw-1-vw-1} could be
extendedwithsomelabelXw-vw.
Therefore,whenenumeratingX1=v1theremovaloftheothervaluesofX1still
leaves the w-compound label {X1-v1, ..., Xw-vw} to satisfy the relevant
constraints.
16 December 2005 Foundations of Logic and Constraint Programming 16
Graph Width and k-Consistency
Example (cont.):
The process can proceed recursively, by successive enumeration of the
variables in increasing order. After each enumeration the networkwill have
onevariablelessandvaluesmighteventuallyberemovedfromthedomainof
theremainingvariables,ensuringthatthesimplifiednetworkremainsstrongly
4-consistent.
Eventually,anetworkwithonly4variablesisreached,andanenumerationis
obviouslypossiblewithouttheneed,ever,ofbacktracking.
1 2 3 4 5 6 72 3
5 6
1
7
4
16 December 2005 Foundations of Logic and Constraint Programming 17
Graph Width and k-Consistency
Theideasillustratedinthepreviousexamplecouldleadtoaformalproof(by
induction)ofthefollowing
Theorem:
Any constraint network strongly k-consistent,maybeenumeratedbacktrack
free if there isanorderingOaccording towhich theconstraintgraphhasa
widthlessthenk.
Inpractice,ifsuchanorderingOexists,theenumerationisdoneinincreasing
orderofthevariables,maintainingthesystemk-consistentafterenumeration
ofeachvariable.
Thisresultisparticularlyinterestingforlargeandsparsenetworks,wherethe
added cost of maintaining say, path-consistency (polinomial) may
compensatedbynotincurringinbacktracking(exponential).
16 December 2005 Foundations of Logic and Constraint Programming 18
MWO Heuristics
Strong k-consistency is of course very costly to maintain, in computational
terms,andthisisusuallynotdone.
Nevertheless,andspeciallyinconstraintnetworkswithlowdensityandwhere
the widths of the nodes vary significantly with the orderings used, the
orderingsleadingtolowergraphwidthsmaybeusedtoheuristicallyselectthe
variabletolabel.Specifically,onemaydefinethe
MWO Heuristics(Minimum Width Ordering):
The Minimum Width Ordering heuristics suggests that the variables of a
constraintproblemareenumerated,increasingly,insomeorderingthatleads
toaminimalwidthoftheprimalconstraintgraph.
16 December 2005 Foundations of Logic and Constraint Programming 19
MWO Heuristics
The definition refers the primal graph of a constraints problem, which
coincideswith the graph for binary constraints (arcs). For n-ary constraints,
the primal graph includes an arc between any variables connected by an
hyper-arcintheproblemhyper-graph.
For example, thegraphbeingusedcouldbe theprimal graphof aproblem
with2quaternaryconstraints(C1245andC1346)and3ternaryconstraints(C123,
C457andC467).
C123 --> arcsa12,a13ea23
C1245 --> arcsa12,a14,a15,a24,a25anda45
C1346 --> arcsa13,a14,a16,a34,a36anda46
C457 --> arcsa45,a47ea57
C467 --> arcsa46,a47ea67
2 3
5 6
1
7
4
16 December 2005 Foundations of Logic and Constraint Programming 20
MWO Heuristics
TheapplicationoftheMWOheuristcs,requiresthedeterminationoforderings
O leading to the lowest primal constraint graphwidth. The following greedy
algorithmcanbeusedtodeterminesuchorderings.
function sorted_vars(V,C): Node List; if V = {N} then %onlyonevariable sorted_vars <- N else N <- arg Vi min {degree(Vi,C) | Vi in V} %Nisoneofthenodeswithlessneighbours C’<- C \ arcs(N,C) V’<- V \ {N} sorted_vars <- sorted_vars(V’,C’) & N end if end function
16 December 2005 Foundations of Logic and Constraint Programming 21
MWO Heuristics
- Theordering[1,2,3,4,5,6,7]obtained,leadstoawidth3forthegraph.
2 3
5 6
1
7
4
2 3
5 6
1
4
2 3
5
1
4
2 3
1
4
2 3
1
2
1
Example:
1. In the graph, node 7 has least degree (=3). Onceremoved node 7, nodes 5 and 6 have least degree(=3).
2. Node6isnowremoved.
3. Nodes5,4(degree3),3(degree2)and2(degree1)aresubsequentlyremoved,asshownbelow.
16 December 2005 Foundations of Logic and Constraint Programming 22
MWO and MDO Heuristics
2 3
5 6
1
7
4
Both the MWO and the MDO heuristic start the enumeration by those
variables with more variables adjacent in the graph, aiming at the earliest
detectionofdeadends.
(noticethatinthealgorithmtodetectminimalwidthordering,thelastvariables
arethosewithleastdegree).
Example:
MWO and MDO orderings are not necessarily
coincident, and may be used to break ties. For
example,thetwoMDOorderings
O1 = [4,1,5,6,2,3,7]
O2 = [4,1,2,3,5,6,7]
inducedifferentwidths(4and3).
16 December 2005 Foundations of Logic and Constraint Programming 23
Cycle-Cut Sets
TheMWOheuristicisparticularlyusefulwhensomehighlevelofconsistency
ismaintained,asshown in the theoremrelating itwithstrongk-consistency,
whichisageneralisationtoarbitraryconstraintnetworksoftheresultobtained
withconstrainttrees.
However, since such consistency is hard to maintain, an alternative is to
enumeratetheproblemvariablesinorderto,assoonaspossible,simplifythe
constraintnetworkintoaconstrainttree.
From there, a backtrack free search may proceed, provided directed arc
consistency is maintained, which is relatively inexpensive from a
computationalviewpoint
Thisisthebasicideaofcycle-cutsets.
16 December 2005 Foundations of Logic and Constraint Programming 24
Cycle-Cut Sets
Obviously,oneisinterestedincycle-cutsetswithlowest cardinality.
Consider a constraint network with n variables with domains of size d. If a
cycle-cutsetofcardinalitykisfound,thesearchcomplexityisreducedto
1. atupleintheseknodeswithtimecomplexityO(dk);and
2. maintenanceof(directed)arcconsistency intheremainingtreewithn-k
nodes,withtimecomplexityO(ad2).
Sincea = n-k-1 foratreewithn-knodes,andasumingthatk is“small”, the
totaltimecomplexityisthus
O(n dk+2).
16 December 2005 Foundations of Logic and Constraint Programming 25
CCS Heuristics
Onemaythusdefinethe
CCS Heuristics(Cycle-Cut Sets)
The Cycle-Cut Sets heuristics suggests that the first variables of a
constraintproblemtobeenumeratedarethosethatformacycle-cutsetwith
leastcardinality.
Unfortunately, there seems to be no good algorithm to determine optimal
cycle-cutsets(i.e.withleastcardinality).
Two possible approximations correspond to use the sorted_vars algorithm
withtheinverseordering,orsimplystartwiththenodeswithmaximumdegree
(MDO).
16 December 2005 Foundations of Logic and Constraint Programming 26
CCS Heuristics
Example:
Usingalgorithsorted_varsbutselectingfirstthe
nodeswithhighestdegree,wewouldgetthe
followingvariableselection:
1. Removenode4(degree6)andget
2. Removenode1(degree4)andget
2 3
5 6
1
7
4
2 3
5 6
1
7
4
3. Now, inclusion of any of the other nodes (all with degree 2) turns the
constraint graph into a tree. Selecting node 2, we get the cycle-cut set
{1,2,4},whichisoptimal.
16 December 2005 Foundations of Logic and Constraint Programming 27
Cycle-Cut Sets
Example:
• Enumeranting first variables 1 and 4, the graph
becomes.
• Therefore, addinganyother node to the set {1,4}
eliminates cycle 2-3-6-7-5-2, and turn the
remainingnodesintoatree.
• Sets{1,4,2},{1,4,3},{1,4,5},{1,4,6}and{1,4,7},are
thuscycle-cutsets.
• For example, after enumeration of variables from
the cycle-cut set {1,4,7}, the remaining constraint
graphisatree.
2 3
5 6
1
7
4
2 3
5 6
1
7
4
16 December 2005 Foundations of Logic and Constraint Programming 28
MBO Heuristics
Definition(Bandwidth of a graph G, induced by O):
GivenatotalorderingOofthenodesofagraph,thebandwidthofagraphG,
inducedbyO,isthemaximumdistancebetweenadjacentnodes.
2 6
3 5
1
4
7
Example:
WithorderingO1, thebandwithofthegraphis5,the
distancebetweennodes1-6and2-7.Ifthechoiceof
variableX6determinesthechoiceofX1,changesof
X1 will only occur after irrelevantly backtracking
variablesX5,X4,X3andX2!!!
1 2 3 4 5 6 7
16 December 2005 Foundations of Logic and Constraint Programming 29
MBO Heuristics
Definition(Bandwidth of a graph G):
ThebandwidthofagraphGisthelowestbandwidthofthegraphinducedby
anyofitsorderingsO.
Example:
With orderingO2, the bandwidth is 3, the distance
betweenadjacentnodes2/5,3/6and4/7.
Noorderinginducesalowerbandwidthtothegraph.
Therefore,thegraphbandwidthis3!!!
1 2 3 4 5 6 7
2 3
4 6
1
7
5
16 December 2005 Foundations of Logic and Constraint Programming 30
MBO Heuristics
Theconceptofbandwidthisthebasisofthefollowing
MBO Heuristics(Minimum Bandwidth Ordering)
TheMinimum Width Orderingheuristicssuggeststhatthevariablesofa
constraintproblemareenumerated,increasingly,insomeorderingthatleads
totheminimalbandwidthoftheprimalconstraintgraph.
Example:
TheMBOheuristicsuggeststheuseofanheuristic
succhasO2,thatinducesabandwidthof3.
1 2 3 4 5 6 7
2 3
4 6
1
7
5
16 December 2005 Foundations of Logic and Constraint Programming 31
MBO Heuristics
The use of the MBO heuristics with constraint propagation is somewhat
problematicsince:
The constraint graphs in which it ismore useful should be sparse and
possessingnonodewithhighdegree. In the lattercase, thedistanceto
thefarthestapartadjacentnodedominates.
Theprincipleexploitedbytheheuristics,avoidirrelevantbacktracking,is
obtained,hopefullymoreefficiently,byconstraintpropagation.
Noefficientalgorithmsexisttocomputethebandwidthforgeneralgraphs
(see[Tsan93],ch.6).
16 December 2005 Foundations of Logic and Constraint Programming 32
MBO Heuristics
Thesedifficultiesareillustratedbysomeexamples
Difficulty 1:
The constraint graphs in which it is more useful should be sparse and
possessingnonodewithhighdegree. In the lattercase, thedistance to the
farthestapartadjacentnodedominates
Example:
Node 4, with degree 6, determines that the
bandwith may be no less than 3 (for orderings
with3nodesbeforeand3nodesafternode4).
Many orderings exist with this bandwidth.
However if node 3were connected to node 7,
thebandwidthwouldbe4.
5 6
1 2
3
7
4
16 December 2005 Foundations of Logic and Constraint Programming 33
MBO Heuristics
Difficulty 2:
Irrelevantbacktrackingishandledbyconstraintpropagation.
Example:
AssumethechoiceofX6isdeterminantforthechoice
ofX1.Constraintpropagationwillpossiblyempty the
domainofX6 ifabadchoice ismade in labelingX1,
before(some)variablesX2,X3,X4andX5arelabeled,
thusavoidingtheirirrelevantbacktracking.
2 6
3 5
1
4
7
TheMBOheuristicsisthusmoreappropriatewithbacktrackingalgorithmswithout
constraintpropagation.
1 2 3 4 5 6 7
16 December 2005 Foundations of Logic and Constraint Programming 34
MBO Heuristics
Difficulty 3:
Noefficientalgorithmsexisttocomputebandwidthforgeneralgraphs.
Ingeneral,lowerwidthscorrespondtolowerbandwidths,butthebestorderings
inbothcasesareusuallydifferent.
A B
C
D
E
F
G
H
C D A B E F G H
<<Width(minimal)=2;Bandwidth=5
<<Width=3; Bandwidth(minimal)=4
C D E A B F G H
16 December 2005 Foundations of Logic and Constraint Programming 35
Dynamic Heuristics
In contrast to the static heuristics discussed (MWO, MDO, CCS e MBO)
variableselectionmaybedetermineddynamically.
Instead of being fixed before enumeration starts, the variable is selected
taking into account the propagation of previous variable selections (and
labellings).
Inadditiontoproblemspecificheuristics,thereisageneralprinciplethathas
showngreatpotential,thefirst-failprinciple.
The principle is simple: when a problem includes many interdependent
“tasks”,startsolvingthosethataremostdifficult. It isnotworthwastingtime
withtheeasiestones,sincetheymayturntobeincompatiblewiththeresults
ofthedifficultones.
16 December 2005 Foundations of Logic and Constraint Programming 36
First-Fail Heuristics
Therearemanywaysofinterpretingandimplementingthisgenericfirst-fail
principle.
Firstly,thetaskstoperformtosolveaconstraintsatisfactionproblemmaybe
consideredtheassignmentofvaluestotheproblemvariables.Howto
measuretheirdifficulty?
Enumeratingbyitselfiseasy(asimpleassignment).Whatturnsthetasks
difficultistoassesswhetherthechoiceisviable,afterconstraintpropagation.
Thisassessmentishardtomakeingeneral,sowemayconsiderfeaturesthat
areeasytomeasure,suchas
Thedomainofthevariables
Thenumber of constraints(degree)theyparticipatein.
16 December 2005 Foundations of Logic and Constraint Programming 37
First-Fail Heuristics
Thedomainofthevariables
Intuitively,ifvariablesX1/X2havem /m2valuesintheirdomains,andm2 > m1,
itispreferabletoassignvaluestoX1,becausethereislesschoiceavailable!
Inthelimit,ifvariableX1hasonlyonevalueinitsdomain,(m1 = 1),thereisno
possiblechoiceandthebestthingtodoistoimmediatelyassignthevalueto
thevariable.
Anotherwayofseeingtheissueisthefollowing:
Ontheonehand,the“chance”toassignagoodvaluetoX1ishigherthan
thatforX2.
On the other hand, if that value proves to be a bad one, a larger
proportionofthesearchspaceiseliminated.
16 December 2005 Foundations of Logic and Constraint Programming 38
First-Fail Heuristics: Example
Example:
In the 8 queens problem, where
queensQ1,Q2eQ3,werealready
enumerated,we have the following
domainsfortheotherqueens
Q4 in {2,7,8}, Q5 in {2,4,8}, Q6 in {4}, Q7 in {2,4,8}, Q8 in {2,4,6,7}.
Hence, thebest variable toenumeratenext shouldbeQ6,notQ4 thatwould
followinthe“natural”order.
Inthisextremecaseofsingletondomains,node-consistencyachievespruning
similartoarc-consistencywithlesscomputationalcosts!
1 1
1
1
1
1
11
1
1
1
1
1
12
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
33
16 December 2005 Foundations of Logic and Constraint Programming 39
First-Fail Heuristics
Thenumber of constraints(degree)ofthevariables
Thisheuristics isbasically theMaximumDegreeOrdering (MDO)heuristics,
but now the degree of the variables is assessed dynamically, after each
variableenumeration.
Clearly,themoreconstraintsavariableisinvolvedin,themoredifficultitisto
assignagoodvaluetoit,sinceithastosatisfyalargernumberofconstraints.
Of course, and like in the case of the domain size, this decision is purely
heuristic.Theeffectof theconstraintsdependsgreatlyon theirpropagation,
whichdependsinturnontheprobleminhand,whichishardtoantecipate.
16 December 2005 Foundations of Logic and Constraint Programming 40
Problem Dependent Heuristics
In certain typesof problems, theremightbeheuristics speciallyadapted for
theproblemsbeingsolved.
Forexample,inschedulingproblems,wheretheyshouldnotoverlapbuthave
to take place in a certain period of time, it is usually a good heuristic to
“scatter”themasmuchaspossiblewithintheallowedperiod.
This suggests that one should start by enumerating first the variables
correspondingtotasksthatmaybeperformedinthebeginningandtheendof
theallowedperiod,thusallowing“space”fortheotherstoexecute.
Insuchcase,thedynamicchoiceofthevariablewouldtakeintoaccountthe
valuesinitsdomain,namelytheminimumandmaximumvalues.
16 December 2005 Foundations of Logic and Constraint Programming 41
Mixed Heuristics
Taking intoaccount the featuresof theheuristicsdiscussedso far,onemay
consider the use of mixed strategies, that incorporate some of these
heuristics.
For example the static cycle-cut sets (CCS) heuristics suggests a set of
variablestoenumeratefirst,soastoturntheconstraintgraphintoatree.
Within this cycle-cut set one may use a first-fail dynamic heuristic (e.g.
smallestdomainsize).
Ontheotherhand,evenastaticheuristiclikeMinimalWidthOrdering(MWO),
may be turned “dynamic”, by reassessing the width of the graphs after a
certain number of enumerations, to take into account the results of
propagation.
16 December 2005 Foundations of Logic and Constraint Programming 42
Value Choice Heuristics
Someformsofassigninglikelihoodsarethefollowing:
Ad hoc choice:
Againinschedulingproblems,onceselectedthevariablewithlowest/highest
values in its domain, the natural choice for the value will be the
lowest/highest,whichsomehow“optimises”thelikelihoodofsuccess.
Lookahed:
Onemay try to antecipate for each of the possible values the likelihood of
success by evaluating after its propagation the effect on an aggregated
indicatoronthesizeofthedomainsnotyetassigned(asdonewiththekappa
indicator),chosingtheonethatmaximisessuchindicator.
16 December 2005 Foundations of Logic and Constraint Programming 43
Value Choice Heuristics
Optimisation:
Inoptimisationproblems,wherethereissomefunctiontomaximise/minimise,
onemaygetboundsforthatfunctionwhenthealternativevaluesarechosen
forthevariable,orcheckhowtheychangewiththeselectedvalue.
Ofcourse,theheuristicwillchosethevaluethateitheroptimisesthebounds
inconsideration,orthatimprovesthemthemost.
Notice that in this case, the computation of the boundsmay be performed
either before propagation takes place (less computation, but also less
information)oraftersuchpropagation.
.
16 December 2005 Foundations of Logic and Constraint Programming 44
Heuristics in SICStus
Beingbasedon theConstraintLogicProgrammingparadigm,aprogram in
SICStushasthestructuredescribedbefore
Problem(Vars):-
Declaration of Variables and Domains,
Specification of Constraints,
Labelling of the Variables.
In the labelling of the variablesX1, X2, ..., Xn, of some list Lx, one should
specifytheintendedheuristics.
Although these heuristicsmay be programmed explicitely, there are some
facilitiesthatSICStusprovides,bothforvariable selectionandvalue choice.
16 December 2005 Foundations of Logic and Constraint Programming 45
Heuristics in SICStus
The simplest form to specify enumeration is through a buit-in predicate,
labeling/2,where
the1stargumentisalistofoptions,possiblyempty
The2ndargumentisalistLx = [X1, X2, ..., Xn]ofvariablestoenumerate
By default, labeling([ ], Lx) selects variables X1, X2, ..., Xn, from list Lx,
according to their position, “from left to right”. The value chosen for the
variableistheleastvalueinthedomain.
Thispredicatecanbeusedwithnooptionsforstaticheuristics,providedthat
thevariablesaresortedinthelistLxaccordingtotheintendedordering.
16 December 2005 Foundations of Logic and Constraint Programming 46
Heuristics in SICStus
Withanempty listofoptions,predicate labeling([ ],L) is in factequivalent to
predicateenumerating(L)below
enumerating([]).
enumerating([Xi|T]):-
indomain(Xi),
enumerating(T).
wherethebuilt-inpredicate,indomain(Xi),chosesvaluesforvariableXiin
increasingorder.
There are other possibilities for user control of value choice. The current
domainofavariable,maybeobtainedwithbuilt-infd_predicatefd_dom/2.For
example
?- X in 1..5, X #\=3, fd_dom(X,D).
D = (1..2)\/(4..5),
X in(1..2)\/(4..5) ?
16 December 2005 Foundations of Logic and Constraint Programming 47
Heuristics in SICStus
Usually, it is not necessary to reach this low level of programming, and a
numberofpredefinedoptionsforpredicatelabeling/2canbeused.
Theoptionsof interest forvaluechoice for theselectedvariableareup and
down, with the obviousmeaning of chosing the values from the domain in
increasinganddecreasingorder,respectively.
Hence,toguaranteethatthevalueofsomevariableischosenindecreasing
order without resorting to lower-level fd_predicates, it is sufficient, to call
predicatelabeling/2withoptiondown
labeling([down],[Xi])
16 December 2005 Foundations of Logic and Constraint Programming 48
Heuristics in SICStus
Theoptionsof interest for variable selectionare leftmost, min, max, ff, ffc
and variable(Sel)
leftmost-isthedefaultmode.
o Variablesaresimplyselectedbytheirorderinthelist.
min, max - the variablewith the lowest/highest value in its domain is
selected.
o Useful, for example, in many applications of scheduling, as
discussed.
ff, ffc - implements the first-fail heuristics, selection the variablewith a
domain of least size, breaking ties with the number of constraints, in
whichthevariableisinvolved.
16 December 2005 Foundations of Logic and Constraint Programming 49
Heuristics in SICStus
variable(Sel)
This is themostgeneralpossibility.Selmustbedefined in theprogram
asapredicate,whoselast3parametersareVars, Selected, Rest.Given
the list of Vars to enumerate, thepredicate should returnSelected as
thevariabletoselect,Restbeingthelistwiththeremainingvariables.
Otherparametersmaybeusedbefore the last3.Forexample, ifoption
variable(includes(5)) isused, thensomepredicate includes/4mustbe
specified,suchas
includes(V, Vars, Selected, Rest)
which should chose, from the Vars list, a variable, Selected, that
includesVinitsdomain.
16 December 2005 Foundations of Logic and Constraint Programming 50
Heuristics in SICStus
Noticethatalltheseoptionsofpredicatelabeling/2maybeprogrammedata
lower level, using the adequate primitives available from SICStus for
inspectionofthedomains.TheseReflexivePredicates,namedfd_predicates
include
fd_min(?X, ?Min)fd_max(?X, ?Max)fd_size(?X, ?Size)fd_degree(?X, ?Degree)
withtheobviousmeaning.Forexample,
?- X in 3..8, Y in 1..5, X #< Y,
fd_size(X,S), fd_max(X,M), fd_degree(Y,D).
D = 1, M = 4, S = 2,
X in 3..4, Y in 4..5 ?
16 December 2005 Foundations of Logic and Constraint Programming 51
Heuristics in SICStus
Programqueens_fd_hsolvesthenqueensproblem
queens(N,M,O,S,F)
withvariouslabellingoptions:
Variable Selection
Option ff isused.
Variables that are passed to predicate labeling/2 may or may not be
(M=2/1) sorted from the middle to the ends (for example,
[X4,X5,X3,X6,X2,X7,X1,X8])bypredicate
my_sort(2, Lx, Lo).
Value Choice
Rather than starting enumeration from the lowest value, an offset
(parameterO)isspecifiedtostartenumeration“half-way”.
16 December 2005 Foundations of Logic and Constraint Programming 52
Alternatives to Pure Backtracking
Constraint Logic Programming uses, by default, depth first search withbacktrackinginthelabellingphase.
Despite being “interleaved” with constraint propagation, and the use of
heuristics,theefficiencyofsearchdependscriticallyofthefirstchoicesdone,
namelythevaluesassignedtothefirstvariablesselected.
Backtracking “chronologically”, these values may only change when the
values of the remaining k variables are fully considered (after some O(2k)
time in the worst case). Hence, alternatives have been proposed to pure
depthfirstsearchwithchronologicalbacktracking,namely
Intelligent backtracking,
Iterative broadening,
Limited discrepancy;and
Incremental time-bound search.
16 December 2005 Foundations of Logic and Constraint Programming 53
Alternatives to Pure Backtracking
In chronological backtracking, when the enumeration of a variable fails,
backtrackingisperformedonthevariablethatimmediatelyprecededit,even if
this variable is not to blame for the failure.
Various techniques for inteligent backtracking, or dependency directed
search, aimat identifying the causesof the failureandbacktrackdirectly to
thefirstvariablethatparticipatesinthefailure.
Somevariantsofintelligentbacktrackingare:
Backjumping ;
Backchecking ;and
Backmarking .
16 December 2005 Foundations of Logic and Constraint Programming 54
Intelligent Backtracking
Backjumping
Failingthelabelingofavariable,all
variablesthatcausethefailureof
eachofthevaluesareanalysed,and
the“highest”ofthe“least”variables
isbacktracked
In the example, variable Q6, could not be labeled, and backtracking is
performedonQ4,the“highestoftheleast“variablesinvolvedinthefailureofQ6.
All positions of Q6 are, in fact, incompatible with the value of some variable
lowerthanQ4.
1 3 4 2 5 4 5 3 5 1 2 3
16 December 2005 Foundations of Logic and Constraint Programming 55
Intelligent Backtracking
Backchecking and Backmarking
These techniquesmaybeusefulwhen the testingofconstraintsondifferent
variables is very costly. The key idea is tomemorise previous conflicts, in
ordertoavoidrepeatingthem.
In backchecking, only the assignments that caused conflicts are
memorised.
Embackmarking theassignments thatdidnot causeconflictsarealso
memorised.
The use of these techniqueswith constraint propagation is usually not very
effective (with a possible exception of SAT solvers, with nogood clause
learning), since propagation antecipates the conflicts, somehow avoiding
irrelevantbacktracking.
16 December 2005 Foundations of Logic and Constraint Programming 56
Iterative Broadening
In iterative broadening it isassigneda limitb, to thenumberof times thata
nodeisvisited(boththeinitialvisitandthosebybacktracking),i.e.thenumber
ofvaluesthatmaybechosenforavariable.Ifthisvalueisexceeded,thenode
anditssuccessorsarenotexploredanyfurther.
• In theexample,assumingb = 2,
the search space prunned is
shadowed.
Of course, if the search fails for a given b, this value is iteratively increased,
hencetheiterativebroadeningqualification.
1 2 3 4 1 2 3 4 1 2 3 4
1 2 3
1
16 December 2005 Foundations of Logic and Constraint Programming 57
Limited discrepancy
Limited discrepancy assumes that thevaluechoiceheuristicmayonly faila
(small)numberof times. It directs thesearch for the regionswheresolutions
more likely lie, by limiting to some number d the number of times that the
suggestionmadebytheheuristicisnottakenfurther.
• In the example, assuming
heuristicoptionsattheleftand
d=3thesearchspaceprunned
isshadowed.
Again, if the search fails, d may be incremented and the search space is
increasinglyincremented.
1 2 3 4 1 2 3 4 1 2 3 4
1 2 3
1
d = 3d = 2
16 December 2005 Foundations of Logic and Constraint Programming 58
Incremental Time Bounded Search
InITBS,thegoalissimilartoiterativebroadeningorlimiteddiscrepancy,but
implementeddifferently.
Oncechosenthevaluesofthefirstkvariables,foreachlabel{X1-v1,..., Xk-vk}
searchisallowedforagiventimeT.
Ifnosolutionisfound,anotherlabelingistested.Ofcourse,ifthesearchfails
for a certain value of T, this may be increased incrementally in the next
iterations,guaranteeingthatthesearchspaceisalsoincreassingiteratively.
In all these algorithms (iterative broadening, limited discrepancy and
incremental duration) parts of the search space may be revisited.
Nevertheless, the worst-case time complexity of the algorithms is not
worsened.
16 December 2005 Foundations of Logic and Constraint Programming 59
Incremental Time Bounded Search
Forexample, inthecaseof incrementaltime-boundsearchif thesuccessive
andfailediterationsincreasethetimelimitbysomefactor,i.e.Tj+1=Tj,the
iterationswilllast
T1+T2+...+Tj
=T(1++ 2+...+j-1)=
=T(1-j)/(1-)Tj
Ifasolutionisfoundintheiterationj+1,then
thetimespentinthepreviousiterationsisTj.
iterationj+1lastsforTjand,inaverage,thesolutionisfoundinhalfthis
time.
Hence,the“wasted”timeisofthesamemagnitudeofthe“useful”timespent
inthesearch(initerationj+1).