CSCI 360 Introduc*on to Ar*ficial Intelligence

CSCI360
Introduc/ontoAr/ficialIntelligence
Week2:ProblemSolvingandOp/miza/on
ProfessorWei-MinShen
Week3.2
StatusCheckandReview
•  Statuscheck
–  Howisyourhomeworkgoing?
–  Haveyoureadtoday’schapters?
•  Reviewoflastlecture
–  GamePlaying
•  Whatistoday’slecture,doyouknow?
–  CSP,COP,andmaybeLogic
ReviewLastLecture(GamePlay)
•  Keyidea:op/miza/onforyouandyouropponent
•  Whenyoucanseethewholegame(unlimitedresource)
– 
– 
– 
– 
– 
Example,Tic-tac-toe(fig5.1)
Op/mal:CanIalwayswin,orneverloss?
Key:lookaheadtotheend,chosethebest
Minmaxalgorithm
Alpha-betapruning
•  Whencannotseeallthewaytotheend(limitedresource)
–  Howfartolook?(lookaheadtoalevel)
–  Howgoodisastate?
–  Canyoupredictyouropponent?
•  Whatifthereisarandomplayerorelement?
–  Usethe“expectedvalue”basedontheminmaxvalues
–  Israndomplayinganygood?
•  WhatarethebestAIgameplayers?(Goisbea/nghumanplayers)
Today’sLecture
•  Gameplaying
•  ConstraintSa/sfactory
•  Logic(maybe)
ConstraintSa/sfac/on
•  Examples:MapColoring,canyouthinkofanother?
•  Howtoformalizetheproblem?
–  Search:variables,domains,constrainedrela/ons
–  Dothesubsetindependent?
–  Doitcontaincycles?
•  Howtosolvetheproblem?(Randomlyor“Best”-First)
–  GlobalBacktracking
•  Ordervariables:themostconstrainedvariablesfirst
•  Sa/sfyoneata/me,ifstuck,backtracking
–  LocalSearch:heuris/c,findthemin-conflictssolu/onlocally
–  Centralizedvs.Distributed(DCOP)
•  HW:Magicsquare,defineitandsolveit
Example:Map-Coloring
6
ConstraintSa/sfac/on
•  Examples:MapColoring,canyouthinkofanother?
•  Howtoformalizetheproblem?
–  Search:variables,domains,constrainedrela/ons
–  Dothesubsetindependent?
–  Doitcontaincycles?
•  Howtosolvetheproblem?(Randomlyor“Best”-First)
–  GlobalBacktracking
•  Ordervariables:themostconstrainedvariablesfirst
•  Sa/sfyoneata/me,ifstuck,backtracking
–  LocalSearch:heuris/c,findthemin-conflictssolu/onlocally
–  Centralizedvs.Distributed(DCOP)
•  HW:Magicsquare,defineitandsolveit
ConstraintProblems
•  Howtoformallydefinetheproblem?
–  Variables
–  Domains
–  Constraints
•  Whatdowewanttoachieve?
–  Sa/sfiedSolu/ons
–  Op/mizedSolu/ons
8
Example:Map-Coloring
Note:3colors(not2,4,…,why?)
9
DifferentFormula/on:N-Queens
§  Formula/on1:
§  Variables(forposi/on):
§  Domains(foroccupy):
§  Constraints
10
DifferentFormula/on:N-Queens
§  Formula/on2:
§  Variables:
§  Domains:
§  Constraints:
Implicit:
-or-
Explicit:
11
MapColoring
•  Howtoformallydefinetheproblem?
–  Variables=?
–  Domains=?
–  Constraints=?
•  Whatdowewanttoachieve?
–  Sa/sfiedSolu/ons
–  Op/mizedSolu/ons
12
TypesofConstraints
•  Binary:Eachconstraintrelatesatmosttwovariables
•  Mul/ple:Constraintswith>2variables
•  ConstraintGraph:nodesarevariables,arcsshowconstraints
•  Aconstraintcaneitherbe“Sa/sfied”or“havingadegree/cost”
•  GeneralCSPalgorithmsusethegraphstructuretospeedupsearch
–  E.g.,Tasmaniaisanindependentsub-problem!(noconstraintswithothers)
13
ComplexConstraints:Cryptarithme/c
•  Variables:F, T, U, W, R, O, X1, X2, X3
•  Domain:{0,1,2,3,4,5,6,7,8,9}
•  Constraints
all-diff (F, T, U, W, R, O)
O+O=R+10·X1
X1+W+W=U+10·X2
X2+T+T=O+10·X3
X3=F
Everyhigher-orderfinite
constraintcanbebroken
intonbinaryconstraints,
givenenoughauxiliary
constraints
14
ComplexConstraints:Sudoku
§  Variables:
§  Each(open)square
§  Domains:
§  {1,2,…,9}
§  Constraints:
9-wayall-diffforeachcolumn
9-wayall-diffforeachrow
9-wayall-diffforeachregion
15
MapColoring
•  Howtoformallydefinetheproblem?
–  Variables=?
–  Domains=?
–  Constraints=?
•  Whatdowewanttoachieve?
–  Sa/sfiedSolu/ons
–  Op/mizedSolu/ons
16
“Sa/sfying”Solu/ons
(allsolu/onsareequal)
17
Solu/onshave“quali/es”
(e.g.,the“bigger”statespreferthe“deeper”colors)
Canyoufinda
berersolu/on
thanthisone?
18
ConstraintsSa/sfac/on(CSP)
•  Standardsearchproblems:
§  Stateisa“blackbox”:arbitrarydatastructure
§  Goaltest:anyfunc/onoverstates
§  Successorfunc/oncanbeanything
§  Constraintsa/sfac/onproblems(CSPs):
§  Aspecialsubsetofsearchproblems
§  StateisdefinedbyvariablesXiwithvaluesfromadomainD
(some/mesDdependsoni)‫‏‬
§  Goaltestisasetofconstraintsspecifyingallowable
combina/onsofvaluesforsubsetsofvariables
§  Simpleexampleofaformalrepresenta-onlanguage
§  Allowsusefulgeneral-purposealgorithmswithmorepower
thanstandardsearchalgorithms
19
ConstraintOp/miza/on(COP)
§  ConstraintOp/miza/onproblems(COPs):
§  StateisdefinedbyvariablesXiwithvaluesfromadomainD
§  Goaltestisasetofconstraintsspecifyingallowablecombina/onsofvaluesfor
subsetsofvariables
§  Eachsolu/onhasacost:sum[f(x,y)],seekforthebestsolu/on
§  ReadingMaterial
§  Modi,P.J.,W.-M.Shen,M.Tambe,andM.Yokoo.ADOPT:AsynchronousDistributed
ConstraintOp/miza/onwithQualityGuarantees.Ar-ficialIntelligenceJournal,
161(1-2):149–180,January2005
20
Varie/esofCSPs
•  Discretevariables
–  Finitedomains;sizedèO(dn)completeassignments
•  e.g.,BooleanCSPs,incl.Booleansa/sability(NP-complete)
–  Infinitedomain(e.g.,integers,strings,etc.)
•  e.g.,jobscheduling,variablesarestart/enddaysforeachjob
•  needaconstraintlanguage,e.g.,StartJob1+5≤StartJob3
•  linearconstraintssolvable,nonlinearundecidable
•  Con/nuousvariables
–  e.g.,start/end/mesforHubbleTelescopeobserva/ons
–  Linearconstraintssolvableinpoly/mebyLPmethods
21
Varie/esofconstraints
•  Unaryconstraintsinvolveasinglevariable,
–  e.g.,SA≠green
•  Binaryconstraintsinvolvepairsofvariables,
–  e.g.,SA≠WA
•  Higher-orderconstraintsinvolve3ormorevariables,
–  e.g.,cryptarithme/ccolumnconstraints
•  Preferences(so}constraints),e.g.,redisbererthangreen
o}enrepresentablebyacostforeachvariableassignment
àconstrainedop/miza/onproblems
22
Real-worldCSPs
•  Assignmentproblems
–  e.g.,whoteacheswhatclass
•  Timetablingproblems
–  e.g.,whichclassisofferedwhenandwhere?
• 
• 
• 
• 
• 
Hardwareconfigura/on
Spreadsheets
Transporta/onscheduling
Factoryscheduling
Floor-planning
•  No/cethatmanyreal-worldproblemsinvolvereal-valuedvariables
23
ConstraintSa/sfac/on
•  Examples:MapColoring,canyouthinkofanother?
•  Howtoformalizetheproblem?
–  Search:variables,domains,constrainedrela/ons
–  Dothesubsetindependent?
–  Doitcontaincycles?
•  Howtosolvetheproblem?(Randomlyor“Best”-First)
–  GlobalBacktracking
•  Ordervariables:themostconstrainedvariablesfirst
•  Sa/sfyoneata/me,ifstuck,backtracking
–  LocalSearch:heuris/c,findthemin-conflictssolu/onlocally
–  Centralizedvs.Distributed(DCOP)
•  HW:Magicsquare,defineitandsolveit
StandardSearchFormula/on
•  Let'sstartwiththestraigh•orward,dumbapproach,thenimproveitlater
•  Statesaredefinedbythevaluesassignedsofar
–  Ini/alstate:theemptyassignment,{}
–  Successorfunc/on:assignavaluetoanunassignedvariablethatdoesnotconflict
withcurrentassignment.
èfailifnolegalassignments(notfixable!)
–  Goaltest:thecurrentassignmentiscomplete
1)ThisisthesameforallCSPs!
2)Everysolu/onappearsatdepthnwithnvariables
èUserandomsearch?Usedepth-firstsearch
3)Pathisirrelevant,socanalsousecomplete-stateformula/on
4)b=(n-l)datdepthl,hencen!dnleaves!!!!
25
BacktrackingSearch
•  Variableassignmentsarecommuta/ve,i.e.,
[WA=red thenNT=green]sameas[NT=greenthenWA=red]
•  Onlyneedtoconsiderassignmentstoasinglevariableateach
node
èb=dandtherearednleaves
•  Depth-firstsearchforCSPswithsingle-variableassignmentsis
calledbacktrackingsearch
•  BacktrackingsearchisthebasicuninformedalgorithmforCSPs
•  Cansolven-queensforn≈25
26
BacktrackingSearch
27
BacktrackingExample
28
ImprovingBacktrackEfficiency
•  General-purposemethodscangivehugegainsinspeed:
1.Whichvariableshouldbeassignednext?
2.Inwhatordershoulditsvaluesbetried?
3.Canwedetectinevitablefailureearly?
4.Canwetakeadvantageofproblemstructure?
Similartothe“strategy”fornodeexpansioninresearchalgorithms
29
MinimumRemainingValues
•  Minimumremainingvalues(MRV):
–  Choosethevariablewiththefewestlegalvalues
30
DegreeHeuris/c
•  Tie-breakeramongMRVvariables
•  Degreeheuris/c:
Choosethevariablewiththemostconstraintsonremainingvariables
31
LeastConstrainingValue
•  Givenavariable,choosetheleastconstrainingvalue:
–  theonethatrulesoutthefewestvaluesintheremainingvariables
•  Combiningtheseheuris/csmakes1000queensfeasible
32
ForwardChecking
• 
• 
Idea:Keeptrackofremaininglegalvaluesforunassignedvariables
Terminatesearchwhenanyvariablehasnolegalvalues
33
ConstraintPropaga/on
Forwardcheckingpropagatesinforma/onfromassignedtounassignedvariables,but
doesn'tprovideearlydetec/onforallfailures
• 
• 
NTandSAcannotbothbeblue!
Constraintpropaga/onrepeatedlyenforcesconstraintslocally
34
ArcConsistency
•  Simplestformofpropaga/onmakeseacharcconsistent
•  XàY isconsistentiff
foreveryvaluexofXthereissomeallowedy for Y
35
ArcConsistency
•  Simplestformofpropaga/onmakeseacharcconsistent
•  XàY isconsistentiff
foreveryvaluexofXthereissomeallowedy for Y
IfXlosesavalue,neighborsofXneedtoberechecked
36
ArcConsistency
•  Simplestformofpropaga/onmakeseacharcconsistent
•  XàY isconsistentiff
foreveryvaluexofXthereissomeallowedy for Y
IfXlosesavalue,neighborsofXneedtoberechecked
Arcconsistencydetectsfailureearlierthanforwardchecking
Canberunasapreprocessorora}ereachassignment
37
ArcConsistencyAlgorithm
38
CanWeSimplifyaProblem?
•  Doesithavesub-problemsthatareindependent?
–  Tasmaniaandmainlandareindependentsubproblems
•  Iden/fiableasconnectedcomponentsofconstraintgraph
39
ProblemStructure
Supposeeachsubproblemhascvariablesoutof
ntotal
Worst-casesolu/oncostisn/c·dc,linearinn
•  E.g.,n=80,d=2,c=20
•  280=4billionyearsat10millionnodes/sec
•  4·220=0.4secondsat10millionnodes/sec
40
Tree-StructuredCSPs
•  Theorem:iftheconstraintgraphhasnoloops,theCSPcanbesolvedin
O(n·d2)/me
•  ComparetogeneralCSPs,whereworst-case/meisO(dn)
•  Thispropertyalsoappliestologicalandprobabilis/creasoning:
–  animportantexampleoftherela/onbetweensyntac/crestric/onsandthecomplexity
ofreasoning.
41
AlgorithmforTree-StructuredCSPs
1.Chooseavariableasroot,ordervariablesfromroottoleavessuchthateverynode's
parentprecedesitintheordering
2.Forjfromndownto2,applyREMOVEINCONSISTENT(Parent(Xj); Xj)
3.Forjfrom1ton,assignXjconsistentlywithParent(Xj)
42
NearlyTree-StructuredCSPs
• 
Condi/oning:instan/ateavariable,pruneitsneighbors'domains
• 
Cutsetcondi/oning:instan/ate(inallways)asetofvariablessuchthatthe
remainingconstraintgraphisatree
Cutsetsizecèrun/meO(dc·(n-c)d2),veryfastforsmallc
• 
43
Itera/veAlgorithmsforCSPs
•  Hill-climbing,simulatedannealingtypicallyworkwith
“complete”states,i.e.,allvariablesassigned
•  ToapplytoCSPs:
–  allowstateswithunsa/sfiedconstraints
–  operatorsreassignvariablevalues
•  Variableselec/on:randomlyselectanyconflictedvariable
•  Valueselec/onbymin-conflictsheuris/c:
–  choosevaluethatviolatesthefewestconstraints
–  i.e.,hillclimbwithh(n)=totalnumberofviolatedconstraints
44
Example:4-Queens
• 
• 
• 
• 
States:4queensin4columns(44=256states)
Operators:movequeenincolumn
Goaltest:noaracks
Evalua/on:h(n)=numberofaracks
45
Performanceofmin-conflicts
•  Givenrandomini/alstate,cansolven-queensinalmostconstant/mefor
arbitrarynwithhighprobability
–  (e.g.,n=10,000,000)
•  Thesameappearstobetrueforanyrandomly-generatedCSPexceptina
narrowrangeofthera/o
46
Summary
•  CSPsareaspecialkindofSearchproblem:
–  Statesaredefinedbyvaluesofafixedsetofvariables
–  Goal-testsaredefinedbyconstraintsonvariablevalues
Backtracking=depth-firstsearchwithonevariableassignedpernode
Variableorderingandvalueselec/onheuris/cshelpsignificantly
Forwardcheckingpreventsassignmentsthatguaranteelaterfailure
Constraintpropaga/on(e.g.,arcconsistency)doesaddi/onalworkto
constrainvaluesanddetectinconsistenciesearly
•  CSPrepresenta/onallowsanalysis/simplifica/onofproblemstructure
• 
• 
• 
• 
–  Solvingindependentsub-problemsfirst
–  Tree-structuredCSPscanbesolvedinlinear/me
–  Itera/vemin-conflictsisusuallyeffec/veinprac/ce
47
AnexamplethatusesLogic
•  Weleavethisexamplelastbeforeitrequires
theuseoflogic
Example:BooleanSa/sfiability
§  GivenaBooleanexpression,isitsa/sfiable?
§  Verybasicproblemincomputerscience
§  Turnsoutyoucanalwaysexpressin3-CNF
§  3-SAT:findasa/sfyingtruthassignment
49
Example:3-SAT
§  Variables:
§  Domains:
§  Constraints:
Implicitly
conjoined(all
clausesmust
besa-sfied)‫‏‬
50