 So in our last video, we were talking about one of the sort of limitations of Just working off of hill climbing that issue was specifically if I was at sort of a local maxima I would never go to any potential move that was worse for me And so that's actually where we introduced that idea of maybe we want to attempt to control Random and what I mean by that is again if we see that we have a better move if we're Trying to maximize and we see something that makes our value go up. We obviously take that step but again to sort of resolve this issue of No possible moves What we could do is we could utilize Sometimes going down because sometimes going down Right may lead us to some bottom, but that may lead us to some new Better maxima that we've not yet discovered and it so sort of you know again goes the opposite way So when we're trying to minimize yes, we want to always go down But sometimes we want to go up and that actually introduces us to what we call meta heuristics or You often all see the term Biologically inspired AI And the entire idea here is looking at something that we see out there in the world as sort of a natural occurrence and saying hey that might work and So meta heuristics big fancy five dollar word It's just sort of this umbrella term to really kind of talk about all those things that we see out in nature and For at least this first video what I'm gonna focus in on is this idea of simulated annealing now We'll come and we'll explore some of these later on but again what we're just sort of doing is saying Oh, well, maybe there are other heuristics that we could work off of So the best way to think about again that idea of simulated annealing that is another big fancy five dollar word Where's it come from? Well, specifically what we do is we think about it like it's metallurgy Again, if we're thinking about this, we're a we're a blacksmith. We're in a forge You know, we're heating up and banging the swords and what's happening as that whole process is going on if I were to draw sort of the edge of our blade for a moment Right is all of the molecules and atoms that are going on as the blade is being heaten up They're moving very erratically very fast And so what's technically sort of going on there is this idea that you know We may be seeing some bad positions in those molecules, but that's not just the only thing that happens when we start to Work off of you know blacksmithing What we do is then we take that We port That's the best bucket. You'll see Into a bucket of water What happens when we do that is that really hot metal gets quenched into that water and all of those molecules are going to slow down And what happens sort of in that process is we can think about it as very similar to sort of a crystallization process Because the molecules have sort of moved all over the place and they crystallize because the temperatures gotten so low they're locked into place and That's actually gonna allow us sort of to carry that thought with our Optimization problem we can think about sort of this idea of sometimes going down when we had That's not how you start a V You can think about when we sometimes go down is because of just a very high temperature high volatility We're letting the molecules do their dance But then as temperature starts to decline we stop doing the sometimes and only work off of that good times So we allow those worst solutions to happen, but we don't always do them So how do we start to kind of take a look at this? Well, we're gonna start to look at that objective function one more time We still work off of the idea of sort of that F, but now we're gonna call it energy Big ol fit. That's not big old fancy E And so what we're looking at when we start to compare our state our state With a next possible state is that we're looking at this idea of the delta E or change in E As you can sort of see here what we can do with that is just a very simple subtraction So taking whatever that next was going to be or what that next is going to be Minus what we currently are and if that is a larger than zero number That's a good move that distance is a positive move in the right direction And so from our perspective, that's where we look at that always Go up Again probability of going up is one meaning. Yes, we do it But more specifically again, we're trying to look at when We're dealing with that sometimes going down and that's when we get a move that is not so good We take our next and then we subtract our current position and we still get a delta E But what if that delta E is negative? Well in that case again, if we're thinking about it, that's not a good step forward Not a good step forward But we still want to accept it because we're working off of that idea that right now We're in a highly volatile situation and the step is okay How we do that is we determine the probability through E raised to the power of delta Energy over what our current temperature is at this given time step Lot of words lot of stuff going on there right and so the big idea here is again What's happening is that this suddenly becomes proportional to the energy difference. So as this Energy difference occurs If it's a large distance, it's not a good move and it's a really really bad move But if it's a super small, you know difference, that's not so bad And then secondly, we've got the idea of T. T is working by going down, right? I want my T my T started at say for example 100 I want to gradually reach maybe one or zero. Well, you can't divide by zero So gradually go down to one and when that's occurring this number Is going to get much lower and we'll see sort of an example of that in just a bit But just to kind of help calm your minds a little bit All we need to do is if we're trying to minimize instead is all we have to do is flip Which one is subtracted first or from the other one? So to take a look at this, there's a very simple pseudocode going on here The first part is again, if we're thinking about sort of my prior video What we're doing is not a search tree Right, we're not doing this. We're going through a loop. Well, how often does that loop run? Technically, it can run an infinite number of times again Could just keep on running and you know, hopefully we eventually reach some point Where we can stop it. But again, it can just run forever Then specifically, we look at that idea of T and T declining as lowercase T in this case goes down or in our case, you know, it's going from one to a million, whatever, whatever we're looking at is specifically We could design out our temperature decline schedule Well, what that means is again, maybe we start with a very high temp But as you notice, it's a very, very quick drop off Before leveling out and so T doesn't change Doesn't change there. That's the way we can write that That works. That's one approach that we could do Here's another approach that we could do The point being is what we're looking to do is just establish T is some value Because again, looking at that algorithm, we have Our difference proportional to T So we wanted to gradually start to go down Then what we're looking at is specifically, you know, oh, well, if T hits zero We could consider that good enough and, you know, we're done Or you could strip this out entirely and just not ever have this be a part of the algorithm Again, you just constantly are moving through your search Slowly declining T however you want Maybe you wanted to go down by a percentage instead of a hard, you know, by one number Either way works, but specifically what we're looking at is when we had some configuration Right, we're going to look at all of the possible steps that that configuration could do so C one a C one B C one C C one D These are all just possible steps that we could work off of And specific to simulated annealing in that idea of Controlling Random I just pick one. I just Doesn't matter. I'm not looking for the best. I'm not looking for the worst. I'm just going to pick one of them I'm going to say for this case, you know, oh this time around I pick C one B That's it Then what I do is I say well, what is the delta? This is technically the delta E of that candidate So that would be this C one B minus C one whatever the evaluation the f score the fitness measure of C one was Check the difference once again if it's greater than zero again if we're trying to maximize Immediately go to it. We're done. Awesome. All right We're good But more specifically when we're dealing with when it's not not A good Move We want to sometimes do it. We need to sometimes do it again to potentially break out of local maximas And to do that again, what we're looking at is this idea Of some proportional distance again We want this to happen because again, this is going to allow us to calculate that out We figure out what that probability is. Let's arbitrarily say that our p currently equals, you know 65 Right. So there's a 65 chance we take that value because what that does is then if I show it again P being 65 I'm going to generate some random number Some random number between zero and one Because all right. Well if that number is less than 65 So let's say I generated a point 25 Well happen that happens to be a true statement. That is less than 65. Therefore, we can say, okay Well, that is you going into that new move That works same kind of thing going on if it doesn't if it's 75 This is not a true statement We don't make the step this time. So again, let's sort of play this out. See what happens here So again, we're working off of our simple example. I've generated a fitness score of 106 I have all my potential candidates again red switching with orange red switching with green red switching with blue red switching with purple Again, just working off of those to make it easier for me But my point being is okay. I can make one swap Which one do I pick? I pick one at random. I pick arbitrarily one at random because again We're making the assessment of whether or not we do this again with sort of our our next approach If we're dealing with something that is better again, let's say arbitrarily We want to maximize our value and we randomly picked, uh, you know, this third option Well again, that's a better move. So yes, we obviously take that move But then you notice I have three other possibilities I have sort of a worse move here a worse move here and a worse move here If I picked any one of those again, we have to say I might take that route And to do that again, what we're looking at is this idea Let's say if we picked this one Well, the delta E for this is again 93 minus 106 Which turns into a negative 13 negative 13 means it's a bad move. It's less than zero So we have to make a few more steps So again, if we're looking at that pseudocode, we generate out What is the probability that we go to that move and it's e to the negative 13 negative 13 over t Okay What's t to kind of take this into perspective? Here's just me, you know, cheating with a little python So let's arbitrarily say that t is 100, right? And we're going to just make it go down by one every single time So for i in range, uh, t to one with a step of negative one, right? That in mind again, what I'm trying to do is calculate out the probability So the prob is going to equal math dot e. You can see I sort of cheated here and I already had that imported in math dot e raised to the power of negative 13 over t Or sorry, it would be i in this case And so again, that's just going to be 100 99 98 97 96, et cetera up to one And so I'm going to just print that out. So here's that i here's the value And then what is the probability at that particular temperature level? We take this we run it and what you can see here specifically Is again when t is 100, right? We have an 87 chance of making that step 87.8 percent chance and then as t goes down 87.7 87.6 87.5 If I start to jump down to let's say 65, right that probability is now 87 or 81.9 If I keep jumping down to say, uh, 35 probability of making this step or selecting this 93 would be a 68.9 or 69 As we get down to a 10, you know, where temperature is super low at this point Our probability is starting to hit 0.273, right? We're starting to get lower and lower until as we reach ultimately to those zero values The probability becomes so low that we don't do it So again, what this allows us to do is we make these types of selections Because the hope is that after we made this hop there May be a chance At Something better Then 106 And again, that's the sole purpose because we're maybe getting out of that that that Little local maxima that we're dealing with we may be able to Move towards a better local maxima or the the mythical global maxima that may be impossible for us to otherwise find out And that's simulated annealing in a nutshell