Optimization Problems in AI
Transcript
[MUSIC PLAYING] BRIAN YU: OK, welcome back everyone to an Introduction to Artificial Intelligence with Python. And now, so far, we've taken a look at a couple of different types of problems. We've seen classical search problems where we're trying to get from an initial state to a goal by figuring out some optimal path. We've taken a look at adversarial search where we have a game-playing agent that is trying to make the best move. We've seen knowledge-based problems where
we're trying to use logic and inference to be able to figure out and draw some additional conclusions. And we've seen some probabilistic models as well where we might not have certain information about the world, but we want to use the knowledge about probabilities that we do have to be able to draw some conclusions. Today we're going to turn our attention to another category of problems generally known as "optimization problems" where optimization is really all about choosing the best option from a set of possible options. And we've already seen optimization in some contexts, like game playing where we're trying to create an AI that chooses the best move out of a set of possible moves. But what we'll take a look at today is a category of types of problems and algorithms to solve them that can be used in order to deal with a broader range of potential optimization problems. And the first of the algorithms that we'll take a look at is known as a "local search."
And local search differs from search algorithms we've seen before in the sense that the search algorithms we've looked at so far, which are things like breadth-first search or A* search, for example, generally maintain a whole bunch of different paths that we're simultaneously exploring, and we're looking at a bunch of different paths at once trying to find our way to the solution. On the other hand, in local search, this is going to be a search algorithm that's really just going to maintain a single node and looking at a single state, and we'll generally run this algorithm by maintaining that single node and then moving ourselves to one of the neighboring nodes throughout this search process. And this is generally useful in contexts not like these problems, which we've seen before, like a maze-solving situation where we're trying to find our way from the initial state to the goal by following some path.
But local search is most applicable when we really don't care about the path at all, and all we care about is what the solution is. And in the case of a solving a maze, the solution was always obvious. You could point to the solution. You know exactly what the goal is. And the real question is, what is the path to get there? But local search is going to come up in cases where figuring out exactly what the solution is, exactly what the goal looks like is actually the heart of the challenge. And to give an example of one of these kinds of problems, we'll consider a scenario where we have two types of buildings, for example, and we have houses and hospitals. And our goal might be, in a world that's formatted as this grid where we have a whole bunch of houses, a house here, a house here, two houses over there, maybe we want to try and find a way to place two hospitals on this map, so maybe a hospital here and a hospital there.
And the problem now is we want to place two hospitals on the map, but we want to do so with some sort of objective. And our objective in this case is to try and minimize the distance of any of the houses from a hospital. So you might imagine, all right, what's the distance from each of the houses to their nearest hospital? There are a number of ways we could calculate that distance, but one way is using a heuristic we've looked at before, which is the Manhattan distance, this idea of how many rows and columns would you have to move inside of this grid layout in order to get to a hospital, for example. And it turns out if you take each of these four houses and figure out, all right, how close are they to their nearest hospital, you get something like this where this house is three away from a hospital. This house is six away, and these two houses are each four away.
And if you add all those numbers up together, you get a total cost of 17, for example. So for this particular configuration of hospitals, a hospital here and a hospital there, that state, we might say, has a cost of 17. And the goal of this problem now that we would like to apply a search algorithm to figure out is, can you solve this problem to find a way to minimize that cost, minimize the total amount if you sum up all of the distances from all the houses to the nearest hospital? How can we minimize that final value? And if we think about this problem a little bit more abstractly, and abstracting away from this specific problem, and thinking more generally about problems like it, you can often formulate these problems by thinking about them as a state-space landscape, as we'll soon call it. Here in this diagram of a state-space landscape, each of these vertical bars represents a particular state that our world could be in. So for example, each of these vertical bars
represents a particular configuration of two hospitals. And the height of this vertical bar is generally going to represent some function of that state, some value of that state. So maybe, in this case, the height of the vertical bar represents what is the cost of this particular configuration of hospitals in terms of, what is the sum total of all of the distances from all of the houses to their nearest hospital? And generally speaking, when we have a state-space landscape, we want to do one of two things. We might be trying to maximize the value of this function, trying to find a global maximum, so to speak, of this state-space landscape, a single state whose value is higher than all of the other states that we could possibly choose from. And generally, in this case, when we're trying to find a global maximum, we'll call the function that we're trying to optimize them some "objective function," some function that measures for any given state
how good is that state such that we can take any state, pass it into the objective function, and get a value for how good that state is. And ultimately, what our goal is is to find one of these states that has the highest-possible value for that objective function. An equivalent but reverse problem is the problem of finding a global minimum, some state that has a value after you passed it into this function that is lower than all of the other possible values that we might choose from. And generally speaking, when we're trying to find a global minimum, we call the function that we're calculating a "cost function." Generally, each state has some sort of cost, whether that cost is a monetary cost, or a time cost, or, in the case of the houses and hospitals we've been looking at just now, a distance cost in terms of how far away each of the houses is from a hospital. And we're trying to minimize the cost, find the state that has the lowest possible value of that cost.
So these are the general types of ideas that we might be trying to go for within a state-space landscape, trying to find a global maximum or trying to find a global minimum. And how exactly do we do that? We'll recall that in local search, we generally operate this algorithm by maintaining just a single state, just some current state represented inside of some node, maybe inside of a data structure where we're keeping track of where we are currently. And then, ultimately, what we're going to do is, from that state, move to one of its neighbor states, so in this case represented in this one-dimensional space by just the state immediately to the left or to the right of it. But for any different problem, you might define what it means for there to be a neighbor of a particular state. In the case of a hospitals, for example, that we were just looking at, a neighbor might be moving one hospital one space to the left, or to the right,
or up, or down, some state that is close to our current state but slightly different and, as a result, might have a slightly different value in terms of its objective function or in terms of its cost function. So this is going to be our general strategy in local search, to be able to take a state, maintaining some current node, and move where we're looking at in this state-space landscape in order to try to find a global maximum or a global minimum somehow. And perhaps the simplest of algorithms that we could use to implement this idea of local search is an algorithm known as "hill climbing." And the basic idea of hill climbing is, let's say, I'm trying to maximize the value of my state. I'm trying to figure out where the global maximum is. I'm going to start in the state. And generally, what hill climbing is going to do is it's going to consider the neighbors of that state, that from this state I could go left, or I could go right.
And this neighbor happens to be higher, and this neighbor happens to be lower. And in hill climbing, if I'm trying to maximize the value, I'll generally pick the highest one I can. Between the state to the left and right of me, this one is higher. So I'll go ahead and move myself to consider that state instead. And then I'll repeat this process, continually looking at all of my neighbors and picking the highest neighbor, doing the same thing, looking at my neighbors, picking the highest of my neighbors until I get to a point like right here where I consider both of my neighbors, and both of my neighbors have a lower value than I do. This current state has a value that is higher than any of its neighbors. And at that point, the algorithm terminates. And I can say, all right, here I have now found the solution. And the same thing works in exactly the opposite way for trying to find a global minimum, but the algorithm is fundamentally the same.
If I'm trying to find a global minimum and, say, my current state starts here, I'll continually look at my neighbors, pick the lowest value that I possibly can until I eventually, hopefully, find that global minimum, a point at which, when I look at both of my neighbors, they each have a higher value, and I'm trying to minimize the total score, or cost, or value that I get as a result of calculating some sort of cost function. So we can formulate this graphical idea in terms of pseudocode. And the pseudocode for hill climbing might look like this. We define some function called "hill climb" that takes as input the problem that we're trying to solve. And generally, we're going to start in some sort of initial state. So I'll start with a variable called "current" that is keeping track of my initial state, like an initial configuration of hospitals. And maybe some problems lend themselves to an initial state, some place where you begin.
In other cases, maybe not, in which case we might just randomly generate some initial state just by choosing two locations for hospitals at random, for example, and figuring out from there how we might be able to improve. But that initial state, we're going to store inside of "current." And now here comes our loop, some repetitive process we're going to do again and again until the algorithm terminates. And what we're going to do is first say, let's figure out all of the neighbors of the current state. From my state, what are all of the neighboring states for some definition of what it means to be a neighbor? And I'll go ahead and choose the highest valued of all of those neighbors and save it inside of this variable called "neighbor," so keep track of the highest-valued neighbor. This is in the case where I'm trying to maximize the value. In a case where I'm trying to minimize the value, you might imagine here you'll pick the neighbor with the lowest
possible value. But these ideas are really fundamentally interchangeable. And it's possible, in some cases, there might be multiple neighbors that each have an equally high value or an equally low value in the minimizing case. And in that case, we can just choose randomly from among them. Just choose one of them, and save it inside of this variable "neighbor." And then the key question to ask is, is this neighbor better than my current state? And if the neighbor, the best neighbor that I was able to find is not better than my current state, well, then the algorithm is over, and I'll just go ahead and return the current state. If none of my neighbors are better, then I may as well stay where I am is the general logic of the hill-climbing algorithm. But otherwise, if the neighbor is better, then I may as well move to that neighbor. So you might imagine setting "current" equal to "neighbor"
where the general idea is if I'm at a current state and I see a neighbor that is better than me, then I'll go ahead and move there. And then I'll repeat the process, continually moving to a better neighbor until I reach a point at which none of my neighbors are better than I am. And at that point, we'd say the algorithm can just terminate there. So let's take a look at a real example of this with these houses and hospitals. So we've seen now that if we put the hospitals in these two locations, that has a total cost of 17. And now we need to define, if we're going to implement this hill-climbing algorithm, what it means to take this particular configuration of hospitals, this particular state and get a neighbor of that state. And a simple definition of "neighbor" might be just let's pick one of the hospitals and move it by one square to the left, or right, or up, or down, for example. And that would mean we have six possible neighbors from this particular configuration.
And we could take this hospital and move it to any of these three possible squares, or we take this hospital and move it to any of those three possible squares. And each of those would generate a neighbor. And what I might do is say, all right, here is the locations and the distances between each of the houses and their nearest hospital. Let me consider all of the neighbors and see if any of them can do better than a cost of 17. And it turns out there are a couple of ways that we could do that, and it doesn't matter if we randomly choose among all the ways that are the best. But one such possible way is by taking a look at this hospital here and considering the directions in which it might move if we hold this hospital constant. If we take this hospital and move it one square up, for example, that doesn't really help us. It gets closer to the house up here, but it gets further away from the house down here, and it doesn't really change anything
for the two houses along the left-hand side. But if we take this hospital on the right and move it one square down, it's the opposite problem. It gets further away from the house up above, and it gets closer to the house down below. The real idea, the goal should be to be able to take this hospital and move it one square to the left. By moving it one square to the left, we move it closer to both of these houses on the right without changing anything about the houses on the left. For them, this hospital is still the closer one, so they aren't affected. So we're able to improve the situation by picking a neighbor that results in a decrease in our total cost. And so we might do that, move ourselves from this current state to a neighbor by just taking that hospital and moving it. And at this point, there's not a whole lot that can be done with this hospital, but there's still other optimizations we can make, other neighbors we can move to that
are going to have a better value. If we consider this hospital, for example, we might imagine that right now it's a bit far up, that both of these houses are a little bit lower, so we might be able to do better by taking this hospital and moving it one square down, moving it down so that now, instead of a cost of 15, we're down to a cost of 13 for this particular configuration. And we can do even better by taking the hospital and moving it one square to the left. Now, instead of a cost of 13, we have a cost of 11 because this house is one away from the hospital. This one is four away. This one is three away, and this one is also three away. So we've been able to do much better than that initial cost that we had using the initial configuration just by taking every state and asking ourselves the question, can we do better by just making small, incremental changes, moving to a neighbor, moving to a neighbor, and moving to a neighbor after that?
And now, at this point, we can potentially see that, at this point, the algorithm is going to terminate. There's actually no neighbor we can move to that is going to improve the situation, get us a cost that is less than 11. Because if we take this hospital and move it up or to the right, well, that's going to make it further away. If we take it and move it down, that doesn't really change the situation. It gets further away from this house but closer to that house. And likewise, the same story was true for this hospital. Any neighbor we move it to, up, left, down, or right, is either going to make it further away from the houses and increase the cost, or it's going to have no effect on the cost whatsoever. And so the question we might now ask is, is this the best we could do? Is this the best placement of the hospitals we could possibly have? And it turns out the answer is "no" because there's a better way that we could place these hospitals.
And in particular, there are a number of ways you could do this. But one of the ways is by taking this hospital here and moving it to this square, for example, moving it diagonally by one square, which was not part of our definition of "neighbor." We could only move left, right, up, or down. But this is, in fact, better. It has a total cost of 9. It is now closer to both of these houses. And as a result, the total cost is less. But we weren't able to find it. Because in order to get there, we had to go through a state that actually wasn't any better than the current state that we had been on previously. And so this appears to be a limitation or a concern you might have as you go about trying to implement a hill-climbing algorithm is that it might not always give you the optimal solution. If we're trying to maximize the value of any particular state, we're trying to find the global maximum, a concern
might be that we could get stuck at one of the local maxima, highlighted here in blue, where a local maxima is any state whose value is higher than any of its neighbors. If we ever find ourselves at one of these two states when we're trying to maximize the value of the state, we're not going to make any changes. We're not going to move left or right. We're not going to move left here because those states are worse. But yet we haven't found the global optimum. We haven't done as best as we could do. And likewise, in the case of the hospitals, what we're ultimately trying to do is find a global minimum, find a value that is lower than all of the others. But we have the potential to get stuck at one of the local minimum, any of these states whose value is lower than all of its neighbors but still not as low as the local minimum. And so the takeaway here is that it's not always going to be the case that when we run this naive,
hill-climbing algorithm that we're always going to find the optimal solution. There are things that could go wrong. If we started here, for example, and tried to maximize our value as much as possible, we might move to the highest possible neighbor, move to the highest possible neighbor, move to the highest possible neighbor, and stop, and never realize that there is actually a better state way over there that we could have gone to instead. And other problems you might imagine just by taking a look at this state-space landscape are these various different types of plateaus, something like this flat local maximum here where all six of these states each have the exact same value. And so, in the case of the algorithm we showed before, none of the neighbors are better, so we might just get stuck at this flat local maximum. And even if you allowed yourself to move to one of the neighbors, it wouldn't be clear which neighbor you would ultimately move to,
and you could get stuck here as well. And there's another one over here. This one is called a "shoulder." It's not really a local maximum because there's still places where we can go higher-- not a local minimum because we can go lower. So we can still make progress, but it's still this flat area where if you have a local search algorithm, there is potential to get lost here, unable to make some upward or downward progress depending on whether we're trying to maximize or minimize and, therefore, another potential for us to be able to find a solution that might not actually be the optimal solution. And so because of this potential, the potential that hill climbing has to not always find us the optimal result, it turns out there are a number of different varieties and variations on the hill-climbing algorithm that help to solve the problem better depending on the context. And depending on the specific type of problem, some of these variants
might be more applicable than others. What we've taken a look at so far is a version of hill climbing generally called "steepest-ascent hill climbing" where the idea of steepest-ascent hill climbing is we are going to choose the highest-valued neighbor in the case where we're trying to maximize or the lowest-valued neighbor in cases where we're trying to minimize. But generally speaking, if I have five neighbors and they're all better than my current state, I will pick the best one of those five. Now, sometimes, that might work pretty well. It's sort of a greedy approach of trying to take the best operation at any particular time step. But it might not always work. There might be cases where actually I want to choose an option that is slightly better than me but maybe not the best one because that, later on, might lead to a better outcome ultimately. So there are other variants that we might consider of this basic hill-climbing algorithm.
One is known as "stochastic hill climbing." And in this case, we choose randomly from all of our higher-valued neighbors. So if I'm at my current state and there are five neighbors that are all better than I am, rather than choosing the best one as steepest ascent would do, stochastic will just choose randomly from one of them, thinking that if it's better, then it's better, and maybe there's a potential to make forward progress even if it is not locally the best option I could possibly choose. First-choice hill climbing ends up just choosing the very first highest-valued neighbor, but it follows behaving on a similar idea. Rather than consider all of the neighbors, as soon as we find a neighbor that is better than our current state, we'll go ahead and move there, so maybe some efficiency improvements there and maybe has the potential to find a solution that the other strategies weren't able to find. And with all of these variants, we still suffer
from the same potential risk, this risk that we might end up at a local minimum or a local maximum. And we can reduce that risk by repeating the process multiple times. So one variant of hill climbing is random-restart hill climbing where the general idea is we'll conduct hill climbing multiple times. If we apply a steepest-ascent hill climbing, for example, we'll start at some random state, try and figure out how to solve the problem, and figure out what is the local maximum or local minimum we get to. And then we'll just randomly restart and try again, choose a new starting configuration, try and figure out what the local maximum or minimum is, and do this some number of times. And then after we've done it some number of times, we can pick the best one out of all of the ones that we've taken a look at. So there's another option we have access to as well. And then, although I said the generally local search will usually
just keep track of a single node and then move to one of its neighbors, there are variants of hill climbing that are known as "local beam searches" where, rather than keep track of just one current best state, we're keeping track of k highest-valued neighbors, such that rather than starting at one random initial configuration, I might start with three, or four, or five, randomly generate all the neighbors, and then pick like the three, or four, or five best of all of the neighbors that I find and continually repeat this process with the idea being that now I have more options that I'm considering and more ways that I could potentially navigate myself to the optimal solution that might exist for a particular problem. So let's now take a look at some actual code that can implement some of these kinds of ideas, something like steepest-ascent hill climbing, for example, for trying to solve this hospital problem.
So I'm going to go ahead and go into my hospital's directory where I've actually set up the basic framework for solving this type of problem. I'll go ahead and go into hospitals.py, and we'll take a look at the code we've created here. I've defined a class that is going to represent the state space. So the space has a height, and a width, and also some number of hospitals. So you can configure, how big is your map? How many hospitals should go here? We have a function for adding a new house to the state space and then some functions that are going to get me all of the available spaces for if I want to randomly place hospitals in particular locations. And here now is the hill-climbing algorithm. So what are we going to do in the hill-climbing algorithm? Well, we're going to start by randomly initializing where the hospitals are going to go. We don't know where the hospitals should actually be, so let's just randomly place them.
So here, I'm running a loop for each of the hospitals that I have. I'm going to go ahead and add a new hospital at some random location. So I basically get all of the available spaces, and I randomly choose one of them as where I would like to add this particular hospital. I have some logging output and generating some images, which we'll take a look at a little bit later. But here is the key idea. So I'm going to just keep repeating this algorithm. I could specify a maximum of how many times I want it to run, or I could just run it up until it hits a local maximum or a local minimum. And now, we'll basically consider all of the hospitals that could potentially move, so consider each of the two hospitals or more hospitals if there are more than that, and consider all of the places where that hospital could move to, some neighbor of that hospital that we can move the neighbor to and then see,
is this going to be better than where we were currently? So if it is going to be better, then we'll go ahead and update our best neighbor and keep track of this new best neighbor that we found. And then afterwards, we can ask ourselves the question, if best neighbor cost is greater than or equal to the cost of the current set of hospitals, meaning if the cost of our best neighbor is greater than the current cost, meaning our best neighbor is worse than our current state, well, then we shouldn't make any changes at all. And we should just go ahead and return the current set of hospitals. But otherwise, we can update our hospitals in order to change them to one of the best neighbors. And if there are multiple that are all equivalent, I'm here using random.choice to say, go ahead and choose one randomly. So this is really just a Python implementation of that same idea that we were just talking about,
this idea of taking a current state, some current set of hospitals, generating all of the neighbors, looking at all of the ways we could take one hospital and move it one square to the left, or right, or up, or down, and then figuring out, based on all of that information, which is the best neighbor or the set of all the best neighbors, and then choosing from one of those. And each time, we go ahead and generate an image in order to do that. And so now what we're doing is if we look down in the bottom, I'm going to randomly generate a space with height 10 and width 20. And I'll say, go ahead and put three hospital somewhere in the space. I'll randomly generate 15 houses that I just go ahead and add in random locations. And now I'm going to run this hill-climbing algorithm in order to try and figure out where we should place those hospitals. So we go ahead and run this program by running "python hospitals."
And we see that we started-- our initial state had a cost of 72, but we were able to continually find neighbors that were able to decrease that cost, decrease to 69, 66, 63, so on and so forth, all the way down to 53 as the best neighbor we were able to ultimately find. And we can take a look at what that looked like by just opening up these files. So here, for example, was the initial configuration. We randomly selected a location for each of these 15 different houses and then randomly selected locations for 1, 2, 3 hospitals that were just located somewhere inside of the state space. And if you add up all the distances from each of the houses to their nearest hospital, you get a total cost of about 72. And so now the question is, what neighbors can we move to that improve the situation? And it looks like the first one the algorithm found was by taking this house that was over there on the right and just moving it to the left. And that probably makes sense.
Because if you look at the houses in that general area, really these five houses look they're probably the ones that are going to be closest to this hospital over here. Moving it to the left decreases the total distance at least to most of these houses, though it does increase that distance for one of them. And so we're able to make these improvements to the situation by continually finding ways that we can move these hospitals around until we eventually settle at this particular state that has a cost of 53. Or we figured out a position for each of the hospitals, and now none of the neighbors that we can move to are actually going to improve the situation. We can take this hospital, and this hospital, and that hospital and look at each of the neighbors, and none of those are going to be better than this particular configuration. And again, that's not to say that this is the best we could do.
There might be some other configuration of hospitals that is a global minimum. And this might just be a local minimum, that is, the best of all of its neighbors but maybe not the best in the entire possible state space. And you could search through the entire state space by considering all of the possible configurations for hospitals. But ultimately, that's going to be very time intensive, especially as our state space gets bigger and there might be more and more possible states. It's going to take quite a long time to look through all of them. And so being able to use these sort of local search algorithms can often be quite good for trying to find the best solution we can do. And especially if we don't care about doing the best possible and we just care about doing pretty good and finding a pretty good placement of those hospitals, then these methods can be particularly powerful. But of course, we can try and mitigate some of this concern by instead
of using hill climbing to use random restart, this idea of rather than just hill climb one time, we can hill climb multiple times and, say, try hill climbing a whole bunch of times on the exact same map and figure out, what is the best one that we've been able to find? And so I've here implemented a function for random restart that restarts some maximum number of times. And what we're going to do is repeat that number of times this process of just go ahead and run the hill-climbing algorithm, figure out what the cost is of getting from all the houses to the hospitals, and then figure out, is this better than we've done so far? So I can try this exact same idea where instead of running hill climbing, I'll go ahead and run random_restart. And I'll randomly restart maybe 20 times, for example. And we'll go ahead, and now I'll remove all the images and then rerun the program. And now we started by finding a original state.
When we initially ran hill climbing, the best cost we were able to find was 56. Each of these iterations is a different iteration of the hill-climbing algorithm. We're running hill climbing not one time but 20 times here, each time going until we find a local minimum, in this case. And we look and see each time, did we do better than we did the best time we've done so far? So we went from 56 to 46. This one was greater, so we ignored it. This one was 41, which was less, so we went ahead and kept that one. And for all of the remaining 16 times that we tried to implement hill climbing and we tried to run the hill-climbing algorithm, we couldn't do any better than that 41. Again, maybe there is a way to do better that we just didn't find, but it looks like that way ended up being a pretty good solution to the problem. That was attempt number 3 starting from counting at zero. So we can take a look at that, open up number 3,
and this was the state that happened to have a cost of 41, that after running the hill-climbing algorithm on some particular, random initial configuration of hospitals, this is what we found was the local minimum in terms of trying to minimize the cost. And it looks like we did pretty well, that this hospital is pretty close to this region. This one is pretty close to these houses here. This hospital looks about as good as we can do for trying to capture those houses over on that side. And so these sorts of algorithms can be quite useful for trying to solve these problems. But the real problem with many of these different types of hill climbing, steepest ascents, stochastic, first choice, and so forth is that they never make a move that makes our situation worse. They're always going to take ourselves in our current state, look at the neighbors, and consider, can we do better than our current state, and move to one of those neighbors.
Which of those neighbors we choose might vary among these various different types of algorithms, but we never go from a current position to a position that is worse than our current position. And ultimately, that's what we're going to need to do if we want to be able to find a global maximum or a global minimum. Because sometimes if we get stuck, we want to find some way of dislodging ourselves from our local maximum or local minimum in order to find the global maximum or the global minimum or increase the probability that we do find it. And so the most popular technique for trying to approach the problem from that angle is a technique known as "simulated annealing," simulated because it's modeling after a real physical process of annealing where you can think about this in terms of physics, a physical situation where you have some system of particles. And you might imagine that when you heat up a particular physical system, there's a lot of energy there.
Things are moving around quite randomly. But over time, as the system cools down, it eventually settles into some final position. And that's going to be the general idea of simulated annealing. We're going to simulate that process of some high-temperature system where things are moving around randomly quite frequently but, over time, decreasing that temperature until we eventually settle at our ultimate solution. And the idea is going to be if we have some state-space landscape that looks like this and we begin at its initial state here, if we're looking for a global maximum and we're trying to maximize the value of the state, our traditional hill-climbing algorithms would just take the state, and look at the two neighbor ones, and always pick the one that is going to increase the value of the state. But if we want some chance of being able to find the global maximum, we can't always make good moves. We have to sometimes make bad moves and allow ourselves
to make a move in a direction that actually seems, for now, to make our situation worse such that later we can find our way up to that global maximum in terms of trying to solve that problem. Of course, once we get up to this global maximum, once we've done a whole lot of the searching, then we probably don't want to be moving to states that are worse than our current state. And so this is where this metaphor for annealing starts to come in where we want to start making more random moves and, over time, start to make fewer of those random moves based on a particular temperature schedule. So the basic outline looks something like this. Early on in simulated annealing, we have a higher temperature state. And what we mean by a "higher temperature state" is that we are more likely to accept neighbors that are worse than our current state, that we might look at our neighbors, and if one of our neighbors is worse than the current state, especially if it's not all that much worse,
if it's pretty close but just slightly worse, then we might be more likely to accept that and go ahead and move to that neighbor anyways. But later on as we run simulated annealing, we're going to decrease that temperature. And at a lower temperature, we're going to be less likely to accept neighbors that are worse than our current state. Now, to formalize this and put a little bit of pseudocode to it, here is what that algorithm might look like. And we have a function called "simulated annealing" that takes as input the problem we're trying to solve and also potentially some maximum number of times we might want to run the simulated annealing process, how many different neighbors we're going to try and look for. And that value is going to vary based on the problem you're trying to solve. We'll again start with some current state that will be equal to the initial state of the problem.
But now, we need to repeat this process over and over for max number of times, repeat some process some number of times where we're first going to calculate a temperature. And this temperature function takes the current time t, starting at 1 going all the way up to max, and then gives us some temperature that we can use in our computation where the idea is that this temperature is going to be higher early on, and it's going to be lower later on. So there are a number of ways this temperature function could often work. One of the simplest ways is just to say it is like the proportion of time that we still have remaining. Out of max units of time, how much time do we have remaining? You start off with a lot of that time remaining. And as time goes on, the temperature is going to decrease because you have less and less of that remaining time still available to you. So we calculate a temperature for the current time.
And then we pick a random neighbor of the current state. No longer are we going to be picking the best neighbor that we possibly can or just one of the better neighbors that we can. We're going to pick a random neighbor. It might be better. It might be worse. But we're going to calculate that. We're going to calculate delta E, "E" for "energy" in this case, which is just, how much better is the neighbor than the current state? So if delta E is positive, that means the neighbor is better than our current state. If delta E is negative, that means the neighbor is worse than our current state. And so we can then have a condition that looks like this. If delta E is greater than 0, that means the neighbor state is better than our current state. And if ever that situation arises, we'll just go ahead and update "current" to be that neighbor. Same as before, move where we are currently to be the neighbor because the neighbor is better than our current state.

Ask anything
We've read this whole study set, so questions about it get the best answers.