Page 1
CSE473:Ar+ficialIntelligenceConstraintSa+sfac+onProblems
Instructor:LukeZe@lemoyer
UniversityofWashington[TheseslideswerecreatedbyDanKleinandPieterAbbeelforCS188IntrotoAIatUCBerkeley.AllCS188materialsareavailableath@p://ai.berkeley.edu.]
Page 2
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
Page 3
ConstraintSa+sfac+onProblems
Page 4
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
Page 6
Example:MapColoring
§ Variables:
§ Domains:
§ Constraints:adjacentregionsmusthavedifferentcolors
§ Solu+onsareassignmentssa+sfyingallconstraints,e.g.:
Implicit:
Explicit:
Page 7
Example:N-Queens
§ Formula+on1:§ Variables:§ Domains:§ Constraints
Page 8
Example:N-Queens
§ Formula+on2:§ Variables:
§ Domains:
§ Constraints:Implicit:
Explicit:
Page 10
ConstraintGraphs
§ BinaryCSP:eachconstraintrelates(atmost)twovariables
§ Binaryconstraintgraph:nodesarevariables,arcsshowconstraints
§ General-purposeCSPalgorithmsusethegraphstructuretospeedupsearch.E.g.,Tasmaniaisanindependentsubproblem!
[Demo:CSPapplet(madeavailablebyaispace.org)--n-queens]
Page 11
ScreenshotofDemoN-Queens
Page 12
Example:Cryptarithme+c
§ Variables:
§ Domains:
§ Constraints:
Page 13
Example:Sudoku
§ Variables:§ Each(open)square
§ Domains:§ {1,2,…,9}
§ Constraints:
9-wayalldiffforeachrow
9-wayalldiffforeachcolumn
9-wayalldiffforeachregion
(orcanhaveabunchofpairwiseinequalityconstraints)
Page 14
Example:TheWaltzAlgorithm
§ TheWaltzalgorithmisforinterpre+nglinedrawingsofsolidpolyhedraas3Dobjects
§ AnearlyexampleofanAIcomputa+onposedasaCSP
? § Approach:
§ Eachintersec+onisavariable§ Adjacentintersec+onsimposeconstraints
oneachother§ Solu+onsarephysicallyrealizable3D
interpreta+ons
Page 15
Example:TheWaltzAlgorithm
§ TheWaltzalgorithmisforinterpre+nglinedrawingsofsolidpolyhedraas3Dobjects
§ AnearlyexampleofanAIcomputa+onposedasaCSP
§ Approach:§ Eachintersec+onisavariable§ Adjacentintersec+onsimposeconstraints
oneachother§ Solu+onsarephysicallyrealizable3D
interpreta+ons
?
Page 16
Varie+esofCSPsandConstraints
Page 17
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)
Page 18
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)
Page 19
Real-WorldCSPs
§ Assignmentproblems:e.g.,whoteacheswhatclass§ Timetablingproblems:e.g.,whichclassisofferedwhenandwhere?§ Hardwareconfigura+on§ Transporta+onscheduling§ Factoryscheduling§ Circuitlayout§ Faultdiagnosis§ …lotsmore!
§ Manyreal-worldproblemsinvolvereal-valuedvariables…
Page 21
StandardSearchFormula+on
§ Standardsearchformula+onofCSPs
§ Statesdefinedbythevaluesassignedsofar(par+alassignments)§ Ini+alstate:theemptyassignment,{}§ Successorfunc+on:assignavaluetoanunassignedvariable
§ Goaltest:thecurrentassignmentiscompleteandsa+sfiesallconstraints
§ We’llstartwiththestraighqorward,naïveapproach,thenimproveit
Page 22
SearchMethods
§ WhatwouldBFSdo?
§ WhatwouldDFSdo?
§ Whatproblemsdoesnaïvesearchhave?
[Demo:coloring--dfs]
Page 23
VideoofDemo
Coloring--DFS
Page 24
BacktrackingSearch
Page 25
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
Page 26
BacktrackingExample
Page 27
BacktrackingSearch
§ Backtracking=DFS+variable-ordering+fail-on-viola+on§ Whatarethechoicepoints?
[Demo:coloring--backtracking]
Page 28
VideoofDemo
Coloring–Backtracking
Page 29
VideoofDemo
Coloring–Backtracking
Page 30
ImprovingBacktracking
§ General-purposeideasgivehugegainsinspeed§ …butit’salls+llNP-hard
§ Filtering:Canwedetectinevitablefailureearly?
§ Ordering:§ Whichvariableshouldbeassignednext?(MRV)§ Inwhatordershoulditsvaluesbetried?(LCV)
§ Structure:Canweexploittheproblemstructure?
Page 31
ArcConsistencyandBeyond
Page 32
ForwardChecking
§ Idea:Keeptrackofremaininglegalvaluesforunassignedvariables(usingimmediateconstraints)
§ Idea:Terminatewhenanyvariablehasnolegalvalues
WA SA NT Q
NSW V
Page 35
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)
Page 36
ArcConsistencyofanEn+reCSP§ Asimpleformofpropaga+onmakessureallarcsaresimultaneouslyconsistent:
§ Arcconsistencydetectsfailureearlierthanforwardchecking§ Important:IfXlosesavalue,neighborsofXneedtoberechecked!§ Mustrerunamereachassignment!
Remember:Deletefromthetail!
WA SA
NT Q
NSW
V
Page 37
ConstraintPropaga+on
Page 39
Limita+onsofArcConsistency
§ Amerenforcingarcconsistency:§ Canhaveonesolu+onlem§ Canhavemul+plesolu+onslem§ Canhavenosolu+onslem(andnotknowit)
§ Arcconsistencys+llrunsinsideabacktrackingsearch!
Whatwentwronghere?
Page 40
Ordering:MinimumRemainingValues
§ Minimumremainingvalues(MRV):§ Choosethevariablewiththefewestlegalvalues
§ Whyminratherthanmax?§ Alsocalled“mostconstrainedvariable”§ “Fail-fast”ordering
Page 41
Ordering:DegreeHeuris+c
§ Tie-breakeramongMRVvariables§ Degreeheuris+c:
§ Choosethevariablepar+cipa+nginthemostconstraintsonremainingvariables
§ Whymostratherthanfewestconstraints?
Page 42
Ordering:LeastConstrainingValue
§ Givenachoiceofvariable:§ Choosetheleastconstrainingvalue§ Theonethatrulesoutthefewestvaluesinthe
remainingvariables§ Notethatitmaytakesomecomputa+onto
determinethis!
§ Whyleastratherthanmost?
§ Combiningtheseheuris+csmakes1000queensfeasible
Page 43
Propaga+onwithOrdering
Page 45
K-Consistency§ Increasingdegreesofconsistency
§ 1-Consistency(NodeConsistency):Eachsinglenode’sdomainhasavaluewhichmeetsthatnode’sunaryconstraints
§ 2-Consistency(ArcConsistency):Foreachpairofnodes,anyconsistentassignmenttoonecanbeextendedtotheother
§ K-Consistency:Foreachknodes,anyconsistentassignmenttok-1canbeextendedtothekthnode.
§ Higherkmoreexpensivetocompute
§ (Youneedtoknowthek=2case:arcconsistency)
Page 46
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)
Page 48
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
Page 49
Tree-StructuredCSPs
§ Theorem:iftheconstraintgraphhasnoloops,theCSPcanbesolvedinO(nd2)+me§ ComparetogeneralCSPs,whereworst-case+meisO(dn)
§ Thispropertyalsoappliestoprobabilis+creasoning(earlier):anexampleoftherela+onbetweensyntac+crestric+onsandthecomplexityofreasoning
Page 50
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?)
Page 51
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
Page 52
ImprovingStructure
Page 53
NearlyTree-StructuredCSPs
Page 54
NearlyTree-StructuredCSPs
§ Condi+oning:instan+ateavariable,pruneitsneighbors'domains
§ Cutsetcondi+oning:instan+ate(inallways)asetofvariablessuchthattheremainingconstraintgraphisatree
§ Cutsetsizecgivesrun+meO((dc)(n-c)d2),veryfastforsmallc
Page 55
CutsetCondi+oning
SA
SA SA SA
Instan+atethecutset(allpossibleways)
ComputeresidualCSPforeachassignment
SolvetheresidualCSPs(treestructured)
Chooseacutset
Page 56
CutsetQuiz
§ Findthesmallestcutsetforthegraphbelow.
Page 57
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
≠ ≠
Page 58
Itera+veImprovement
Page 59
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
Page 60
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]
Page 61
VideoofDemoItera+veImprovement–nQueens
Page 62
VideoofDemoItera+veImprovement–Coloring
Page 63
PerformanceofMin-Conflicts
§ Givenrandomini+alstate,cansolven-queensinalmostconstant+meforarbitrarynwithhighprobability(e.g.,n=10,000,000)!
§ Thesameappearstobetrueforanyrandomly-generatedCSPexceptinanarrowrangeofthera+o
Page 64
Summary:CSPs
§ CSPsareaspecialkindofsearchproblem:§ Statesarepar+alassignments§ Goaltestdefinedbyconstraints
§ Basicsolu+on:backtrackingsearch
§ Speed-ups:§ Ordering§ Filtering§ Structure
§ Itera+vemin-conflictsisomeneffec+veinprac+ce
Page 66
LocalSearch
§ Treesearchkeepsunexploredalterna+vesonthefringe(ensurescompleteness)
§ Localsearch:improveasingleop+onun+lyoucan’tmakeitbe@er(nofringe!)
§ Newsuccessorfunc+on:localchanges
§ Generallymuchfasterandmorememoryefficient(butincompleteandsubop+mal)
Page 67
HillClimbing
§ Simple,generalidea:§ Startwherever§ Repeat:movetothebestneighboringstate§ Ifnoneighborsbe@erthancurrent,quit
§ What’sbadaboutthisapproach?§ Complete?§ Op+mal?
§ What’sgoodaboutit?
Page 68
HillClimbingDiagram
Page 69
HillClimbingQuiz
Star+ngfromX,wheredoyouendup?
Star+ngfromY,wheredoyouendup?Star+ngfromZ,wheredoyouendup?
Page 70
SimulatedAnnealing§ Idea:Escapelocalmaximabyallowingdownhillmoves
§ Butmakethemrareras+megoeson
70
Page 71
SimulatedAnnealing
§ Theore+calguarantee:§ Sta+onarydistribu+on:§ IfTdecreasedslowlyenough,willconvergetoop+malstate!
§ Isthisaninteres+ngguarantee?
§ Soundslikemagic,butrealityisreality:§ Themoredownhillstepsyouneedtoescapealocalop+mum,thelesslikelyyouaretoevermakethemallinarow
§ Peoplethinkhardaboutridgeoperatorswhichletyoujumparoundthespaceinbe@erways
Page 72
Gene+cAlgorithms
§ Gene+calgorithmsuseanaturalselec+onmetaphor§ KeepbestNhypothesesateachstep(selec+on)basedonafitnessfunc+on§ Alsohavepairwisecrossoveroperators,withop+onalmuta+ontogivevariety
§ Possiblythemostmisunderstood,misapplied(andevenmaligned)techniquearound
Page 73
Example:N-Queens
§ Whydoescrossovermakesensehere?§ Whenwouldn’titmakesense?§ Whatwouldmuta+onbe?§ Whatwouldagoodfitnessfunc+onbe?