Paul's Python CoursePPC

Project 3 of 12, for Aaesha

Gridworld Solver

For Aaesha, planning the way a mind with a map does: the worth of every tile on the crab's racing grid, without a single race. Policy evaluation, value iteration and policy iteration.

About an hour and a half, in 14 steps.

Sign up free to open itLog in

Act 8 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • AaeshaA thousand races say the traditional line is worth about minus eight, but that's only about.
  • AaeshaRats in a maze learn a map of it in their heads, then plan with it.
  • AaeshaYou've already got the map, with every tile and every chance on it, which is enough to work it out exactly.
  • NeilI'd like to know, exactly, whether I've been racing it wrong all term.
  • PaulThen work it out instead of racing it.

Write dp.py, using your racing grid in track.py. Values and policies are dictionaries keyed by tile; gamma is 0.9 unless you're told otherwise.

  1. q_value(values, tile, action, gamma=0.9): what an action is worth on a tile, given the values of the tiles it can lead to.
  2. evaluate(policy, gamma=0.9, theta=1e-6): the value of every tile under a policy, by sweeping until no value changes by more than theta. The finish and the pool stay at 0.
  3. greedy(values, gamma=0.9): the best action on every tile still racing, looking one step ahead.
  4. value_iteration(gamma=0.9, theta=1e-6): the best values.
  5. policy_iteration(policy, gamma=0.9): evaluate and improve until the policy stops changing; returns (policy, values).

Run as a program, it prints the traditional line's values, the best values, the best line, and whether policy iteration agrees.

What it prints
The traditional line
   4.5   6.7   9.1   0.0
   2.7     #   5.1   0.0
  -7.6  -8.6  -8.4  -9.8

The best line
   4.5   6.7   9.1   0.0
   2.7     #   5.1   0.0
   1.2   1.2   2.8   0.1
> > > F
^ # ^ P
^ > ^ <

Policy iteration agrees: True

What you learn

What's in it

  1. How good a tile is to stand on
  2. One step from the finish
  3. Sweep until nothing changes
  4. Two sweeps
  5. The best a tile can be worth
  6. Free seconds
  7. Evaluate, improve, repeat
  8. A longer corridor
  9. How Paul would do it
  10. Fix Riya's version
  11. Make the track known
  12. Three rounds
  13. Ship it