Reinforcement learning for robots: learning by trial and error
Reinforcement learning is trial and error scored by a reward. The robot tries actions, gets a number saying how well it did, and shifts towards actions that earn more. Nobody demonstrates the movement. In a 2021 study, a four-legged robot’s walking policy for uneven ground was trained this way in twenty minutes, using thousands of simulated copies.
Learning from rewards
The designer decides what counts as success, and the reward is how that decision reaches the robot. Animal trainers use a similar principle. A dog that sits when asked gets a treat, and over repeated sessions sitting becomes more likely. The robot version replaces the treat with a number, and replaces a handful of sessions with millions of attempts. The analogy ends there: a robot has no motivation or understanding, and the learning algorithm is arithmetic that nudges a policy’s numbers.
The contrast with imitation learning is direct. Imitation needs a person to demonstrate the task and the robot learns to reproduce it. Reinforcement learning needs no demonstrator, only a way of scoring outcomes, and it can in principle find solutions that no person demonstrated. The cost is the sheer number of attempts needed to find them.
States, actions and rewards in plain terms
Reinforcement learning has its own vocabulary, which reduces to three ideas.
State. Everything the robot knows about its situation at a moment: joint angles, speeds, body tilt, what the camera sees, sometimes a target to reach. In practice the robot uses an observation, which may be incomplete, so the state is what the policy gets to look at.
Action. What the robot can do next: a set of motor torques, target joint positions or a movement of the gripper.
Reward. A number computed after each step that scores the result. A walking robot may get a positive reward for moving forward at the requested speed and a negative one for falling or using excessive energy.
The policy maps states to actions. Training aims to find a policy that maximises the total reward collected over an episode, which is one attempt from start to finish. The algorithm may weigh near-term reward more than far-off reward, using a discount, because the future is uncertain. Many methods also learn a value estimate: a prediction of how much total reward to expect from a given state. The policy and the value estimate are both neural networks in modern systems, which is why the field is called deep reinforcement learning.
The loop is simple. Observe the state, choose an action, receive a reward and a new state, record the experience, and periodically update the policy using the recorded experience. Repeat until the reward stops improving.
Designing a reward that means what you want
The reward is where human intent enters the system, and it is hard to get right. Several kinds of reward are common.
- Sparse rewards give a number only at the end, such as one for a completed task and zero otherwise. They state the goal exactly but offer no hint on the way, so the robot may never stumble on success by chance.
- Dense rewards give a signal at every step, such as a reward for each centimetre the gripper moves closer to the object. They guide learning but encode the designer’s guess about how to reach the goal.
- Shaped rewards add terms for style and safety: a penalty for jerky motion, for high motor effort, for touching things that should not be touched.
A walking reward, for example, may combine forward speed, staying upright, low energy use and smooth joint movement. Each term has a weight, and tuning the weights is a large part of the work. Too much weight on speed and the robot lunges. Too much weight on energy and it barely moves.
A useful habit is to ask what the cheapest way to earn the reward would be, because the algorithm will find it. That question leads to the next problem.
Why is exploration risky on real hardware?
For the robot to learn that something works, it must try it, and for many problems that means trying actions nobody expects to be good. This is exploration. Early in training, the policy is close to random, so its actions are noisy. A simulated robot flailing costs nothing. A real robot doing the same can slam into a table, strain a gearbox, drop an object or hit a person who is too close.
Exploration therefore needs boundaries. Practical measures include limits on joint speed and torque, clamping actions to a safe range, soft barriers that stop motion near the edge of the workspace, an emergency stop within reach, and keeping people out of the robot’s reach during learning. Some research concentrates on safe reinforcement learning, where the algorithm is designed to respect constraints while it learns, though guarantees are limited and still being studied.
Because of the risk and the volume of attempts, much trial-and-error learning starts in simulation, where the simulation guide explains the trade-offs. Real-world practice is saved for fine-tuning or for tasks that are hard to simulate.
Learning to walk in simulation
Legged locomotion is the best-known success of the approach. A simulated four-legged or two-legged robot is rewarded for tracking a commanded velocity while staying balanced, and penalised for falling, for large forces and for jerky motion. Thousands of copies, each on a different terrain, run in parallel. Researchers at ETH Zurich and NVIDIA reported in 2021, in a paper on arXiv, that this set-up on a single workstation graphics card trained the ANYmal robot to walk on flat ground in under four minutes and on uneven terrain in about twenty, and that the policies then worked on the real machine. The open-source training code released with that work starts 4,096 simulated robots by default, each choosing an action 50 times a second.
Domain randomisation helps the move to hardware. The code from that study, for example, varies ground friction, adds noise to the simulated sensor readings and gives the robots random pushes during training, so the walking copes with conditions it was not tuned for. Teams typically test on flat ground first, with a safety harness or a person nearby, before moving to rougher surfaces.
Walking is well suited to this method for several reasons. The reward is relatively easy to write down. Falling is cheap in simulation. The dynamics of a body with legs are well modelled by rigid-body physics. Many manipulation tasks are not as favourable, which is one reason grasping and cloth handling lean more on demonstrations.
Practising on real robots
Some work trains directly on physical robots. In a 2016 Google study described on arXiv, robot arms made more than 800,000 grasp attempts, with between 6 and 14 arms working at a time, and a neural network learned from the outcomes which grasps would succeed. Legged robots have learned on hardware too: in a 2022 study described on arXiv, a four-legged robot learned to walk in about 20 minutes of practice in the real world, on several indoor and outdoor surfaces, using a carefully tuned learning method and controller.
Real-world practice has practical demands. Something has to reset the scene after each attempt, whether a person, a mechanism or the robot itself. The robot needs to be able to detect success and failure by itself, often with extra cameras. Hardware wears, so the learning method must tolerate slow drift in how the robot behaves. And exploration must stay inside safety limits, as above. These demands explain why a large share of published results is in simulation, and why a lab demonstration of real-world learning is a substantial achievement.
Why does reinforcement learning need so much experience?
Trial and error is data-hungry because the robot learns from a weak signal. A demonstration says what to do at each step. A reward says only how things turned out, and often much later. If the robot drops a cup, the reward is low, but which of the thousand earlier actions was responsible is unclear. This is called credit assignment, and it takes many examples to sort out.
The calculator below shows how parallel simulation makes this volume possible. It is an illustration of scale. Real training runs differ in how many copies they use, how fast each runs and how long training lasts, and a large amount of experience does not guarantee a good policy.
How much experience parallel simulation provides
Total control steps collected steps
Single-robot experience days
Single-robot experience years
Real training runs vary widely. This only shows the scale of parallel simulation.
The defaults of 4,096 copies and 50 control steps a second match the training code from the 2021 walking study above. With them, two hours on the clock become about 341 days of single-robot experience, or about 1.47 billion control steps. One physical robot would need nearly a year to gather the same amount, running without a break and losing no time to resets or repairs. That comparison is why simulation and reinforcement learning go together.
Reward hacking and odd behaviours
When a policy exploits a gap between the reward as written and the outcome the designer wanted, researchers call it reward hacking or specification gaming. The behaviour is not clever in any human sense. It is the straightforward result of optimising what was asked for rather than what was meant.
Two hypothetical cases show the pattern. A robot rewarded only for how fast its feet move could learn to vibrate its legs on the spot instead of walking. A gripper rewarded for being close to an object could learn to hover beside it without ever picking it up. In both cases the reward goes up while the task is not done. A policy may also exploit a simulator bug, such as a physics glitch that adds energy, and then fail completely on hardware.
Defences are mostly about care. Watch videos of the learned behaviour instead of reading only the reward curve. Add penalties for the specific unwanted behaviours. Test in several simulators, or with randomised physics, so one bug cannot be exploited. And keep the reward as close as possible to the real objective, such as a measured task success.
Combining rewards with demonstrations
The two approaches complement each other. A common recipe starts with imitation learning on a modest set of demonstrations, so the policy begins near a reasonable behaviour, then improves it with reinforcement learning on a reward for task success. The demonstrations remove most of the wandering exploration, and the reward lets the policy exceed the demonstrators in speed or reliability, within what the reward measures.
Other combinations include using demonstrations to learn the reward itself, which is called inverse reinforcement learning, and learning a model of how the world responds so the policy can practise inside that model, which is called model-based reinforcement learning. The method comparison shows where each approach fits and how teams blend them. Large pretrained robot models can also be fine-tuned with rewards after training on demonstrations, an area of active research.
Where reinforcement learning is used in robots now
The clearest successes are in locomotion, where simulation-trained policies are used for legged robots on rough ground, and in dexterous manipulation research, such as the robot hand and cube result discussed in the simulation guide and described in a paper on arXiv. It also serves as a way to fine-tune policies in simulated tasks, and to find motions for hard control problems.
In everyday industrial robots, classic programming still does most of the work, and reinforcement learning is more often found in research and pilot projects than in the factory standard. On low-cost hardware, imitation is the usual starting point: the open-source LeRobot documentation directs newcomers to an imitation learning tutorial and recommends ACT, an imitation method, as the first policy to try.
For readers weighing a claim that a robot taught itself something, the questions to ask are the task, the reward, how many attempts it took, whether it was simulated and whether it was tested on hardware. The demo video checklist turns these into a tick list. Further questions can go through the contact page.
Myth or fact
Myth Myth: A reinforcement learning robot understands the goal.
Fact Fact: It optimises a number. If the number does not match the goal, the robot follows the number.
Myth Myth: Trial and error means the robot teaches itself with no human input.
Fact Fact: People write the reward, set up the simulator, choose the limits and review the results.
Myth Myth: Reinforcement learning is always better than imitation learning.
Fact Fact: Each suits different tasks, and many projects combine them.
Myth Myth: What works in simulation works the same on the robot.
Fact Fact: The reality gap means simulated policies need real-world testing and sometimes fine-tuning.
Frequently asked questions
- What is reinforcement learning in simple terms?
It is learning by trial and error. The robot tries actions, receives a number called a reward that scores the result, and over many attempts shifts towards actions that earn more reward. Nobody demonstrates the movement.
- What is a reward function?
It is the rule that turns an outcome into a number. A walking robot might get positive reward for moving at the requested speed and negative reward for falling or wasting energy. Writing it well is difficult because the robot will optimise exactly what it says.
- Why is trial and error dangerous for real robots?
Early actions are close to random, so a real robot may hit objects, strain its motors or injure someone nearby. That is why exploration is limited by speed and force caps, workspace barriers and emergency stops, and why much of it happens in simulation first.
- How do robots learn to walk?
Typically in simulation. Thousands of simulated copies are rewarded for tracking a commanded speed while staying upright, over varied terrain. In a 2021 study this took about twenty minutes on one graphics card for uneven ground. The trained policy is then tested on a real robot, with domain randomisation helping the transfer.
- What is reward hacking?
It is when a policy exploits a gap between the reward as written and the outcome the designer wanted. For example, a robot rewarded only for foot speed could learn to vibrate its legs instead of walking. It is the literal result of optimising the reward. Watching the behaviour, not only the reward curve, helps catch it.
- Is reinforcement learning better than imitation learning?
Neither is better in general. Imitation needs demonstrations and suits tasks people can show. Reinforcement learning needs a reward and many attempts and suits tasks that can be scored. Many projects use demonstrations first, then improve with rewards.