38
CS 343: Artificial Intelligence Constraint Satisfaction Problems Prof. Scott Niekum The University of Texas at Austin [These slides are based on those of Dan Klein and Pieter Abbeel for CS188 Intro to AI at UC Berkeley. All CS188 materials are available at http://ai.berkeley.edu.]

lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

CS343:ArtificialIntelligenceConstraintSatisfactionProblems

Prof.ScottNiekum

TheUniversityofTexasatAustin[TheseslidesarebasedonthoseofDanKleinandPieterAbbeelforCS188IntrotoAIatUCBerkeley.AllCS188materialsareavailableathttp://ai.berkeley.edu.]

Page 2: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

WhatisSearchFor?

▪ Assumptionsabouttheworld:asingleagent,deterministicactions,fullyobservedstate,discretestatespace

▪ Planning:sequencesofactions▪ Thepathtothegoalistheimportantthing▪ Pathshavevariouscosts,depths▪ Heuristicsgiveproblem-specificguidance

▪ Identification:assignmentstovariables▪ Thegoalitselfisimportant,notthepath▪ Allpathsatthesamedepth(forsomeformulations)▪ CSPsarespecializedforidentificationproblems

Page 3: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

ConstraintSatisfactionProblems

Page 4: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

ConstraintSatisfactionProblems

▪ Standardsearchproblems:▪ Stateisa“blackbox”:arbitrarydatastructure▪ Goaltestcanbeanyfunctionoverstates▪ Successorfunctioncanalsobeanything

▪ Constraintsatisfactionproblems(CSPs):▪ Aspecialsubsetofsearchproblems▪ StateisdefinedbyvariablesXiwithvaluesfroma

domainD (sometimesDdependsoni)▪ Goaltestisasetofconstraintsspecifyingallowable

combinationsofvaluesforsubsetsofvariables

▪ Allowsusefulgeneral-purposealgorithmswithmorepowerthanstandardsearchalgorithms

Page 5: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

CSPExamples

Page 6: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Example:MapColoring

▪ Variables:

▪ Domains:

▪ Constraints:adjacentregionsmusthavedifferentcolors

▪ Solutionsareassignmentssatisfyingallconstraints,e.g.:

Implicit:

Explicit:

Page 7: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Example:N-Queens

▪ Formulation1:▪ Variables:▪ Domains:

▪ Constraints

Page 8: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Example:N-Queens

▪ Formulation2:▪ Variables:

▪ Domains:

▪ Constraints:Implicit:

Explicit:

Page 9: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

ConstraintGraphs

Page 10: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

ConstraintGraphs

▪ BinaryCSP:eachconstraintrelates(atmost)twovariables

▪ Binaryconstraintgraph:nodesarevariables,arcsshowconstraints

▪ General-purposeCSPalgorithmsusethegraphstructuretospeedupsearch.E.g.,Tasmaniaisanindependentsubproblem!

Page 11: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Example:Cryptarithmetic

▪ Variables:

▪ Domains:

▪ Constraints:

Page 12: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Example:Sudoku

▪ Variables:

▪ Each(open)square

▪ Domains:

▪ {1,2,…,9}

▪ Constraints:

9-wayalldiffforeachrow

9-wayalldiffforeachcolumn

9-wayalldiffforeachregion

(orcanhaveabunchofpairwiseinequalityconstraints)

Page 13: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

VarietiesofCSPs

▪ DiscreteVariables▪ Finitedomains

▪ SizedmeansO(dn)completeassignments▪ E.g.,BooleanCSPs,includingBooleansatisfiability(NP-complete)

▪ Infinitedomains(integers,strings,etc.)▪ E.g.,jobscheduling,variablesarestart/endtimesforeachjob▪ Linearconstraintssolvable,nonlinearundecidable

▪ Continuousvariables▪ E.g.,start/endtimesforHubbleTelescopeobservations▪ LinearconstraintssolvableinpolynomialtimebyLPmethods

Page 14: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

VarietiesofConstraints

▪ VarietiesofConstraints▪ Unaryconstraintsinvolveasinglevariable(equivalenttoreducing

domains),e.g.:

▪ Binaryconstraintsinvolvepairsofvariables,e.g.:

▪ Higher-orderconstraintsinvolve3ormorevariables: e.g.,cryptarithmeticcolumnconstraints

▪ Preferences(softconstraints):▪ E.g.,redisbetterthangreen▪ Oftenrepresentablebyacostforeachvariableassignment▪ Givesconstrainedoptimizationproblems▪ (We’llignoretheseuntilwegettoBayes’nets)

Page 15: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Real-WorldCSPs

▪ Assignmentproblems:e.g.,whoteacheswhatclass▪ Timetablingproblems:e.g.,whichclassisofferedwhenandwhere?▪ Hardwareconfiguration▪ Transportationscheduling▪ Factoryscheduling▪ Circuitlayout▪ Faultdiagnosis▪ …lotsmore!

▪ Manyreal-worldproblemsinvolvereal-valuedvariables…

Page 16: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

SolvingCSPs

Page 17: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

StandardSearchFormulation

▪ StandardsearchformulationofCSPs

▪ Statesdefinedbythevaluesassignedsofar(partialassignments)▪ Initialstate:theemptyassignment,{}▪ Successorfunction:assignavaluetoanunassignedvariable

▪ Goaltest:thecurrentassignmentiscompleteandsatisfiesallconstraints

▪ We’llstartwiththestraightforward,naïveapproach,thenimproveit

Page 18: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

SearchMethods

▪ WhatwouldBFSdo?

▪ WhatwouldDFSdo?

Page 19: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Demo:DFSCSP

Page 20: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

SearchMethods

▪ WhatwouldBFSdo?

▪ WhatwouldDFSdo?

▪ Whatproblemsdoesnaïvesearchhave?

Page 21: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

BacktrackingSearch

Page 22: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

BacktrackingSearch

▪ BacktrackingsearchisthebasicuninformedalgorithmforsolvingCSPs

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

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

▪ Depth-firstsearchwiththesetwoimprovements iscalledbacktrackingsearch(notthebestname)

▪ Cansolven-queensforn≈25

Page 23: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

BacktrackingExample

Page 24: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

BacktrackingSearch

▪ Backtracking=DFS+variable-ordering+fail-on-violation

Page 25: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Demo:Backtracking

Page 26: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

ImprovingBacktracking

▪ General-purposeideasgivehugegainsinspeed

▪ Ordering:▪ Whichvariableshouldbeassignednext?▪ Inwhatordershoulditsvaluesbetried?

▪ Filtering:Canwedetectinevitablefailureearly?

▪ Structure:Canweexploittheproblemstructure?

Page 27: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

▪ Filtering:Keeptrackofdomainsforunassignedvariablesandcrossoffbadoptions

▪ Forwardchecking:Crossoffvaluesthatviolateaconstraintwhenaddedtotheexistingassignment

Filtering:ForwardChecking

WASANT Q

NSW

V

Page 28: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Demo:BacktrackingwithForwardChecking

Page 29: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Filtering:ConstraintPropagation

▪ Forwardcheckingpropagatesinformationfromassignedtounassignedvariables,butdoesn'tprovideearlydetectionforallfailures:

▪ NTandSAcannotbothbeblue!▪ Whydidn’twedetectthisyet?▪ Constraintpropagation:reasonfromconstrainttoconstraint

WASA

NT Q

NSW

V

Page 30: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

ConsistencyofASingleArc

▪ AnarcX→ Yisconsistentiffforeveryxinthetailthereissomeyintheheadwhichcouldbeassignedwithoutviolatingaconstraint

▪ Forwardchecking:Enforcingconsistencyofarcspointingtoeachnewassignment

Deletefromthetail!

WA SA

NT Q

NSW

V

Page 31: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

ArcConsistencyofanEntireCSP

▪ Asimpleformofpropagationmakessureallarcsareconsistent:

▪ Important:IfXlosesavalue,neighborsofXneedtoberechecked!▪ Arcconsistencydetectsfailureearlierthanforwardchecking▪ Canberunasapreprocessororaftereachassignment▪ What’sthedownsideofenforcingarcconsistency?

Remember:Deletefromthetail!

WA SA

NT Q

NSW

V

Page 32: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

EnforcingArcConsistencyinaCSP

▪ Runtime:O(n2d3),canbereducedtoO(n2d2)▪ …butdetectingallpossiblefutureproblemsisNP-hard–why?

Page 33: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Demo:Arcconsistency

Page 34: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

LimitationsofArcConsistency

▪ Afterenforcingarcconsistency:▪ Canhaveonesolutionleft▪ Canhavemultiplesolutionsleft

▪ Canhavenosolutionsleft(andnotknowit)

▪ Arcconsistencystillrunsinsideabacktrackingsearch! Whatwent

wronghere?

Page 35: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Ordering

Page 36: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Ordering:MinimumRemainingValues

▪ VariableOrdering:Minimumremainingvalues(MRV):▪ Choosethevariablewiththefewestlegalleftvaluesinitsdomain

▪ Whyminratherthanmax?

▪ Alsocalled“mostconstrainedvariable”

▪ “Fail-fast”ordering

Page 37: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Ordering:LeastConstrainingValue

▪ ValueOrdering:LeastConstrainingValue▪ Givenachoiceofvariable,choosetheleastconstrainingvalue

▪ I.e.,theonethatrulesoutthefewestvaluesintheremainingvariables

▪ Notethatitmaytakesomecomputationtodeterminethis!(E.g.,rerunningfiltering)

▪ Whyleastratherthanmost?

▪ Combiningtheseorderingideasmakes 1000queensfeasible

Page 38: lecture5 - cs.utexas.edu · Title: lecture5 Created Date: 9/17/2018 3:00:55 AM

Demo:Backtracking+ForwardChecking+Ordering