1. Home
  2. Explore
  3. Computer Science
  4. Search Algorithms Lecture

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Search Algorithms Lecture

Transcript

Okay. So, Hi, everyone. So, uh, our plan for today is to continue talking about search. So, so that's, uh, what we're going to start doing, finish off some of the stuff we started talking about last time, and then after that, uh, switch to some of the more interesting topics like learning. So a few announcements. Um, so the solutions to the old exams are online now. So if you guys wanna start studying for the exam, you can do that. So, so start looking at some of those problems, I think, that would be useful. Um, actually, let me start with the Search 2 lecture because I think that might be, like, that has a, a review of some of the topics we've talked about. So it might be easier to do that. Also, I'm not connected to the network, so we're not gonna do the questions, uh, or show the videos because I have, I have a hard time connecting to the network in this room. Okay. All right. So, so let's start- continue talking about search. Uh, so if you guys remember, uh,

we had this, this city block problem. So let's go back to that problem and let's just try to do a review of some of the, some of the search, search algorithms we talked about last time. So, uh, so suppose you want to travel from City 1 to City n only going forward, and then from City n you wanna go backwards, so and back to City 1 going only backwards, okay? So, so you- so the problem statement is kind of like this. You're starting in City 1, you're going- you're going forward and you're getting to some City n. So maybe we're doing that on this. And then after that, you wanna go backwards and get to, get to City 1 again. So you go into some of these cities, okay? So, so that's the goal, and then the cost of going to- from any city i to city j is equal to cij, okay? So, so that's it. So, the question is: What- which one of these following algorithms could you use to solve this problem? And it could be multiple of them.

So- so we have depth-first search, breadth-first search, dynamic programming, and uniform cost search. And these were the algorithms we talked about last time. So, uh, maybe just talk to your neighbors for a minute and then we can do votes on each one of these. Yes, question? Just needed to ask [inaudible] The [OVERLAPPING]? Okay. Let me check that again. Thank you. Thank you for. [BACKGROUND] All right, so let's maybe start talking about this.

So how about depth-first search like how many people think we can use depth-first search? How many people think we can't use depth-first search? There's -a very like good split. [LAUGHTER] So, some of the people think we can't use depth-first search, what, what are some reasons maybe just like call it out. The depth first-search, the assumption was that based upon the cost is zero. Yes, that's right. Yeah, so here we are basically going from City 1 to city n. Each one of these edges had a cost of cij. I'm just saying cij is greater than or equal to 0. That's the only thing I'm saying about cij. But if you remember depth-first search, you really wanted the cost to just be equal to 0 because if you remember that whole tree, like the whole point of depth-first search was I could just stop whenever I could find a solution. And we were assuming that the costs of all the edges is just equal to zero.

So we can't really use depth first search here, because, because our cost is not 0. So assuming, like now that you know that reasoning, how about breadth first-search? Can we use breadth-first search? Yes? All of that moving from one city to city n that is not the city n. So that's a good point. So, so what suggesting is can we think about the problem as going from City 1 to City n? And then after that, like introduce like a whole new problem that continues that and starts from City n and goes to City 1. Let me get back to that point like in a second, because like you could potentially think about that -actually like that might be an interesting way of thinking about it.

But, but irrespective of that I can't use depth first-search. So I'm -so far I'm just talking about depth first-search. Irrespective of how I'm looking at the problem, the costs are gonna be uh, non-zero. So because the costs are going to be non-zero, I can't use depth-first search. So, so let's talk about that first. So how about breadth first-search? Can I use breadth-first search? [inaudible] That's exactly right. So we cannot use breadth-first search here because for breadth first-search. If you remember, you really wanted all the costs to be the same. They didn't need to be 0, but they needed to be the same thing because then you could just go over the levels. And here I'm not- like I'm not saying I'm not putting any restrictions on cij being the same thing. Okay? So now let's talk about dynamic programming. How about dynamic programming? Can we use dynamic programming? All right, so that looks right, right you like we could use dynamic programming here.

