All lessons in World Models: Learn, Generate, Plan, and Evaluate

Separate State, Observation, and Action

Write the timing and information contract for a learned transition.

25 minutes · Free lesson

What you will learn

  • Assign states, observations and actions to the correct time interval.
  • Write an action-conditioned transition and reward contract.
  • Detect a record that leaks future information into a prediction.

You receive a position reading at 10:00:00.000 and issue a push immediately afterward. Another reading arrives 250 milliseconds later. Which reading is an input to the predictor, and which is the outcome it must explain? A single off-by-one choice can create a model that looks accurate in a dataset but applies the wrong force when controlling the cart.

Use explicit time indices before choosing a neural architecture. They clarify both what is observed and when that information becomes available.

Separate the physical state from its measurement

A state describes variables sufficient for the transition contract. Our reference cart uses position x and velocity v. An observation is the measurement delivered to the controller; initially it contains both coordinates. A camera-only controller would receive an image instead. The environment can have a well-defined state even when the controller cannot observe it directly.

An action is the command applied during an interval. The course uses u = -1, 0 or 1. These are dimensionless actuator settings, not meters or velocities. The reference time step is 0.25 seconds. Changing the interval changes the effective dynamics; do not mix records with different intervals without representing that difference.

Define the transition before calculating it

For illustration, ignore process noise. The nominal reference uses v_next = 0.92 × v + 0.25 × u, followed by x_next = x + 0.25 × v_next. Position uses the updated velocity. A formula using old velocity would describe a different discretization and produce different data.

The learner is allowed to inspect this reference for diagnosis. The fitted model still obtains its coefficients from training records. When reporting performance, keep this simple, realizable model family in view: a linear reference is unusually favorable to a linear fit. Later shift tests change that relationship.

Worked example

One transition with explicit timing

  1. At time t, x = 1.0 and v = -0.4. Choose u = 1 for the next quarter-second.
  2. The next velocity is 0.92 × -0.4 + 0.25 × 1 = -0.118.
  3. The next position is 1.0 + 0.25 × -0.118 = 0.9705.
  4. A positive push reduced the speed toward the target; it did not immediately reverse the motion.
  5. Record the input state [1.0, -0.4], action 1 and next state [0.9705, -0.118]. Do not place the next velocity among the predictor's current inputs.

Define rewards and costs with the same care

A reward is the signal to maximize; a cost is the signal to minimize. We use positive cost: squared position plus 0.1 times squared velocity plus 0.02 times squared action. The running cost is evaluated after each executed transition. A terminal planning penalty guides the finite search but is reported separately from the executed running-cost metric.

This distinction matters when comparing controllers. A predicted objective that includes a terminal estimate is not numerically interchangeable with the measured running cost. State which quantity each report contains. Likewise, a boundary violation is an additional diagnostic, not an event you can hide by averaging it into cost.

Audit the interface in code

Open worlds.py and read environment, features, predict and cost. Trace where the current state becomes the next state. Then run python3 worlds.py collect --episodes 10 --steps 20 --out .work/small.json. Inspect selected records in your editor rather than printing the entire dataset.

Check that consecutive records in an episode share the prior next state as the following current state. Check that the action appears once and governs the transition to that record's next state. An episode reset begins a new sequence; it is never explained as the effect of the last action of the previous episode.

Exercise

Using the noiseless reference, start at x = 0 and v = 1 with u = -1. Calculate the next state.

Check your answer

The next velocity is 0.92 - 0.25 = 0.67. The next position is 0 + 0.25 × 0.67 = 0.1675. The cart still moves positively while braking.

Exercise

A record contains current position, next velocity and current action as features for next position. Why is a small error misleading for deployment?

Check your answer

Next velocity is unavailable before executing the action. It leaks an outcome into the input and makes prediction easier than the deployed information contract permits. Use the current measurement or a velocity estimate based only on past observations.

Summary

Specify which variables exist, which are observed, and when each becomes available. Align the action with its outcome and distinguish measured cost from a planner's terminal estimate.

Next action

Remove velocity from the observation and examine why identical positions can require different actions.

Choose another lesson