1 00:00:00 --> 00:00:00 2 00:00:00 --> 00:00:01 The following content is provided under a 3 00:00:01 --> 00:00:03 Creative Commons license. 4 00:00:03 --> 00:00:06 Your support will help MIT OpenCourseWare continue to 5 00:00:06 --> 00:00:10 offer high quality educational resources for free. 6 00:00:10 --> 00:00:13 To make a donation, or view additional materials from 7 00:00:13 --> 00:00:17 hundreds of MIT courses, visit MIT OpenCourseWare 8 00:00:17 --> 00:00:21 at ocw.mit.edu. 9 00:00:21 --> 00:00:25 PROFESSOR: OK, we're now, kind of on the home stretch, and 10 00:00:25 --> 00:00:28 we're entering the part of the course, that's actually my 11 00:00:28 --> 00:00:30 favorite part of the course. 12 00:00:30 --> 00:00:33 I can't promise it will be your favorite part of the course, 13 00:00:33 --> 00:00:37 but I hope so, at least for many of you. 14 00:00:37 --> 00:00:42 Throughout the term, we've been talking about ways to solve 15 00:00:42 --> 00:00:45 problems using computation. 16 00:00:45 --> 00:00:49 And one of the key lessons that I hope you're beginning to 17 00:00:49 --> 00:00:53 absorb is that we might use a completely different way to 18 00:00:53 --> 00:00:57 solve a problem with the computer than we would have 19 00:00:57 --> 00:01:02 used if we didn't have a computer handy. 20 00:01:02 --> 00:01:07 In particular, we might often use brute force, which you've 21 00:01:07 --> 00:01:11 never use with a pencil and paper, we might not do the 22 00:01:11 --> 00:01:15 mental gyrations required to try and formulate a closed form 23 00:01:15 --> 00:01:18 solution, but just guess a bunch of answers using 24 00:01:18 --> 00:01:22 successive approximation until we got there. 25 00:01:22 --> 00:01:25 A number of different techniques. 26 00:01:25 --> 00:01:27 And that's really what we're going to be doing for the 27 00:01:27 --> 00:01:30 rest of the term now. 28 00:01:30 --> 00:01:33 Except we won't be talking about algorithms per 29 00:01:33 --> 00:01:36 se, or not very much. 30 00:01:36 --> 00:01:41 Instead we'll be talking about more general techniques for 31 00:01:41 --> 00:01:45 using computers to solve problems that are 32 00:01:45 --> 00:01:46 actually hard. 33 00:01:46 --> 00:01:51 They don't just look hard, they in many cases really are hard. 34 00:01:51 --> 00:01:55 The plan of the next set of lectures is, I want to start 35 00:01:55 --> 00:01:59 with a simple example to give you a flavor of some 36 00:01:59 --> 00:02:02 of these issues. 37 00:02:02 --> 00:02:07 In the course of going through that example, I'll illustrate 38 00:02:07 --> 00:02:11 some both thinking tools and some software tools that can be 39 00:02:11 --> 00:02:14 used for tackling the example. 40 00:02:14 --> 00:02:19 Then abstract from the example to try and put in 41 00:02:19 --> 00:02:23 a more general framework. 42 00:02:23 --> 00:02:27 And then, dive back down and use that framework to 43 00:02:27 --> 00:02:31 tackle a series of other interesting problems. 44 00:02:31 --> 00:02:35 Some things I would like you do think about learning along the 45 00:02:35 --> 00:02:55 way, is moving from an informal problem description to a more 46 00:02:55 --> 00:03:06 formal problem statement. 47 00:03:06 --> 00:03:09 So that's, in part, what we did with the optimization 48 00:03:09 --> 00:03:10 problems, right? 49 00:03:10 --> 00:03:13 We looked at an informal description about optimizing 50 00:03:13 --> 00:03:16 your way through the MIT curriculum, and then could 51 00:03:16 --> 00:03:21 formulate it using sigmas and other bizarre notation to try 52 00:03:21 --> 00:03:25 and formalize what we were really trying to do. 53 00:03:25 --> 00:03:29 And we did that as a preamble to writing any code. 54 00:03:29 --> 00:03:32 First understand the problem, formally, and 55 00:03:32 --> 00:03:34 then move on to the code. 56 00:03:34 --> 00:03:36 And we won't always be totally formal. 57 00:03:36 --> 00:03:44 I often like to use the word rigorous instead of formal. 58 00:03:44 --> 00:03:48 Implying that it won't look like math, per se, but 59 00:03:48 --> 00:03:53 it'll be precise and relatively unambiguous. 60 00:03:53 --> 00:03:56 So that will be one thing that I want you to think about as 61 00:03:56 --> 00:04:02 we go through these problems. 62 00:04:02 --> 00:04:19 Another thing is inventing computational models. 63 00:04:19 --> 00:04:23 Almost every meaningful program we write is in some sense 64 00:04:23 --> 00:04:26 modeling the actual world. 65 00:04:26 --> 00:04:29 For writing a program to figure out how to keep a bridge from 66 00:04:29 --> 00:04:33 falling down, we're modeling the physics of bridges and 67 00:04:33 --> 00:04:34 wind and things like that. 68 00:04:34 --> 00:04:39 If we're writing a program to try and help us decide what 69 00:04:39 --> 00:04:43 stocks to buy or sell, we're trying to model the stock 70 00:04:43 --> 00:04:45 market in some sense. 71 00:04:45 --> 00:04:48 If we're trying to figure out who's going to win the Super 72 00:04:48 --> 00:04:52 Bowl, we're modeling football teams. 73 00:04:52 --> 00:04:56 But almost every problem, you build a piece of software, and 74 00:04:56 --> 00:05:02 you hope that it has some ability to mimic the actual 75 00:05:02 --> 00:05:05 situation you care about. 76 00:05:05 --> 00:05:09 And it's not the program we care about, per se, it's the 77 00:05:09 --> 00:05:14 world, and the program is merely a mechanism to help us 78 00:05:14 --> 00:05:16 understand the world better. 79 00:05:16 --> 00:05:19 And so we're going to ask the question, have we built a good 80 00:05:19 --> 00:05:23 model, in the computation, that gives us insight 81 00:05:23 --> 00:05:27 into the world? 82 00:05:27 --> 00:05:35 As we do this, we'll see that we'll be dealing with and 83 00:05:35 --> 00:05:51 exploiting randomness. 84 00:05:51 --> 00:05:54 Depending upon your outlook of life, one of the sad things in 85 00:05:54 --> 00:05:57 the world or one of the happy things in the world, is 86 00:05:57 --> 00:06:00 that it's unpredictable. 87 00:06:00 --> 00:06:07 And things happen either at random or seemingly at random. 88 00:06:07 --> 00:06:12 It may be that if we had a deep enough understanding at the 89 00:06:12 --> 00:06:17 level of single atoms of the way the world works, we could 90 00:06:17 --> 00:06:20 model the weather and discover that, in fact, the weather 91 00:06:20 --> 00:06:25 patterns are not random but entirely predictable. 92 00:06:25 --> 00:06:29 Since we can't do that, we have to assume that they really are 93 00:06:29 --> 00:06:33 random, and we build models of the weather that assume a 94 00:06:33 --> 00:06:37 certain amount of randomness in it. 95 00:06:37 --> 00:06:39 Maybe if we understood the stock market well enough, we 96 00:06:39 --> 00:06:44 could have predicted the collapse in October. 97 00:06:44 --> 00:06:48 But since nobody does understand it well enough, when 98 00:06:48 --> 00:06:50 people model the stock market they assume that there's a 99 00:06:50 --> 00:06:53 certain amount of randomness, certain amount of 100 00:06:53 --> 00:06:55 stochastic things. 101 00:06:55 --> 00:07:00 So, as far as we can observe the world, almost every 102 00:07:00 --> 00:07:05 interesting part of the world has randomness in it. 103 00:07:05 --> 00:07:10 And so when we build models, we will have to build models that 104 00:07:10 --> 00:07:16 are, if we want to be formal, we'll talk about a stochastic, 105 00:07:16 --> 00:07:18 which is just a fancy way of saying they incorporate 106 00:07:18 --> 00:07:22 randomness. 107 00:07:22 --> 00:07:25 So we haven't yet done that this semester, but almost 108 00:07:25 --> 00:07:28 everything we do from here on in, we will deal with a 109 00:07:28 --> 00:07:32 certain amount of randomness. 110 00:07:32 --> 00:07:36 What else are we going to be looking at? 111 00:07:36 --> 00:07:44 We'll be looking at the notion of making sense of data. 112 00:07:44 --> 00:07:49 As we look at, again, modeling the world, what we discover, 113 00:07:49 --> 00:07:51 there's a lot of data out there. 114 00:07:51 --> 00:07:55 If you work at Walmart and you're trying to decide what to 115 00:07:55 --> 00:07:59 stock on the shelves, you're building a model of what the 116 00:07:59 --> 00:08:03 customers might buy under certain circumstances, and that 117 00:08:03 --> 00:08:07 model is going to take account the entire history of 118 00:08:07 --> 00:08:10 what customer's have bought in the past. 119 00:08:10 --> 00:08:13 And that's, if you're Walmart, a lot of data. 120 00:08:13 --> 00:08:17 And so given that you have a lot of data about what's 121 00:08:17 --> 00:08:21 happened in the past, how can we interpret that, for example, 122 00:08:21 --> 00:08:25 to get insight into the future? 123 00:08:25 --> 00:08:27 And we do a lot of that. 124 00:08:27 --> 00:08:30 And so we will, for example, look at how can we draw 125 00:08:30 --> 00:08:35 pictures that help us visualize what's going on with the data, 126 00:08:35 --> 00:08:38 rather than trying to read say, a million numbers and inferring 127 00:08:38 --> 00:08:40 something from them. 128 00:08:40 --> 00:08:43 This is a big part of what people do with computers, is 129 00:08:43 --> 00:08:46 try and figure out how to understand large 130 00:08:46 --> 00:08:48 amounts of data. 131 00:08:48 --> 00:08:52 And then finally, as we go through this last third of 132 00:08:52 --> 00:09:01 the course, I want to spend time in evaluating the 133 00:09:01 --> 00:09:09 quality of answers. 134 00:09:09 --> 00:09:13 It's easy to write a program that spits out a number, or 135 00:09:13 --> 00:09:15 string, or anything else. 136 00:09:15 --> 00:09:18 What's hard is to convince yourself that what it's 137 00:09:18 --> 00:09:23 spitting out is actually telling you the truth. 138 00:09:23 --> 00:09:27 And so we're going to look at the question about, you've 139 00:09:27 --> 00:09:29 written a program to do something relatively 140 00:09:29 --> 00:09:33 complicated, you get an answer out. 141 00:09:33 --> 00:09:35 You wouldn't have written the program if you knew what the 142 00:09:35 --> 00:09:37 answer was in advance, right? 143 00:09:37 --> 00:09:40 This is not like a high school physics experiment where you 144 00:09:40 --> 00:09:43 know what the answer should be. 145 00:09:43 --> 00:09:46 Here you went to the trouble of writing the program because you 146 00:09:46 --> 00:09:48 didn't know what the answer was. 147 00:09:48 --> 00:09:51 Now the program gives you an answer, should you believe it, 148 00:09:51 --> 00:09:54 or shouldn't you believe it? 149 00:09:54 --> 00:09:56 There are people who say, well, that's what the computer 150 00:09:56 --> 00:09:58 said, it must be true. 151 00:09:58 --> 00:10:01 By now you all have enough experience with programs 152 00:10:01 --> 00:10:05 that lie to know that's not the case. 153 00:10:05 --> 00:10:08 And so we'll be talking about how do you go about looking at 154 00:10:08 --> 00:10:13 the results, and deciding whether to believe them or not. 155 00:10:13 --> 00:10:17 This is a lot of stuff, and we'll be coming back to this. 156 00:10:17 --> 00:10:22 I'll be skipping around from topic this topic. 157 00:10:22 --> 00:10:26 It may seem a little bit random, but there's actually 158 00:10:26 --> 00:10:29 a method to my madness. 159 00:10:29 --> 00:10:32 Part of it is, I believe that repeated exposure to some of 160 00:10:32 --> 00:10:35 these things is a good way to learn it. 161 00:10:35 --> 00:10:38 And so I'll be revisiting the same topic in more and more 162 00:10:38 --> 00:10:41 depth as we go, rather than taking a topic and 163 00:10:41 --> 00:10:42 exhausting it. 164 00:10:42 --> 00:10:46 So if you think about searches, this is more of a breadth first 165 00:10:46 --> 00:10:49 search than a depth first search. 166 00:10:49 --> 00:10:51 All right. 167 00:10:51 --> 00:10:54 Think about these things as we now go through the 168 00:10:54 --> 00:10:56 next set of lectures. 169 00:10:56 --> 00:10:59 So, on for the first example. 170 00:10:59 --> 00:11:07 Consider the following situation: a seriously drunken 171 00:11:07 --> 00:11:10 university student, and I emphasize university as opposed 172 00:11:10 --> 00:11:15 to institute here, is standing in the middle of a field. 173 00:11:15 --> 00:11:18 Every second, he takes a step in some direction or another, 174 00:11:18 --> 00:11:22 but, you know, just sort of pretty random, because 175 00:11:22 --> 00:11:27 he's really out of it. 176 00:11:27 --> 00:11:30 But the field is constrained that for the moment we'll 177 00:11:30 --> 00:11:32 assume that, well, we'll come back to that. 178 00:11:32 --> 00:11:34 Now I'm going to ask you just a question. 179 00:11:34 --> 00:11:37 So you've got this student who, every second or so, takes a 180 00:11:37 --> 00:11:40 step in some direction or another. 181 00:11:40 --> 00:11:45 If the student did this for 500 seconds or 1000 seconds, how 182 00:11:45 --> 00:11:50 far do you expect the student would be from where he started? 183 00:11:50 --> 00:11:52 And I say he because most of the drunks 184 00:11:52 --> 00:11:56 are, of course, males. 185 00:11:56 --> 00:11:57 Anybody want to, what do you think? 186 00:11:57 --> 00:11:59 STUDENT: Back where he started. 187 00:11:59 --> 00:12:02 PROFESSOR: So we have a thing that, on average, and of course 188 00:12:02 --> 00:12:06 since there's randomness it won't be the same every time. 189 00:12:06 --> 00:12:09 Pretty much where he started. 190 00:12:09 --> 00:12:10 Let me ask a question. 191 00:12:10 --> 00:12:13 So if you believe that, you believe he'd be probably 192 00:12:13 --> 00:12:19 the same distance in 1000 seconds as in 500 seconds. 193 00:12:19 --> 00:12:22 Anyone want to posit the counterposition? 194 00:12:22 --> 00:12:24 That in fact, the longer the clock runs, the further the 195 00:12:24 --> 00:12:29 student will be from where he started? 196 00:12:29 --> 00:12:31 Nobody? 197 00:12:31 --> 00:12:32 All right, what do you think? 198 00:12:32 --> 00:12:49 STUDENT: [INAUDIBLE] 199 00:12:49 --> 00:12:52 PROFESSOR: All right, that's a good answer. 200 00:12:52 --> 00:12:57 So the answer there was, well, if you asked for your best 201 00:12:57 --> 00:13:00 guess of where the student was, the best guess is where 202 00:13:00 --> 00:13:03 the student started. 203 00:13:03 --> 00:13:07 On the other hand, if you ask how far was the student from 204 00:13:07 --> 00:13:11 where he started, the best guess wouldn't be zero. 205 00:13:11 --> 00:13:15 That's a great answer, because it addresses the fact that you 206 00:13:15 --> 00:13:18 have to be very careful what question you're asking. 207 00:13:18 --> 00:13:20 And there are subtle differences between those two 208 00:13:20 --> 00:13:24 questions, and they might have very different answers. 209 00:13:24 --> 00:13:29 So that's part of that first step, going from an informal 210 00:13:29 --> 00:13:33 description, to trying to formalize it or be more 211 00:13:33 --> 00:13:36 rigorous about what is exactly the problem 212 00:13:36 --> 00:13:40 you're trying to solve. 213 00:13:40 --> 00:13:43 All right, we'll take a vote. 214 00:13:43 --> 00:13:47 And everyone has to vote here, because it's not a democracy 215 00:13:47 --> 00:13:51 where you're allowed to not vote. 216 00:13:51 --> 00:13:58 And the question I'm asking is, is the expected difference, 217 00:13:58 --> 00:14:06 does the expected distance from the origin grow over time, or 218 00:14:06 --> 00:14:10 remain constant, roughly? 219 00:14:10 --> 00:14:14 Who thinks it grows over time? 220 00:14:14 --> 00:14:18 Who thinks it remains constant? 221 00:14:18 --> 00:14:21 Well, the constants have it. 222 00:14:21 --> 00:14:23 But just because that's the way most people vote, it 223 00:14:23 --> 00:14:26 doesn't make it true. 224 00:14:26 --> 00:14:30 Now let's find out what the actual truth is. 225 00:14:30 --> 00:14:33 All right. 226 00:14:33 --> 00:14:38 So let's start by sketching a simple model of the situation. 227 00:14:38 --> 00:14:43 And, one of the things I want to stress as we do these 228 00:14:43 --> 00:14:55 things, in developing anything of this nature, start simple. 229 00:14:55 --> 00:14:58 So start with some simple approximation 230 00:14:58 --> 00:15:00 to the real problem. 231 00:15:00 --> 00:15:05 Check that out, and then, if it turns out not to be a good 232 00:15:05 --> 00:15:09 enough model of the world, add some complications, but 233 00:15:09 --> 00:15:11 don't start with the complicated model. 234 00:15:11 --> 00:15:14 Always start with the simple model. 235 00:15:14 --> 00:15:16 So I'm going to start with the simple model. 236 00:15:16 --> 00:15:20 And I'm going to assume, as we've seen before, that we have 237 00:15:20 --> 00:15:30 Cartesian coordinates and that the player is, or the drunk is, 238 00:15:30 --> 00:15:33 standing on a field that has been cut to resemble a 239 00:15:33 --> 00:15:36 piece of graph paper. 240 00:15:36 --> 00:15:38 They got the groundskeeper from Fenway Park or something, 241 00:15:38 --> 00:15:41 there, and it looks like a beautiful piece of paper. 242 00:15:41 --> 00:15:46 Furthermore, I'm going to assume for simplicity, that the 243 00:15:46 --> 00:15:50 student can only go in one of four directions: north, 244 00:15:50 --> 00:15:55 south, east, or west. 245 00:15:55 --> 00:15:58 OK, we could certainly generalize that, and we will, 246 00:15:58 --> 00:16:01 to something more complicated, but for now we'll 247 00:16:01 --> 00:16:03 keep it simple. 248 00:16:03 --> 00:16:06 And we'll try and go to the board and draw 249 00:16:06 --> 00:16:08 what might happen. 250 00:16:08 --> 00:16:13 So the student starts here and takes a step in one 251 00:16:13 --> 00:16:15 direction or another. 252 00:16:15 --> 00:16:19 So as the mathematicians say, without loss of generality, 253 00:16:19 --> 00:16:25 let's just assume that the first step is here. 254 00:16:25 --> 00:16:31 Well what we know for sure, is after one step the student is 255 00:16:31 --> 00:16:35 further from the origin than at the start. 256 00:16:35 --> 00:16:38 But, that doesn't tell us a lot. 257 00:16:38 --> 00:16:41 What happens after the second step? 258 00:16:41 --> 00:16:44 Well, the student could come back to the origin and be 259 00:16:44 --> 00:16:52 closer, that's one possibility, the student could go up here, 260 00:16:52 --> 00:16:56 in which case the student is a little further, the student 261 00:16:56 --> 00:16:59 could go down here, in which case the student is a little 262 00:16:59 --> 00:17:03 further, or the student could go over here, in which case 263 00:17:03 --> 00:17:06 the student is twice as far. 264 00:17:06 --> 00:17:09 So we see for the second step, three times out of four 265 00:17:09 --> 00:17:12 you get further away. 266 00:17:12 --> 00:17:15 What happens in the third step? 267 00:17:15 --> 00:17:17 So let's look at this one. 268 00:17:17 --> 00:17:22 Well, the student could come here, which is closer, could 269 00:17:22 --> 00:17:27 come here, which is closer, could go there, which is 270 00:17:27 --> 00:17:29 further, or could go there, which is further. 271 00:17:29 --> 00:17:33 So the third step, with equal probability, the student is 272 00:17:33 --> 00:17:38 further or closer if the student is here. 273 00:17:38 --> 00:17:40 And we could continue. 274 00:17:40 --> 00:17:45 So as you can see, it gets pretty complicated, as 275 00:17:45 --> 00:17:49 you project how far out it could always get. 276 00:17:49 --> 00:17:52 All right, and this is symmetric to this, but this 277 00:17:52 --> 00:17:55 is yet a different case. 278 00:17:55 --> 00:18:00 So, this sort of says, OK, I'm going to get tired of 279 00:18:00 --> 00:18:02 drawing things on the board. 280 00:18:02 --> 00:18:05 So, being who I am, I say, let's write a 281 00:18:05 --> 00:18:07 program to do this. 282 00:18:07 --> 00:18:10 And in fact, what I'm going to write a program to 283 00:18:10 --> 00:18:28 do, is simulate what is called a random walk. 284 00:18:28 --> 00:18:30 And these will be two themes we're going to spend 285 00:18:30 --> 00:18:31 a lot of time on. 286 00:18:31 --> 00:18:37 Simulation, where we try and build the model that pretends 287 00:18:37 --> 00:18:40 it's the real world and simulates what goes on, 288 00:18:40 --> 00:18:43 and a random walk. 289 00:18:43 --> 00:18:47 Now, I'm giving you the classic story about a random walk which 290 00:18:47 --> 00:18:51 you can visualize, at least I hope, but as we'll see, random 291 00:18:51 --> 00:18:56 walks are very general, and are used to address a lot 292 00:18:56 --> 00:18:58 of real problems. 293 00:18:58 --> 00:19:02 So we'll write this program, and I want to start by 294 00:19:02 --> 00:19:07 thinking about designing the structure of the solution. 295 00:19:07 --> 00:19:11 Because one of the things I'm trying to do in this next set 296 00:19:11 --> 00:19:16 of lectures is bring together a lot of the things we have 297 00:19:16 --> 00:19:19 talked about over the course of the semester, about how we go 298 00:19:19 --> 00:19:22 about designing and building programs. 299 00:19:22 --> 00:19:26 Trying to give you a case study, if you will. 300 00:19:26 --> 00:19:30 So let's begin in line with what Professor Grimson talked 301 00:19:30 --> 00:19:32 about, about thinking about what might be the appropriate 302 00:19:32 --> 00:19:36 data abstractions. 303 00:19:36 --> 00:19:41 Well, so, I think it would be good to have a location, since 304 00:19:41 --> 00:19:49 after all the whole problem talks about where the drunk is. 305 00:19:49 --> 00:19:53 I decided I also wanted to introduce an abstraction called 306 00:19:53 --> 00:19:58 compass point to capture the notion that the student is 307 00:19:58 --> 00:20:01 going north, south, east, or west. 308 00:20:01 --> 00:20:05 And later, maybe I'll decide that needs to be more 309 00:20:05 --> 00:20:07 complicated and that the student can go north by 310 00:20:07 --> 00:20:12 northwest, or maybe all sorts of things. 311 00:20:12 --> 00:20:17 But that it would probably pay to separate the notion of 312 00:20:17 --> 00:20:21 direction, which is what this is, from location. 313 00:20:21 --> 00:20:23 Maybe I should have called this direction instead of compass 314 00:20:23 --> 00:20:26 point, but I didn't. 315 00:20:26 --> 00:20:29 But, I thought about the problem and said, well, 316 00:20:29 --> 00:20:31 these things really are separate locations. 317 00:20:31 --> 00:20:34 Where you are and where you might head from there 318 00:20:34 --> 00:20:39 are not the same, so let's separate them. 319 00:20:39 --> 00:20:43 Then I said, well of course, there is this notion that the 320 00:20:43 --> 00:20:48 person is in the field, so maybe I want to have field 321 00:20:48 --> 00:20:49 as a separate thing. 322 00:20:49 --> 00:20:54 So think of that as the whole Cartesian plain, as opposed to 323 00:20:54 --> 00:20:58 a point in the plane, which is what the location is. 324 00:20:58 --> 00:21:03 And finally, I better have a drunk, because after all this 325 00:21:03 --> 00:21:07 problem is all about drunks. 326 00:21:07 --> 00:21:10 All right, so we're now going to look at some code. 327 00:21:10 --> 00:21:15 I made this code as simple as I could to illustrate the points 328 00:21:15 --> 00:21:17 I wanted to illustrate. 329 00:21:17 --> 00:21:22 This means I left out a lot of things that ought to be 330 00:21:22 --> 00:21:23 included in a good program. 331 00:21:23 --> 00:21:25 So I want to just warn you. 332 00:21:25 --> 00:21:28 So for example, I've already told you that when I build data 333 00:21:28 --> 00:21:32 abstractions, I always put in an underbar underbar str 334 00:21:32 --> 00:21:35 function so that I can print them when I'm debugging. 335 00:21:35 --> 00:21:38 Well, you won't see that in this code, because I felt it 336 00:21:38 --> 00:21:41 just cluttered the code up to make the points. 337 00:21:41 --> 00:21:43 You'll see less defensive programming than I 338 00:21:43 --> 00:21:45 would normally use. 339 00:21:45 --> 00:21:47 But again I wanted to sort of pare things down to the 340 00:21:47 --> 00:21:51 essence, since the essence is, all by itself, probably 341 00:21:51 --> 00:21:58 confusing enough. 342 00:21:58 --> 00:22:00 All right. 343 00:22:00 --> 00:22:00 So let's look at it. 344 00:22:00 --> 00:22:04 You have this on your double-sided handout. 345 00:22:04 --> 00:22:09 This is on side 1one So at the top, you'll see that 346 00:22:09 --> 00:22:11 I'm importing three things. 347 00:22:11 --> 00:22:14 Well, math you've heard about. 348 00:22:14 --> 00:22:16 And I'm importing that because I'm going to 349 00:22:16 --> 00:22:18 need a square root. 350 00:22:18 --> 00:22:22 Random, you haven't heard about. 351 00:22:22 --> 00:22:27 This is a package that lets me choose things at random, and 352 00:22:27 --> 00:22:31 I'll show you some of the ways we can use it, but it actually 353 00:22:31 --> 00:22:43 provides something that technically is pseudo-random. 354 00:22:43 --> 00:22:46 Which means that as far as we can tell, it's behaving 355 00:22:46 --> 00:22:51 randomly, but since the computer is in itself a 356 00:22:51 --> 00:22:56 deterministic machine, it's not really random. 357 00:22:56 --> 00:22:59 But it's so close to random that we might as well 358 00:22:59 --> 00:23:04 pretend it is, you can't tell that it isn't. 359 00:23:04 --> 00:23:05 So we'll import random. 360 00:23:05 --> 00:23:08 That will let us make random guesses of things like, give 361 00:23:08 --> 00:23:11 me a random number between 0 and 1. 362 00:23:11 --> 00:23:13 And it will give you a random number. 363 00:23:13 --> 00:23:16 And finally something called Pylab. 364 00:23:16 --> 00:23:19 Anyone here use Matlab? 365 00:23:19 --> 00:23:23 All right, well then, you'll find Pylab kind of comforting. 366 00:23:23 --> 00:23:32 Pylab brings into Python a lot of the features of Matlab And 367 00:23:32 --> 00:23:34 if you haven't used Matlab, don't worry, because we'll 368 00:23:34 --> 00:23:36 explain everything. 369 00:23:36 --> 00:23:39 For the purposes of this set of lectures, the next few 370 00:23:39 --> 00:23:42 lectures, at least, the only thing we're going to get out of 371 00:23:42 --> 00:23:46 this is a bunch of tools for drawing pretty graphs. 372 00:23:46 --> 00:23:48 And we won't get to those today, probably, so 373 00:23:48 --> 00:23:50 don't worry about it. 374 00:23:50 --> 00:23:55 But it's a nice package, which you'll be glad to learn. 375 00:23:55 --> 00:23:59 OK, now let's move on to the code, which has got things that 376 00:23:59 --> 00:24:01 should look familiar to you. 377 00:24:01 --> 00:24:03 And I know there used to be a laser -- ah, here's 378 00:24:03 --> 00:24:05 the laser pointer. 379 00:24:05 --> 00:24:12 So we'll see at the top location, it is a class. 380 00:24:12 --> 00:24:16 By now you've gotten as familiar as you want to be in 381 00:24:16 --> 00:24:18 many senses, with classes. 382 00:24:18 --> 00:24:22 It's got an underbar underbar init that gives me, 383 00:24:22 --> 00:24:24 essentially, a point with an x-coordinate and 384 00:24:24 --> 00:24:28 a y-coordinate. 385 00:24:28 --> 00:24:32 It's got get coords, the third function, or method, and what 386 00:24:32 --> 00:24:34 you can see what that does is it's returning a 387 00:24:34 --> 00:24:36 tuple of two values. 388 00:24:36 --> 00:24:41 I could have had it get x and get y, but it turned out, in 389 00:24:41 --> 00:24:43 fact, my first iteration of this it did have a get x and 390 00:24:43 --> 00:24:48 get y, and when I looked at the code that was using it, I 391 00:24:48 --> 00:24:52 realized whenever I got one, I wanted both, and it just seemed 392 00:24:52 --> 00:24:54 kind of silly, so I did something that made the using 393 00:24:54 --> 00:24:59 code a little bit better. 394 00:24:59 --> 00:25:02 It's got get distance, the last method. 395 00:25:02 --> 00:25:05 You saw this in Professor Grimson's lecture, where he 396 00:25:05 --> 00:25:09 used the Pythagorean theorem to basically compute the distance 397 00:25:09 --> 00:25:13 on a hypotenuse, to tell you how far a point was 398 00:25:13 --> 00:25:15 from the origin. 399 00:25:15 --> 00:25:17 That's why I wanted math. 400 00:25:17 --> 00:25:20 And then the one thing it has that it's unlike the example 401 00:25:20 --> 00:25:25 you saw from Professor Grimson, was up here, this move. 402 00:25:25 --> 00:25:29 And this basically takes a point, a location, and an x- 403 00:25:29 --> 00:25:34 and a y- coordinate, and returns another location, in 404 00:25:34 --> 00:25:40 which I've incremented the x and the y, perhaps 405 00:25:40 --> 00:25:43 incrementing by 0. 406 00:25:43 --> 00:25:47 And so now you can see how I'm going to be able to mimic 407 00:25:47 --> 00:25:53 1 step or any number of steps by using this move. 408 00:25:53 --> 00:25:59 Any questions about this? 409 00:25:59 --> 00:26:01 Great, all right. 410 00:26:01 --> 00:26:06 The next one you'll see is compass point. 411 00:26:06 --> 00:26:08 Remember, this was the abstraction that was going 412 00:26:08 --> 00:26:12 to let me conveniently deal with directions. 413 00:26:12 --> 00:26:18 The first thing you'll see is, there's a global variable in 414 00:26:18 --> 00:26:23 the sense that it's external to any of the methods, and you'll 415 00:26:23 --> 00:26:28 note it's not self dot possibles, but just possibles. 416 00:26:28 --> 00:26:34 Because I don't want a new copy of this for every instance. 417 00:26:34 --> 00:26:37 And this tells me the possible directions, which I've 418 00:26:37 --> 00:26:40 abbreviated as n, s, e, and w. 419 00:26:40 --> 00:26:43 And I leave it to you to figure out what those 420 00:26:43 --> 00:26:47 abbreviations stand for. 421 00:26:47 --> 00:26:53 Then I've got init, which takes a point and it first checks to 422 00:26:53 --> 00:26:59 see if the point is in self dot possibles. 423 00:26:59 --> 00:27:01 Should I have bothered saying self? 424 00:27:01 --> 00:27:03 Do I need to write self there? 425 00:27:03 --> 00:27:05 This is a test of classes. 426 00:27:05 --> 00:27:09 It'll work. 427 00:27:09 --> 00:27:11 What do you think? 428 00:27:11 --> 00:27:16 Who thinks I need to write self, and who thinks I don't? 429 00:27:16 --> 00:27:19 Who thinks I need to write self, raise your hand? 430 00:27:19 --> 00:27:22 Who thinks I don't need to write self? 431 00:27:22 --> 00:27:24 The don't needs have it. 432 00:27:24 --> 00:27:27 So in an act of intense bravery, since I have not run 433 00:27:27 --> 00:27:33 it without this, we'll see what happens when it comes 434 00:27:33 --> 00:27:38 time to run the program. 435 00:27:38 --> 00:27:42 So if point is in possibles, it will take self dot p 436 00:27:42 --> 00:27:45 t will be assigned p t. 437 00:27:45 --> 00:27:49 Otherwise, I'll raise a value error. 438 00:27:49 --> 00:27:52 And this is a little piece of programming, and what I 439 00:27:52 --> 00:27:56 typically do at a minimum, when I raise these exceptions, 440 00:27:56 --> 00:28:00 is make sure that it says where it's coming from. 441 00:28:00 --> 00:28:03 So this says it's gonna raise this value error in the 442 00:28:03 --> 00:28:07 method compass point dot underbar underbar init, 443 00:28:07 --> 00:28:09 underbar underbar. 444 00:28:09 --> 00:28:13 This is so when I run the program, and I see a message, 445 00:28:13 --> 00:28:15 I don't have to scratch my head figuring out where 446 00:28:15 --> 00:28:17 did things go wrong? 447 00:28:17 --> 00:28:21 So just a convention I follow a lot. 448 00:28:21 --> 00:28:25 And then I've got move here. 449 00:28:25 --> 00:28:30 And I'll move self some distance, and you can 450 00:28:30 --> 00:28:31 see what I'm doing. 451 00:28:31 --> 00:28:41 If self is north, I'm going to return 0 in distance. 452 00:28:41 --> 00:28:45 So I'm now getting a tuple, which will basically be used to 453 00:28:45 --> 00:28:49 say, all right, we're going to implement x by 0 and y by 454 00:28:49 --> 00:28:51 distance, which is what you think of about 455 00:28:51 --> 00:28:54 moving due north. 456 00:28:54 --> 00:29:00 And if it's south, we'll increment x by 0, and increment 457 00:29:00 --> 00:29:05 y by minus distance, heading down the graph. 458 00:29:05 --> 00:29:09 And similarly for east and west. 459 00:29:09 --> 00:29:12 And in the sad event I call this with something that's not 460 00:29:12 --> 00:29:20 north, south, east, or west, again I'll raise an exception. 461 00:29:20 --> 00:29:22 OK, little bit at a time. 462 00:29:22 --> 00:29:25 So I hope that nothing here looks complicated. 463 00:29:25 --> 00:29:29 In fact, it should all look kind of boring. 464 00:29:29 --> 00:29:34 Now we'll get to field, which should look a 465 00:29:34 --> 00:29:38 little less boring. 466 00:29:38 --> 00:29:43 So now I've got a field. 467 00:29:43 --> 00:29:53 And, well, before I get to field, I want to go back to 468 00:29:53 --> 00:29:55 something about the first 2 abstractions: compass 469 00:29:55 --> 00:29:58 point and location. 470 00:29:58 --> 00:30:03 Whenever we design one of these abstractions, we're making some 471 00:30:03 --> 00:30:06 things possible and some things impossible. 472 00:30:06 --> 00:30:13 We're making decisions, and so what decisions are encapsulated 473 00:30:13 --> 00:30:16 in these first two classes? 474 00:30:16 --> 00:30:22 Well, one decision is that it's a 2-dimensional space. 475 00:30:22 --> 00:30:26 I basically said we've got x and y and that's all we've got. 476 00:30:26 --> 00:30:29 This student cannot fly. 477 00:30:29 --> 00:30:33 The student cannot dig a hole and go into the ground, we're 478 00:30:33 --> 00:30:37 only moving in 2 dimensions, so that's fixed. 479 00:30:37 --> 00:30:41 The other decision I made is that this student can only 480 00:30:41 --> 00:30:45 move in 1 of 4 directions. 481 00:30:45 --> 00:30:48 So, couple of good things here. 482 00:30:48 --> 00:30:51 One, I've simplified the world, which will make it easier for 483 00:30:51 --> 00:30:53 us to see what's going on. 484 00:30:53 --> 00:30:58 But two, I know where I've made those decisions, and later if I 485 00:30:58 --> 00:31:02 want to go back and say, you know it'd be fun if the student 486 00:31:02 --> 00:31:05 could fly, let's say we're a drunken pigeon instead 487 00:31:05 --> 00:31:07 of a drunken student. 488 00:31:07 --> 00:31:11 Or it would be a good idea to realize that, in fact, they 489 00:31:11 --> 00:31:13 might head off at any weird angle. 490 00:31:13 --> 00:31:16 I know what code I have to change. 491 00:31:16 --> 00:31:20 It's encapsulated in a single place. 492 00:31:20 --> 00:31:26 So that's why possibles is part of compass point, rather than 493 00:31:26 --> 00:31:31 scattered throughout the program. 494 00:31:31 --> 00:31:33 So this is nice way to think about it. 495 00:31:33 --> 00:31:37 All right, let's move on and look at field. 496 00:31:37 --> 00:31:41 So field, it's got an init, it takes a self, a drunk, and a 497 00:31:41 --> 00:31:52 location, and puts the drunk in the field at that location. 498 00:31:52 --> 00:31:54 It can move. 499 00:31:54 --> 00:31:59 So let's look at what happens when we try and move self in 500 00:31:59 --> 00:32:02 some direction, in some distance. 501 00:32:02 --> 00:32:10 We say the old location is the current location, and then x c 502 00:32:10 --> 00:32:16 and y c, think of that as x-change and y-change, get 503 00:32:16 --> 00:32:21 c p dot move of dist. 504 00:32:21 --> 00:32:28 Where is it gonna find c p dot move? 505 00:32:28 --> 00:32:32 It's going to use compass point dot move, because as we'll see, 506 00:32:32 --> 00:32:38 c p is an object of type, of class, compass point. 507 00:32:38 --> 00:32:42 So this is a normal and typical way we structure 508 00:32:42 --> 00:32:44 things with classes. 509 00:32:44 --> 00:32:47 The move of one class is defined using the move 510 00:32:47 --> 00:32:51 of another class. 511 00:32:51 --> 00:32:56 This gives us the modularity we so prize. 512 00:32:56 --> 00:33:01 And then I'll say self dot location is old loc dot 513 00:33:01 --> 00:33:05 move of x c and x c. 514 00:33:05 --> 00:33:09 Now this is oldloc dot move is going to get the move 515 00:33:09 --> 00:33:14 from class location. 516 00:33:14 --> 00:33:17 And you'll remember, that was the thing that just added 517 00:33:17 --> 00:33:22 appropriate values to x and y. 518 00:33:22 --> 00:33:26 So I'll use compass point to get those values, and then 519 00:33:26 --> 00:33:30 oldloc to get the new location. 520 00:33:30 --> 00:33:32 Then I'll be able to get the loc and I'll be 521 00:33:32 --> 00:33:35 able to get the drunk. 522 00:33:35 --> 00:33:38 So, let me ask the same question about field I've 523 00:33:38 --> 00:33:41 asked about the others? 524 00:33:41 --> 00:33:47 What interesting decisions, if any, have I embodied in the way 525 00:33:47 --> 00:33:52 I've designed and implemented this abstraction? 526 00:33:52 --> 00:34:00 There's at least one pretty interesting decision here. 527 00:34:00 --> 00:34:02 Just interesting to me because my first version 528 00:34:02 --> 00:34:06 was far more complicated. 529 00:34:06 --> 00:34:12 Well, how many drunks can I have in a field at a time? 530 00:34:12 --> 00:34:15 We have an answer which will not be recorded, because it was 531 00:34:15 --> 00:34:18 merely raising a finger, but it happened to be the correct 532 00:34:18 --> 00:34:23 number of fingers: one. 533 00:34:23 --> 00:34:29 And it was this finger that got pointed at me. 534 00:34:29 --> 00:34:33 We've embodied the fact that you can have one drunk, exactly 535 00:34:33 --> 00:34:35 one drunk in the field. 536 00:34:35 --> 00:34:41 This is a solitary alcoholic. 537 00:34:41 --> 00:34:43 Later we might say, well, it would be fun to put a whole 538 00:34:43 --> 00:34:45 bunch of drunken students in and watch what happens when 539 00:34:45 --> 00:34:48 they bump into each other. 540 00:34:48 --> 00:34:50 And we'll actually later give you a problem set, not with 541 00:34:50 --> 00:34:53 students, but with other things where there might be more 542 00:34:53 --> 00:34:55 than one in a field. 543 00:34:55 --> 00:34:57 But here, I've made that decision. 544 00:34:57 --> 00:35:01 But again, I know where I've made it, and if later I go back 545 00:35:01 --> 00:35:04 and say let's put a bunch of them in, I don't have to change 546 00:35:04 --> 00:35:07 compass point, I don't have to change location, I only 547 00:35:07 --> 00:35:10 have to change field. 548 00:35:10 --> 00:35:13 And we'll also see, I don't have to change drunk. 549 00:35:13 --> 00:35:18 So again, it's very nice that the class structure, the 550 00:35:18 --> 00:35:21 modularity let's us have decisions in exactly 551 00:35:21 --> 00:35:24 one place in our code. 552 00:35:24 --> 00:35:26 Usually important. 553 00:35:26 --> 00:35:31 All right, now let's look at drunk. 554 00:35:31 --> 00:35:37 So the drunk has a name. 555 00:35:37 --> 00:35:41 And, like everything else, a move operation. 556 00:35:41 --> 00:35:44 This is the most complicated and interesting move. 557 00:35:44 --> 00:35:48 Because here is where I'm encapsulating the decisions 558 00:35:48 --> 00:35:52 about what the drunk actually does. 559 00:35:52 --> 00:35:54 That the drunk, for example, doesn't head north and 560 00:35:54 --> 00:35:57 just keep on going. 561 00:35:57 --> 00:36:02 So, let's look at what happens here. 562 00:36:02 --> 00:36:07 It's got three parameters: self, the field the drunk 563 00:36:07 --> 00:36:11 is moving in, and time. 564 00:36:11 --> 00:36:13 How long the drunk is going to move. 565 00:36:13 --> 00:36:16 And you may notice something that you haven't seen before, 566 00:36:16 --> 00:36:26 at least some of you, time equals one is sitting up there. 567 00:36:26 --> 00:36:32 There. 568 00:36:32 --> 00:36:38 Python allows us to have what are called default 569 00:36:38 --> 00:36:48 values for parameters. 570 00:36:48 --> 00:36:54 What this says is that if I call this method without that 571 00:36:54 --> 00:36:58 last argument, rather than getting an error message saying 572 00:36:58 --> 00:37:02 it expects three arguments it only got two, it chooses the 573 00:37:02 --> 00:37:08 default value, in this case one for the third argument. 574 00:37:08 --> 00:37:13 This is actually a pretty useful programming paradigm, 575 00:37:13 --> 00:37:18 because there's often a very sensible default. 576 00:37:18 --> 00:37:21 And it can simplify things. 577 00:37:21 --> 00:37:24 It's not an intrinsically interesting or important part 578 00:37:24 --> 00:37:26 of what I'm showing you here. 579 00:37:26 --> 00:37:30 The reason I'm showing it is, you'll be getting a problem set 580 00:37:30 --> 00:37:33 in which default values are there because we're using 581 00:37:33 --> 00:37:35 some other things. 582 00:37:35 --> 00:37:39 And as you bring in libraries and modules from elsewhere, 583 00:37:39 --> 00:37:42 you'll find that there are a lot of these things. 584 00:37:42 --> 00:37:44 And in fact a lot of the functions you've already been 585 00:37:44 --> 00:37:49 using for the built-in types happen to have default values. 586 00:37:49 --> 00:37:54 For example, when you look at things in the init part 587 00:37:54 --> 00:38:01 of a range, it's actually choosing, say, 0 as a start. 588 00:38:01 --> 00:38:02 But don't worry about it. 589 00:38:02 --> 00:38:06 This just says if you don't pass it a time, use 1. 590 00:38:06 --> 00:38:16 And then what it does, is it says, if field dot get drunk 591 00:38:16 --> 00:38:25 is not equal to self, raise an exception. 592 00:38:25 --> 00:38:27 OK, you've asked me to move a drunk, it doesn't happen 593 00:38:27 --> 00:38:31 to be in the field. 594 00:38:31 --> 00:38:33 That's not very interesting. 595 00:38:33 --> 00:38:35 But now we come to the interesting part. 596 00:38:35 --> 00:38:40 For i in range time, here by the way range does have a 597 00:38:40 --> 00:38:48 default value of 0, and since I didn't supply it, it used it. 598 00:38:48 --> 00:38:52 Point equals compass point, and here's the most interesting 599 00:38:52 --> 00:38:58 thing, random dot choice of compass point dot possibles. 600 00:38:58 --> 00:39:03 Random dot choice is a function in the random module which 601 00:39:03 --> 00:39:10 we've imported and it takes as an argument a sequence, a list 602 00:39:10 --> 00:39:13 of some sort, a container, and it picks a random 603 00:39:13 --> 00:39:15 element out of that. 604 00:39:15 --> 00:39:21 So here we're passing it in, four possible values, and 605 00:39:21 --> 00:39:25 it's just going to pick one of them at random. 606 00:39:25 --> 00:39:31 So you can see by my having encapsulated the possibles 607 00:39:31 --> 00:39:37 array in compass point, I don't have to worry in drunk about 608 00:39:37 --> 00:39:44 how many possible directions this person could head off in. 609 00:39:44 --> 00:39:49 And then it calls field dot move, with the resultant 610 00:39:49 --> 00:39:54 point, and in this case one. 611 00:39:54 --> 00:39:57 All right? 612 00:39:57 --> 00:40:02 We've now finished all of this sort of groundwork that we need 613 00:40:02 --> 00:40:05 to go and actually build the simulation that will 614 00:40:05 --> 00:40:09 model our problem. 615 00:40:09 --> 00:40:13 So let's look at how we use all of this. 616 00:40:13 --> 00:40:20 I've now got a function perform trial, which will take 617 00:40:20 --> 00:40:26 a time in a field. 618 00:40:26 --> 00:40:31 So I'll get a starting point of f dot getloc, wherever the 619 00:40:31 --> 00:40:35 drunk happens to be at this point in time in the field. 620 00:40:35 --> 00:40:42 And then for t in range 1 to time plus 1, I'm going to call 621 00:40:42 --> 00:40:47 field dot get drunk, that will give me the drunk that's in the 622 00:40:47 --> 00:40:56 field, and then I'll get the move function for that drunk. 623 00:40:56 --> 00:41:06 So what you see here is I can actually, in this dot notation, 624 00:41:06 --> 00:41:09 take advantage of the fact, where are we, we're down here, 625 00:41:09 --> 00:41:14 I've got to get far enough away that I can see it 626 00:41:14 --> 00:41:17 -- Where was I? 627 00:41:17 --> 00:41:19 STUDENT: You were in the right place right 628 00:41:19 --> 00:41:21 there, down slightly. 629 00:41:21 --> 00:41:24 PROFESSOR: -- down slightly, here, right. 630 00:41:24 --> 00:41:29 I've called the function, which has returned an object, f 631 00:41:29 --> 00:41:35 dot get drunk returns an object of class drunk. 632 00:41:35 --> 00:41:39 And then I can select the method associated with that 633 00:41:39 --> 00:41:44 object, the move method, and move the drunk. 634 00:41:44 --> 00:41:48 This gets back to a point that we've emphasized before, 635 00:41:48 --> 00:41:53 that these objects are first class citizens in Python. 636 00:41:53 --> 00:41:56 You can use functions and methods to generate them, 637 00:41:56 --> 00:42:01 and use them just as if you'd typed them. 638 00:42:01 --> 00:42:07 So that will get me that, and then I'll call the move of 639 00:42:07 --> 00:42:11 that drunk with the field. 640 00:42:11 --> 00:42:18 Then I'll get the location, the new location, and then I'll 641 00:42:18 --> 00:42:24 say, distance equals new loc dot get distance of start, how 642 00:42:24 --> 00:42:28 far is the new location from wherever the starting 643 00:42:28 --> 00:42:30 location was? 644 00:42:30 --> 00:42:35 I'll append it to a list, and then I'll return it. 645 00:42:35 --> 00:42:43 So now I have a list, and in the list I have how far away 646 00:42:43 --> 00:42:46 the drunk is after each time step. 647 00:42:46 --> 00:42:49 I'm just collecting the list of distances. 648 00:42:49 --> 00:42:51 So I don't have a list of locations, I don't know where, 649 00:42:51 --> 00:43:00 I can't plot the trajectory, but I can plot the distances. 650 00:43:00 --> 00:43:10 OK, now, let's put it all together. 651 00:43:10 --> 00:43:15 So I'll say the drunk is equal to, is drunk of Homer Simpson. 652 00:43:15 --> 00:43:18 I had thought about using the name of someone on campus and 653 00:43:18 --> 00:43:21 decided that, since it was going to be taped for 654 00:43:21 --> 00:43:26 OpenCourseWare, I didn't want to go there. 655 00:43:26 --> 00:43:29 Sometimes I lack courage. 656 00:43:29 --> 00:43:34 Then for i in range 3, and all this says is I'm going to test, 657 00:43:34 --> 00:43:38 I'm going to run the simulation three different times, f is 658 00:43:38 --> 00:43:44 equal to field of drunk and location 0, 0, starting in 659 00:43:44 --> 00:43:47 the middle of the field. 660 00:43:47 --> 00:43:55 Distances equal perform trial 500, 500 steps on that field, 661 00:43:55 --> 00:43:58 and the rest of it is magic you don't need to understand 662 00:43:58 --> 00:44:01 because we'll come back to next lecture, which is, how do I put 663 00:44:01 --> 00:44:04 all this in a pretty picture? 664 00:44:04 --> 00:44:08 So ignore all of that for now, and that's just the Pylab stuff 665 00:44:08 --> 00:44:11 for plotting the pictures. 666 00:44:11 --> 00:44:22 All right, we're there, let's run it. 667 00:44:22 --> 00:44:24 Huh. 668 00:44:24 --> 00:44:27 Remember the change we made? 669 00:44:27 --> 00:44:31 Guess what? 670 00:44:31 --> 00:44:40 Well, we know how to fix that. 671 00:44:40 --> 00:44:43 And we'll come back to it and ask what's really 672 00:44:43 --> 00:44:46 going on here. 673 00:44:46 --> 00:44:52 What does self refer to here? 674 00:44:52 --> 00:44:57 It refers to the class compass point, rather than an 675 00:44:57 --> 00:45:00 instance of the class. 676 00:45:00 --> 00:45:04 Again, driving home the point that classes are objects 677 00:45:04 --> 00:45:05 just like everything else. 678 00:45:05 --> 00:45:13 STUDENT: [INAUDIBLE] 679 00:45:13 --> 00:45:15 PROFESSOR: Oh, sorry. 680 00:45:15 --> 00:45:16 Thank you. 681 00:45:16 --> 00:45:23 It did it have in here, right. 682 00:45:23 --> 00:45:28 All right, now let's run it, see what we get. 683 00:45:28 --> 00:45:32 We get a picture. 684 00:45:32 --> 00:45:36 Well, so we see that, at least for these three 685 00:45:36 --> 00:45:43 tests, the majority of the public was wrong. 686 00:45:43 --> 00:45:48 It seems that the longer we run it, the further from the origin 687 00:45:48 --> 00:45:52 Homer seems to be getting. 688 00:45:52 --> 00:45:53 Well, let's try it again. 689 00:45:53 --> 00:45:56 Maybe we just got unlucky three times. 690 00:45:56 --> 00:46:00 You can see from the divergence here, that, of course, 691 00:46:00 --> 00:46:01 a lot of things go on. 692 00:46:01 --> 00:46:10 I hope you can also see the advantage of being able to 693 00:46:10 --> 00:46:17 plot it rather than having to look at those arrays. 694 00:46:17 --> 00:46:19 Well, we get different answers, but you know what? 695 00:46:19 --> 00:46:25 The trend still seems pretty clear. 696 00:46:25 --> 00:46:30 It does seem over time, that we wander further away. 697 00:46:30 --> 00:46:33 I know, further away. 698 00:46:33 --> 00:46:40 So, looks at least for the moment, that 699 00:46:40 --> 00:46:43 perhaps we were wrong. 700 00:46:43 --> 00:46:45 Well, this is rather silly for me to keep running 701 00:46:45 --> 00:46:47 it over and over again. 702 00:46:47 --> 00:46:54 So clearly what I ought to do is, do it in a 703 00:46:54 --> 00:46:56 more organized way. 704 00:46:56 --> 00:47:26 So now if we go back, and see the, kind of the right way to 705 00:47:26 --> 00:47:29 do it, I'm going to get rid of this stuff here, which was 706 00:47:29 --> 00:47:34 used just to stop the previous computation. 707 00:47:34 --> 00:47:40 I'm going to write a function called perform sim. 708 00:47:40 --> 00:47:44 And what that does, is it takes the time and the number of 709 00:47:44 --> 00:47:49 trials that we wanted to do. 710 00:47:49 --> 00:47:54 It takes distlist equals the empty list in this case, to 711 00:47:54 --> 00:48:00 start with, and then it is basically going to call perform 712 00:48:00 --> 00:48:04 trial, the function we already looked at over and over again. 713 00:48:04 --> 00:48:07 And get a whole bunch of lists back, a list 714 00:48:07 --> 00:48:10 each time it calls it. 715 00:48:10 --> 00:48:18 And then, compute an average. 716 00:48:18 --> 00:48:21 Well, not yet, so it says, it says d equals drunk, field 717 00:48:21 --> 00:48:26 start in zero, distances equals perform trial, append the new 718 00:48:26 --> 00:48:32 list, and what it does, is it returns a list of lists. 719 00:48:32 --> 00:48:37 Where each list is what we had for the previous trials. 720 00:48:37 --> 00:48:49 Make sense? 721 00:48:49 --> 00:48:52 And then I'm going to have 1 more function call, 722 00:48:52 --> 00:48:53 answer a question. 723 00:48:53 --> 00:48:58 Which takes the maximum time and the number of trials, 724 00:48:58 --> 00:49:03 and it's going to do some statistics on all of the lists. 725 00:49:03 --> 00:49:08 We'll come back to this a week from today. 726 00:49:08 --> 00:49:11 If you are, have absolutely nothing to do for the next 727 00:49:11 --> 00:49:14 week, I'll give you a hint. 728 00:49:14 --> 00:49:19 I have salted a bug here in this latest code, and you 729 00:49:19 --> 00:49:22 might have some fun looking at what that bug is. 730 00:49:22 --> 00:49:22