Everything looks okay, cij's are positive, looks fine. Um, how about, um, actually one question? So, so don't they have cycles here? We kind of, briefly talked about this already. So, don't I have like this cycle here? Uh, we can think about possibly going from one to n and then n to one. Yes, so this is a suggestion that, that we have already like heard twice. So we could actually use dynamic programming here even if it kinda looks like we have a cycle and the reasons we

can kinda use this trick were we can basically draw this out again. And for going forward basically go all the way here, and then after that we're going backwards, kind of include the directionality too. So all I'm doing is I'm extending the state, the state space to not just be the city but be the city in addition to that, it would be direction that we're going. So if I'm in City 4 here, it's City 4 going forward. And if at some point in the future I'm in City, I don't know, 4 again, it's City 4 going backwards. So I'll keep track of both the city and the directionality. And when I do that then I'm kind of breaking the cycle. Like I'm not putting any cycles here and I can actually use dynamic programming, okay? Does that make sense? And then uniform cost search. That, that also sounds good too, right? Like Uniform cost search, you could actually use that. Doesn't matter if you have cycles or not. And then we have positive, positive, non-negative costs.

So we could use uniform cost search. Okay? All right, so this was just a quick review of some of the things we talked about last time. And, um, another thing we talked about last time was this notion of state. Okay, so, so we started talking about tree search algorithms and at some point, uh, we switched to dynamic programming and uniform cost search where we are, uh, like we don't need to- like we don't need to have this exponential blow up. And the reason behind that was we have memoization. And in addition to that we have this notion of state. Okay? And so, what is a state? A state is a summary of all past actions that are sufficient for us to choose the future optimally. So, so we need to be really careful about choosing our state. So in this previous question, uh, we looked at past actions. So if you look at like all cities that you go over it can be in City 1, then 3, then 4, 5, 6 and city 3 again. So in terms of state,

the things that you wanna keep track of is what city you are in. But in addition to that, you wanna have the directionality because you, you need to know like where you are and how you're getting back. Okay? So, and we did a couple of examples around that trying to figure out what is, what is like a specific notion of state for various problems. All right. So, so we started last time talking about search problems and, and we started formalizing it. So if you remember our paradigm of modeling and inference and learning we started kind of modeling search problems using this formalism where we defined a starting state, that's s start. And then we talked about the actions of s, which is a function over our states which returns all possible actions. And then we talked about the cost function. So the cost function can take a state and action and tell us what is the cost of that, that, that, that edge. And then we talked about the successor function which takes

a state and action and tells us where we end up at. And again, we had this end function that was just checking if you're in an end state or not. So these were all the things that we needed to, to define a search problem and we kind of tried that and a couple of examples to try an example. The City example, all of that. Okay? And then after talking about these, these different ways of, um, thinking about search problems, um, we started talking about various types of inference algorithms. So we talked about tree search. So depth first search, breadth first search, depth first search with iterative deepening, um, backtracking search. And then after that we talked about some of these graph search type algorithms like, uniform cost search an- and, uh, dynamic programming. So last time we did an example of, um, uniform cost search but we didn't get to prove the correctness of it. So I want to switch to some of the last, er, last, last time's, um, slides to just go over this,

this quick theorem and then after that just switch back to, to this lecture. Okay. So uniform cost search. Like, if you remember what we were doing in uniform cost search, we had three different sets. We had an export set which was basically the set of states that we have visited, and we are sure how to get to them, and we know the optimal path, and we know everything about them. We had this frontier set which was a set with, with a set of states that we have got to them, but we're not sure if, if the cost that we have the best cost, cost. There might be a better way of getting to them and you don't know it. Like you're not sure yet. And then we have the unexplored, er, set of states which are basically states that we haven't seen yet. So we did this example where we started with all the states in the unexplored set and then we moved into the frontier and then from the frontier, we move them to the explored set. So, so this was the example that we did on the board.

