Define observations, actions, and rewards
Build a small reinforcement-learning contract before training anything.
Opening the Python editor…
Requirements and learning goals
Before you start
- Basic Python
- An understanding of feedback
You will be able to
- Separate observation from hidden state
- Define an action space
- Identify reward loopholes
The browser exercise above works without extra setup. The project brief below assumes the listed prerequisites and gives you a task and checks to perform. Need a guided first project?
Reinforcement learning, or RL, trains a decision-making through trial and error. The policy receives an observation, chooses an action, and learns from a numeric . It is useful when you can simulate repeated attempts at a task, such as balancing or reaching. Here you will define that learning problem before spending time on training.
The Go2/G1 workflow runs your app against robot sensor and command interfaces. Use those observations as policy inputs; keep diagnostic ground truth exclusively for evaluation.
Define the decision loop
An agent observes, acts, and receives a reward after the environment advances. Maximizing that reward only solves the intended task when the reward and observation contract express the task correctly.
For a simulated cart, use target distance and velocity as observations, with bounded acceleration as the action. Record units, shapes, update rate, and the source of every observation. A future physical implementation needs to measure or estimate each input.
Score transitions by hand
def reward(before, after, effort, collision):
progress = abs(before) - abs(after)
return progress - 0.01 * effort**2 - 5.0 * int(collision)
print(f"{reward(2.0, 1.8, 0.4, False):.4f}")
print(f"{reward(0.1, 0.3, 0.4, False):.4f}")The output is:
0.1984
-0.2016The first transition reduces distance by 0.20, then pays an effort penalty of 0.01 × 0.4² = 0.0016. The second increases distance by 0.20 and pays the same penalty. Reward therefore distinguishes progress from activity: spending effort alone earns nothing.
Keep the first transition but set collision to True. Predict the result before running it.
With no progress, effort 0.4, and no collision, the result is −0.0016. Crossing from signed distance +0.10 to −0.10 gives zero progress because the absolute distance is unchanged, even though the cart passed the target. These illustrative weights are not a validated robot objective.
Look for loopholes
Can the agent repeatedly collect a goal bonus? Does ending early avoid future penalties? Does an input reveal a hidden target coordinate that hardware cannot observe?
Define task termination separately from a time-limit truncation. For this example, success requires absolute distance below 0.05 meters and absolute speed below 0.02 m/s. Terminate on success or collision; otherwise truncate after 200 steps. A cart 0.03 meters away but moving at 0.40 m/s has not succeeded. At the same distance and 0.01 m/s it has. A cart still 0.30 meters away at step 200 reaches the time limit without succeeding. If success and the limit occur on the same step, record task termination for this contract.
These rules make stopping near the target part of the task. They also prevent an from collecting repeated success rewards after completion. Gymnasium's step contract exposes termination and truncation separately.
Run the app through the simulated robot's sensor interfaces instead of reading the physics engine's world state. The observer view can show more than the robot perceives. Current profiles also expose diagnostic ground-truth topics; exclude those from policy inputs. This separation is an application contract, not an absolute anti-cheating guarantee.
Even sensor-shaped data can be unrealistically clean. Test noise, bias, delay, and dropouts, then compare with real measurements.
Finish with hand-scored transitions and a written observation/action specification. Optional extension: run random actions and check resets and reward totals before adding a learner.
Before you move on
Can you do each of these?
- Separate observation from hidden state
- Define an action space
- Identify reward loopholes
0 of 2 checkpoints solved. Try checkpoint 1