17
1 CSE 473: Artificial Intelligence Constraint Satisfaction Problems III Factored (aka Structured) Search [With many slides by Dan Klein and Pieter Abbeel (UC Berkeley) available at http://ai.berkeley.edu.] Backtracking Search § Backtracking search is the basic uninformed algorithm for solving CSPs § Start with Depth First Search § “backtracking search” IS a Kind of depth first search with these 2 details: § Idea 1: One variable at a time § Variable assignments are commutative, so fix ordering § I.e., [WA = red then NT = green] same as [NT = green then WA = red] § Only need to consider assignments to a single variable at each step § Idea 2: Check constraints as you go § I.e. consider only values which do not conflict previous assignments § Might have to do some computation to check the constraints § “Incremental goal test” § Can solve n-queens for n » 25

CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

1

CSE473:ArtificialIntelligenceConstraintSatisfactionProblemsIIIFactored(akaStructured)Search

[WithmanyslidesbyDanKleinandPieterAbbeel (UCBerkeley)availableathttp://ai.berkeley.edu.]

BacktrackingSearch

§ BacktrackingsearchisthebasicuninformedalgorithmforsolvingCSPs

§ StartwithDepthFirstSearch§ “backtrackingsearch”ISaKindofdepthfirstsearchwiththese2details:

§ Idea1:Onevariableatatime§ Variableassignmentsarecommutative,sofixordering§ I.e.,[WA=redthenNT=green]sameas[NT=greenthenWA=red]§ Onlyneedtoconsiderassignmentstoasinglevariableateachstep

§ Idea2:Checkconstraintsasyougo§ I.e.consideronlyvalueswhichdonotconflictpreviousassignments§ Mighthavetodosomecomputationtochecktheconstraints§ “Incrementalgoaltest”

§ Cansolven-queensforn» 25

Page 2: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

2

ImprovingBacktracking

§ General-purposeideasgivehugegainsinspeed

§ Ordering:§ Whichvariableshouldbeassignednext?§ Inwhatordershoulditsvaluesbetried?

§ Filtering:Canwedetectinevitablefailureearly?

§ Structure:Canweexploittheproblemstructure?

§ Filtering:Keeptrackofdomainsforunassignedvariablesandcrossoffbadoptions§ Forwardchecking:Crossoffvaluesthatviolateaconstraintwhenaddedtotheexisting

assignment

Filtering:ForwardChecking

WASANT Q

NSWV

[Demo:coloring-- forwardchecking]

Page 3: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

3

ConsistencyofaSingleArc

§ AnarcX® Yisconsistent iff foreveryxinthetailthereissomeyintheheadwhichcouldbeassignedwithoutviolatingaconstraint

§ Forwardchecking:Enforcingconsistencyofarcspointingtoeachnewassignment

Deletefromthetail!

WA SA

NT Q

NSW

V

ArcConsistencyofanEntireCSP§ Asimpleformofpropagationmakessureallarcsareconsistent:

§ Important:IfXlosesavalue,neighborsofXneedtoberechecked!§ Arcconsistencydetectsfailureearlier thanforwardchecking§ Canberunasapreprocessoror aftereachassignment§ What’sthedownside ofenforcingarcconsistency?

WA SANT Q

NSW

V

Page 4: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

4

AC-3algorithmforArcConsistency

§ Runtime:O(n2d3),canbereducedtoO(n2d2)§ …butdetectingall possiblefutureproblemsisNP-hard– why?

[Demo:CSPapplet(madeavailablebyaispace.org)-- n-queens]

LimitationsofArcConsistency

§ Afterenforcingarcconsistency:§ Canhaveonesolutionleft§ Canhavemultiplesolutionsleft§ Canhavenosolutionsleft

(andnotknowit)

§ EvenwithArcConsistencyyoustillneedbacktrackingsearch!§ Couldrunatevenstepofthatsearch§ Usuallybettertorunitonce,beforesearch

Whatwentwronghere?

Page 5: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

5

VideoofDemoArcConsistency– CSPApplet– nQueens

VideoofDemoColoring– BacktrackingwithForwardChecking–ComplexGraph

Page 6: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

6

VideoofDemoColoring– BacktrackingwithArcConsistency–ComplexGraph

K-Consistency

Page 7: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

7

K-Consistency§ Increasingdegreesofconsistency

§ 1-Consistency(NodeConsistency):Eachsinglevariable’sdomainhasavaluewhichmeetsthatvariablesunaryconstraints

§ 2-Consistency(ArcConsistency):Foreachpairofvariables,anyconsistentassignmenttoonecanbeextendedtotheother

§ 3-Consistency(PathConsistency):Foreverysetof3vars,anyconsistentassignmentto2ofthevariablescanbeextendedtothethirdvar

§ K-Consistency:Foreachknodes,anyconsistentassignmenttok-1canbeextendedtothekth node.

§ Higherkmoreexpensivetocompute

§ (Youneedtoknowthealgorithmfork=2case:arcconsistency)

StrongK-Consistency

§ Strongk-consistency:alsok-1,k-2,…1consistent

§ Claim:strongn-consistencymeanswecansolvewithoutbacktracking!

§ Why?§ Chooseanyassignmenttoanyvariable§ Chooseanewvariable§ By2-consistency,thereisachoiceconsistentwiththefirst§ Chooseanewvariable§ By3-consistency,thereisachoiceconsistentwiththefirst2§ …

Page 8: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

8

Ordering

BacktrackingSearch

Page 9: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

9

Ordering:MinimumRemainingValues

§ VariableOrdering:Minimumremainingvalues(MRV):§ Choosethevariablewiththefewestlegalleftvaluesinitsdomain

§ Whyminratherthanmax?§ Alsocalled“mostconstrainedvariable”§ “Fail-fast”ordering

§ Tie-breakeramongMRVvariables§ Whatistheveryfirststatetocolor?(Allhave3valuesremaining.)

§ Maximumdegreeheuristic:§ Choosethevariableparticipatinginthemostconstraintsonremainingvariables

§ Whymostratherthanfewestconstraints?

Ordering:MaximumDegree

Page 10: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

10

Ordering:LeastConstrainingValue

§ ValueOrdering:LeastConstrainingValue§ Givenachoiceofvariable,choosetheleastconstrainingvalue

§ I.e.,theonethatrulesoutthefewestvaluesintheremainingvariables

§ Notethatitmaytakesomecomputationtodeterminethis!(E.g.,rerunningfiltering)

§ Whyleastratherthanmost?

§ Combiningtheseorderingideasmakes1000queensfeasible

RationaleforMRV,MD,LCV

§ Wewanttoenterthemostpromisingbranch,butwealsowanttodetectfailurequickly

§ MRV+MD:§ Choosethevariablethatismostlikelytocausefailure§ Itmustbeassignedatsomepoint,soifitisdoomedtofail,bettertofindoutsoon

§ LCV:§ Wehopeourearlyvaluechoicesdonotdoomustofailure§ Choosethevaluethatismostlikelytosucceed

Page 11: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

11

Structure

ProblemStructure

§ Extremecase:independentsubproblems§ Example:Tasmaniaandmainlanddonotinteract

§ Independentsubproblems areidentifiableasconnectedcomponentsofconstraintgraph

§ Supposeagraphofnvariablescanbebrokenintosubproblems ofonlycvariables:§ Worst-casesolutioncostisO((n/c)(dc)),linearinn§ E.g.,n=80,d=2,c=20§ 280 =4billionyearsat10millionnodes/sec§ (4)(220)=0.4secondsat10millionnodes/sec

Page 12: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

12

Tree-StructuredCSPs

§ Theorem:iftheconstraintgraphhasnoloops,theCSPcanbesolvedinO(nd2)time§ ComparetogeneralCSPs,whereworst-casetimeisO(dn)

§ Thispropertyalsoappliestoprobabilisticreasoning(later):anexampleoftherelationbetweensyntacticrestrictionsandthecomplexityofreasoning

Tree-StructuredCSPs§ Algorithmfortree-structuredCSPs:

§ Order:Choosearootvariable,ordervariablessothatparentsprecedechildren

§ Removebackward:Fori =n:2,applyRemoveInconsistent(Parent(Xi),Xi)§ Assignforward:Fori =1:n,assignXi consistentlywithParent(Xi)

§ Runtime:O(nd2)(why?)

Page 13: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

13

Tree-StructuredCSPs

§ Claim1:Afterbackwardpass,allroot-to-leafarcsareconsistent§ Proof:EachX®YwasmadeconsistentatonepointandY’sdomaincouldnothave

beenreducedthereafter(becauseY’schildrenwereprocessedbeforeY)

§ Claim2:Ifroot-to-leafarcsareconsistent,forwardassignmentwillnotbacktrack§ Proof:Inductiononposition

§ Whydoesn’tthisalgorithmworkwithcyclesintheconstraintgraph?

§ Note:we’llseethisbasicideaagainwithBayes’nets

ImprovingStructure

Page 14: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

14

NearlyTree-StructuredCSPs

§ Conditioning:instantiateavariable,pruneitsneighbors'domains

§ Cutset conditioning:instantiate(inallways)asetofvariablessuchthattheremainingconstraintgraphisatree

§ Cutset sizecgivesruntimeO((dc)(n-c)d2),veryfastforsmallc

Cutset Conditioning

SA

SA SA SA

Instantiatethecutset(allpossibleways)

ComputeresidualCSPforeachassignment

SolvetheresidualCSPs(treestructured)

Chooseacutset

Page 15: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

15

Cutset Quiz

§ Findthesmallestcutset forthegraphbelow.

LocalSearchforCSPs

Page 16: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

16

IterativeAlgorithmsforCSPs

§ Localsearchmethodstypicallyworkwith“complete”states,i.e.,allvariablesassigned

§ ToapplytoCSPs:§ Takeanassignmentwithunsatisfiedconstraints§ Operatorsreassignvariablevalues§ Nofringe!Liveontheedge.

§ Algorithm:Whilenotsolved,§ 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

[Demo:n-queens– iterativeimprovement(L5D1)][Demo:coloring– iterativeimprovement]

Page 17: CSE 473: Artificial Intelligence - …...3 Consistency of a Single Arc An arc X ®Y is consistent iff for every x in the tail there is some y in the head which could be assigned without

17

PerformanceofMin-Conflicts

§ Givenrandominitialstate,cansolven-queensinalmostconstanttimeforarbitrarynwithhighprobability(e.g.,n=10,000,000)!

§ Thesameappearstobetrueforanyrandomly-generatedCSPexcept inanarrowrangeoftheratio

Summary:CSPs

§ CSPsareaspecialkindofsearchproblem:§ Statesarepartialassignments§ Goaltestdefinedbyconstraints

§ Basicsolution:backtrackingsearch

§ Speed-ups:§ Ordering§ Filtering§ Structure(cutset conditioning)

§ Iterativemin-conflictsisofteneffectiveinpractice