Okay? And, and we realized that, like, even if we have cycles, we can actually do this algorithm and then we, we ended up finding the best path being from A to B to C to D and that costs 3. So, uh, let's actually implement uniform cost search, uh, so I think we didn't do this last time. So going back to, um, our set of, ah, so, so we started writing up these algorithms for search problems. So we have, we have written dynamic programming already and backtracking search.

So now we can, we can try to kind of implement uniform cost search. And for doing so, we need to have this priority queue data structure. So this is in a util file. I'm just showing you what it like what functions it has, it has an update function, and it has a remove min function. So, so it's just a data structure that I'm gonna use for my frontier. Because like, my frontier I'm popping off things off my frontier. So I'm going to use this data structure. All right. So let's go back to uniform cost search. So we're going to define this frontier, where we are adding states to- from unexplored sets, you're adding states to the frontier. Okay? And it's going to be a priority queue so, so we have that data structure because we've just imported util. And you're going to basically add the start state with a cost of 0 to the frontier. So that's the first thing we do. And then after that, like, while the frontier is not empty.

So while true, what we're going to do is, uh, we're going to remove the minimum, uh, past cost element from the frontier. So, so basically just pop off the frontier that the best thing that exists there, and just move that to the explored set. Okay. So when I pop off the thing from the frontier, basically I get this past cost and I get the state. Okay? All right. So, so if, if, er, you're in an end-state, then you're just going to return that past cost with the history. I'm not putting the history here for now, I'm just returning the cost. Okay. So after popping off this state from the frontier, the thing we were doing was you were adding the children of that. So, um, the way we do that is we're gonna use this successor and cost function that we defined last time. So we can basically iterate over action new state and costs and this successor and cost function. And, and basically update our frontier, by adding these new states to it.

Okay. And then the cost that you are going to add is cost plus past cost if, if that is better. So, um, so that's what the update function of the frontier does. And that's pretty much it. Like that is uniform cost search. You add stuff to the frontier, you pop off stuff from the frontier. And, and that way you explore and remove things from unexplored set to the explored set. So let's just try that out. Looks like it is doing the right thing. So it got the same value as dynamic programming. So, er, looks like it kinda works okay. [NOISE] So, um, this code is also online. So if you want to take a look at it, um, later, actually it's not what I wanted. Um yeah. Okay. All right. So, so that was- and here's also the pseudo-code of uniform cost search. Okay? Okay. So we have- is there a question right there? What's the runtime of uniform cost search [inaudible]. That's a good point. So so what's- the question is what's the runtime of uniform cost search?

So the runtime of uniform cost search is order of n log n, where the log n is because of, like, the bookkeeping of, of the priority queue, uh, and you're going over all the edges. So, so if you can think of n here as the edges and worst-case scenario if you have a fully connected graph, it's technically n squared log n. But in practice, er, we have [inaudible] graph so people usually refer to that just n log n where n is the number of states that you have explored. And it's actually not all of the states. It's the states that you have explored. Okay? And dynamic programming, it's order of n. So technically, like, dynamic programming is slightly better but really depends. Yeah, certainty. Actually go first and then I'll get you back. Is the only difference between this and Dijkstra's is that you just don't have all [inaudible] beginning? That wasn't- the question is what's the difference between this and Dijkstra's algorithm,

they're very similar, the only difference is, this is trying to solve a search problem. So you're not like exploring all the states. When you get to the solution, you get to the solution and then you just return that Dijkstra, you're going from- you're basically exploring all of, all of the states in the- in your graph. What's your question? [inaudible]. All right. Sounds good. Okay. So, uh, I just want to quickly, er, talk about this correctness theorem. So, so for uniform cost search we actually have a correctness theorem which basically says uniform cost search does the right thing. So, uh, what basically this theorem says is, if you have a state that you are popping off the frontier and removing it from the frontier to the explored, then it's priority, that value which is equal to past cost of s is actually the minimum cost of getting to, to, to the state s. So what this is saying is, let's say that this is my explored set.

