Vous êtes sur la page 1sur 21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

CS161RecitationNotesMinimaxwithAlpha
BetaPruning
Theminimaxalgorithmisawayoffindinganoptimalmoveinatwoplayergame.
Alphabetapruningisawayoffindingtheoptimalminimaxsolutionwhileavoiding
searchingsubtreesofmoveswhichwon'tbeselected.Inthesearchtreeforatwo
playergame,therearetwokindsofnodes,nodesrepresentingyourmovesandnodes
representingyouropponent'smoves.Nodesrepresentingyourmovesaregenerally
drawnassquares(orpossiblyupwardpointingtriangles):
ThesearealsocalledMAXnodes.ThegoalataMAXnodeisto
maximizethevalueofthesubtreerootedatthatnode.Todothis,
aMAXnodechoosesthechildwiththegreatestvalue,andthat
becomesthevalueoftheMAXnode.
Nodesrepresentingyouropponent'smovesaregenerallydrawnascircles(or
possiblyasdownwardpointingtriangles):
ThesearealsocalledMINnodes.ThegoalataMINnodeisto
minimizethevalueofthesubtreerootedatthatnode.Todothis,
aMINnodechoosesthechildwiththeleast(smallest)value,and
thatbecomesthevalueoftheMINnode.
Alphabetapruninggetsitsnamefromtwoboundsthatarepassedalongduringthe
calculation,whichrestrictthesetofpossiblesolutionsbasedontheportionofthe
searchtreethathasalreadybeenseen.Specifically,

Betaistheminimumupperboundofpossiblesolutions
Alphaisthemaximumlowerboundofpossiblesolutions
Thus,whenanynewnodeisbeingconsideredasapossiblepathtothesolution,it
canonlyworkif:
whereNisthecurrentestimateofthevalueofthenode.
Tovisualizethis,wecanuseanumberline.Atanypointintime,alphaandbetaare
http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

1/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

lowerandupperboundsonthesetofpossiblesolutionvalues,likeso:

Astheproblemprogresses,wecanassumerestrictionsabouttherangeofpossible
solutionsbasedonminnodes(whichmayplaceanupperbound)andmaxnodes
(whichmayplacealowerbound).Aswemovethroughthesearchtree,thesebounds
typicallygetcloserandclosertogether:

Thisconvergenceisnotaproblemaslongasthereissomeoverlapintherangesof
alphaandbeta.Atsomepointinevaluatinganode,wemayfindthatithasmoved
oneoftheboundssuchthatthereisnolongeranyoverlapbetweentherangesof
alphaandbeta:

Atthispoint,weknowthatthisnodecouldneverresultinasolutionpaththatwe
willconsider,sowemaystopprocessingthisnode.Inotherwords,westop
generatingitschildrenandmovebacktoitsparentnode.Forthevalueofthisnode,
weshouldpasstotheparentthevaluewechangedwhichexceededtheotherbound.
Todemonstrateminimaxwithalphabetapruning,weusethefollowingminimax
treeasanexample:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

2/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Forthepurposesofthisminimaxtutorial,thistreeisequivalenttothelist
representation:
(
(((317)(212))((15)(250)))
(((25)(3))((214)))
)

Asanaside,ifthiswerearealworldminimaxproblem,youwouldn'thavethetree
allpregeneratedlikethat.Ifyou'vealreadywastedthespacetogenerateallthe
statesandthetimetocalculatealltheevaluationvaluesatthedetermineddepth,
thenthetimetodominimaxisnegligible.Thealphabetapruningismeanttoavoid
havingtogenerateallthestatesandcalculatealltheevaluationfunctions.Checkthis
pageoutifyouwantsomenotesonhowarealworldversionofminimaxwithalpha
betapruningwouldvaryfromyourversion.
Fortherestofthisexample,I'llshowonlythepartofthetreethat'sbeenevaluated
sofaroriscurrentlybeingevaluated.I'llalsodescribethebehaviorasifthiswerea
situationwhereyouweregeneratingthechildstatesinsteadofjusttraversingthe
treethat'sgiventoyou.Inthatspirit,we'retryingtofindthebestmovebylooking
aheadtwofullmoves(i.e.twomoveseachmymeandmyopponent).Thuswewillgo
toadepthof4inthetree,thenevaluatethestate.
Atthestartoftheproblem,youseeonlythecurrentstate(i.e.thecurrentpositionof
piecesonthegameboard).Asforupperandlowerbounds,allyouknowisthatit'sa
numberlessthaninfinityandgreaterthannegativeinfinity.Thus,here'swhatthe
initialsituationlookslike:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

