复旦大学大数据学院 constraint satisfaction problems · local search versus systematic...
TRANSCRIPT
复旦大学大数据学院School of Data Science, Fudan University
DATA130008 Introduction to Artificial Intelligence
Constraint Satisfaction Problems魏忠钰
March 27th,2019
Constraint Satisfaction Problems
Constraint Satisfaction Problems
§ Planning:sequencesofactions§ Thepathtothegoalistheimportantthing
§ Pathshavevariouscosts(e.g.depths)
§ Heuristicsgiveproblem-specificguidance
§ Identification:assignmentstovariables§ Thegoalitselfisimportant,notthepath
§ E.g.Nqueens,Sudoku
§ Allpathsatthesamedepth(forsomeformulations)
§ ConstraintSatisfactionProblems(CSPs)arespecializedforidentificationproblems
Constraint Satisfaction Problem
§ Standardsearchproblems:§ Stateisa“blackbox”:arbitrarydatastructure
§ Goaltestcanbeanyfunctionoverstates
§ Constraintsatisfactionproblems(CSPs):§ Stateisrepresentedasafeaturevector
§ Goaltestisasetofconstraints toidentifytheallowablefeaturevector
§ 2
Feature Vector
§ Wehave§ Asetofkvariables (orfeatures)§ Eachvariablehasadomain ofdifferentvalues.§ Astateisspecifiedbyanassignmentofavalueforeachvariable.§ height={short,average,tall},§ weight={light,average,heavy}
§ Apartialstateisspecifiedbyanassignmentofavaluetosomeofthevariables.
§ Acompletestateisapossiblesolutiontotheproblem.
CSP Examples
Example: Map Coloring
§ Variables:
§ Domains:
§ Constraints:adjacentregionsmusthavedifferentcolors
§ Solutionsareassignmentssatisfyingallconstraints,e.g.:
Implicit:
Explicit:
Example: N-Queens
§ Formulation1:§ Variables:§ Domains:§ Constraints
Example: N-Queens
§ Formulation2:§ Variables:
§ Domains:
§ Constraints:
Implicit:
Explicit:
Constraint Graphs
Constraint Graphs
§ BinaryCSP:eachconstraintrelates(atmost)twovariables§ Binaryconstraintgraph:nodesarevariables,arcsshow
constraints
Example: Cryptarithmetic
§ Variables:
§ Domains:
§ Constraints:
Example: Sudoku
§ Variables:§ Each(open)square
§ Domains:§ {1,2,…,9}
§ Constraints:
9-wayalldiff foreachrow
9-wayalldiff foreachcolumn
9-wayalldiff foreachregion
(orcanhaveabunchofpairwiseinequalityconstraints)
Transform N-nary CSPs to Binary constraints one
§ ShowhowasingleternaryconstraintsuchasA+B=Ccanbeturnedinto3binaryconstraintsbyusingauxiliaryvariables.Youmayassumefinite domains.Nextshowhowconstraintswithmorethan3variablescanbetreatedsimilarly.
§ Step1:IntroduceanewvariableZ.§ Domainisthesetofallpossible3-tuplessatisfyingA+B=C.{(a1,b1,c1),(a2,b2,c2),…}
§ Step2:CreatenewconstraintsbetweenZandthreeoriginalvariablesrespectively(thepositioninthetupleislinkedtoanoriginalvariable).§ < 𝑍, 𝐴 >∈ {(((𝑎1, 𝑏1, 𝑐1)𝑎1), …}§ < 𝑍, 𝐵 >∈ {(((𝑎1, 𝑏1, 𝑐1), 𝑏1), …}§ < 𝑍, 𝐶 >∈ {((𝑎1, 𝑏1, 𝑐1))𝑐1), …}
Varieties of CSPs and Constraints
Varieties of CSPs
§ DiscreteVariables§ Finitedomains
§ SizedmeansO(dn) completeassignments§ Infinitedomains(integers,strings,etc.)
§ E.g.,jobscheduling,variablesarestart/endtimesforeachjob
§ Continuousvariables§ A + B = C(A,B,Carerealnumbers)
Varieties of Constraints
§ VarietiesofConstraints§ Unaryconstraintsinvolveasinglevariable:
§ Binaryconstraintsinvolvepairsofvariables,e.g.:
§ Higher-orderconstraintsinvolve3ormorevariables: e.g.,cryptarithmetic columnconstraints
§ Global Constraints, e.g.: AllDiff()
§ Preferences(softconstraints):§ i.e.appleisbetterthanbanana§ Oftenrepresentablebyacostforeachvariableassignment
Real-World CSPs
§ Assignmentproblems:e.g.,whoteacheswhatclass§ Timetablingproblems:e.g.,whichclassisofferedwhenandwhere?§ Hardwareconfiguration§ Transportationscheduling§ Factoryscheduling§ Circuitlayout§ Faultdiagnosis§ …lotsmore!
Solving CSPs
Standard Search Formulation
§ StandardsearchformulationofCSPs
§ Statesdefinedbythevaluesassignedsofar(partialassignments)§ Initialstate:theemptyassignment,{}§ Successorfunction:assignavaluetoanunassignedvariable§ Goaltest:thecurrentassignmentiscompleteandsatisfiesallconstraints
§We’llstartwiththestraightforward,naïveapproach,thenimproveit
Search Methods
§WhatwouldBFSdo?
§WhatwouldDFSdo?
§Whatproblemsdoesnaïvesearchhave?§ Ordering ofvariables.§ Choicesofvalues.§ Theutilizationofconstraints.
Backtracking Search
Backtracking Search
§ BacktrackingsearchisthebasicuninformedalgorithmforsolvingCSPs
§ Idea1:Onevariableatatime in fixed ordering§ Variableassignmentsarecommutative,sofixordering§ I.e.,[WA=redthenNT=green]sameas[NT=greenthenWA=red]
§ Idea2:Checkconstraintsasyougo§ I.e.consideronlyvalueswhichdonotconflictpreviousassignments§ Mighthavetodosomecomputationtochecktheconstraints§ “Incrementalgoaltest”
§ Depth-firstsearchwiththesetwoimprovements§ iscalledbacktrackingsearch
Backtracking Example
Backtracking Search
• Backtracking=DFS+variable-ordering+fail-on-violation• Whatarethechoicepoints?
Improving Backtracking
§Ordering:§ Whichvariableshouldbeassignednext?§ Inwhatordershoulditsvaluesbetried?
§ Filtering:Canwedetectinevitablefailureearly?
§ Structure:Canweexploittheproblemstructure?
Ordering
Ordering: Minimum Remaining Values
§ VariableOrdering:Minimumremainingvalues(MRV):§ Choosethevariablewiththefewestlegalleftvaluesinitsdomain
§Whyminratherthanmax?§ Alsocalled“mostconstrainedvariable”§ “Fail-fast”ordering
Ordering: Least Constraining Value
§ ValueOrdering:LeastConstrainingValue§ Givenachoiceofvariable,choosetheleastconstrainingvalue
§ I.e.,theonethatrulesoutthefewestvaluesintheremainingvariables
§ Notethatitmaytakesomecomputationtodeterminethis!(E.g.,rerunningfiltering)
§Whyleastratherthanmost?
Filtering: Forward Checking
§ Forwardchecking:Crossoffvaluesthatviolateaconstraintwhenaddedtotheexistingassignment
WASANT Q
NSWV
Filtering: Constraint Propagation
§ Forwardcheckingpropagatesinformationfromassignedtounassignedvariables,butdoesn'tprovideearlydetectionforallfailures:
§ NTandSAcannotbothbeblue!§ Constraintpropagation:reasonfromconstrainttoconstraint
WA SA
NT Q
NSW
V
Consistency of A Single Arc
§ AnarcX® Yisconsistent iff foreveryxinthetailthereissomeyintheheadwhichcouldbeassignedwithoutviolatingaconstraint
§ Forwardchecking:Enforcingconsistencyofarcspointingtoeachnewassignment
Deletefromthetail!
WA SA
NT Q
NSW
V
Arc Consistency of an Entire CSP
§ Asimpleformofpropagationmakessureallarcsareconsistent:
§ Important:IfXlosesavalue,neighborsofXneedtoberechecked!§ Arcconsistencydetectsfailureearlierthanforwardchecking§ Canberunasapreprocessororaftereachassignment§ What’sthedownsideofenforcingarcconsistency?
Remember:Deletefromthetail!
WA SANT Q
NSW
V
Enforcing Arc Consistency in a CSP
• Runtime:O(n2d3),canbereducedtoO(n2d2)• How?• Checkexercise6.13formoreinformation.
Limitations of Arc Consistency
§ Afterenforcingarcconsistency:§ Canhaveonesolutionleft§ Canhavemultiplesolutionsleft§ Canhavenosolutionsleft(andnotknowit)
§ Arcconsistencystillrunsinsideabacktrackingsearch!
Whatwentwronghere?
K-Consistency
K-Consistency
§ Increasingdegreesofconsistency
§ 1-Consistency(NodeConsistency):Eachsinglenode’sdomainhasavaluewhichmeetsthatnode’sunaryconstraints
§ 2-Consistency(ArcConsistency):Foreachpairofnodes,anyconsistentassignmenttoonecanbeextendedtotheother
§ K-Consistency:Foreachknodes,anyconsistentassignmenttok-1canbeextendedtothekth node.
§ Higherkmoreexpensivetocompute
Strong K-Consistency
§ Strongk-consistency:alsok-1,k-2,…1consistent
§ Claim:strongn-consistencymeanswecansolvewithoutbacktracking!
§ Why?1. Chooseanyassignmenttoanyvariable2. Chooseanewvariable3. By2-consistency,thereisachoiceconsistentwiththefirst4. Chooseanewvariable5. By3-consistency,thereisachoiceconsistentwiththefirst26. …
§ Lotsofmiddlegroundbetweenarcconsistencyandn-consistency!(e.g.k=3,calledpathconsistency)
§ O(n2d) forasolutiontoStrongK-Consistencyproblem.
Structure
Problem Structure
§ Extremecase:independentsub-problems§ Example:Tasmaniaandmainlanddonotinteract
§ Independentsub-problemsareidentifiableasconnectedcomponentsofconstraintgraph
§ Supposeagraphofnvariablescanbebrokenintosub-problemsofonlycvariables:§ Worst-casesolutioncostisO((n/c)(dc)),linearinn§ E.g.,n=80,d=2,c=20§ 280 =4billionyearsat10millionnodes/sec§ (4)(220)=0.4secondsat10millionnodes/sec
Tree-Structured CSPs
§ Theorem:iftheconstraintgraphhasnoloops,theCSPcanbesolvedinO(nd2)time§ ComparetogeneralCSPs,whereworst-casetimeisO(dn)
Tree-Structured CSPs
§ Algorithmfortree-structuredCSPs:
§ Order:Choosearootvariable,ordervariablessothatparentsprecedechildren
§ Checkthearcconsistencyfromchildtoparent§ Assignvaluefromroottoleaves
Tree-Structured CSPs
§ Algorithmfortree-structuredCSPs:
§ Runtime:O(nd2)(why?)
Tree-Structured CSPs
§ Claim1:Afterbackwardpass,allroot-to-leafarcsareconsistentØEachX®YwasmadeconsistentatonepointandY’sdomaincouldnothavebeenreducedthereafter(becauseY’schildrenwereprocessedbeforeY)
§ Claim2:Ifroot-to-leafarcsareconsistent,forwardassignmentwillnotbacktrack
ØGiven one value of a parent node, there is always a valid value in thechild node to make the assignment consistent.
§ Whydoesn’tthisalgorithmworkwithcyclesintheconstraintgraph?
Improving Structure
Nearly Tree-Structured CSPs
§ Conditioning:instantiateavariable,pruneitsneighbors'domains§ CycleCutset conditioning:instantiate(inallways)asetofvariablessuchthattheremainingconstraintgraphisatree
§ CycleCutset sizecgivesruntimeO((dc)(n-c)d2)
SA
Cutset Conditioning
SA
SA SA SA
Instantiatethecutset(allpossibleways)
ComputeresidualCSPforeachassignment
SolvetheresidualCSPs(treestructured)
Chooseacutset
Cutset Excercise
§ Findthesmallestcyclecutset forthegraphbelow.
Tree Decomposition
§ Idea:createatree-structuredgraphofmega-variables§ Eachmega-variableencodespartoftheoriginalCSP§ Sub-problemsoverlaptoensureconsistentsolutions
M3
{(NT=r,SA=g,Q=b),(NT=b,SA=g,Q=r),…}
Agree:(M1,M2)Î{((WA=g,SA=g,NT=g),(NT=g,SA=g,Q=g)),…}
NSW
SA
¹Q
¹ ¹
Agreeonsharedvars
M1 M2
{(WA=r,SA=g,NT=b),(WA=b,SA=r,NT=g),…}
Agreeonsharedvars
NT
SA
¹WA
¹ ¹
Q
SA
¹NT
¹ ¹
Agreeonsharedvars
M4
V
SA
¹NSW
¹ ¹
Iterative Improvement
Iterative Algorithms for CSPs
§ Localsearchmethodstypicallyworkwith“complete”states,i.e.,allvariablesassigned
§ ToapplytoCSPs:§ Takeanassignmentwithunsatisfiedconstraints§ Operators reassignvariablevalues
§ Algorithm:,§ Startfromacompleteassignment(whilenotasolution)§ Variableselection:randomlyselectanyconflictedvariable§ Valueselection:min-conflictsheuristic:
§ Chooseavaluethatviolatesthefewestconstraints§ I.e.,hillclimbwithh(n)=totalnumberofviolatedconstraints
Example: 4-Queens
• States:4queensin4columns(44 =256states)• Operators:movequeenincolumn• Goaltest:noattacks• Evaluation:c(n)=numberofattacks
Min-Conflicts Algorithm
§ Justlikeahillclimbingapproach
Summary: CSPs
§ CSPsareaspecialkindofsearchproblem:§ Statesarepartialassignments§ Goaltestdefinedbyconstraints
§ Basicsolution:backtrackingsearch
§ Speed-ups:§ Ordering§ Filtering§ Structure
§ Iterativemin-conflictsisofteneffectiveinpractice§~50 formostoftheN-queenproblems
Local Search
§ Systematicsearches:keeppathsin memory,andrememberalternativessosearchcanbacktrack.Solutionisapathtoagoal.
§ Pathmaybeirrelevant,ifonlythefinalconfigurationisneeded(8-queens,ICdesign,networkoptimization,…)
Local Search versus Systematic Search
§ SystematicSearch§ BFS,DFS,IDS,Best-First,A*§ Keepssomehistoryofvisitednodes§ Alwayscompleteforfinitesearchspaces,some
versionscompleteforinfinitespaces§ Goodforbuildingupsolutionsincrementally
§ State=partialsolution§ Action=extendpartialsolution
Local Search versus Systematic Search
§ LocalSearch§ Gradientdescent,Greedylocalsearch,Simulated
Annealing,GeneticAlgorithms§ Doesnotkeephistoryofvisitednodes§ Notcomplete.Maybeabletoarguewill
terminatewith“highprobability”§ Goodfor“fixingup”candidatesolutions
§ State=completecandidatesolutionthatmaynotsatisfyallconstraints
§ Action=makeasmallchangeinthecandidatesolution
Local search
§ HillClimbing§ Simulatedannealing§ Localbeamsearch§ GeneticAlgorithm§ Gradientdecentforcontinuesfunction
Hill-climbing search
§ Generatenearbysuccessorstatestothecurrentstate§ Pickthebestandreplacethecurrentstatewiththatone.
Hill-climbing search
§ Localmaximum:apeakthatislowerthanthehighestpeak,soasuboptimalsolutionisreturned
§ Plateau:theevaluationfunctionisflat,resultinginarandomwalk
§ Ridge:resultinasequenceoflocalmaximathatisverydifficultforgreedyalgorithmstonavigate.
Local maximum Plateau Ridge
States Where Greedy Search Must Succeed
objectivefunctio
n
Target
States Where Greedy Search Might Succeed
objectivefunctio
n
Target
States Where Greedy Search Will NOT Succeed
objectivefunctio
n
Target
Greedy Search Landscape
objectivefunctio
nob
jectivefunctio
n
LocalMinimum
Plateau
Target
Local search
§ HillClimbing§ Simulatedannealing§ Localbeamsearch§ GeneticAlgorithm§ Gradientdecentforcontinuesfunction
Simulated Annealing
Simulated Annealing
§ Combinationofrandomwalkandhillclimbing.§ Generatearandomnewneighborfromcurrentstate.§ Ifit’sbettertakeit.§ Ifit’sworsethentakeitwithsomeprobabilityproportional
tothetemperatureandthedeltabetweenthenewandoldstates.
Simulated Annealing
§ Thealgorithmwandersaroundduringtheearlypartsofthesearch,hopefullytowardagoodgeneralregionofthestatespace
§ Towardtheend,thealgorithmdoesamorefocusedsearch,makingfewbadmoves
Local search
§ HillClimbing§ Simulatedannealing§ Localbeamsearch§ GeneticAlgorithm§ Gradientdecentforcontinuesfunction
Local Beam Search
§ Keeptrackofkstatesratherthanjustone,asinhillclimbing
§ Beginswithkrandomlygeneratedstates§ Ateachstep,allsuccessorsofallkstatesaregenerated§ Ifanyoneisagoal,Done!§ Otherwise,selectsbestksuccessorsfromthecomplete
list,andrepeats
Local Search in Continuous Spaces
initial state vector( ) quantity to optimized
step sizeuntil Goal_Test( ) do
( )
Sf S
SfS S SS
a
a
==
=
¶= -
¶
negativesteptominimizefpositivesteptomaximizef
Wrap-up
§ Ifyouareinterested,refertochapter4.1– 4.2