So this is my explored set, and then right here is my frontier, and I have a start state, okay? And then I have some state s, that right now I have decided that I am popping off s from the frontier to explored because that is the best thing that has the best past cost. So what the theorem says is, this, this path that I have from s_start to s, is the shortest path possible to get to get to the state s. Okay. So the way to prove that is to show that the cost

of this path is lower than any other path, paths that go from s_start to s. So let's say there is some other path, this green one, that goes from s_start to s some other way. And, and the way that it goes to s is it should probably leave the, the explored set of states from some state called t maybe to some goes- go to some other state u and then from u go to s. u and s can be the same thing. But what the point of it is, if I have this other path that goes through- to s,

it needs to leave the explored set from some state t. Okay. So what I want to show is I want to show that the, the cost of the green line, I want to show that that is greater than the cost of the black line. Okay. All right. So the cost of the green line, what is the cost of the green line? It's gonna be the cost to here, and then cost of t to u, and the cost of u to s. So I can say well, this cost is actually greater than or equal to, um, priority of t, because that is the cost of getting to t,

plus cost of t to u. And I'm just dropping this, this last part. The u to s, I'm just dropping it. Okay. So cost of green is at least equal to priority of t plus cost of t. t, t to u. Okay. Well, what does that equal to? Priority is just a number, right? It's just a number that you are getting off the, the, the, priority queue. So that is actually equal to past cost of T, plus cost of t to u. Okay. And, and this value is going to actually be greater than or equal to priority of u.

Well, why is that? Because if u is in my frontier, I've, I've visited u. So I already have some priority value for u. And, and the value that I've assigned for the priority of u, is either equal to this past cost of t plus cost of t, t to u, because I've like, seen that using my explored, using my frontier. So I've definitely seen this or it is something better that, that I don't know what it is. Right? So, so priority of u is going to be less than or equal to this past cost of t plus cost of t to u. Okay. And well, what do I know in terms of priority of u and priority of s? Well, I know priority of u is going to be greater than or equal to priority of s. Well, why is that? Because I already know I'm popping off s next, I'm not popping off U, like, like I've- I know I'm popping off the, the thing that has the least amount of priority, and the least value here, and that's s, and well, that is equal to,

er, cost of the black line, black line. Okay. All right. So that was just a quick, like proof of why the uniform cost search always returns kind of the best minimum cost path type [NOISE]. All right. So let's go to the slides again. So, um, just a comparison, quick comparison between dynamic programming of uniform cost search. So, uh, we talked about dynamic programming. We know it doesn't allow cycles, but in terms of, uh, action cost, it can be anything like,

like you can have negative costs, you can have positive costs. And, er, in terms of, um, complexity is order of n, and then uniform cost search, you can have cycles. So that is cool. But the problem is, the costs need to be non-negative, and into order of n log n. And if you have- if you end up in a situation where you have cycles and your costs are actually negative, there is this other algorithm called Bellman-Ford, that we are not talking about in this class, but you could actually like have a different algorithm that addresses those sort of the things. Okay. All right, how am I doing on time? Okay. So that was, that was this idea of inference. Right now we have like a good series of ways of going about doing inference, uh, for search problems, you have to formalize them. And now the plan for this lecture is, is to think about learning. So how are we going to go about learning when we have search problems? [NOISE] And when our search problem is not fully specified,

and there are things in the search problems that are not specified and you want to learn what they are, like the costs, okay. So, uh, so that's going to be the first part of the lecture, and then towards the end of the lecture, we're going to talk about a few other algorithms that make things faster. So, so smarter ways of making things faster. We're going to talk about A star and some sort of relaxation type strategies, okay. All right. So, um, so let's go back to our transportation problem. So, so this was our transportation problem where, er, we had a start state and we can either walk, and by walking we can go from state s to state s plus 1, and that costs one, or we can take a tram, a magic tram that takes us from state s to state 2s, and that costs 2, okay, and we want to get to state n. So, uh, we can formalize that as a search problem. We can like we saw it- we saw this last time, we can actually try to find what is the best path to get from

