Overview
This script loads a trained adaptive gait policy and runs it in the MuJoCo viewer with real-time visualization of gait parameter adaptations. It supports both trained policies and baseline (pure gait controller without residuals) for comparison. Key Features:- Real-time visualization in MuJoCo viewer
- Live gait parameter monitoring in console
- Trajectory recording to JSON
- Baseline comparison mode
- Pause/resume simulation with spacebar
- Fullscreen support for presentations
Basic Usage
Evaluate Trained Policy
Run Baseline (No Residuals)
Command-Line Arguments
Model Loading
Path to trained model file (
.zip). Required unless --baseline is set.Example: runs/adaptive_gait_20260304_143022/final_model.zipPath to VecNormalize statistics file (
.pkl). Highly recommended for policies trained with observation normalization.Without this file, the policy will receive unnormalized observations and likely perform poorly.Example: runs/adaptive_gait_20260304_143022/vec_normalize.pklSimulation Control
Duration to run simulation in seconds (wall-clock time).Example:
--seconds 60 for 1 minute of playbackUse deterministic actions (mean of policy distribution) instead of sampling. Recommended for evaluation to reduce stochasticity.
Disable automatic environment reset on episode termination. Useful for visualization when you want to freeze on failure state.
Environment Configuration
Use flat terrain (
model/world.xml) instead of rough terrain (model/world_train.xml). Useful for testing policy on easier terrain.Run baseline mode with zero actions (pure gait controller without residuals). Use this to compare against the learned policy.When set,
--model is not required.Visualization Options
Start viewer in fullscreen mode. Requires GLFW backend (
MUJOCO_GL=glfw). Falls back to maximized window if fullscreen unavailable.Hides left and right UI panels for cleaner presentation view.Data Recording
Save trajectory data (time, x, y, z position) to JSON file.Example:
--save-trajectory outputs/trajectory.jsonOutput format:Interactive Controls
Keyboard Shortcuts
| Key | Action |
|---|---|
| Space | Pause/unpause simulation |
| ESC | Close viewer and exit |
- Mouse drag: Rotate camera
- Right-click drag: Pan camera
- Scroll wheel: Zoom in/out
- Double-click: Select body for tracking
Console Output
Real-Time Gait Parameters
The script prints current gait parameters every 1 second:Episode Termination
When the robot tips over or reaches max steps:Final Statistics
At the end of playback:Example Workflows
1. Quick Policy Test (30 seconds)
2. Compare Policy vs. Baseline
3. Test on Flat Terrain
4. Fullscreen Demo for Presentation
5. Record Long Trajectory
Interpreting Gait Adaptation
Step Height (step_height)
- Baseline: ~0.040m
- Expected adaptation: Increases on rough terrain (0.045-0.060m) to clear obstacles
- High std (>0.005m): Policy actively modulating step height ✓
- Low std (<0.002m): Policy not using adaptive behavior ✗
Step Length (step_length)
- Baseline: ~0.060m
- Expected adaptation: Decreases on rough terrain for stability, increases on flat terrain for speed
- Typical range: 0.050-0.075m
Cycle Time (cycle_time)
- Baseline: ~0.800s
- Expected adaptation: Increases (slower gait) on difficult terrain for stability
- Typical range: 0.700-1.000s
Body Height (body_height)
- Baseline: ~0.050m
- Expected adaptation: Small adjustments for stability and obstacle clearance
- Typical range: 0.045-0.055m
Troubleshooting
Policy Performs Poorly
Problem: Robot tips over immediately or behaves erratically. Solutions:- Ensure
--normalizepoints to the correct.pklfile from training - Check that model and normalization stats are from the same training run
- Try
--flatto test on easier terrain - Compare with
--baselineto verify environment setup
Viewer Not Opening
Problem: Script runs but no window appears. Solutions:- Check
DISPLAYenvironment variable is set (Linux) - Try different backend:
MUJOCO_GL=glfworMUJOCO_GL=osmesa - Verify MuJoCo installation:
python -c "import mujoco; print(mujoco.__version__)"
Fullscreen Not Working
Problem:--fullscreen falls back to windowed mode.
Solutions:
- Set backend explicitly:
MUJOCO_GL=glfw python3 play_adaptive_policy.py ... - Check if running in headless environment (fullscreen requires display)
- Use maximized window fallback (automatic)
Output Files
Trajectory JSON
When using--save-trajectory, the output file contains:
Use Cases for Trajectory Data
- Distance traveled: Compute
sqrt((x_final - x_start)^2 + (y_final - y_start)^2) - Average speed: Distance / duration
- Path visualization: Plot (x, y) trajectory
- Stability analysis: Check Z position variance
- Policy comparison: Compare trajectories from different checkpoints
See Also
- train_adaptive_gait_ppo.py - Training script
- AdaptiveGaitEnv - Environment documentation
- DiagonalGaitController - Gait controller reference