6.01 Final Exam: Fall 2009: Answers
6.01 Final Exam: Fall 2009: Answers
Name: ANSWERS
For staff use: 1. 2. 3. 4. 5. 6. total: /20 /10 /20 /20 /10 /20 /100
DataFun data __init__(self, data) plot(self, xMin, xMax, step) LinInterpFun val(self, x) NNFun val(self, x)
Every instance of a subclass of Fun that we create will have two methods: val, which takes an x value and returns the associated y value; and plot, which takes a description of the minimum and maximum x values for plotting, and the spacing between plotted points, and makes a plot. Different functions will be represented internally in different ways. In this problem, we will im plement the Fun, PyFun, DataFun, and LinInterpFun classes; the NNFun class is in the diagram to illustrate where we might put a subclass of DataFun that uses a different interpolation strategy.
A. The class Fun is an abstract superclass. It wont be useful to make an instance of it, but we can put the plot method, which is shared among its subclasses, in it. The plot method should create two lists:
xVals is a list whose rst value is xMin, and whose subsequent values are xMin + step, xMin + step + step, etc., stopping so that the last value is as large as it can be, but less than xMax. step will typically be a oat. yVals is a list of the y values of the function, corresponding to the x values in xVals. You can use the val(self, x) method, which is required to be dened for any actual instance of a subclass of Fun.
Then, it can call the global function makePlot on those lists. Assume makePlot is already dened. So, for example,
makePlot([1, 3, 8], [-2, 5, 2])
Implement the plot method. Recall that Pythons range only works with integer step size.
class Fun: def plot(self, xMin, xMax, step):
xVals = [] yVals = [] x = xMin while x <= xMax: xVals.append(x) yVals.append(self.val(x)) x += step makePlot(xVals, yVals)
B. The PyFun class is a subclass of Fun. It represents a function just using a Python procedure of a single argument. It should operate as follows:
>>> >>> 4
>>> 9
>>> t1 = PyFun(lambda x: x**2)
t1.val(2)
t1.val(-3)
t1.plot(-2, 10, 0.2)
C. The DataFun class is a subclass of Fun, representing functions using a list of (x, y) data points, and using an interpolation rule to evaluate the function at values of x that dont appear in the data set. The data points are not sorted in any particular way. Different subclasses of DataFun will provide different interpolation strategies by implementing different val methods. DataFun is an abstract class, because it does not itself provide a val method. It will provide useful __init__ and plot methods, however.
The __init__ method should take a list of (x, y) data pairs as input and store it in an attribute called data. The plot method should rst plot the function with regularly-spaced x values, using the plot method from the parent class, Fun; then it should plot the actual data points stored in the data attribute.
Implement the DataFun class.
class DataFun(Fun): def __init__(self, data): self.data = data def plot(self, xMin, xMax, step): Fun.plot(self, xMin, xMax, step) makePlot([x for (x,y) in self.data], [y for (x,y) in self.data])
This part of the problem is worth 5 points; we suggest that you do it only after you have nished the rest of the exam. D. The LinInterpFun class is a subclass of DataFun. Its only method is val, which performs linear interpolation. That is, given an xi value as input, it should look through the data to nd the xlo value that is, of all x values less than or equal to xi , closest to xi , and the xhi value that is, of all x values greater than or equal to xi , closest to xi . Let ylo be the y value associated with xlo and yhi be the y value associated with xhi . The method should return the linear interpolation value of y for input xi , which is:
(yhi ylo ) .
(xhi xlo )
If the query xi is lower than any x value in the data, then the method should return the y value associated with the smallest x value. Values of xi that are higher than any x value in the data should be handled analogously. You can assume that all numbers are oats; do not assume that the data are sorted in any way. Here is an example plot made from an LinInterpFun instance. The large squares are the actual stored data points.
t3 = LinInterpFun([(3, 10), (-1, 1), (8, -2), (3.5, 4)])
t3.plot(-2, 10, 0.2)
B. Write a single state machine class MySM such that MySM().transduce(inputList) gives the same result as thing(inputList), if inputList is a list of numbers. Remember to include a done method, that will cause it to terminate at the same time as thing.
class MySM(sm.SM): startState = (0,0) def getNextValues(self, state, inp): (x, y) = state y += inp if y >= 100: return ((x + 1, 0), y) return ((x, y), y) def done(self, state): (x, y) = state return x >= 3
C. Recall the denition of sm.Repeat(m, n): Given a terminating state machine m, it returns a new terminating state machine that will execute the machine m to completion n times, and then terminate. Use sm.Repeat and a very simple state machine that you dene to create a new state machine MyNewSM, such that MyNewSM is equivalent to an instance of MySM.
class Sum(sm.SM): startState = 0 def getNextValues(self, state, inp): return (state + inp, state + inp) def done(self, state): return state > 100 myNewSM = sm.Repeat(Sum(), 3)
10
+
S
Sensor
We can buy sensor A or sensor B as shown in the diagrams below. For each of them we can also specify the gain k at the time we order it.
+
R
Sensor A
R
Sensor B
A. Just considering the sensor, not yet connected into the system, if we feed a step function (going from 0 to 1) into a sensor A with k = 0.5, what will the output value be (approximately) after 100 steps? Assume the system starts at rest.
11
B. Just considering the sensor, not yet connected into the system, if we feed a step function (going from 0 to 1) into a sensor B with k = 0.8, what will the output value be (approximately) after 100 steps? Assume the system starts at rest.
1.6 C. If you plug sensor A into the overall system, what is the system function relating Y to X? (Leave k as a variable)
6.01 Final Exam Fall 09 D. If we put a sensor of type B into the system, then the overall system function is
12
kR2
R
R + kR + 1
1 (3 7j) 8
Is it stable?
Yes
Does it oscillate?
Yes
2. for k = 0.1, the poles of the system are at 0.77 and 0.13. Is it stable? Yes Does it oscillate? No
13
3. If you were buying a sensor of type B, which of these gains would give you faster response? Explain. Pick k = 0.25 because it converges faster.
Scratch Paper
14
V1 =
7.5 V
V2 =
5.0 V
V3 =
2.5 V
If they are incorrect, can you change the resistor values to make it work? If so, how? Correct
6.01 Final Exam Fall 09 Here is the circuit that Emerson suggests:
+10V
15
R1 = 100 + V1
R2 = 100 + V2
R3 = 100 + V3
R4 = 100
V1 =
10 V
V2 =
10 V
V3 =
10 V
If they are incorrect, can you change the resistor values to make it work? If so, how? No.
6.01 Final Exam Fall 09 Here is the circuit that Flann suggests:
16
+10V
+ -
V1 V2
+ -
V3
V1 =
8.75 V
V2 =
5V
V3 =
1.25 V
If they are incorrect, can you change the resistor values to make it work? If so, how? Set R4 = 100 and R5 = 100
17
1. Write a formula for V+ (the voltage on the positive input to the op-amp) in terms of Vin and the resistor values for this circuit:
V+ = R3 (10R2 + R1 Vin ) R1 R3 + R1 R2 + R2 R3
6.01 Final Exam Fall 09 2. Write a formula for Vout in terms of V+ and the resistor values for this circuit:
18
Vout = R4 + R5 R4 V+
3. For each of these relationships, state whether it is possible to choose resistor values that make it hold in the circuit above. Write Yes or No; it is not necessary to provide the resistor values. a. Vout = 2.5
3 16 Vin
No
b. Vout = 2.5 +
3 16 Vin
Yes
c. Vout = 2.5 +
3 16 Vin
No
19
Pr(O = m | S = w) = 0.9
Pr(O = m | S = s) = 0.7
Pr(O = m | S = c) = 0.1
Assume our initial belief state is Pr(S = w) = 0.4, Pr(S = s) = 0.5, Pr(S = c) = 0.1. 1. What would our belief state be after doing the experiment and observing n?
1 15 9 , , 7 28 28
20
B. Now, lets assume that the state of the planet is a Markov process whose transitions can be described, on the scale of centuries, as follows (of course, this is completely climatologically bogus):
St+1 w w St s c 0.7 0.4 0.0 s 0.3 0.2 0.3 c 0.0 0.4 0.7
1. Circle the following sequences of states that are possible. a. w, s, w, w, c, c, s, w Not possible b. c, c, c, s, s, c, s, w Possible c. w, s, c, w, s, c, s, w Not possible 2. If we were certain that climate was stable in some century t, and we didnt have any experimental evidence, what would our belief state about the state of the climate in century t + 2 be?
21
The farmer has a goat, a wolf and a head of cabbage. They come to a river (theyre on the left bank) and need to get everything and everyone to the other side (the right bank). Theres a boat there that ts at most two of them; the farmer must always be one of the two in the boat. If the farmer leaves the goat alone with the cabbage, the goat will eat the cabbage (so thats not a legal state). Similarly, if the farmer leaves the goat alone with the wolf... (so thats not a legal state).
Let n(s) be the number of objects (wolf, goat, cabbage) that are on the incorrect side of the river in state s. 1. Andrea suggests that a good heuristic would be n(s)1. Is it admissible? Why or why not? Yes, always less than the number of steps to go.
2. Bobbie suggests that a good heuristic would be 2n(s)1. Is it admissible? Why or why not?
Yes, always less than or equal the number of steps to go.
3. Casey suggests that a good heuristic would be 3n(s)1. Is it admissible? Why or why not?
No, on the rst step, 3n(s) 1 = 9 but there are 7 steps to go.
4. Which heuristic would be likely to reduce the search the most, while retaining optimality of the answer? Use Bobbies, 2n(s) 1, its the largest admissible heuristic.
22
B. We need to travel over a network of roads through the mountains in the snow. Each road has a current condition: clear, slippery, or buried. There are two possible vehicles you can use: a sports car, which can only traverse clear roads or an SUV, which can traverse any road. You start in the sports car (in location S), but if you are driving one vehicle, and youre in the same location as another vehicle, you can trade vehicles; if you drive your sports car to the location of the SUV (which starts in location A), and trade, then when you move, you will move with the SUV and the sports car will be left at that location. We will specify the map using the data structure below, which characterizes, for each location, the roads leading out of it. Each road is described by a triple indicating the next location, the length of the road, and the condition of the road.
map1dist = {S A B C D : : : : : [(A, [(S, [(S, [(A, [(A, 2, clear), (B, 1, slippery)],
2, clear), (C, 3, clear), (D, 10, slippery)],
1, slippery), (D, 2, slippery)],
3, clear)],
10, slippery), (B, 2, slippery)]}
We are going to formulate this as a search problem with costs, to be solved using UC search. Let the cost to traverse a road just be the the length of the road times a multiplier: the multiplier is 1 for the sports car and 2 for the SUV. There is a cost of 1 for the action of swapping cars. The possible actions are to drive on one of the roads emanating from a current location or to swap cars. 1. What information do you need to keep in each state? How will you represent it in Python? Our location and which vehicle we are using: (loc, car) We dont need to keep in the state where the other vehicle is. Once we change vehicles, we dont ever change back.
2. How would you represent the starting state (as a Python expression)?
(S, car)
23
3. What would you pass in as the second argument to ucSearch.search, the goal test, if the goal is to end in location D? Write Python expression(s)
lambda s: s[0] == D
4. Let the actions be described by (action, roadNum), where action is one of drive or swapCars, and roadNum is an integer that means which road to drive on out of an intersection. The roadnum can be used as an index into the list of results in map1dist. When action is swapCars, then the roadNum is ignored. If drivingDynamics is an instance of sm.SM that describes this planning domain, using your state representation, what would the output of this expression be:
>>> drivingDynamics.transduce([(drive, 0), (swapCars, 0), (drive, 1)])
24
5. From that same start state, what path through state space would be found by breadth-rst search, when the goal is to be in location D? Provide a list of states.
[(S, car), (A, car), (A, suv), (D, suv)]
6. From that same start state, what path through state space would be found by uniform-cost search? Provide a list of states.
[(S, car), (A, car), (A, suv), (S, suv), (B, suv), (D, suv)]
13
25
26
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.