state 1 to any state n like we saw- like path- like walk walk, tram tram tram, walk tram tram. This is one potential like optimal path that one can get, okay? But the thing is, uh, the world is not perfect like, like modeling is actually really hard, like it's not that we always have this nice model with everything. And we could end up in scenarios where we have a search problem, and, and we don't actually know what the costs of our actions are. So we don't actually know what the cost of walking is, or what the cost of tram is. But maybe we actually have access to, to this optimal path. Like, maybe I know the optimal path is walk walk tram tram tram, walk tram tram, but I don't know what the costs are. So the point of learning is, is to go about learning what these cost values are based on this, this optimal path that we have. So, so I want to actually learn the costs of walking is 1, and the cost of tram is 2.

And this is actually a common problem that we have like in machine learning in general. So like for example, um, you might have data from, uh, how a person does something or like how a person, let's say, like grasps an object. And I, I have no idea what was the cost that the person who was optimizing to grasp an object, right, but I have like the trajectory I know like what, what the path they took when they picked up an object. So what I can do is, if I have access to that path of how they picked up an object, then from that I can actually learn what was the cost function that they were optimizing, because then I can put that cost function maybe on a, on a robot that does the same thing. Question? [inaudible] like five or something. That's a good question. So the question is, is it possible to have multiple solutions here? Yes, so we are gonna actually see that like later, like what sort of the solutions that we gonna get, are there,

ther- there could be cases where we have multiple solutions. The ratio of it is the thing that matters. So if you have like, walk is 1, tram is 4, if you get to an 8, you kind of get the same sort of behavior. Uh, and then it also depends on what sort of data you have. Like if your data allowed you to actually recover the, the, the true solution. So, so we're gonna actually talk about all these cases, okay? All right. Okay. So if you think about it, when the way- the search problem we were trying to solve, this, this was the inference problem, was when you are, you are given kind of a search formulation and you are given a cost, and, and our goal was to find the sequence of actions, this optimal sequence of actions, that was the shortest path or the best path and, and some path or some way, and this is a forward problem. So search is this forward problem, where you're given a cost and you want to find a sequence of actions, okay.

So it's interesting because learning in some sense is, is an inverse problem. It's the inverse of, of search. So the inverse of search is, if you give me that sequence of actions, the, the best sequence of actions that you've got, then can you figure out what the cost is? So, so in some sense you can think of learning as this inverse problem of, of search and, and we are going to kind of address that. So I'm going to go over one example to, to talk about, er, learning. Um, and I'm actually going to use the notation of, uh, the machine lea- learning lectures that we had, um, at the beginning of like last week basically. So, um, let's say that we have, ah, maybe I can draw this. [NOISE] Um, yeah, I will just draw the scheme. So let's say we have a search problem without costs, and, and that's our input. So if- so, so we are kind of framing this problem of learning as a prediction problem. And if you remember prediction problems,

in prediction problems we had, ah, an input. So our input was x, okay. And in, in this case you are saying our input is a search problem, search problem without costs, okay? So that is my input. And then we have outputs. And in this case my, my output y is this optimal sequence of actions that one could get- gets, so it's the solution path, so it's a solution path, okay. And what I wanna do is, I wanna- like, like if you remember machine learning,

the idea was, I would wanna find this predictor, this f function, f that we take an input, f of x, and then it would basically return the solution path in other settings and it would generalize. So, so that was kind of the idea that we explored in machine learning, and you kinda wanna do the same thing in here. So, uh, let's start with- um, I'm going to draw that here. So let's start with an example where we are in city 1, and then maybe we walk to city 2, so we can walk to city 2. And then from there, maybe I have two options. I can keep walking to get to city 4. So I can do walk walk walk. Or maybe I can take the tram and end up in city 4, okay? And, and the thing is I don't actually know what the costs of these, these actions are, I don't know what the cost of do- uh, walk is, what the cost of tram is. Okay? But one thing I know is that my, my solution path, my y is equal to walk, walk, and walk.

