Skip to main content
2022-10-23 en

Safe Reinforcement Learning Beyond Reward Penalties

“Safe reinforcement learning” can refer to very different contracts. An expected accident budget over an episode, a maximum probability of failure, and a hard actuator limit at every timestep are not interchangeable. The first design decision is therefore the semantics of the constraint, not the optimizer used to solve it.

Reward and safety are different signals

A constrained Markov decision process (CMDP) extends an MDP with one or more cost signals. For a policy πθ\pi_\theta, define discounted reward and cost returns as

JR(θ)=Eτπθ[t=0γtrt],JCi(θ)=Eτπθ[t=0γtci,t].J_R(\theta)=\mathbb{E}_{\tau\sim\pi_\theta}\left[\sum_{t=0}^{\infty}\gamma^t r_t\right],\qquad J_{C_i}(\theta)=\mathbb{E}_{\tau\sim\pi_\theta}\left[\sum_{t=0}^{\infty}\gamma^t c_{i,t}\right].

The standard expected-cost problem is

maxθJR(θ)subject toJCi(θ)di,i=1,,m.\max_\theta J_R(\theta) \quad\text{subject to}\quad J_{C_i}(\theta)\le d_i,\qquad i=1,\ldots,m.

This formulation is useful because it gives reward and cost distinct units and budgets. It does not imply that every trajectory, or every action, is safe. A policy can satisfy an expected cost limit while still producing rare severe violations. If the application requires a per-step limit, it must be modeled explicitly, for example as

gi(st,at)0for every t.g_i(s_t,a_t)\le 0\qquad\text{for every }t.

Chance constraints, worst-case constraints, and almost-sure constraints establish still different contracts. Reporting only average episode cost hides those distinctions.

Why a fixed reward penalty is fragile

A common baseline replaces reward with rtαctr_t-\alpha c_t. It can be useful for diagnosis, but choosing α\alpha asks one scalar to encode units, risk tolerance, and changing constraint sensitivity. A high penalty may make learning overly conservative; a low penalty may make violations profitable.

Lagrangian methods make that trade-off adaptive:

L(θ,λ)=JR(θ)i=1mλi(JCi(θ)di),λi0.\mathcal{L}(\theta,\lambda)=J_R(\theta)-\sum_{i=1}^{m}\lambda_i\left(J_{C_i}(\theta)-d_i\right),\qquad \lambda_i\ge 0.

The policy seeks higher reward under the current prices λi\lambda_i, while the dual variables increase when estimated costs exceed their budgets. This is more principled than a fixed penalty, but it is not a hard runtime shield. Convergence statements depend on assumptions, step-size schedules, estimation error, and the function class; transient violations can still occur.

CPO: a trust-region update for expected constraints

Constrained Policy Optimization (CPO) follows the trust-region idea behind TRPO. Near the current parameters, it approximates reward improvement linearly, cost changes linearly, and policy KL quadratically. The practical local subproblem has the form

maxΔθ  gΔθsubject toci+biΔθ0,12ΔθHΔθδ.\max_{\Delta\theta}\;g^\top\Delta\theta \quad\text{subject to}\quad c_i+b_i^\top\Delta\theta\le 0,\qquad \frac{1}{2}\Delta\theta^\top H\Delta\theta\le\delta.

Here gg is the reward-policy gradient, bib_i is a cost-policy gradient, cic_i is the current estimated constraint excess, and HH is a local curvature matrix associated with average KL. Solving this quadratic approximation produces a direction that trades reward improvement against predicted feasibility inside a trust region; line search and recovery steps are then used to handle approximation error or an initially infeasible iterate.

The distinction between theorem and implementation matters. CPO derives policy-performance and cost bounds involving total variation distance, which is a distance or metric—not “total variation divergence.” Pinsker’s inequality connects it to KL:

DTV(p,q)12DKL(pq).D_{\mathrm{TV}}(p,q)\le\sqrt{\frac{1}{2}D_{\mathrm{KL}}(p\Vert q)}.

The paper establishes near-constraint-satisfaction guarantees under its assumptions and motivates a tractable update. A neural implementation still uses sampled advantages, finite batches, local Taylor approximations, conjugate-gradient solves, and line search. Those approximations mean CPO should not be advertised as an unconditional guarantee of zero violations. It targets expected cumulative constraints, and its empirical safety must be measured during training as well as after convergence.

Safety layers: correct the action before execution

For an instantaneous continuous-action constraint, a different pattern is to let an unconstrained policy propose a0a_0 and project it to a nearby feasible action. The safety-layer work models each next-step safety signal locally as an affine function of the action and solves

a=argmina12aa022subject tocˉi(s)+gi(s)aCi,i=1,,m.a^*=\arg\min_a\frac{1}{2}\lVert a-a_0\rVert_2^2 \quad\text{subject to}\quad \bar c_i(s)+g_i(s)^\top a\le C_i,\qquad i=1,\ldots,m.

Under the paper’s local linearization assumptions, this projection has an efficient correction rule. The experiments reported zero violations in the studied physics-based environments. That empirical result is not a universal shield: safety depends on the learned constraint model being accurate where the policy visits, on a feasible action existing, and on unmodeled dynamics and latency remaining within the design margin.

A safety layer also changes the learning problem. The policy proposes a0a_0, but the environment executes aa^*. Replay should record the executed action, while diagnostics should retain both values and the correction magnitude. Large, frequent corrections indicate that the actor is delegating control to the shield rather than learning the feasible region.

Choose the method from the constraint contract

Constraint contractSuitable starting pointWhat it does not guarantee by itself
Expected discounted or average cost budgetLagrangian method or CPO-style updateNo per-step or per-trajectory hard safety
Explicit differentiable instantaneous constraintProjection or optimization layerNo protection against model error or infeasibility
Unknown safety boundary with trusted logged dataLearned shield with uncertainty marginNo safety outside supported data without additional assumptions
Catastrophic event that must not occurIndependent certified controller, interlock, or verified backup policyReward optimization cannot replace system-level assurance

These components can be layered. For example, a CMDP optimizer can manage long-term energy or wear while a deterministic controller enforces actuator limits. The important point is to avoid claiming that an expectation constraint has become a hard physical guarantee merely because the same variable is called “cost.”

Evaluation must expose violations

Safe-RL evaluation needs more than final reward and mean cost:

  • reward and constraint return over environment steps, including early exploration;
  • violation rate, magnitude, maximum, and high quantiles—not just the mean;
  • the number of unsafe training episodes before convergence;
  • feasibility rate and correction norm for action projections;
  • results under model mismatch, disturbances, and held-out initial states;
  • multiple random seeds with confidence intervals and explicit failure cases.

The baseline set should include an unconstrained policy, a fixed penalty, an adaptive Lagrangian, and—when relevant—a non-learning feasible controller. If a method succeeds only because its initial policy is already safe, that assumption belongs in the result.

Engineering boundaries

Treat the learned policy as one layer in a safety case, not as the entire case. Validate cost sensors independently, define what happens when no feasible action exists, bound solver latency, and keep a deterministic fallback outside the neural policy. During deployment, monitor the distribution shift of both observations and constraint-model residuals. A method can satisfy its training CMDP and still violate the real system’s unmodeled requirements.

The core distinction is straightforward: CMDP algorithms manage a statistical budget, trust regions limit how aggressively the policy changes, and safety layers modify individual actions. They solve related but different problems. Precise constraint semantics are what prevent a mathematical “near-satisfaction” result from being mistaken for a physical guarantee.

Further reading