 So in our last video we were talking about sort of the limitations that a normal minimax search can do and we said specifically that they were Exhausted because I have to traverse all the possible nodes on each Possible route along the way, and I'm not going to draw them all out But you can see this can become very exponentially harder Because again, we run into a situation where again, yes, you can find the solution, but yeah, it took you 10 years to make one move bill So we don't want that instead we want to find ways that we can optimize this approach So with that in mind what we can look at is this notion of pruning searches Maybe for example if I ended up going down this route I knew I said I wouldn't if I found that. Oh, this is a super bad This is a super bad move. Well Don't even bother doing the rest of the searching just stop there and say well Hey, you know, this is not a good move at all Let's not even worry about considering that because I know that men would never even think about doing that move So let's not even bother searching it and that's helped save us a little bit of time But how we do this is through something known as Alpha beta pruning and in a sense what we're going to be doing is not only are we tracking sort of our Move, right? We're we're not just tracking our move anymore But we're also going to be tracking some alpha and some beta Values along the way and specifically what alpha is going to be tracking is whatever the largest value max has seen out of its children And then whatever and then beta is going to be tracking whatever the lowest value men has seen throughout its children And again, this is going to be useful for us because what that allows us to do is if we ever see sort of a bad move We can just halt it now. We do this by starting out with working through a few sort of starting points We're gonna start with making alpha negative infinity and beta positive infinity now The reason for this is again, if you're trying to implement this say for example, remember that you have sort of your disposal maybe some version of Something you can use as a proxy for a Positive and negative infinity so for example in Java while you don't have something that could be infinite You do have something that like integer dot max or min value and that's sort of a hint if you are using Java Either way again, what we're doing is we're going to be passing not only the move to our children, right? If I make sort of this move Right here. I'm also giving it so I have sort of an alpha that has a negative infinity in a beta That has a negative infinity or sorry a positive infinity and that's also getting passed as well So this node also now has a negative infinity and a positive infinity and so what we're going to be doing is we're only going to be updating these nodes Depending on which Person would be selecting this node so alpha only changes if we're at an alpha node and Beta only changes when we're at a beta or a min node again They're sort of tracking the best and worst moves so far now specifically how we sort of have a little bit more efficiency in our code is We're going to use a rule if at any point in time alpha is larger than or equal to beta We're going to abandon this search or this sort of Branch of our search because again, it's not worth it So we're just going to use this as sort of our our kind of basis point So again, if we're working through this, you know, again, I like to kind of use the the notions of The tribal pointing upward is for my max nodes and we are always starting off with a max again The notion here is that this is My move so I want to maximize my efforts then min is always the downward angle because again min is their move and their move is You know, they're going to be trying to do things worse than me So if I were to take this action, whatever this action turns out to be again I'm going to also pass down that alpha and That beta along the way and this will change as we start to reach our terminal nodes So again as we sort of see this in play This is where we do have to work a little algorithmically So we have to act like a robot for a second because remember this is recursion that's happening So this move occurred well then this move occurred then we see oh, we're at a terminal node So what happens well, we return terminal we return that three back to men and then men's going to do a Check well between my current beta and this new beta which one is better which one is smaller Three is smaller and so as a result Beta is going to change and become three. So now what happens? Well, we just keep iterating through our actions What if beta instead did this move well, that's a terminal state So we bring up that nine we do the same thing which one is better which one is smaller Well, it's still three so beta doesn't change and as you can already sort of guess We would do the same thing with that eight Now specifically what occurs afterwards well since we've traversed all of the nodes here This search is done. We've reached effectively our base. We've traversed all the nodes So we're done and after that traversal if you were kind of checking out the pseudo code Previously in the last video that means we're going to be passing up that three. So alpha or sorry not alpha Max sees a three as our possible move and so it's doing its own Evaluation which one of these is better. Well in this case three is Bigger than negative infinity. So three is better Now one thing that I had didn't really talk about but I do want to at least stress is along the way We were making the check is alpha greater than beta Right when we made this change for beta to three We did the same evaluation. It just resulted in a false because alpha is negative infinity, right? So we just never needed to like Stop searching but now as we move down into this next search Again alpha and beta have been changed at my parent node And so the result is that the alpha is going to be three and the beta is going to be positive infinity Notice how even though beta was three here. That's not changing here Again, it's because in a max node only alpha is changing Either way just to walk through this a little bit in a little more detail again We see that that three happens. We pass that three upward. We move that three downward So again as we're sort of making our traversal again This alpha and this beta is being traversed downward because this may not be a terminal node The algorithm doesn't know that until it gets there and let's say it got a two Well, again, if we're looking at this we traversed down. We saw a terminal node. We moved up We see the two We had a negative or sorry. We had a positive infinity there We then compared it to a two two was better beta became two But specifically notice that rule that I was just talking about We stop searching if at any point in time we run into alpha being greater than beta The reason why is begin because again if max takes this move, right again if Max takes This move Oh, well, this is no different than me like from a chess game exposing my king Too early into the game. That's a very bad move because I I have a lot of Vulnerability when I do that and so as a consequence, right? Oh, well, here's that we saw that if I did Take this exposing my king too early move or Unleashing my queen too early. I'm not good at chess Either way you can see. Oh, well, this is such a bad move if I took this. Well, then min is already going to be doing a worse move than three So from a algorithmic standpoint, again, we're trying to optimize that minimax search We're going to say, you know what? I already know that this is just a bad situation that I'm not going to play out and so I'm going to Not bother searching any more of the pathways down this route. I don't care Uh, it doesn't matter if you know, it it's one that'll win me, you know the world championship done care because I'll never get there because Men's gonna pick that too men's gonna do the checkmate and like, you know, it's gonna do the scholar move Thingy in chess and just win automatically and be done So we just say screw it We're done with the search We have exhausted again, this is uh, three at this time We just say, hey, you know what this node? We're just gonna consider it a two Not don't don't care. It could be worse, right? It could be even worse than a two But for our sake, we're just gonna evaluate it as a two We know that in max's decision between this action and this action Right three's gonna be at the best that we're going to get so far But again, we pass that up. We see that two, right? And then we ask the same question. Which one of these is better three or two Three is the Same one. So we don't see any changes going on there. And then that's going to get passed along the way So again, just to kind of reiterate what we've done here again We see the two we've made that update that two is stopping that we make any other searches We pass that two up alpha doesn't change because two is smaller than three and we pass that down again So now once again, we're dealing with uh, an alpha of a three and a beta of a positive infinity We take those and we're going to pass them down again Seeing that terminal state Okay, well we see that uh, we went down We move up And we're doing the comparison again because we're at a min node Which one of these is better? All right. Well, uh, clearly eight is much much smaller than positive infinity. So min And in their infinite wisdom would pick this eight move Okay, well, then we're going to go down to the next route We see a five. Well that five is getting passed up. Well, hey, you know that five Was much less than that eight. So min from min's perspective, that's a better move. So uh, that eight is no longer gonna happen And then once again, we we go down. We see that we're at a two. Oh, well, hey That two comes up, uh, five versus two Five is much less than two. So beta became two now for our sake We've traversed everything. So we're done, right, you know, technically this is happening And so if D from D there were there was another potential move whatever it might be, right? Uh, again, it could be negative a billion. It could be win the world championship It's a billion doesn't matter. We've already established that from what we've seen Min's gonna pick something worse than what is our current best target So we are not even going to make that as a consideration if it, you know, happened There's that and so again, we pass that to upward. So again beta at this point is a three Or sorry, b is a three. C is a two D Gets the value of a two. We pass that to we see that it's not better We see that we're at the end. And so what was the move that got us that three? It was this one ta-da So let's work through a second one. I'm gonna work through this as a another approach. So again Uh, pause the video for a second. See if you can solve it without my assistance Okay, I'm assuming you did that. So let's work through this again Alpha is being passed down as a negative infinity and beta is being passed down as a positive infinity That was this move then we move down We are technically passing it, but we're at a terminal state. So we see that we're gonna move up to the three We see that the three Is much smaller than beta And so we change it to a three. We are still asking that question. Is alpha greater than beta? No, because again alpha is negative infinity. So we're gonna keep on doing our search So we move down again. We see a terminal state. We move back up again eight versus three. We don't change because again eight Bigger than three. So we do the traversal again We see a seven we do that same song and dance. Are we Doing anything nope And so this node once again just like we saw in the last example We would say that this value is a three Then what well again, we're done with all the moves. So this branch this pathway is done So we're gonna pass that three up and we're gonna see. Oh, well is three if we look at the Alpha this time, which one of these is the bigger number? It's the three so alpha just became a three And when we then traverse the next potential action, we're passing down the alpha Of three and the beta Of positive infinity So again the same song and dance goes on we go down. We see a three we pass it up We do the comparison which one of these is better. Well, the three is so that gets Truncated or uh updated and guess what we've violated our We've established that we had a stopping rule alpha cannot be greater than or equal to three So we stop so again We are pruning out We don't ever bother doing this nine because even though it's much better than anything from the last pathway Min would never take it and then we just see this arbitrary move of a five Well, we're just not gonna take it and when I say take it. I mean we're not gonna search it remember We're planning right now. So I'm just not going to look down these pathways because again I've already found a pathway that min is gonna perform at the same level right again Doesn't matter. We're just from a searching standpoint not even gonna consider them Then what all right? Well, we pass up the three. We see three versus three. They're not, you know This move or this original move was still the same. It's our de facto right now And so then we once again pass down. So we get our alpha at three We get our beta once again at positive infinity We move down. We see a four. We pass that four up Well four is much better than positive infinity. So we update And then what well again, we're passing down. We're passing back up And we see that three versus four. So which one of these is smaller? It's the three And so once again, hey, remember, you know, oh, we have our rule alpha Cannot be greater than or equal to beta if it is don't bother doing any of the searches They're only potentially going to get worse from here. So from our sake we say I'm not even gonna bother doing this search Because I don't care. I don't I don't care what it is. It may be worse So just to update this this c is also a three and finally we establish that this d is a three We pass that up. We see that the three is still, you know, again, we're still dealing with Our alpha is already established as a three And so we are going to ultimately take this pathway Here Now you may be asking yourself, but all of the other moves, you know had threes and, you know, technically this Had some better routes while the big again the thing we're looking at is What if it didn't, right? We don't know. We're still in that searching process We're still iterating through all the possible children. So even if it did, right? I know for a fact that this is sort of the best way Everything else is either as good again as good or potentially worse We don't know and if you're thinking, hey, well, you know, this route also leads into some other good ones What I say to that is reevaluate what your your evaluations are, right? Again, how you evaluate your terminal nodes What I'm saying for my example here is again This three this three they are equal to each other in all possible situations So again, if you're saying that there may be some other Potential moves like that nine, what if min doesn't pick optimally? Yes, well, you do the search and you still kind of follow the strategy of let me Maximize my potential search ways, but that's again, that's more finicky Then you imagine if you follow sort of this approach, it does sort of work out quite nicely And in fact, if we sort of look at sort of an example deep thought not from hitchhiker's guide, but the actual chess Playing robot the original, uh, I don't know the the current You know AI, you know winner, but deep thought was sort of the the, you know, big one from back in the day Specifically how it worked was it would think for our 10 to 14 moves ahead So you can imagine I well if you're kind of looking at this from a game AI perspective, maybe you can design out Modes so like an easy mode would be I only think or the agent only thinks three moves ahead You know as an opponent right not you because you're you're wanting to play against AI Uh, the opponent is only going to think three moves ahead And then if you want to increase the difficulty they think six moves ahead And if they want to think hard mode, right they go nine moves ahead if you want to build out a dark souls mode I don't know it thinks 20 moves ahead. I I don't know, uh, however At least for that sake, okay, so we've talked about alpha beta minimax pruning But you need to implement it and that is what we're going to be talking about in the next video Here's a little sneak peek if you just want to cheat and use this and not have Watch that video. But again, we'll go through and explicitly talk about well, what does operator mean? What does Best move mean? What does depth limit mean? So stay tuned and I'll see you in the next one