So, um, so one way to go about this is to actually start with some initialization of, of these costs. So the way we're defining these costs are going to be, uh, I'm going to use the word, um, I'm gonna write here maybe. I'll just write up here. I'm going to use w like, because I want to use the same notation as as the learning lectures. So w is going to be the weights that o- of, of each one of my actions. I have two actions. In this case I can either walk or I can take the tram so I'm going to call them action 1. So w of action 1 is w of walking. And then w of action 2 is w of taking the tram. So action 2 is taking the tram. So I'm defining these w values, and the way I'm defining these weights is just as a function of actions.

This could technically be a function of state and actions but right now I'm just simplifying this and I'm saying the w's is this values, the costs of walking just depend- the cost of going from 1-2 just depends on my action. It doesn't depend on what state I'm in. You could imagine settings where it actually depends on like what city you are in too, okay? So, so then under that scenario what is the cost of, cost of y? It is going to be w walk, plus w walk, plus w walk.

Okay? So what I'm suggesting is let's just start with something. Let's just start with- yeah, like let's just start with these weights. So I'm gonna say walking costs 3. And it's always going to cost 3. Again, the reason it's always going to cost 3 is I'm basically saying my weights only depend on the action, they don't depend on state. So it's always going to cost three. And I'm going to say well why not let's just say, the tram takes the cost of 2. Okay? So this doesn't like look right but like let's just say I assume this is the right solution, okay? So now what I wanna do is I want to be able to update these weights, update these values in a way that I can get this optimal path that I have, this, this walk, walk, walk. Okay? So how can I do that? So I started with these random initializations of what the weights are. Okay? So now that I've done that I can, I can try to figure out what is the optimal, optimal path here based on these weights. So what is my prediction,

so that is y prime. That is my prediction based on these weights that I've just set up in terms of like what the optimal path is. Well, what is that? That is walk tram because this costs 5 and this costs 9. So with these weights, these random weights that have just come up with I'm going to pick walk and tram. And that is my prediction. Okay? So now what we wanna do is you want to update our w's based on the fact that our true label is walk, walk, walk and our prediction is walk, tram. Okay? And, and the algorithm that kind of does this, this does like the most like silliest thing possible. So, so what it does is it's going to first look at the truth value of W. Okay? So it's going to look at- so, so, so the weights are starting from- so I decided that this guy is 3 and I decided that this guy is 2, and I'm gonna update them.

So I'm going to look at every action in this path. And for every action in this path I'm going to down-weight the, the weight of that. Well why am I going to do that? Because I- I don't want to penalize that, right? This is the true thing. I want the weight of the true thing to be small. So I see walk. I'm like okay so I see walk. The weight of that was 3. I'm going to down-weight that by 1. I'm gonna make that two. I see walk again. So I'm gonna bring that with 1. I see walk again, I'm going to subtract one again. I end up at 0. Okay? Now I'm gonna go over my prediction and then for every action I see here I'm going to bring it up, bring the cost, uh, the, the weight up by 1. So I see you walk again here, I'm going to bring it up by 1. So, so, these were subtract, subtract, subtract, bring it up by one because it's over my y prime. And then I see tram. And then because I see tram, I'm going to bring this up by 1. And that ends up in 3.

So my new weights here are going to be three- the, the, the, the weight of walk just became 1 and then the weight of tram just became 3. Okay? And, and now I can kind of repeat doing this and see if that gets me this, this optimal solution or not. So I'm going to try running my search algorithm. If I run my search algorithm this path, this path costs 3, this path costs 4. So I'm actually going to get this path and this path. So my new prediction is just going to be walk, walk, walk.

