FixTheRobot Explore Robotics / Robot Learning / M6 / M6-L1

This lab works on phones, but writing code is much easier on a laptop.

Robot Learning · Deep and Continuous-Action Reinforcement Learning · M6-L1 · Advanced

Compute Returns and Advantages

Experiment log · Scout at Meridian General Hospital

Experiment log, Meridian General Hospital, logistics corridor. Scout's new policy-gradient trainer runs, but its learning curve is strange: episodes that end in a crash seem to make the approach to the pillar look attractive, and episodes that merely run out of time are punished as if they had failed. The training logs are flat lists of rewards and value estimates from several episodes written one after another.

You will write the three pieces that turn such a batch into a learning signal: returns that stop at each episode boundary, advantages that treat a goal or a crash differently from a time-limit cut, and the two statistics (normalised advantages and explained variance) that the update and the logs depend on. Every batch is made of real rollouts in the Scout hall: a noisy expert, a reckless straight-line driver that hits the pillar, and episodes stopped early by the step limit.

Your task

    Simulation
    solution.pyLoading Python…
    Ctrl+Enter runs
    What Python can I use?

    Plain Python with math and heapq. Your code runs in a background worker: an endless loop is stopped after a few seconds and the page stays usable. It runs in your browser and is never sent anywhere.

    Hints

    What this lab teaches

    Stage 1 writes discounted returns over a flat batch, restarting the recursion at every episode boundary. Stage 2 writes generalised advantage estimation and the one detail that decides whether it is right at the end of an episode: the next-state value is zero after a termination (goal or crash) but is the critic's estimate after a truncation (the time limit). Stage 3 writes advantage normalisation and explained variance, including the edge cases (a constant batch, a critic that is off by a constant).

    Grading is result-based and exact: your lists are compared with the reference on batches you have not seen, with different discount factors and lambda values and different mixes of goal, crash and cut-off episodes. The charts draw the exact values as bars and yours as dots, with the episode boundaries marked.

    Theory for this lab

    Deep and Continuous-Action Reinforcement Learning labs

    1. M6-L1: Compute Returns and Advantages: this lab.
    2. M6-L2: Implement the PPO Clipped Objective
    3. M6-L3: Tune a PPO Run
    4. M6-L4: Diagnose a Diverging Training Run

    Educational simplification: the critic is the true discounted return scaled down, shifted and with noise added, not a trained network; batches hold 100 to 250 steps from six or seven episodes; Scout has no sensor noise. Real trainers vectorise many environments and store values in arrays, but the boundary logic is the same.

    Get the next lab

    Get notified when new robotics labs are released. One email per new lab, nothing else.