Project 3: Reinforcement Learning(Thanks to John DeNero and Dan Klein.)
Due October 30th, 2014, 2:30pm
In this project, you will implement value iteration and Q-learning. You will test your agents first on Gridworld (from class), then apply them to a simulated robot controller (Crawler) and Pacman.
The code for this project contains the following files:
Files you will edit
Files you should read but NOT edit
Files you can ignore
What to submit: You will fill in portions of
Evaluation: Your code will be autograded for technical correctness. Please do not change the names of any provided functions or classes within the code, or you will wreak havoc on the autograder. If your code works correctly on one or two of the provided examples but doesn't get full credit from the autograder, you most likely have a subtle bug that breaks one of our more thorough test cases; you will need to debug more fully by reasoning about your code and trying small examples of your own. That said, bugs in the autograder are not impossible, so please do contact the staff if you believe that there has been an error in the grading.
Academic Integrity: We will be checking your code against other submissions in the class for logical redundancy. If you copy someone else's code and submit it with minor changes, we will know. These cheat detectors are quite hard to fool, so please don't try. We trust you all to submit your own work only; please don't let us down. If you do, we will pursue the strongest consequences available to us.
Getting Help: You are not alone! If you find yourself stuck on something, contact the course staff for help. Office hours, section, and Piazza are there for your support; please use them. If you can't make our office hours, let us know and we will schedule more. We want these projects to be rewarding and instructional, not frustrating and demoralizing. But, we don't know when or how to help unless you ask.
To get started, run Gridworld in manual control mode, which uses the arrow keys:
python gridworld.py -m
You will see the two-exit layout from class. The blue dot is the agent. Note that when you press up, the agent only actually moves north 80% of the time. Such is the life of a Gridworld agent!
You can control many aspects of the simulation. A full list of options is available by running:
python gridworld.py -h
The default agent moves randomly
python gridworld.py -g MazeGrid
You should see the random agent bounce around the grid until it happens upon an exit. Not the finest hour for an AI agent.
Note: The Gridworld MDP is such that you first must enter a pre-terminal state (the double boxes shown in the GUI) and then take the special 'exit' action before the episode actually ends (in the true terminal state called
Look at the console output that accompanies the graphical output (or use
As in Pacman, positions are represented by
Question 1 (6 points) Write a value iteration agent in
Value iteration computes k-step estimates of the optimal values, Vk. In addition to running value iteration, implement the following methods for
These quantities are all displayed in the GUI: values are numbers in squares, Q-values are numbers in square quarters, and policies are arrows out from each square.
Important: Use the "batch" version of value iteration where each vector Vk is computed from a fixed vector Vk-1 (like in lecture), not the "online" version where one single weight vector is updated in place. The difference is discussed in Sutton & Barto in the 6th paragraph of chapter 4.1.
Note: A policy synthesized from values of depth k (which reflect the next k rewards) will actually reflect the next k+1 rewards (i.e. you return k+1). Similarly, the Q-values will also reflect one more reward than the values (i.e. you return Qk+1). You may either return the synthesized policy k+1 or the actual policy for the kth iteration, k, which you'll get if you store optimal actions from the most recent round of value iteration updates.
The following command loads your
python gridworld.py -a value -i 100 -k 10
Hint: On the default BookGrid, running value iteration for 5 iterations should give you this output:
python gridworld.py -a value -i 5
Hint: Use the
Grading: Your value iteration agent will be graded on a new grid. We will check your values, Q-values, and policies after fixed numbers of iterations and at convergence (e.g. after 100 iterations).
Question 2 (1 point)
python gridworld.py -a value -i 100 -g BridgeGrid --discount 0.9 --noise 0.2
Grading: We will check that you only changed one of the given parameters, and that with this change, a correct value iteration agent should cross the bridge. Note that this part is tested with our value iteration agent, so if you have an error in value iteration, your agent may cross the bridge for incorrect settings of the parameters.
Question 3 (5 points) Consider
In this question, you will choose settings of the discount, noise, and living reward parameters for this MDP to produce optimal policies of several different types. Your setting of the parameter values for each part should have the property that, if your agent followed its optimal policy without being subject to any noise, it would exhibit the given behavior. If a particular behavior is not achieved for any setting of the parameters, assert that the policy is impossible by returning the string
Here are the optimal policy types you should attempt to produce:
You can check your answers by running value iteration on DiscountGrid with specified discount, noise, and living reward parameters as follows:
python gridworld.py -a value -i 100 -g DiscountGrid --discount 0.9 --noise 0.2 --livingReward 0.0
Note: You can check your policies in the GUI. For example, using a correct answer to 3(a), the arrow in (0,1) should point east, the arrow in (1,1) should also point east, and the arrow in (2,1) should point north.
Grading: We will check that the desired policy is returned in each case. As in question 2, we test your parameter settings using our value iteration agent.
Note that your value iteration agent does not actually learn from experience. Rather, it ponders its MDP model to arrive at a complete policy before ever interacting with a real environment. When it does interact with the environment, it simply follows the precomputed policy (e.g. it becomes a reflex agent). This distinction may be subtle in a simulated environment like a Gridword, but it's very important in the real world, where the real MDP is not available.
Question 4 (5 points) You will now write a Q-learning agent, which does very little on construction, but instead learns by trial and error from interactions with the environment through its
Important: Make sure that in your
With the Q-learning update in place, you can watch your Q-learner learn under manual control, using the keyboard:
python gridworld.py -a q -k 5 -mRecall that
Grading: We will run your Q-learning agent on an example of our own and check that it learns the same Q-values and policy as our reference implementation when each is presented with the same set of examples.
Question 5 (2 points) Complete your Q-learning agent by implementing epsilon-greedy action selection in
python gridworld.py -a q -k 100Your final Q-values should resemble those of your value iteration agent, especially along well-traveled paths. However, your average returns will be lower than the Q-values predict because of the random actions and the initial learning phase.
You can choose an element from a list uniformly at random by calling the
Question 6 (1 points) First, train a completely random Q-learner with the default learning rate on the noiseless BridgeGrid for 50 episodes and observe whether it finds the optimal policy.
python gridworld.py -a q -k 50 -n 0 -g BridgeGrid -e 1Now try the same experiment with an epsilon of 0. Is there an epsilon and a learning rate for which it is highly likely (greater than 99%) that the optimal policy will be learned after 50 iterations?
Note: Your response should be not depend on the exact tie-breaking mechanism used to choose actions. This means your answer should be correct even if for instance we rotated the entire bridge grid world 90 degrees.
Question 7 (1 point) With no additional code, you should now be able to run a Q-learning crawler robot:
python crawler.pyIf this doesn't work, you've probably written some code too specific to the
This will invoke the crawling robot from class using your Q-learner. Play around with the various learning parameters to see how they affect the agent's policies and actions. Note that the step delay is a parameter of the simulation, whereas the learning rate and epsilon are parameters of your learning algorithm, and the discount factor is a property of the environment.
Grading: We give you a point for free here, but play around with the crawler anyway!
Approximate Q-learning and State Abstraction
Question 8 (1 points) Time to play some Pacman! Pacman will play games in two phases.
In the first phase, training, Pacman will begin to learn about the values of positions and actions.
Because it takes a very long time to learn accurate Q-values even for tiny grids, Pacman's training games
run in quiet mode by default, with no GUI (or console) display. Once Pacman's training is complete,
he will enter testing mode. When testing, Pacman's
python pacman.py -p PacmanQAgent -x 2000 -n 2010 -l smallGridNote that
Hint: If your
Note: If you want to experiment with learning parameters, you can use the option
Note: While a total of 2010 games will be played, the first 2000 games will not be displayed because of the option
Note: If you want to watch 10 training games to see what's going on, use the command:
python pacman.py -p PacmanQAgent -n 10 -l smallGrid -a numTraining=10
During training, you will see output every 100 games with statistics about how Pacman is faring. Epsilon is positive during training, so Pacman will play poorly even after having learned a good policy: this is because he occasionally makes a random exploratory move into a ghost. As a benchmark, it should take about 1,000 games before Pacman's rewards for a 100 episode segment becomes positive, reflecting that he's started winning more than losing. By the end of training, it should remain positive and be fairly high (between 100 and 350).
Make sure you understand what is happening here: the MDP state is the exact board configuration facing Pacman, with the now complex transitions describing an entire ply of change to that state. The intermediate game configurations in which Pacman has moved but the ghosts have not replied are not MDP states, but are bundled in to the transitions.
Once Pacman is done training, he should win very reliably in test games (at least 90% of the time), since now he is exploiting his learned policy.
However, you will find that training the same agent on the seemingly simple
Pacman fails to win on larger layouts because each board configuration is a separate state with separate Q-values. He has no way to generalize that running into a ghost is bad for all positions. Obviously, this approach will not scale.
Question 9 (3 points)
Implement an approximate Q-learning agent that learns weights for features of states, where many states might share the same features. Write your implementation in
Note: Approximate Q-learning assumes the existence of a feature function f(s,a) over state and action pairs, which yields a vector f1(s,a) .. fi(s,a) .. fn(s,a) of feature values. We provide feature functions for you in
The approximate Q-function takes the following form
where each weight wi is associated with a particular feature fi(s,a). In your code, you should implement the weight vector as a dictionary mapping features (which the feature extractors will return) to weight values. You will update your weight vectors similarly to how you updated Q-values:
Note that the
python pacman.py -p ApproximateQAgent -x 2000 -n 2010 -l smallGrid
Once you're confident that your approximate learner works correctly with the identity features, run your approximate Q-learning agent with our custom feature extractor, which can learn to win with ease:
python pacman.py -p ApproximateQAgent -a extractor=SimpleExtractor -x 50 -n 60 -l mediumGridEven much larger layouts should be no problem for your
python pacman.py -p ApproximateQAgent -a extractor=SimpleExtractor -x 50 -n 60 -l mediumClassic
If you have no errors, your approximate Q-learning agent should win almost every time with these simple features, even with only 50 training games.
Grading: We will run your agent on the
python pacman.py -p ApproximateQAgent -a extractor=SimpleExtractor -x 50 -n 150 -l mediumGrid -q -f
Congratulations! You have a learning Pacman agent!