They're going to be the same thing. My weights are not gonna change. I'm going to converge. Yes. Is it always one? So I'm talking about a very simplified version of this but yeah it is always one. So the very simplified version of this is this version where I'm saying the w's just depend on, on actions. If you, if you make the weights depend on state and actions, there is a more generalized form of this. This is called the stru- er, the structure pe- er, perceptron algorithm, we'll talk about- briefly talk about the, the version where there is a state action too, but for this case we are just depending on action. You're literally just bring it up by one or by whatever like by whatever you bring it up here, you gotta bring it down by the same thing. So, so it's plus and minus a whatever a is. There's a question. [inaudible] why we do the plus 1 after we do all the minus 1s? So why am I doing the minus 1s? So I'll get to that. So, so when I look at y here, right?

Like this is the thing that I really wanted. So if I- so when I see walk I realize that walking was a good thing, so I need to bring down the weight of that. But if, if the weights that I already had like knew that walking is pretty good then like the weights that I already had knew that walking is pretty good, I should like cancel that out. So, so that's why we are doing the plus 1 because like at this stage like I knew walking is pretty good up here like like my prediction also said walk. So if, if I'm subtracting it, I should add it to, to kind of like get them cancel that. But like right here, like I didn't know walking is good so I'm going to bring down the weight of that and then bring up the weight of, uh, tram. [inaudible]. Yeah. So, so I mistakenly thought tram, uh, is the way to go. So to avoid that next time around, I'm going to make the cost of tram higher so I don't take that route anymore. And there's a question there.

So here- only like the only reason why [inaudible] in the second- in the, the y prime is because we know the y prime is different from y. Yes. But then, like what if like we have like a long sequence and y prime is only different in like one small location and like would that change the weights sufficiently? Yeah. So if, if, er, so you're asking. Okay, if my y and y prime, prime are kinda like the same thing walk, walk, walk or something and then at the very end this last one they're going to be different. Yeah. So like we were just and for that last one we are just adding one, right? So, so it does like weighted, er, it does actually address that and it just run- you can run it until you get the sequences to be exactly the same thing so you don't have any mistakes. Yeah. There's a question back there. Does it matter if our new cost become negative? Uh, does it matter if our new costs become- it depends on what, sort of, search algorithm you are using.

Uh, at the end of the day it's fine if you're using dynamic programming so I can have like a negative cost here and I'm just calling, uh, like dynamic programming at the end of the day with that and that is fine. Yeah, it's fine if the cost becomes negative. There's a question. In this problem we want to find the true cost for walk and tram, but we ended up converging to 1. So this becomes a problem. Sorry, did not supposed- Just like the end result for this algorithm we got is 1 for walk and 3 for tram. And the real result, like in the previous example was 1 and 2. 1 and 2. Right, yes. Yeah. So the, so the question is, er, we got here 1 and 3. Is this actually right? Like, like if you remember like when we define this tram problem, we said walking costs 1 and tram costs 2 but we never got that. Well, the reason we never got that is the solution we are going to get here is just based on our, our training data.

So if my training data is just walk, walk, walk, this is like the best thing I can get and I can kind of like converge to this solution where, where the two end up being equal. I don't have any mistakes on this. If I have more like data points then I'm going to do this longer and actually try it out on other training data and, and then I might converge to a different thing. Is there any rule for as far as initializing the weight? Is- I, I, I, I, I'm assuming when- the fu- uh, further when we are from the actual truth, the longer it's going to take to, uh, actually converge. It's- o- okay so the question is how do we initialize? So in na- in a natural algorithm you're just initializing with 0. So we're initializing everything by 0. It's actually not that bad because you just, you just basically have this sequence and in the- for the more general case you're computing a feature value that you just compute the full thing and you just do one single subtraction.