3/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

whichisequivalentto

Sincetheboundsstillcontainavalidrange,westarttheproblembygeneratingthe
firstchildstate,andpassingalongthecurrentsetofbounds.Atthispointoursearch
lookslikethis:

We'restillnotdowntodepth4,soonceagainwegeneratethefirstchildnodeand
passalongourcurrentalphaandbetavalues:

Andonemoretime:

Whenwegettothefirstnodeatdepth4,werunourevaluationfunctiononthestate,
andgetthevalue3.Thuswehavethis:
http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

4/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Wepassthisnodebacktotheminnodeabove.Sincethisisaminnode,wenow
knowthattheminimaxvalueofthisnodemustbelessthanorequalto3.Inother
words,wechangebetato3.

Notethatthealphaandbetavaluesathigherlevelsinthetreedidn'tchange.When
processingactuallyreturnstothosenodes,theirvalueswillbeupdated.Thereisno
realgaininproagatingthevaluesupthetreeifthereisachancetheywillchange
againinthefuture.Theonlypropagationofalphaandbetavaluesisbetweenparent
andchildnodes.
Ifweplotalphaandbetaonanumberline,theynowlooklikethis:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

5/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Nextwegeneratethenextchildatdepth4,runourevaluationfunction,andreturna
valueof17totheminnodeabove:

Sincethisisaminnodeand17isgreaterthan3,thischildisignored.Nowwe've
seenallofthechildrenofthisminnode,sowereturnthebetavaluetothemaxnode
above.Sinceitisamaxnode,wenowknowthatit'svaluewillbegreaterthanor
equalto3,sowechangealphato3:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

6/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Noticethatbetadidn'tchange.Thisisbecausemaxnodescanonlymakerestrictions
onthelowerbound.Furthernotethatwhilevaluespasseddownthetreearejust
passedalong,theyaren'tpassedalongonthewayup.Instead,thefinalvalueofbeta
inaminnodeispassedontopossiblychangethealphavalueofitsparent.Likewise
thefinalvalueofalphainamaxnodeispassedontopossiblychangethebetavalue
ofitsparent.
Atthemaxnodewe'recurrentlyevaluating,thenumberlinecurrentlylookslike
this:

Wegeneratethenextchildandpasstheboundsalong:

Sincethisnodeisnotatthetargetdepth,wegenerateitsfirstchild,runthe
evaluationfunctiononthatnode,andreturnit'svalue:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

7/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Sincethisisaminnode,wenowknowthatthevalueofthisnodewillbelessthanor
equalto2,sowechangebetato2:

Thenumberlinenowlookslikethis:

Asyoucanseefromthenumberline,thereisnolongeranyoverlapbetweenthe
regionsboundedbyalphaandbeta.Inessense,we'vediscoveredthattheonlyway
wecouldfindasolutionpathatthisnodeisifwefoundachildnodewithavalue
http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

8/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

thatwasbothgreaterthan3andlessthan2.Sincethatisimpossible,wecanstop
evaluatingthechildrenofthisnode,andreturnthebetavalue(2)asthevalueofthe
node.
Admittedly,wedon'tknowtheactualvalueofthenode.Therecouldbea1or0or
100somewhereintheotherchildrenofthisnode.Buteveniftherewassucha
value,searchingforitwon'thelpusfindtheoptimalsolutioninthesearchtree.The
2aloneisenoughtomakethissubtreefruitless,sowecanpruneanyotherchildren
andreturnit.

That'sallthereistobetapruning!
Backattheparentmaxnode,ouralphavalueisalready3,whichismorerestrictive
than2,sowedon'tchangeit.Atthispointwe'veseenallthechildrenofthismax
node,sowecansetitsvaluetothefinalalphavalue:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

9/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Nowwemoveontotheparentminnode.Withthe3forthefirstchildvalue,we
knowthatthevalueoftheminnodemustbelessthanorequalto3,thuswesetbeta
to3:

Nowthegraphofalphaandbetaonanumberlinelookslikethis:

Sincewestillhaveavalidrange,wegoontoexplorethenextchild.Wegeneratethe
maxnode...
http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

10/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

...it'sfirstchildminnode...

...andfinallythemaxnodeatthetargetdepth.Allalongthispath,wemerelypass
thealphaandbetaboundsalong.

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

11/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Atthispoint,we'veseenallofthechildrenoftheminnode,andwehaven'tchanged
thebetabound.Sincewehaven'texceededthebound,weshouldreturntheactual
minvalueforthenode.Noticethatthisisdifferentthanthecasewherewepruned,
inwhichcaseyoureturnedthebetavalue.Thereasonforthiswillbecomeapparent
shortly.

Nowwereturnthevaluetotheparentmaxnode.Basedonthisvalue,weknowthat
thismaxnodewillhaveavalueof15orgreater,sowesetalphato15:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

12/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Nowthegraphofalphaandbetaonanumberlinelookslikethis:

Onceagainthealphaandbetaboundshavecrossed,sowecanprunetherestofthis
node'schildrenandreturnthevaluethatexceededthebound(i.e.15).Noticethatif
wehadreturnedthebetavalueofthechildminnode(3)insteadoftheactualvalue
(15),wewouldn'thavebeenabletoprunehere.

Nowtheparentminnodehasseenallofit'schildren,soitcanselecttheminimum
http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

13/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

valueofit'schildren(3)andreturn.

Finallywe'vefinishedwiththefirstchildoftherootmaxnode.Wenowknowour
solutionwillbeatleast3,sowesetthealphavalueto3andgoontothesecondchild.

Passingthealphaandbetavaluesalongaswego,wegeneratethesecondchildofthe
rootnode...

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

14/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

...anditsfirstchild...

...anditsfirstchild...

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

15/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

...anditsfirstchild.Nowweareatthetargetdepth,sowecalltheevaluation
functionandget2:

Theminnodeparentusesthisvaluetosetit'sbetavalueto2:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

16/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Nowthegraphofalphaandbetaonanumberlinelookslikethis:

Onceagainweareabletoprunetheotherchildrenofthisnodeandreturnthevalue
thatexceededthebound.Sincethisvalueisn'tgreaterthanthealphaboundofthe
parentmaxnode,wedon'tchangethebounds.

Fromhere,wegeneratethenextchildofthemaxnode:
http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

17/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Thenwegenerateitschild,whichisatthetargetdepth.Wecalltheevaluation
functionandgetitsvalueof3.

Theparentminnodeusesthisvaluetosetitsupperbound(beta)to3:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

18/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Atthispointthenumberlinegraphofalphaandbetalookslikethis:

Inotherwords,atthispointalpha=beta.Shouldweprunehere?Wehaven't
actuallyexceededthebounds,butsincealphaandbetaareequal,weknowwecan't
reallydobetterinthissubtree.
Theanswerisyes,weshouldprune.Thereasonisthateventhoughwecan'tdo
better,wemightbeabletodoworse.Remember,thetaskofminimaxistofindthe
bestmovetomakeatthestaterepresentedbythetoplevelmaxnode.Asithappens
we'vefinishedwiththisnode'schildrenanyway,sowereturntheminvalue3.

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

19/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Themaxnodeabovehasnowseenallofitschildren,soitreturnsthemaximum
valueofthoseithasseen,whichis3.

Thisvalueisreturnedtoitsparentminnode,whichthenhasanewupperboundof
3,soitsetsbetato3:

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

20/21

10/16/2015

CS161RecitationNotesMinimaxwithAlphaBetaPruning

Nowthegraphofalphaandbetaonanumberlinelookslikethis:

Onceagain,we'reatapointwherealphaandbetaaretied,soweprune.Notethata
realsolutiondoesn'tjustindicateanumber,butwhatmoveledtothatnumber.
Ifyouweretorunminimaxonthelistversionpresentedatthestartoftheexample,
yourminimaxwouldreturnavalueof3and6terminalnodeswouldhavebeen
examined.

http://web.cs.ucla.edu/~rosen/161/notes/alphabeta.html

21/21

Vous aimerez peut-être aussi