Skip to the lesson
FixTheRobot Explore Robotics / Robot Learning / M5 Reward Design / M5-T5

Robot Learning · Theory · M5

M5-T5 — Constraints, Safety and Multi-Objective Rewards

A penalty is a price. A price can be paid. Whether Scout pays it depends on the exchange rate you wrote, not on what you meant.

  • Lesson 5 of 5
  • Module 5: Reward Design
  • Theory
  • About 17 minutes to read

A delivery robot has to be quick, must not fall down the stairwell, and should move smoothly enough that patients are not startled. Three goals, one number per step. The obvious way to combine them is to add them with weights. This lesson shows what that sum really says, how to read the trade-off it hides, what a constraint is and how it differs from a penalty, how Lagrangian methods connect the two, and why none of this is enough on its own to make a robot safe. All numbers come from exact value iteration on Scout’s ward and the stairwell corridor. Lab M5-L3 lets you move the weights and watch.

Learning objectives

After this lesson, you should be able to:

  • Explain what the weights of a weighted-sum reward mean as exchange rates, and find the penalty at which the optimal route changes.
  • Read a Pareto front of time against risk and say which points a weighted sum can reach.
  • State a constrained MDP, write the Lagrangian, and explain how the multiplier plays the role of a penalty chosen to meet a limit.
  • Say why safety cannot rest on a reward term alone, and name what else is needed (shield, limits, monitoring, review).

The intuition

Suppose a hospital porter is paid by the delivery and docked a fine for every accident. If the fine is small compared to the time saved by cutting a corner, a rational porter cuts the corner and pays the fine. Nobody is surprised: the fine is a price. A learner is that porter, with a perfect calculator. The question for the designer is therefore not “is the penalty large?” but “at what exchange rate between minutes and accidents does the behaviour change, and is the exchange rate I set on the right side of it?”

The figure shows the answer for the slippery stairwell corridor. Every dot is the optimal behaviour for one penalty. Between a penalty of 7 and 40 the behaviour hardly changes (7.7 percent of runs fall, then 5.3), even though the penalty grew by a factor of six. The zero-fall behaviour appears only at about 295, and it is a different kind of behaviour: slow, 32 steps instead of 12. The trade-off lives in the gaps between the dots, and the weighted sum picks a dot, not the gap.

Optimal policies for different stairwell penaltiesA scatter of expected steps against probability of falling. Penalty 0 gives 96 percent falls in 1.5 steps. Penalty minus 1 gives 27 percent in 8.7 steps. Minus 7 gives 7.7 percent in 11.8 steps. Minus 40 gives 5.3 percent in 12.4 steps. Minus 295 gives no falls but 32 steps. A dashed line joins the last two points.0204060801000714212835probability of falling (percent)expected steps to finishpenalty 0penalty -1penalty -7penalty -40penalty -295Raising the penalty from 7 to 40 buys 2.4 points of safety;the last 5 percent cost a different behaviour and 20 more steps.
Stairwell corridor with 10 percent slip. One optimal policy per penalty; the dashed segment is the best mixture of the last two policies.

The mathematics

Weighted sums

With several goals the reward is a sum r = rtime + wfall rfall + wcomfort rcomfort. Only the ratios of the weights matter: multiplying all of them by 2 changes the return scale, not the optimal policy. A weight is therefore an exchange rate.

Weighted sum
rt=−c⏟time: cost per step  −  λ 1[fall]⏟safety: penalty per fall  −  κ 1[turn]⏟comfort: penalty per change of directionr_t = -\underbrace{c}_{\text{time: cost per step}} \;-\; \underbrace{\lambda\,\mathbf 1[\text{fall}]}_{\text{safety: penalty per fall}} \;-\; \underbrace{\kappa\,\mathbf 1[\text{turn}]}_{\text{comfort: penalty per change of direction}}
The policy changes when
λ ΔP(fall)=c ΔE[steps]+κ ΔE[turns]\lambda\,\Delta P(\text{fall}) = c\,\Delta E[\text{steps}] + \kappa\,\Delta E[\text{turns}]
c
cost of one step (0.1 on the ward, 1 on the corridor).
λ
penalty for a fall. In a probability-times-penalty sum it is the price of 100 percent of a fall.
κ
comfort penalty per change of the commanded direction, a stand-in for jerk (see below).
ΔP, ΔE
differences between two candidate behaviours, for example a short risky route and a long safe one.

Between two behaviours the one with the larger weighted total wins. So the weights set where the switch happens, and everything on either side of that point looks identical to the learner: it makes no difference whether the penalty is 1 or 20 if the route flips at 24.

Pareto fronts

A behaviour is Pareto optimal if no other behaviour is better in one measure without being worse in another. The set of such points in the (time, risk) plane is the Pareto front. Sweeping the weight of one term traces some of the front. A weighted sum with positive weights reaches only the corners of the convex hull of the achievable points, and a deterministic policy gives you corners only. Between two corner policies there is a straight line, reached by mixing them: choose the safe policy for a fraction q of episodes and the fast one for the rest. Expected values are linear in q, so a mixture reaches every point on that segment, which is why constraints can ask for exactly what a weighted sum cannot hit.

Thresholds and constrained MDPs

Instead of paying for risk, require a bound on it. A constrained MDP (Altman, 1999) has a reward and one or more costs, and asks for the best policy among those whose expected cost stays below a limit:

maximise
Jr(π)=E[∑ γt rt]J_{r}(\pi )=E[\sum \,\gamma ^{t}\,r_{t}]
subject to Jc(π)=E[∑ ct]≤d\text{subject to}\,J_{c}(\pi )=E[\sum \,c_{t}]\le d
Lagrangian
L(π, λ)=Jr(π)−λ(Jc(π)−d),L(\pi ,\,\lambda )=J_{r}(\pi )-\lambda (J_{c}(\pi )-d),
λ≥0\lambda \ge 0
solve
max⁡π min⁡λ≥0 L(π, λ)\max _{\pi }\,\min _{\lambda \ge 0}\,L(\pi ,\,\lambda )
ct
a cost signal separate from the reward, for example 1 on the step of a fall and 0 otherwise.
d
the allowed expected total cost, for example a fall probability of 1 percent.
λ
the Lagrange multiplier: the price of the constraint, found to meet the limit and not chosen by hand.

For a fixed λ the Lagrangian is an ordinary MDP with reward r − λ c: exactly the weighted sum. The difference is who sets λ. A designer who writes the weighted sum guesses it. A Lagrangian method raises λ when the constraint is violated and lowers it when there is slack (a dual ascent step λ ← max(0, λ + η (Jc − d))) so that the learner ends at the multiplier that satisfies the limit. Practical algorithms built on this idea exist for deep policies (for example Achiam et al., 2017); the García and Fernández survey (2015) sorts the broader field of safe reinforcement learning into methods that change the objective and methods that change the exploration. In a finite constrained MDP the optimum may need a randomised policy (Altman), which is the mixture above.

Two cautions. The limit holds in expectation: a 1 percent bound allows one fall in a hundred runs, it does not forbid the fall. And it holds for the model: if the real slip is higher, so are the falls.

Comfort: jerk and its interactions

Jerk is the rate of change of acceleration, and passengers and payloads feel it. A tabular grid has no acceleration, so the stand-in used here is a turn penalty: a cost κ for every change of the commanded direction. The state is extended with the previous action so the reward is a function of the state, as lesson M4-T1 requires. In continuous control one subtracts a smoothness penalty such as κ‖at − at−1‖² from the reward. Comfort terms interact with the others in two ways. They add to the cost of any route with turns, so the safe detour (more turns) can become relatively more expensive and the policy flips back to the risky route. And a large enough comfort weight makes standing still the cheapest policy, because doing nothing has no jerk.

Shields and safety layers

A shield sits between the learned policy and the actuators and replaces any action that could lead to a forbidden state by a safe one (Alshiekh et al., 2018; lesson M4-T7 had a one-step shield on the cliff). It does not care what the reward says. Hybrid systems of a learned policy inside a classical controller with limits are the subject of lesson M9-T4, and staged rollout and monitoring of lesson M10-T5.

A worked example

Every number is computed with RLCore.gridworld and exact value iteration; fall probability, expected steps and expected turns are then evaluated exactly (undiscounted) for the resulting policy from the start state. Worlds: ward, the 7 × 9 ward with slip 0.2, step −0.1, goal +10, the stairwell ends the episode, γ = 0.95, penalty λ varied; corridor, the 4 × 8 stairwell corridor with slip 0.1, step −1, goal +10, the stairwell ends the episode, γ = 0.99. (This corridor differs from lesson M4-T6’s deterministic one, where a fall resets Scout.)

1. A stairwell penalty sweep on the ward.

Stairwell penalty sweep on the ward
Penalty λOptimal routeExpected stepsP(fall)V(start)
0short, past the stairwell edge12.5311.11%3.968
2short12.5311.11%3.837
10short12.5311.11%3.314
20short12.5311.11%2.659
30long, around the stairwell22.810.00%2.382
100long22.810.00%2.382

The optimal route is identical for every penalty from 0 to 20. The policy switches at λ = 24.24, where the two routes have the same value. A designer who tests −10, the same size as the goal reward, sees no change from −2 and believes the penalty “does nothing”; one who sets −10 believing it is large still has 1 run in 9 falling (about 11.1 percent). The behaviour changes only when the exchange rate passes about 242 steps of time per fall (24.24 / 0.1). Doubling or halving the step cost moves the threshold in proportion: only the ratio matters.

The slip matters too. At slip 0, a penalty of −10 gives 10 steps and no falls. At slip 0.1 the same penalty gives 11.12 steps and 5.26 percent falls. At 0.2, 12.53 and 11.11 percent. At 0.3 the optimal route is already the long one (21.56 steps, no falls). The same penalty is adequate in one ward and inadequate in the next, which is what lab M5-L3 tests.

2. The corridor and its Pareto points. Sweeping the penalty in steps of 1 from 0 to −1000 gave eight different optimal policies:

Optimal corridor policies by penalty
Penalty λ fromExpected stepsP(fall)What it does
01.4696.15%Steps off the edge: falling is a free exit from a cost of 1 per step (lesson M5-T4).
18.7027.37%Goes straight along the row next to the stairwell.
711.777.67%Climbs to the second row for part of the way.
9, 12, 1911.96, 12.07, 12.176.62%, 6.10%, 5.73%Climbs earlier each time, keeping off the edge for more of the corridor.
4012.395.26%Stays two rows above the stairwell until the last column.
29531.960.00%Presses against the wall at the start and waits for a slip to move it upward.

The figure plots them. Raising the penalty from 7 to 40 reduced falls by 2.4 percentage points (7.67 to 5.26) and added 0.6 steps. The remaining 5.26 percent needed λ ≈ 295 and 19.6 more steps. A penalty of 40 is already four times the goal reward, and still 1 run in 19 falls. It is the cheapest-penalty trap: the penalty grows by orders of magnitude and the risk by almost nothing, until the whole behaviour changes.

The zero-fall policy deserves a second look. Without a better action, it stays on the start cell, bumps into the left wall and waits for slip to push it up: safe, 32 steps, and a behaviour that would depend entirely on the slip being exactly what the model says. A reward term produced it correctly; nobody would deploy it.

3. A constraint instead of a price. Suppose the requirement is P(fall) ≤ 1 percent on the ward. A deterministic policy has two choices: short (11.11 percent) or long (0 percent), so it must be long, 22.81 steps. Mixing, to meet the cap exactly, is cheaper: take the long route with probability q = 1 − 0.01 / 0.1111 = 0.91 per episode, giving 21.89 expected steps and exactly 1 percent. For a cap of 5 percent the mixture q = 0.55 gives 18.19 steps against 22.81. On the corridor with a cap of 1 percent, mixing the 5.26 percent and the 0 percent policies with q = 0.81 gives 28.24 steps against 31.96. This is the constrained optimum (Altman), not reachable by any single weighted sum. Whether a randomised policy is acceptable is an engineering question: it means that on some days Scout takes the risky route on purpose.

4. Dual ascent on the ward. Cap 5 percent, start with λ = 0, step size η = 200, update λ ← max(0, λ + η (P(fall) − 0.05)), and solve the MDP at each λ:

First six dual ascent iterations
IterationλPolicyP(fall)Expected steps
10.00short11.11%12.53
212.22short11.11%12.53
324.44long0.00%22.81
414.44short11.11%12.53
526.67long0.00%22.81
616.67short11.11%12.53

The multiplier crosses the switching value 24.24 and the policy flips every iteration or two. The sequence of policies never settles (over 40 iterations the long policy was used in 52.5 percent of them, close to the exact mixture 55 percent). A Lagrangian method needs the average of the policies, or a small step size and a learned multiplier, to land on the constrained optimum. With a policy that is itself trained, this oscillation shows up as unstable training, which is one reason constrained methods are harder to tune than weighted sums.

5. Comfort interacts. The ward with a turn penalty κ per change of commanded direction (state extended with the last action; turns counted on the commanded actions):

Safety and comfort together
λκExpected stepsP(fall)Expected turns
10012.5311.11%1.50
10113.1611.11%1.37
10215.7611.11%1.16
105465.4510.90%1.03
30022.810.00%5.50
300.222.810.00%5.50
300.512.5311.11%1.50
305102.710.33%1.03

At λ = 30 the safe detour has 5.5 expected turns against 1.5 for the short route. A comfort weight of 0.223 flips the optimum back to the risky route (bisection): the comfort term costs safety. The penalty needed to keep falls below 1 percent rises with κ: 24.2 at κ = 0, 29.4 at 0.2, 37.1 at 0.5 and 49.8 at 1. At κ = 5 the robot mostly waits instead of turning: 465 expected steps at λ = 10, and 102.7 at λ = 30. Smoothness bought by doing nothing is the dock loop of lesson M5-T4 in another form.

What the simulation simplifies. Two small worlds, a single hazard, exact slip, and a turn count instead of jerk. The optimisation uses discounted returns (γ 0.95, 0.99) and the reported falls and steps are undiscounted, so the switching penalty would move a little if discounting were different. The exact penalties (24.24, 295) belong to these layouts; the pattern (long plateaus, then a jump; weights that interact; a stalling optimum for large weights) is the lesson.

Engineering implications and limitations

Why safety cannot rest on a reward term alone

This section covers engineering concepts for learners; it is not a safety case, and a real robot that works near people needs a hazard analysis by qualified people under the applicable standards. The reasons a reward term, however well tuned, is not enough are structural:

  • A penalty is a price, not a prohibition. The ward at λ = 20 still has 11.1 percent falls. The optimiser will pay any price that the exchange rate makes worth paying.
  • Expected values hide rare events. A constraint on the average still allows the single fall that matters. Rare events are hard to see in training: a 0.1 percent failure needs thousands of episodes to observe at all.
  • The reward may itself be wrong. Lesson M5-T4 showed a bonus that overrode the task; a safety term is another line of the same specification, with the same capacity for loopholes (the corridor’s zero-fall policy waits for a slip).
  • The model may be wrong. The slip of 0.1 or 0.2 above was a number in a table. A penalty tuned in simulation does not carry its fall probability to a floor that is wetter (lesson M1-T6, and Module 10).
  • A learned policy is approximate. Outside its training states there is no promise about what it does (lesson M3-T3).

What helps is a stack of independent layers, each of which does not rely on the layers above being right: a shield or safety layer that vetoes actions into forbidden states (Alshiekh et al., 2018) and speed, force and region limits implemented outside the learned policy (lesson M9-T4); monitoring and out-of-distribution detection with a safe fallback (lesson M10-T5); hardware stops that do not depend on software; staged rollout with people watching; and review of the specification and the logs. The learned policy and its reward are the performance layer. They may be allowed to be wrong at the layers below.

Choosing among penalties, constraints and shields

Penalty, constraint, shield
ToolUse it whenIt does not give you
Weighted penaltyThe trade-off is real and both sides are acceptable (a little more time for a little more comfort).A bound on any single term. Switching can be sudden (a plateau from 0 to 20, a jump at 24).
Constraint on expected costThere is a limit to respect on average and a cost signal you can measure in training.A guarantee per episode; correctness if the model is wrong.
Shield / safety layerSome events must not happen and you have a model of what is unsafe.More than the model knows; no help for harms it cannot detect.
Human review and staged rolloutAlways.Speed.

Practical advice

  1. Log every term of the reward, and each safety and comfort measure, separately, per episode. Never optimise or judge a single combined number.
  2. Sweep the weights, plot the front, and write down where the behaviour changes. Choose the weights from the plot, not from a guess, and re-run the sweep when the world (slip, load, floor) changes.
  3. State safety requirements as limits on measured quantities (for example, fall probability below a bound on a held-out set of starts) and check them with an independent test, not with the training reward.
  4. Watch for stalling: when a comfort or safety weight is raised, check that the robot still finishes the task.
  5. Put forbidden events under a shield or a limit outside the learner, and keep the penalty as a secondary signal.

Common misconceptions

“A large penalty makes the forbidden event impossible.”

It makes it expensive. On the ward, penalties from 0 to 20 give the same route and 11.1 percent falls; the route changes only past 24.24.

“If the weights are tuned, the sum has found the best trade-off.”

A weighted sum finds one corner of the front, and nothing tells you what lies between corners. The mixture of the 5.26 percent and 0 percent policies (28.24 steps at a 1 percent cap) is unreachable by any single weight.

“Adding a comfort term is free.”

On the ward at λ = 30 a comfort weight of only 0.5 sends Scout back to the risky route (11.1 percent falls), and 5 makes it nearly stand still.

“A constraint means the robot never exceeds the limit.”

It bounds an expected total in a model. A 1 percent cap allows falls; a wrong slip makes them more frequent.

Engineering takeaways

  1. The weights of a sum are exchange rates; only their ratios matter, and the optimal behaviour is constant between switching points (24.24 on the ward).
  2. Raising a penalty can do nothing for a long range and then change the whole behaviour: on the corridor, 7 to 40 cut falls from 7.7 to 5.3 percent and 295 was needed for none.
  3. A Pareto front lists the achievable trade-offs; a weighted sum reaches its corners, a constraint with a randomised policy reaches the segments between them.
  4. A constrained MDP puts the limit in a constraint and gets the price from a multiplier λ; a Lagrangian method finds λ, with oscillation that needs care.
  5. Comfort terms interact with safety terms and can push the optimum toward risk or toward standing still.
  6. Safety cannot rest on a reward term alone: add shields, limits outside the learner, monitoring, staged rollout and review.

References and further reading

The constrained-MDP textbook, a survey of safe reinforcement learning, and primary sources for constrained policy optimisation and shielding. The numbers in the example come from the computations described there.

  • E. Altman, Constrained Markov Decision Processes, Chapman and Hall/CRC, 1999.
  • J. García and F. Fernández, “A Comprehensive Survey on Safe Reinforcement Learning”, Journal of Machine Learning Research 16, 1437–1480, 2015.
  • J. Achiam, D. Held, A. Tamar and P. Abbeel, “Constrained Policy Optimization”, ICML, 2017.
  • M. Alshiekh, R. Bloem, R. Ehlers, B. Könighofer, S. Niekum and U. Topcu, “Safe Reinforcement Learning via Shielding”, AAAI, 2018.
  • D. Amodei, C. Olah, J. Steinhardt, P. Christiano, J. Schulman and D. Mane, “Concrete Problems in AI Safety”, arXiv:1606.06565, 2016 (safe exploration and avoiding side effects).

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

On the ward the optimal route is the same for every stairwell penalty from 0 to 20 and changes at 24.24. A designer tests penalties of 2 and 10, sees identical behaviour, and concludes that the penalty has no effect. What is wrong with the conclusion, and what does 24.24 / 0.1 tell you?

Explanation

The behaviour is constant on a plateau and switches only past the threshold, so tests on one side say nothing about the other. The ratio 242 is the number of steps of time that one fall must be worth before the long route wins.

Question 2

With a cap of 1 percent on falls, why can a mixture of two routes beat the best single deterministic route, and what does it cost?

Explanation

Expected falls and steps are linear in the mixing probability, so mixing the 11.1 percent short route with the 0 percent long route at q = 0.91 gives exactly 1 percent at 21.89 steps instead of 22.81. It means the risky route is chosen on purpose in some episodes, and the cap holds only on average.

Question 3

Give two reasons why a safety penalty in the reward is not a sufficient safety measure for a hospital robot.

Explanation

It is a price and not a prohibition (the optimiser pays it when the exchange rate favours it), the average hides rare events, and the reward or the simulator model may be wrong. Any two, with a layer outside the learner (a shield, limits, monitoring) as the remedy.

What’s next

Next: lab M5-L3 gives you sliders for time, stairwell and comfort weights, shows each setting’s route, expected time, fall probability and comfort, and asks you to find the setting that quietly trades safety for time. Module 6 then leaves the table behind and brings neural networks into reinforcement learning.

Practice this lesson

Balance Reward Terms

Choose weights for speed, collisions and comfort, and see which settings quietly trade safety for time.

Next lesson

M6-T1 — Function Approximation and Deep RL

Explain why tables stop working, what changes when a network represents a value or a policy, and what can go wrong.

Nothing below is required to finish this lesson.