So it is not that costly actually to do this. Yeah. [inaudible] know the path for a given cost. If you have that input can we incorporate that into the algorithm? So, you're saying if we have some prior knowledge about the cost can we incorporate it? Yeah. Um, that is interesting. So, uh, in this current format. So if you have some prior algorithm maybe you'll like then your prediction is going to be better, right? So if you have some knowledge about it maybe you'll get a better prediction and then based on that you don't update it as much.

So maybe you can incorporate into the search problem. But again this is the most like general form of this algorithm. The simple- kind of, like the simplified version of it also like even like for the action. So not doing anything fancy. It's not doing something that hard either, honestly. Are we worried about overfitting at all? [BACKGROUND] Yeah. So it is going to- it can too- you're- yeah, so I'll show some examples on this. Like we are going to code this up and then we'll see overfitting, kind of, situations. So- so I'll get back to that actually. All right. All right. So, um, all right, so let's move on. Ah, okay. So- so this is just like the things that are on the slides are what I've already talked about. So, uh, yeah, so here's an example. So we start with, 3 for walk and 2 for tram. And then the idea is like how are we going to change the costs so we get the- the solution that we're hoping for.

Um, and- and as I was saying, well, we can assume that the costs only depend on the action. So I'm assuming cost of s, a is just w of a, and in the most general form it- it can depend on- on the state too. Um okay. So then if you take any candidate output path, then what would be the cost of the path? It would just be the sum of these W values over- over all the edges. So it would just be W of a_1 plus W of a_2 plus W of a_3. And as you've seen in this example, the cost of a path is just W of walk, plus W of walk, plus W of walk, or W of walk plus W of tram. So- so that's all this slide is saying. So- so that's how we compute the cost. All right, so- so now, uh, let's actually look at this algorithm like running in practice. Um, okay, let me actually go over the pseudocode. So- so, you start initializing W has to be equal to 0. And then after that we're going to iterate for some amount of T and then we have a training set of examples.

It might not be just one here. I just showed this one example like- like, the only training example I had was- was that walk, walk, walk is a good thing, but you can imagine having multiple training examples for a search problem. And then what you can do is you can compute your prediction so that is y prime given that you have some W and the-then you can start with this W equal to zero and then-then just compute your prediction y prime, and then basically, you can do this plus and minus type of action. So for each action that is in your true y that is in your true label, you're going to subtract 1. So to decrease the cost of true y. And then for each action that is in your prediction you're going to add- add one to- to, kind of, increase the cost of the predicted y. Okay. All right. So let's look at implementing this one. And let's try to look at some examples here. All right. So let's go back to the tram problem.

So this is again the same tram problem. We just want to use the same, sort of, format. Uh, I actually went back and wrote up the history here. If you remember the last time I was saying I'm not returning the history. Now we have a way of returning history of each one of these algorithms cause we are going to call dynamic programming and we need the history. All right. So let's go back to our transportation problem. So we had a cost of 1 and 2 for walking and tram, but what we wanna do is we wanna put parameters there. So you wanna actually put this weight and we can give that to our transportation problem. So in addition to the number of blocks, now I'm going to actually give like the weight of different actions. Okay. All right. So then walking has a weight and, um, [NOISE] tram has a weight. So now I have updated my transportation problem to generally take different weight values. So- so, now we wanna be able to generate

some- some training examples. So that's what I wanna do. I wanna generate different types of training examples that- that we can call so we can get these true labels. So let's assume that the true weights for our training example is just 1 and 2. So- so that is what we really want. Okay. And- and we're going to just wri- write this prediction function that we can call up later to- to- to get different values of y. So the prediction function is going to get the number of blocks. So- so it's going to get, um, N, the number of blocks here. And it is going to act with this path that we want. So it's going to output these- these y values, this different path. Okay. So, all right, so the whole point of prediction is- is basically, like running this f of x function. Um, and we can define our transportation problem with- with n, n weights. And the way we are going to get this is by calling dynamic programming. So someone asked you earlier could the costs be negative?

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent