Skip to content

5. Training in your own code

Any program that trains on Gymnasium environments works. The vector environment is the native gymnasium.vector.VectorEnv, so libraries that take one can use it directly; for Stable-Baselines3, which has its own vector interface, an adapter is included.

Stable-Baselines3

# needs: pip install stable-baselines3
from stable_baselines3 import PPO
from redysim_rl.wrappers import make_sb3

venv = make_sb3("joint_reach", num_envs=64, action_mode="impedance")
model = PPO("MlpPolicy", venv, n_steps=64, batch_size=1024, gamma=0.98,
            policy_kwargs={"log_std_init": -1.0}, device="cpu", seed=0)
model.learn(total_timesteps=20_000)         # about a second; use 1_000_000 for a real run

make_sb3 builds the environment with SAME_STEP autoreset, which is what Stable-Baselines3 expects: the observation at the end of an episode is the first of the next one, and the last one arrives in info["terminal_observation"], with TimeLimit.truncated set for time limits.

The GUI for runs does the same without a script: it sets a run up, shows every checkpoint as an animation while it trains, and compares runs.

The script examples/train_ppo.py trains and evaluates a policy on any task:

python examples/train_ppo.py --task joint_reach --action-mode impedance --steps 1000000

It prints the success rate and the mean final error of a random policy and of the trained one over complete 200-step episodes. Arguments: --task, --robot, --action-mode, --envs, --steps, --seed, --log-std, --save.

What to expect

Three seeds each, on a laptop processor (16 threads), PPO of Stable-Baselines3 with its default networks and the settings of the script; the error is at the end of an episode, mean ± standard deviation over the seeds, and a zero action is the baseline a policy has to beat:

task action mode steps minutes result
joint_reach impedance 1 M 1.7 0.014 ± 0.002 rad, 99 ± 1 % within tolerance (zero action 1.16 rad)
joint_reach torque 1 M 1.4 0.242 ± 0.031 rad, 1 ± 1 % within tolerance (zero action 2.27 rad): not solved
joint_reach PD targets 1 M 1.6 0.124 ± 0.052 rad, 12 ± 8 % within tolerance (zero action 1.16 rad): not solved
task_reach impedance 1.5 M 2.5 0.015 ± 0.005 m, 92 ± 8 % within tolerance (zero action 0.51 m) (2 cm)
platform_reach PD targets 1 M 2.9 0.020 m, 42 % within tolerance (1 cm and 0.05 rad; zero action 0.117 m, 0.8 %), one seed, still improving
space_reach impedance 3 M 9.0 0.072 ± 0.038 m, 88 ± 5 % within tolerance (zero action 1.13 m) (5 cm)
quadruped_velocity PD targets 3 M 450 s does not track the command: velocity error 0.276 m/s, a zero action (hold the pose) gives 0.265
quadruped_velocity PD targets, sharper kernel 10 M 1,570 s tracks the command (error 0.072 m/s, speed 0.21 to 0.45 m/s for commands 0.2 to 0.5) but with a degenerate gait, see Legged robots
wheeled_nav diff drive 3 M 479 s reaches the goal in 78 % of 512 worlds with eight obstacles and hits something in 22 %, in 3.9 s: matches a hand-written reactive controller (79 % and 21 %, 5.8 s) and beats steering for the goal while ignoring the obstacles (71 % and 29 %); a zero action never reaches it
wheeled_nav diff drive 10 M 6,578 s reaches the goal in 91 %, hits something in 9 %, in 3.9 s: ahead of the reactive controller by 13 points (the run shared the machine with another job)
car_parking car drive 3 M 682 s parks in 99 % of 256 starts (the car beside the front parked car, with small noise), hits nothing, in 4.4 s: as good as a manoeuvre written by hand (98 % in 5.4 s); a zero action never parks. The first run, with a heading penalty everywhere and 0.02 a step, stopped 0.26 m short of the gap and parked in 0 %

The arm tasks use the bundled three_link robot; their rows were measured again on it (three seeds), and their minutes are from earlier runs on a similar arm. The space_reach row is the satellite with a seven-joint arm: its three seeds ran in parallel with 4 threads each, so its minutes are not comparable with those of the arm rows. The tolerances are 0.05 rad for joint_reach. The minutes include the evaluations. These are untuned runs of one algorithm; they show that the environments are learnable, not the best a method can do. The two quadruped_velocity rows and the two wheeled_nav rows are single runs, so each is one sample; the wheeled_nav runs share their evaluation worlds (512 worlds, the sampling error is about 2 points). Things that matter:

  • Exploration noise. PPO's default of 1.0 in a [−1, 1] action means random targets over the whole ±π range in the PD modes. Start smaller (log_std_init = −1).
  • Impedance first. Letting the policy choose stiffness as well as target learned the arm tasks fastest; torque and PD targets need more steps (the arm has to be held up against gravity).
  • Walking takes far longer, and the reward decides how. After 3 M steps the quadruped's speed did not follow the command and it did no better than holding the standing pose, which survives by itself; compare any result with that zero-action baseline. After 10 M steps with a sharper tracking kernel it follows the command, but by balancing on three legs with one held in the air, so a velocity reward alone is not enough: the reward needs terms for the pose and for foot slip. Published setups that train a legged velocity policy use hundreds of millions of steps. At the measured 6,700 steps per second, end to end, 100 M steps take about four hours here. In the GUI, with its default reward (which adds the pose and foot-swing terms and a trunk-height penalty to speed tracking) and 256 robots, 15 M steps (17 minutes on 4 cores) give a policy that walks: all four feet step, each about seven times a second, and it covers 3.0 m in 10 s at a command of 0.3 m/s and 4.8 m at 0.5 m/s, with 100 % of 256 new commands followed. One run, one seed.

Speed

examples/benchmark.py measures environment steps per second with a random policy, including the action mode, the physics, the observation and the reward:

python examples/benchmark.py --envs 1 1024

Measured twice on a quiet 16-thread laptop processor (the run-to-run difference was 6 % at the median and 54 % at worst, for task_reach with PD targets, so ranges are given), with 1,024 robots:

tasks torque PD targets impedance
arm tasks (million steps/s) 0.93 to 1.38 0.20 to 0.60 0.17 to 0.49
space robot, 7-joint arm (million steps/s) 0.26 to 0.27 0.08 0.07
quadruped on the ground (thousand control steps/s) 12 to 13 12 11

The PD modes evaluate the force law five times per control step (20 for the quadruped), which is why they are slower. One environment runs 2,000 to 22,000 steps per second (700 to 970 for the quadruped), limited by Python, so train with many robots at once.