CSE473:Ar+ficialIntelligenceConstraintSa+sfac+onProblems
Instructor:LukeZe@lemoyer
UniversityofWashington[TheseslideswerecreatedbyDanKleinandPieterAbbeelforCS188IntrotoAIatUCBerkeley.AllCS188materialsareavailableath@p://ai.berkeley.edu.]
WhatisSearchFor?§ Assump+onsabouttheworld:asingleagent,determinis+cac+ons,fullyobserved
state,discretestatespace
§ Planning:sequencesofac+ons§ Thepathtothegoalistheimportantthing§ Pathshavevariouscosts,depths§ Heuris+csgiveproblem-specificguidance
§ Iden+fica+on:assignmentstovariables§ Thegoalitselfisimportant,notthepath§ Allpathsatthesamedepth(forsomeformula+ons)§ CSPsarespecializedforiden+fica+onproblems
ConstraintSa+sfac+onProblems
ConstraintSa+sfac+onProblems
§ Standardsearchproblems:§ Stateisa“blackbox”:arbitrarydatastructure§ Goaltestcanbeanyfunc+onoverstates§ Successorfunc+oncanalsobeanything
§ Constraintsa+sfac+onproblems(CSPs):§ Aspecialsubsetofsearchproblems§ StateisdefinedbyvariablesXiwithvaluesfroma
domainD (some+mesDdependsoni)§ Goaltestisasetofconstraintsspecifyingallowable
combina+onsofvaluesforsubsetsofvariables
§ Simpleexampleofaformalrepresenta-onlanguage
§ Allowsusefulgeneral-purposealgorithmswithmorepowerthanstandardsearchalgorithms
CSPExamples
Example:MapColoring
§ Variables:
§ Domains:
§ Constraints:adjacentregionsmusthavedifferentcolors
§ Solu+onsareassignmentssa+sfyingallconstraints,e.g.:
Implicit:
Explicit:
Example:N-Queens
§ Formula+on1:§ Variables:§ Domains:§ Constraints
Example:N-Queens
§ Formula+on2:§ Variables:
§ Domains:
§ Constraints:Implicit:
Explicit:
ConstraintGraphs
ConstraintGraphs
§ BinaryCSP:eachconstraintrelates(atmost)twovariables
§ Binaryconstraintgraph:nodesarevariables,arcsshowconstraints
§ General-purposeCSPalgorithmsusethegraphstructuretospeedupsearch.E.g.,Tasmaniaisanindependentsubproblem!
[Demo:CSPapplet(madeavailablebyaispace.org)--n-queens]
ScreenshotofDemoN-Queens
Example:Cryptarithme+c
§ Variables:
§ Domains:
§ Constraints:
Example:Sudoku
§ Variables:§ Each(open)square
§ Domains:§ {1,2,…,9}
§ Constraints:
9-wayalldiffforeachrow
9-wayalldiffforeachcolumn
9-wayalldiffforeachregion
(orcanhaveabunchofpairwiseinequalityconstraints)
Example:TheWaltzAlgorithm
§ TheWaltzalgorithmisforinterpre+nglinedrawingsofsolidpolyhedraas3Dobjects
§ AnearlyexampleofanAIcomputa+onposedasaCSP
? § Approach:
§ Eachintersec+onisavariable§ Adjacentintersec+onsimposeconstraints
oneachother§ Solu+onsarephysicallyrealizable3D
interpreta+ons
Example:TheWaltzAlgorithm
§ TheWaltzalgorithmisforinterpre+nglinedrawingsofsolidpolyhedraas3Dobjects
§ AnearlyexampleofanAIcomputa+onposedasaCSP
§ Approach:§ Eachintersec+onisavariable§ Adjacentintersec+onsimposeconstraints
oneachother§ Solu+onsarephysicallyrealizable3D
interpreta+ons
?
Varie+esofCSPsandConstraints
Varie+esofCSPs
§ DiscreteVariables§ Finitedomains
§ SizedmeansO(dn)completeassignments§ E.g.,BooleanCSPs,includingBooleansa+sfiability(NP-complete)
§ Infinitedomains(integers,strings,etc.)§ E.g.,jobscheduling,variablesarestart/end+mesforeachjob§ Linearconstraintssolvable,nonlinearundecidable
§ Con+nuousvariables§ E.g.,start/end+mesforHubbleTelescopeobserva+ons§ Linearconstraintssolvableinpolynomial+mebyLPmethods
(seecs170forabitofthistheory)
Varie+esofConstraints
§ Varie+esofConstraints§ Unaryconstraintsinvolveasinglevariable(equivalentto
reducingdomains),e.g.:
§ Binaryconstraintsinvolvepairsofvariables,e.g.:
§ Higher-orderconstraintsinvolve3ormorevariables:e.g.,cryptarithme+ccolumnconstraints
§ Preferences(somconstraints):§ E.g.,redisbe@erthangreen§ Omenrepresentablebyacostforeachvariableassignment§ Givesconstrainedop+miza+onproblems§ (We’llignoretheseun+lwegettoBayes’nets)
Real-WorldCSPs
§ Assignmentproblems:e.g.,whoteacheswhatclass§ Timetablingproblems:e.g.,whichclassisofferedwhenandwhere?§ Hardwareconfigura+on§ Transporta+onscheduling§ Factoryscheduling§ Circuitlayout§ Faultdiagnosis§ …lotsmore!
§ Manyreal-worldproblemsinvolvereal-valuedvariables…
SolvingCSPs
StandardSearchFormula+on
§ Standardsearchformula+onofCSPs
§ Statesdefinedbythevaluesassignedsofar(par+alassignments)§ Ini+alstate:theemptyassignment,{}§ Successorfunc+on:assignavaluetoanunassignedvariable
§ Goaltest:thecurrentassignmentiscompleteandsa+sfiesallconstraints
§ We’llstartwiththestraighqorward,naïveapproach,thenimproveit
SearchMethods
§ WhatwouldBFSdo?
§ WhatwouldDFSdo?
§ Whatproblemsdoesnaïvesearchhave?
[Demo:coloring--dfs]
VideoofDemo
Coloring--DFS
BacktrackingSearch
BacktrackingSearch
§ BacktrackingsearchisthebasicuninformedalgorithmforsolvingCSPs
§ Idea1:Onevariableata+me§ Variableassignmentsarecommuta+ve,sofixordering§ I.e.,[WA=redthenNT=green]sameas[NT=greenthenWA=red]§ Onlyneedtoconsiderassignmentstoasinglevariableateachstep
§ Idea2:Checkconstraintsasyougo§ I.e.consideronlyvalueswhichdonotconflictpreviousassignments§ Mighthavetodosomecomputa+ontochecktheconstraints§ “Incrementalgoaltest”
§ Depth-firstsearchwiththesetwoimprovementsiscalledbacktrackingsearch(notthebestname)
§ Cansolven-queensforn≈25
BacktrackingExample
BacktrackingSearch
§ Backtracking=DFS+variable-ordering+fail-on-viola+on§ Whatarethechoicepoints?
[Demo:coloring--backtracking]
VideoofDemo
Coloring–Backtracking
VideoofDemo
Coloring–Backtracking
ImprovingBacktracking
§ General-purposeideasgivehugegainsinspeed§ …butit’salls+llNP-hard
§ Filtering:Canwedetectinevitablefailureearly?
§ Ordering:§ Whichvariableshouldbeassignednext?(MRV)§ Inwhatordershoulditsvaluesbetried?(LCV)
§ Structure:Canweexploittheproblemstructure?
ArcConsistencyandBeyond
ForwardChecking
§ Idea:Keeptrackofremaininglegalvaluesforunassignedvariables(usingimmediateconstraints)
§ Idea:Terminatewhenanyvariablehasnolegalvalues
WA SA NT Q
NSW V
ForwardChecking
AreWeDone?
ConstraintPropaga+on
§ Forwardcheckingpropagatesinforma+onfromassignedtoadjacentunassignedvariables,butdoesn'tdetectmoredistantfailures:
WA SA NT Q
NSW V
§ NT and SA cannot both be blue! § Why didn’t we detect this yet? § Constraint propagation repeatedly enforces constraints (locally)
ArcConsistencyofanEn+reCSP§ Asimpleformofpropaga+onmakessureallarcsaresimultaneouslyconsistent:
§ Arcconsistencydetectsfailureearlierthanforwardchecking§ Important:IfXlosesavalue,neighborsofXneedtoberechecked!§ Mustrerunamereachassignment!
Remember:Deletefromthetail!
WA SA
NT Q
NSW
V
ConstraintPropaga+on
AreWeDone?
Limita+onsofArcConsistency
§ Amerenforcingarcconsistency:§ Canhaveonesolu+onlem§ Canhavemul+plesolu+onslem§ Canhavenosolu+onslem(andnotknowit)
§ Arcconsistencys+llrunsinsideabacktrackingsearch!
Whatwentwronghere?
Ordering:MinimumRemainingValues
§ Minimumremainingvalues(MRV):§ Choosethevariablewiththefewestlegalvalues
§ Whyminratherthanmax?§ Alsocalled“mostconstrainedvariable”§ “Fail-fast”ordering
Ordering:DegreeHeuris+c
§ Tie-breakeramongMRVvariables§ Degreeheuris+c:
§ Choosethevariablepar+cipa+nginthemostconstraintsonremainingvariables
§ Whymostratherthanfewestconstraints?
Ordering:LeastConstrainingValue
§ Givenachoiceofvariable:§ Choosetheleastconstrainingvalue§ Theonethatrulesoutthefewestvaluesinthe
remainingvariables§ Notethatitmaytakesomecomputa+onto
determinethis!
§ Whyleastratherthanmost?
§ Combiningtheseheuris+csmakes1000queensfeasible
Propaga+onwithOrdering
K-Consistency
K-Consistency§ Increasingdegreesofconsistency
§ 1-Consistency(NodeConsistency):Eachsinglenode’sdomainhasavaluewhichmeetsthatnode’sunaryconstraints
§ 2-Consistency(ArcConsistency):Foreachpairofnodes,anyconsistentassignmenttoonecanbeextendedtotheother
§ K-Consistency:Foreachknodes,anyconsistentassignmenttok-1canbeextendedtothekthnode.
§ Higherkmoreexpensivetocompute
§ (Youneedtoknowthek=2case:arcconsistency)
StrongK-Consistency
§ Strongk-consistency:alsok-1,k-2,…1consistent
§ Claim:strongn-consistencymeanswecansolvewithoutbacktracking!
§ Why?§ Chooseanyassignmenttoanyvariable§ Chooseanewvariable§ By2-consistency,thereisachoiceconsistentwiththefirst§ Chooseanewvariable§ By3-consistency,thereisachoiceconsistentwiththefirst2§ …
§ Lotsofmiddlegroundbetweenarcconsistencyandn-consistency!(e.g.k=3,calledpathconsistency)
Structure
ProblemStructure
§ Extremecase:independentsubproblems§ Example:Tasmaniaandmainlanddonotinteract
§ Independentsubproblemsareiden+fiableasconnectedcomponentsofconstraintgraph
§ Supposeagraphofnvariablescanbebrokenintosubproblemsofonlycvariables:§ Worst-casesolu+oncostisO((n/c)(dc)),linearinn§ E.g.,n=80,d=2,c=20§ 280=4billionyearsat10millionnodes/sec§ (4)(220)=0.4secondsat10millionnodes/sec
Tree-StructuredCSPs
§ Theorem:iftheconstraintgraphhasnoloops,theCSPcanbesolvedinO(nd2)+me§ ComparetogeneralCSPs,whereworst-case+meisO(dn)
§ Thispropertyalsoappliestoprobabilis+creasoning(earlier):anexampleoftherela+onbetweensyntac+crestric+onsandthecomplexityofreasoning
Tree-StructuredCSPs§ Algorithmfortree-structuredCSPs:
§ Order:Choosearootvariable,ordervariablessothatparentsprecedechildren
§ Removebackward:Fori=n:2,applyRemoveInconsistent(Parent(Xi),Xi)§ Assignforward:Fori=1:n,assignXiconsistentlywithParent(Xi)
§ Run+me:O(nd2)(why?)
Tree-StructuredCSPs
§ Claim1:Amerbackwardpass,allroot-to-leafarcsareconsistent§ Proof:EachX→YwasmadeconsistentatonepointandY’sdomaincouldnothave
beenreducedthereamer(becauseY’schildrenwereprocessedbeforeY)
§ Claim2:Ifroot-to-leafarcsareconsistent,forwardassignmentwillnotbacktrack§ Proof:Induc+ononposi+on
§ Whydoesn’tthisalgorithmworkwithcyclesintheconstraintgraph?
§ Note:samebasicideaasvariableelimina+oninBayes’nets
ImprovingStructure
NearlyTree-StructuredCSPs
NearlyTree-StructuredCSPs
§ Condi+oning:instan+ateavariable,pruneitsneighbors'domains
§ Cutsetcondi+oning:instan+ate(inallways)asetofvariablessuchthattheremainingconstraintgraphisatree
§ Cutsetsizecgivesrun+meO((dc)(n-c)d2),veryfastforsmallc
CutsetCondi+oning
SA
SA SA SA
Instan+atethecutset(allpossibleways)
ComputeresidualCSPforeachassignment
SolvetheresidualCSPs(treestructured)
Chooseacutset
CutsetQuiz
§ Findthesmallestcutsetforthegraphbelow.
TreeDecomposi+on*§ Idea:createatree-structuredgraphofmega-variables§ Eachmega-variableencodespartoftheoriginalCSP§ Subproblemsoverlaptoensureconsistentsolu+ons
M1 M2 M3 M4
{(WA=r,SA=g,NT=b), (WA=b,SA=r,NT=g), …}
{(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)), …}
Agree on shared vars
NT
SA
≠ WA
≠ ≠
Q
SA
≠ NT
≠ ≠
Agree on shared vars
NSW
SA
≠ Q
≠ ≠
Agree on shared vars
V
SA
≠ NSW
≠ ≠
Itera+veImprovement
Itera+veAlgorithmsforCSPs
§ Localsearchmethodstypicallyworkwith“complete”states,i.e.,allvariablesassigned
§ ToapplytoCSPs:§ Takeanassignmentwithunsa+sfiedconstraints§ Operatorsreassignvariablevalues§ Nofringe!Liveontheedge.
§ Algorithm:Whilenotsolved,§ Variableselec+on:randomlyselectanyconflictedvariable§ Valueselec+on:min-conflictsheuris+c:
§ Chooseavaluethatviolatesthefewestconstraints§ I.e.,hillclimbwithh(n)=totalnumberofviolatedconstraints
Example:4-Queens
§ States:4queensin4columns(44=256states)§ Operators:movequeenincolumn§ Goaltest:noa@acks§ Evalua+on:c(n)=numberofa@acks
[Demo:n-queens–itera+veimprovement(L5D1)][Demo:coloring–itera+veimprovement]
VideoofDemoItera+veImprovement–nQueens
VideoofDemoItera+veImprovement–Coloring
PerformanceofMin-Conflicts
§ Givenrandomini+alstate,cansolven-queensinalmostconstant+meforarbitrarynwithhighprobability(e.g.,n=10,000,000)!
§ Thesameappearstobetrueforanyrandomly-generatedCSPexceptinanarrowrangeofthera+o
Summary:CSPs
§ CSPsareaspecialkindofsearchproblem:§ Statesarepar+alassignments§ Goaltestdefinedbyconstraints
§ Basicsolu+on:backtrackingsearch
§ Speed-ups:§ Ordering§ Filtering§ Structure
§ Itera+vemin-conflictsisomeneffec+veinprac+ce
LocalSearch
LocalSearch
§ Treesearchkeepsunexploredalterna+vesonthefringe(ensurescompleteness)
§ Localsearch:improveasingleop+onun+lyoucan’tmakeitbe@er(nofringe!)
§ Newsuccessorfunc+on:localchanges
§ Generallymuchfasterandmorememoryefficient(butincompleteandsubop+mal)
HillClimbing
§ Simple,generalidea:§ Startwherever§ Repeat:movetothebestneighboringstate§ Ifnoneighborsbe@erthancurrent,quit
§ What’sbadaboutthisapproach?§ Complete?§ Op+mal?
§ What’sgoodaboutit?
HillClimbingDiagram
HillClimbingQuiz
Star+ngfromX,wheredoyouendup?
Star+ngfromY,wheredoyouendup?Star+ngfromZ,wheredoyouendup?
SimulatedAnnealing§ Idea:Escapelocalmaximabyallowingdownhillmoves
§ Butmakethemrareras+megoeson
70
SimulatedAnnealing
§ Theore+calguarantee:§ Sta+onarydistribu+on:§ IfTdecreasedslowlyenough,willconvergetoop+malstate!
§ Isthisaninteres+ngguarantee?
§ Soundslikemagic,butrealityisreality:§ Themoredownhillstepsyouneedtoescapealocalop+mum,thelesslikelyyouaretoevermakethemallinarow
§ Peoplethinkhardaboutridgeoperatorswhichletyoujumparoundthespaceinbe@erways
Gene+cAlgorithms
§ Gene+calgorithmsuseanaturalselec+onmetaphor§ KeepbestNhypothesesateachstep(selec+on)basedonafitnessfunc+on§ Alsohavepairwisecrossoveroperators,withop+onalmuta+ontogivevariety
§ Possiblythemostmisunderstood,misapplied(andevenmaligned)techniquearound
Example:N-Queens
§ Whydoescrossovermakesensehere?§ Whenwouldn’titmakesense?§ Whatwouldmuta+onbe?§ Whatwouldagoodfitnessfunc+onbe?