Paul's Python CoursePPC

Project 2 of 12, for Neil

Gridworld Simulator

Neil's search for a better racing line on the crab cabinet's grid, as a Markov decision process: tiles, moves, slips, rewards, policies and what a race is worth.

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

  • NeilThis is Caitlin's crab cabinet, and on League night everyone races it on the grid.
  • NeilEveryone races the traditional line, along the bottom and then up the far side.
  • PaulWhich goes right past the pool.
  • NeilIt's the shortest way round, but in karting the shortest is hardly ever the fastest, and I want numbers.
  • PaulThen let's work out what it's worth, with the slips included.

Write track.py, the racing grid as a Markov decision process:

...F    F  the finish: +10, and the race is over
.#.P    #  a rock: nothing goes through it
S...    P  the rock pool: -10, and the race is over
        S  the start, (2, 0). Every other tile costs -1.

A tile is a (row, column) pair. Told up, down, left or right, the crab goes that way with chance 0.8, and sideways with chance 0.1 each way. A step off the edge or into the rock leaves it where it was, and still costs -1.

  1. move(tile, direction): where one step leads.
  2. outcomes(tile, action, slip=0.2): a list of every (probability, next tile, reward, over) that can follow.
  3. race(policy, rng, start=(2, 0), slip=0.2, limit=100): one race following a policy, a dictionary from tile to action. Returns the rewards in order.
  4. discounted_return(rewards, gamma).
  5. MY_LINE: your own policy, better than the traditional line.

Run as a program, it races both lines 1000 times and prints each one's average return, with gamma = 0.9.

What it prints
The traditional line
> > > F
^ # ^ P
> > > ^
Average return over 1000 races: -7.79

My line
> > > F
^ # ^ P
^ < ^ <
Average return over 1000 races: 0.92

What you learn

What's in it

  1. Tiles, moves and rewards
  2. Where a move can lead
  3. Into the rock
  4. A rulebook for every tile
  5. After a slip
  6. What a race is worth
  7. Four tiles, then the finish
  8. Now or later
  9. Backwards from the finish
  10. How Paul would do it
  11. Fix Caitlin's version
  12. Make the racing grid
  13. Ship it