Paul's Python CoursePPC

Project 8 of 12, for Caitlin

Tide Gate Controller

Caitlin's Tide Gate game as a Gymnasium environment, kept by a keeper that learned it: a harbour balanced between the mud and the quay, at home.

About 2 hours, in 12 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

  • CaitlinThis is my new cabinet, it's called Tide Gate. There's a little harbour, a sea wall and one gate.
  • CaitlinShut it at high water and the river fills the harbour, but if you open it at low water it drains.
  • YahyaIf you just leave it alone you last about eight hours of game time. I've tried, trust me.
  • CaitlinI want a keeper that plays it properly, and a table that says why it did what it did.
  • PaulIt's a balancing job really, a bit like keeping a pole up on a cart, only wetter.

First, in the workshop: the racing grid in Gymnasium's shape, reset and step, as a plain class.

Then, at home, with real Gymnasium:

  1. gate_env.py: TideGateEnv, a gymnasium.Env. Every quarter of an hour the keeper winds the gate down a notch, leaves it, or winds it up (actions 0, 1, 2). The observation is four numbers: harbour level, sea level, the gate's notch, and how far the sea rose since the last step. The reward is 1.0 for each quarter hour the harbour stays between 2 m and 4 m. Outside that, the episode is terminated; after 200 quarter hours, truncated.
  2. agent.py: a keeper that learns the gate by tabular Q-learning and beats leaving it alone.
What it prints
(.venv) $ python agent.py
Do nothing: safe for 34.5 of 200 quarter hours, on average
The keeper: safe for 200.0 of 200 quarter hours, on average
(.venv) $ pytest
....................                                       [100%]
20 passed in 16.75s

What you learn

What's in it

  1. One shape for every world
  2. Finished, or stopped
  3. Stopped or finished
  4. The same seed, the same run
  5. The real Gymnasium class
  6. How Paul would do it
  7. Make the grid Gymnasium-shaped
  8. Two flags, not one
  9. Readings into bins
  10. Tide Gate, at home
  11. Ship it