# Module 6.4: Reinforcement Learning for Locomotion and Manipulation

- Course: [Course 6: Modern Robot Learning & Embodied AI](https://roboticsconcepts.dev/courses/modern-robot-learning-embodied-ai.md)
- HTML Page: https://roboticsconcepts.dev/courses/modern-robot-learning-embodied-ai/reinforcement-learning-locomotion-manipulation/
- Key Concepts: Dense vs. sparse reward shaping, Action-rate smoothness penalties, PPO & curriculum staging

Imitation learning is great when a human can smoothly teleoperate the task, but nobody wants to teleoperate a 12-motor quadruped sprinting up rocky stairs at 50 Hz. For dynamic balance, locomotion, and forceful contact recovery, Reinforcement Learning (RL) lets the robot discover motor strategies we couldn't easily demonstrate by hand.

## Dense vs. sparse rewards (and the art of not breaking your robot)

If you only give a robot a +10 sparse reward when it reaches a goal, a randomly flailing policy starting two meters away will almost never stumble into the goal by luck — it gets zero learning signal. So we add dense shaping rewards, like subtracting the distance to the target on every step.

Here's the catch: RL optimizers are ruthlessly literal. Whatever loophole exists in your reward formula, the policy will find it. Forget to penalize large motor torques or sudden jumps between consecutive actions, and the policy learns to vibrate violently at 50 Hz or fling itself forward using unrealistic simulator spikes. Crank the energy penalty too high, on the other hand, and the robot discovers a 'lazy' exploit: sit completely still, collect zero action penalties, and ignore the goal altogether.

## PPO across parallel worlds and curriculum staging

Most modern sim-to-real locomotion stacks use Proximal Policy Optimization (PPO) paired with thousands of parallel simulation environments. PPO collects short rollouts across all environments, estimates which actions beat the average return (advantage), and updates the policy cautiously within a clipped trust region so one noisy batch doesn't blow up an already decent gait.

Instead of dropping an untrained policy onto 30-centimeter stairs on step one, teams stage a curriculum: start on flat ground at slow walking speeds, and automatically bump up terrain height or obstacle roughness only when the policy proves it can succeed consistently.

## Interactive Sandbox: Locomotion & Reach Reward Shaping Sandbox

Adjust the Dense distance weight, Sparse goal bonus, and Energy / action penalty sliders in the Reward Shaping Lab below. Watch the reward contour grid and simulated greedy trajectory shift between Clean Reach, Lazy / Reward Hacking, and Overshoot / Oscillating.

## Reading reward diagnostics before touching real motors

Try pushing Energy / action penalty high while keeping Dense distance weight low: the trajectory stops cold near the start because staying put earns a higher total episode return than spending energy to move. Now drop the Energy penalty near zero and crank the goal incentives: the agent sprints straight at the goal, blows past it, and rings back and forth.

In real RL tuning, you spend just as much time inspecting action-rate norms (`||a_t − a_{t−1}||²`) and joint velocity histograms as you do watching the success rate curve. A policy that hits 98% success in sim by slamming motors end-to-end will burn out a real gearbox in five minutes.

## Standard shaped RL step reward with smoothness regularization

```text
r_t = −w_dist · ‖p_t − p_goal‖ + R_goal · 𝟙[success] − w_act · ‖a_t‖² − w_rate · ‖a_t − a_{t−1}‖²
```

Task progress pulls the robot toward the goal, while action magnitude and action-rate penalties keep motor commands smooth enough for physical actuators.

## Check Your Intuition

**Question:** During RL training for a quadruped, your robot scores great episode returns in simulation by standing completely frozen in place instead of walking toward the velocity command. Which bug is the most likely culprit?

- [✓] Your energy, torque, or survival-bonus weights overpower the forward velocity tracking reward
- [ ] PPO's discount factor γ is slightly below 1.0
- [ ] The simulator is running too many parallel environments

**Explanation:** When action/energy penalties or per-step 'alive' bonuses outweigh the reward for making progress, standing still becomes the mathematically optimal reward-hacking strategy.

## Where You'll See This in Modern Robotics

Isaac Lab and MJX locomotion configs routinely combine 6–10 reward terms — linear/angular velocity tracking, foot air-time, orientation stability, torque, and action-rate penalties — tuned via ablation curves before hardware transfer.

## Companion Python Notebook: 04 · Reward Shaping & Smoothness Penalties Lab

Visualize 2D reward landscapes and see how tuning distance, sparse bonus, and action-rate penalties triggers or prevents reward hacking.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-6/04_reinforcement_learning_reward_shaping.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-6/04_reinforcement_learning_reward_shaping.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-6/04_reinforcement_learning_reward_shaping.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-6/04_reinforcement_learning_reward_shaping.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-6/04_reinforcement_learning_reward_shaping.py`
