Towards Safe Reinforcement Learning with a Safety Editor Policy
UM
SE
Env
Abstract
We consider the safe reinforcement learning (RL) problem of maximizing utility with extremely low constraint violation rates.
Assuming no prior knowledge or pre-training of the environment safety model given a task, an agent has to learn, via exploration, which states and actions are safe.
A popular approach in this line of research is to combine a model-free RL algorithm with the Lagrangian method to adjust the weight of the constraint reward relative to the utility reward dynamically.
It relies on a single policy to handle the conflict between utility and constraint rewards, which is often challenging.
We present SEditor, a two-policy approach that learns a safety editor policy transforming potentially unsafe actions proposed by a utility maximizer policy into safe ones.
The safety editor is trained to maximize the constraint reward while minimizing a hinge loss of the utility state-action values before and after an action is edited.
SEditor extends existing safety layer designs that assume simplified safety models, to general safe RL scenarios where the safety model can in theory be arbitrarily complex.
As a firstorder method, it is easy to implement and efficient for both inference and training.
On 12 Safety Gym tasks and 2 safe racing tasks, SEditor obtains much a higher overall safety-weighted-utility (SWU) score than the baselines, and demonstrates outstanding utility performance with constraint violation rates as low as once per 2k time steps, even in obstacle-dense environments.
On some tasks, this low violation rate is up to 200 times lower than that of an unconstrained RL method with similar utility performance.
Code is available at https://github.com/hnyu/seditor .
Introduction
Safety has been one of the major roadblocks in the way of deploying reinforcement learning (RL) to the real world.
Although RL for strategy and video games has achieved great successes (Silver et al., 2016; Vinyals et al., 2019; Berner et al., 2019), the cost of executing actions that lead to catastrophic failures in these cases is low: at most losing a game.
On the other hand, RL for control has also mostly been studied in virtual simulators (Brockman et al., 2016; Tassa et al., 2020).
Aside from the data collection consideration, to circumvent the safety issue ( e.g. , damages to real robots and environments) is another important cause of using these simulators.
When RL is sometimes applied to real robots (Kim et al., 2004; Levine et al., 2016; OpenAI et al., 2019), data collection and training are configured in restrictive settings to ensure safety.
Thus to promote the deployment of RL to more real-world scenarios, safety is a critical topic.
In this paper, we study a safe RL problem of maximizing utility with extremely low constraint violation rates.
There are two general settings of safe RL.
Some existing works make the assumption that the environment safety model is a known prior.
The agent is able to query an oracle function to see if any state is safe or not, without actually visiting that state.
This includes having access to a well calibrated dynamics model (Berkenkamp et al., 2017;
Chow et al., 2018; Thomas et al., 2021) or the set of safe/unsafe states (Turchetta et al., 2020; Luo and Ma, 2021;
Li et al., 2021).
Or they assume that a pre-training stage of safe/unsafe states or policies is performed on offline data, and then the learned safety knowledge is transferred to main tasks (Dalal et al., 2018;
Miret et al., 2020; Thananjeyan et al., 2021).
With these assumptions, they are able to achieve few or even zero constraint violations during online exploration.
However, such assumptions also put restrictions on their applicable scenarios.
This paper focuses on another setting where no prior knowledge or pre-training of safety models is assumed.
The agent only gets feedback on which states are unsafe from its exploration experience.
In other words, it has to learn the safety model (implicitly or explicitly) from scratch and the constraint budget can only be satisfied asymptotically .
Although we cannot completely avoid constraint violations during online exploration, this setting can be especially helpful for sim-to-real transfer (Zhao et al., 2020), or when the training process can be protected from damages.
Prior works combine model-free RL (Bhatnagar and Lakshmanan, 2012; Ray et al., 2019; Tessler et al., 2019; Bohez et al., 2019; Stooke et al., 2020;
Zhang et al., 2020; Qin et al., 2021) or model-based RL (As et al., 2022) with the Lagrangian method (Bertsekas, 1999).
This primal-dual optimization dynamically adjusts the weight of the constraint reward relative to the utility reward, depending on how well the violation rate target is being met.
A single policy is then trained from a weighted combination of the utility and constraint rewards.
However, reconciling utility maximization with constraint violation minimization usually poses great challenges to this single policy.
In this paper, we present SEditor , a general safe RL approach that decomposes policy learning across two polices (Figure 1).
The utility maximizer (UM) policy is only responsible for maximizing the utility reward without considering the constraints.
Its output actions are potentially unsafe.
The safety editor (SE) policy then transforms these actions into safe ones.
It is trained to maximize the constraint reward while minimizing a hinge loss of the utility state-action values before and after an action is edited.
Both UM and SE are trained in an off-policy manner for good sample efficiency.
Our two-policy paradigm is largely inspired by existing safety layer designs (Dalal et al., 2018;
Pham et al., 2018; Cheng et al., 2019; Li et al., 2021) which simplify ( e.g. , to linear or quadratic) environment safety models.
The high-level idea is the same though: modifying a utility-maximizing action only when necessary.
We evaluate SEditor on 12 Safety Gym (Ray et al., 2019) tasks and 2 safe car racing tasks adapted from Brockman et al.
(2016), targeting at very low violation rates.
SEditor obtains a much higher overall safety-weighted-utility (SWU) score (defined in Section 4) than four baselines.
It demonstrates outstanding utility performance with constraint violation rates as low as once per 2k time steps, even in obstacle-dense environments.
Our results reveal that the two-policy cooperation is critical, while simply doubling the size of a single policy network will not lead to comparable results.
The choices of the action distance function and editing function are also important in certain circumstances.
In summary, our contributions are:
a) We extend the existing safety layer works to more general safe RL scenarios where the environment safety model can in theory be arbitrarily complex.
b) We present SEditor, a first-order, easy-to-implement approach that is trained by SGD like most model-free RL methods.
It is efficient during both inference and training, as it does not solve a multi-step inner-level optimization problem when transforming an unsafe action into a safe one.
c) When measuring the distance between an action and its edited version, we show that in some cases the hinge loss of their state-action values is better than the usual L2 distance (Dalal et al., 2018; Pham et al., 2018; Li et al., 2021) in the action space.
We further show that an additive action editing function introduces an effective inductive bias for the safety editor. d) We propose the safety-weighted-utility (SWU) score for quantitatively evaluating a safe RL method.
The score is a soft indicator of the dominance defined by Ray et al. e) Finally, we achieve outstanding utility performance even with an extremely low constraint violation rate ( 5 × 10 − 4 ) in dense-obstacle environments.
On some tasks, this low violation rate is up to 200 times lower than that of a unconstrained RL method with similar utility performance.
Preliminaries
The safe RL problem can be defined as policy search in a constrained Markov decision process (CMDP) ( S , A , p, r, c ) .
The state space S and action space
A are both assumed to be continuous.
The environment transition function p ( s t +1 | s t , a t ) determines the probability density of reaching s t +1 ∈ S after taking action a t
∈ A at state s t ∈ S .
The initial state distribution µ ( s 0 ) deter- mines the probability density of an episode starting at state s 0 .
Both p ( s t + | s t , a t ) and µ ( s 0 ) are usually unknown to the agent.
For every transition ( s t , a t , s t +1 ) , the environment outputs a scalar r ( s t , a t , s t +1 ) which we call the utility reward.
Sometimes one uses the expected reward of taking a t at s t as r ( s t ,
a t ) ≜
E s t +1 ∼ p ( ·| s t ,a t ) r
( s t , a t , s t +1 ) for a simpler notation.
Similarly, the environment also outputs a scalar c ( s t , a t ) as the cost.
To unify the reward and cost notations, we define the constraint reward r c ( s t , a t ) ≜ − c ( s t , a t ) ≤ 0 , and the CMDP becomes ( S , A , p, r, r c ) .
For both utility and constraint rewards, the value is the higher the better .
Finally, we denote the agent’s policy as π ( a t | s t ) which dictates the probability density of taking a t at s t .
For each s t , the utility state value of following π is denoted by V π ( s t ) =
E π,p (cid:80) ∞
t ′ =
t γ t ′ − t r ( s t ′ , a t ′ ) , and the utility state-action value is denoted by Q π ( s t , a t ) =
r ( s t , a t ) + γ E s t +1 ∼ p
V π ( s t +1 ) , where γ ∈ [0 , 1) is the discount for future rewards.
Similarly, we can define V πc and Q πc for the constraint reward.
Then we consider the safe RL objective:
[Equation] [Equation]
where C ≥ 0 is the constraint violation budget.
It might be unintuitive to specify C for a discounted return, so one can rewrite
[Equation] [Equation]
and specify the per-step budget c instead, relating to C by (cid:80) ∞ t =
γ t c = c 1 − γ = C .
Note that c is not strictly imposed on every step.
Instead, it is only in the average sense (by a discount factor), treated as the violation rate target .
The Lagrangian method (Bertsekas, 1999) converts the constrained optimization problem Eq.
1 into an unconstrained one by introducing a multiplier [Equation] Intuitively
[Equation]
it dynamically adjusts the weight λ according to how well the constraint state value satisfies the budget [Equation] by evaluating (approximately) the difference
[Equation]
which is the gradient of λ given π in Eq.
When optimizing π given λ , Eq. 3 becomes
[Equation]
Thus λ can be seen as the weight of the constraint reward when it is combined with the utility reward to convert multi-objective RL into single-objective RL.
This objective can be solved by typical model-free RL algorithms.
For practical implementations, previous works (Ray et al., 2019; Tessler et al., 2019; Bohez et al., 2019) usually perform gradient ascent on the parameters of π and gradient descent on λ simultaneously, potentially with different learning rates.
Approach
We consider a pair of cooperative policies.
The first policy utility maximizer (UM) denoted by π ϕ , optimizes the utility reward by proposing a preliminary action ˆ a ∼ π ϕ ( ·| s ) which is potentially unsafe.
The second policy safety editor (SE) denoted by π ψ , edits the preliminary action by ∆ a ∼ π ψ ( ·| s, ˆ a ) to ensure safety, and the result action a = h
(ˆ a, ∆ a ) is output to the environment, where h represents an editing function.
Note that we condition SE on UM’s output ˆ a .
Together the two policies cooperate to maximize the agent’s utility while maintaining a safe condition (Figure 1).
For simplicity, we will denote the overall composed policy by π ψ ◦ ϕ ( a | s ) . Motivation.
SEditor decomposes a difficult policy learning task that maximizes both utility and safety, into two easier subtasks that focus on either utility or safety, based on the following considerations: a) Different effective horizons. In most scenarios, safety requires either responsive actions (Dalal et al., 2018), or planning a number of steps ahead to prevent the agent entering non-recoverable states.
Thus SE’s actual decision horizon could be short depending on the nature of the safety constraints.
This is in contrast to UM’s decision horizon which is usually long for goal achieving. In other words, we could expect the optimization problem of SE to be easier than that of UM, and
SE has a chance of being learned faster if separated from UM. b) Guarded exploration. From the perspective of UM, its MDP (precisely, action space) is altered by SE.
UM’s actions are guarded by the barriers set up by the SE. Instead of UM being discouraged for an unsafe action ( i.e. , punished with negative signal), SE gives suggestions to UM by redirecting the unsafe action to a safe but also utility-high action to continue its exploration. This guarded exploration leads to a better overall exploration strategy because safety constraints are less likely to hinder UM’s exploration (Figure 4 illustration).
Objectives. We employ an off-policy actor-critic setting for training the two policies.
Given an overall policy π ψ ◦ ϕ , we can use typical TD backup to learn Q π ψ ◦ ϕ
( s, a ) and Q π ψ ◦ ϕ c ( s, a ) parameterized as Q ( s, a ; θ ) and Q c ( s, a ; θ ) respectively, where we use θ to collectively represent the network parameters of the two state-action values.
Given s t +1 ∼ p ( ·| s t , a t ) and a t +1 ∼ π ψ ◦ ϕ ( ·| s t +1 ) , the Bellman backup operator (point estimate) for the utility state-action value is
[Equation]
with the backup operator for the constraint state-action value Q c defined similarly with r c .
Both Q ( s, a ; θ ) and Q c ( s, a, ; θ ) can be learned on transitions ( s t , a t , s t +1 ) sampled from a replay buffer.
For off-policy training of ϕ and ψ , we first transform Eq.
3 into a bi-level optimization surrogate as:
[Equation]
D denotes a replay buffer and Λ π ψ ◦ ϕ is defined by Eq.
We basically have a historical marginal state distribution for training the policies, but still use the initial state distribution µ for training λ .
The motivation for this difference is, when tuning λ , we should always care about how well the policy satisfies our constraint budget starting with µ but not with some historical state distribution.
We continue transforming the off-policy objective (Eq. 6, a) into two,
[Equation]
where d ( a, ˆ a ) is a distance function measuring the change from ˆ a to a .
It is not necessarily proportional to ∆ a because the editing function h could be nonlinear.
The role of SE π ψ is to maximize the constraint reward while minimizing some distance between the actions before and after the modification.
The role of UM π ϕ is to only maximize the utility reward.
However, it only can only do so through the lens of SE π ψ .
In other words, SE has actually changed the action space (also MDP) of UM. This property corresponds to the motivation of guarded exploration mentioned earlier.
We would like to emphasize that, unlike the safety layer (Dalal et al., 2018) which projects unsafe actions based on instantaneous safety costs, the training objective (Eq. 7, b) for SE relies on a safety critic Q c which is learned as the expected future constraint return.
Therefore, maximizing this critic will take into account long-term safety behaviors.
In other words, if there are non-recoverable states where SE can’t do anything to ensure safety, it will edit the agent’s actions long before that to avoid entering those states.
Action editing function.
We choose the editing function h to be mainly additive and non-parametric.
Without loss of generality, we assume a bounded action space [ − A, A ] M , and that both ˆ a and ∆ a are already in this space.
Then we define a = h (ˆ a, ∆ a )
=
min(max(ˆ a + 2∆ a, − A ) , A ) , where min and max are element-wise.
The multiplication by 2 and the clipping make sure that a ∈ [ − A, A ] M , namely, SE has a full control over the final action in that it can overwrite UM’s action if necessary. Because SE can be arbitrarily complex, its output ∆ a could depend on the current state s and the action proposal ˆ a in an arbitrarily complex way. Thus even though the additive operation is simple, the overall editing process is already general enough to represent any modification. This additive editing function is motivated by constraint sparsity .
Usually, constraint violations are only triggered for some states.
Most often, the action proposal by UM is already safe if the agent is far away from obstacles. To explicitly introduce this inductive bias, we use the additive editing function which ensures that the majority of SE’s modifications are close to 0. This makes the optimization landscape of SE easier (see Figure 12 Appendix F for some empirical observations).
Distance function.
Prior works on safety layer design, such as Dalal et al.
; Pham et al.
Li et al.
(2021), set the distance function d ( · , · ) as the L2 distance.
Later we will show that L2 is not always the best option.
Instead, we use the hinge loss of the utility state-action values of ˆ a and a :
[Equation]
This loss is zero if the edited action a already obtains a higher utility state-action value than the preliminary action ˆ a .
In this case, only the constraint Q is optimized by π ψ .
Otherwise, the inner part of Eq.
7 (b) is recovered as Q ( s, a ; θ ) + λQ c ( s, a ; θ ) , as we can drop the term − Q ( s, ˆ a ; θ ) due to its gradient w.r.t.
ψ being zero.
Our distance function in the utility Q space is more appropriate than the
L2 distance in the action space, because eventually we care about how the utility changes after the action is edited.
The L2 distance between the two actions is only an approximation to the change, from the perspective of the Taylor series of Q ( s, a ; θ ) .
Evaluating Λ π ψ ◦ ϕ .
Given a batch of rollout experiences { ( s n , a n ) } Nn =1 following π ψ ◦ ϕ , we approxi- mate the gradient of λ (Eq. 4) as
[Equation]
where c is the violation rate target defined in Eq.
Namely, after every rollout, we collect a batch of constraint rewards, compare each of them to − c , and use the mean of differences to adjust λ .
This approximation allows us to update λ using mini-batches of data instead of having to wait for whole episodes to finish, or having to rely on estimated V π ψ ◦ ϕ c which is usually not accurate. To reduce temporal correlation (which might affect the constraint evaluation) in the rollout batch data, we use multiple parallel environments (Appendix H). This rollout batch is also the one that will be put into the replay buffer.
Training.
To practically train the objectives, we apply SGD to Eq.
6 (b) and Eq.
7 simultaneously, resulting in a first-order method for approximated bi-level optimization (Likhosherstov et al., 2021).
SGD is made possible by applying the re-parameterization trick (Appendix G) to both ˆ a ∼ π ϕ and ∆ a ∼ π ψ .
A computational graph of Eq.
7 is illustrated in Figure 2.
We would like to highlight that a big difference between our SE and the previous safety layers (Dalal et al., 2018; Pham et al., 2018) is that SE directly uses a feedforward prediction ∆ a ∼ π ψ (Eq. 7, a) to replace the optimization result of the objective of transforming unsafe actions into safe ones.
In contrast, previous safety layers construct closed-form solutions or inner-level differentiable optimization steps ( e.g. , quadratic programming (Amos and Kolter, 2017)) for simplified safety models at every inference step.
Our overall approach generalizes to various constraint rewards (safety models) and action distance functions.
Finally, to encourage exploration, we add the entropy terms of π ϕ and π ψ into Eq.
7, with their weights dynamically adjusted according to two entropy targets following Haarnoja et al. Both UM and SE are trained from scratch in our experiments.
Here we discuss an alternative situation where an existing policy is pre-trained to maximize utility, and we use it as the initialization for UM.
In this case, UM cannot be frozen because its MDP (and hence its optimum) will be changed by the evolving SE.
Instead, it should be fine-tuned to adapt to the changing behaviors of SE. Potentially, a pre-trained UM will speed up the convergence of SEditor. We will leave this to our future work.  Experiments
Baselines.
We compare SEditor with four baselines.
- PPO-Lag combines PPO (Schulman et al. 2017) with the Lagrangian method, as done in Ray et al.
Since PPO is an on-policy algorithm, we expect its sample efficiency to be much lower than off-policy algorithms
- FOCOPS (Zhang et al., 2020) is analogous to PPO-Lag with two differences: 1) there is no clipping of the importance ratio, and 2) a KL divergence regularization term with a fixed weight is added to the policy improvement loss, with an early stopping when this term averaged over a rollout batch data violates the trust region constraint.
- SAC-actor2x-Lag combines SAC (Haarnoja et al., 2018) with the Lagrangian method.
Similar to SEditor, SAC-actor2x-Lag trains its policy on states sampled from the replay buffer but trains λ on states generated by the rollout policy .
Its gradient of λ is also estimated by Eq. 9.
The policy network size is doubled.
This is to match the model capacity of having two actors in SEditor. - SAC serves as an unconstrained optimization baseline to calibrate the utility return.
We choose not to compare with second-order CMDP approaches such as CPO (Achiam et al
) or PCPO (Yang et al ) because they require (approximately) computing the
inverse
of the Fisher information matrix, which is prohibitive when the parameter space is large.
Especially for our CNN based policies, second-order methods are impractical.
To analyze the key components of SEditor, we also evaluate two variants of it for ablation studies. - SEditor-L2 defines d ( a, ˆ a ) ≜
∥ a − ˆ a ∥ 2 as done in most prior works of safety layer. - SEditor-overwrite makes SE directly overwrite UM’s action proposal by a = h (ˆ a, ∆ a ) =
∆ a .
All compared approaches including the variants of SEditor, share a common training configuration ( e.g. , replay buffer size, mini-batch size, learning rate, etc) as much as possible.
Specific changes are made to accommodate to particular algorithm properties (Appendix H).
Evaluation metric.
Following Ray et al.
(2019), one method dominates another if “it strictly improves on either return or cost rate and does at least as well on the other”.
Accordingly, we emphasize that the utility reward or constraint violation rate should never be compared in isolation, as one could easily find a method that optimizes either metric very well.
Particularly in the experiments, we set the learning rate of the Lagrangian multiplier λ much larger than that of the remaining parameters.
This ensures that any continued constraint violation will lead to a quick increase of λ and drive the violation rate back to the target level.
This conservative strategy makes some compared methods have similar constraint violation curves, but vastly different utility performance.
For a quantitative comparison of their final performance, we compute the safety-weighted-utility (SWU) scores of the compared methods towards the end of training.
The score is a soft indicator of dominance, and it is a product of two ratios:
[Equation] [Figure]
The reason for having min { 1 , ·} is because we only care when the violation rate is higher than the target.
In practice, UtilityScore can be the episodic utility return (assuming positive) or the success rate.
We choose UtilityScore SAC as UtilityScore UnconstrainedRL .
In our experiments, both UtilityScore and ConstraintViolationRate are averaged over the last 110 of the training steps to reduce variances.
Safety Gym tasks.
In Safety Gym (Ray et al., 2019), a robot with lidar sensors navigates through cluttered environments to achieve goals.
We use the P OINT and C AR robots in our experiments.
Either of them has three tasks: G OAL , B UTTON , and P USH .
Each task has two levels, where level 2 has more obstacles and a larger map size than level 1.
In total we have 3 × 2 × 2 = 12 tasks.
Whenever an obstacle is in contact with the robot, a constraint reward of − 1 is given.
Thus the constraint violation rate can be calculated as the negative average constraint reward.
The utility reward is calculated as the decrement of the distance between the robot (G OAL and B UTTON ) or box [Figure] [Table] [Figure]
[Table]
[Figure] [Table] [Figure] [Table]
[Table]
[Table]
[Figure]
initial position.
The big green circle is the goal location and the other shapes are obstacles.
In summary, SEditor has a better exploration strategy because the barriers set up by SE can redirect UM’s unsafe actions to continue its exploration, while
SAC-actor2x-Lag’s exploration will be hindered in when encountering similar obstacles.
(P USH ) and the goal at every step.
We define a success as finishing a task within a time limit of 1000 .
We emphasize that the agent has no prior knowledge of which states are unsafe.
The map layout is randomized at the beginning of each episode.
This randomization poses a great challenge to the agent because it has to generalize the safety knowledge to new scenarios. We customize the environments to equip the agent with a more advanced lidar sensor in two ways: i)
It is a natural lidar instead of a pseudo one used by Ray et al.
The natural lidar simulates a ray intersecting with an object from an origin. The pseudo lidar can only detect object centers, while the natural lidar reveals shape information of objects, which is required to achieve a very low constraint threshold. ii) Our lidar has 64 bins while the original lidar has only 16 bins.
The 16-bin pseudo lidar’s low precision is a bottleneck of achieving much fewer safety violations.
Without perception precision, the problem is ill-posed for a robot suffering from blind spots to avoid touching objects at all.
We refer the reader to Appendix A.1 for more details of the environment and tasks. Our customized
Safety Gym is available at https://github.com/hnyu/safety-gym . We set the constraint violation target c = 5 × 10 − 4 , meaning that the agent is allowed to violate any constraint only once every 2k steps on average.
This threshold is only 150 of the threshold c = 0 .used by the original Safety Gym experiments (Ray et al., 2019), highlighting the difficulty of our task.
Each compared approach is trained with 9 random seeds.
The training curves are in Figureand the SWU scores are in Table 1.
We see that SEditor obtains much higher SWU scores than the baselines on 7 of the 12 tasks, while being comparable on the rest.
While SAC-actor2x-Lag performs well among the baselines, its double-size policy network does not lead to results comparable to SEditor.
This suggests that SEditor does not simply rely on the large combined capacity of two policy networks to improve the performance, instead, its framework in Figure 1 matters.
Both PPO-Lag and FOCOPS missed all the constraint violation rate targets.
The unconstrained baseline SAC obtains better success rate sample efficiency than SEditor with the C AR robot, but violates constraints much more.
Surprisingly, SAC is worse than SEditor with the P OINT robot regarding success rates, even without constraints.
Finally, we highlight that towards the end of training on C AR B UTTON 1/2,
C AR P USH 2, and C AR G OAL 2, SEditor violates constraints up to 200 times less than SAC does, while achieving success rates on par with SAC (black curves vs. dashed blue curves)!
To analyze the exploration behaviors of SAC, SAC-actor2x-Lag, PPO-Lag, and SEditor, we visualize their rollout trajectories on an example map of P
OINT G OAL 2, at different training stages (the training is done on randomized maps but here we only evaluate on one map).
For each approach, the checkpoints at 10%, 20%, 30%, and 50% of the training process are evaluated and 100 rollout trajectories are generated for each of them.
The trajectories are then drawn on a bird’s-eye sketch map (Figure 4).
We can see that although SAC is able to find the goal location in the very beginning, it tends to explore more widely regardless of the obstacles, and its final paths ignore obstacles.
SAC-actor2x-Lag learns to respect the constraints after 10% of training, but the obstacles greatly hinder its exploration: the trajectories are confined in a small region.
As a result, it takes some time for it to find the goal location.
PPO-Lag is slower at learning the constraints and it even hits obstacles at 50%.
SEditor is able to quickly explore regions between obstacles and refine the navigation paths passing them instead of being blocked.
Our ablation studies (Appendix D Figure 8) show that, in this particular safe RL scenario, SEditor is not sensitive to the choice of distance function: SEditor-L2 achieves similar results with SEditor.
However, the editing function does make a difference: SEditor-overwrite is clearly worse than SEditor in terms of utility performance.
This shows that the inductive bias of the edited action a being close to the preliminary action ˆ a is very effective (see Appendix F).
Our two-stage “propose-and-edit” strategy is more efficient than outputting an action in one shot.
Safe racing tasks.
Our next two tasks, S AFE R ACING and S AFE R ACING O BSTACLE , are adapted from the unconstrained car racing task in Brockman et al.
The goal of either task is to finish a racetrack as fast as possible.
The total reward of finishing a track is 1000 , and it is evenly distributed on the track tiles.
In S AFE R ACING , the car has to stay on the track and receives a constraint reward of − 1 whenever driving outside of it.
In S AFE R ACING O BSTACLE , the car receives a constraint reward of − 1 if it hits an obstacle on the track, but there is no penalty for being off-track.
An episode finishes after every track tile is visited by the car, or after 1000 time steps.
The track (length and shape) and obstacles (positions and shapes) are randomly generated for each episode.
As in Safety
Gym, this randomization could expose a never-experienced safety scenario to the agent at any time.
The agent’s observations include a bird’s-eye view image and a car status vector.
Note that this high-dimensional input space usually poses great challenges to second-order safe RL methods.
Again, the constraint violation rate can be computed as the negative average constraint reward.
We set the constraint violation rate target c = 5 × 10 − 4 .
For evaluation, we use undiscounted episode return as the UtilityScore.
Each compared approach is trained with 9 random seeds.
The training curves are shown in Figure 5 and SWU scores in Table 1.
SEditor has much higher SWU scores than all baselines.
Moreover, it is the only one that satisfies the harsh violation rate target towards the end of training.
Surprisingly, even with constraints SEditor gets a much better utility return than SAC on
S AFE R ACING .
One reason is that without the out-of-track penalty as racing guidance, the car easily gets lost on the map and collects lots of meaningless timesteps.
Although SAC gets a much higher return on S AFE R ACING O BSTACLE , it greatly violates the constraint budget.
Interestingly, the ablation studies show results that are complementary to those on the Safety Gym tasks (Appendix D Figure 9).
Now the action distance function makes a big difference.
Changing it to the L2 distance greatly impacts the utility return, especially on S AFE R ACING O BSTACLE where almost no improvement is made.
This demonstrates that the closeness of two actions does not necessarily reflect the closeness of their state-action values (Appendix E).
Related Work
Safe RL is closely related to multi-objective RL (Roijers et al., 2013), where the agent optimizes a scalarization of multiple rewards given a preference (Van Moffaert et al., 2013), or finds a set of policies covering the Pareto front if no preference is provided (Moffaert and Nowé, 2014).
In our case, the preference for the constraint reward is always changing, because we try to maintain it to a certain level instead of maximizing it.
Trust-region methods (Achiam et al., 2017; Chow et al., 2019; Yang et al., 2020; Liu et al., 2022) for solving CMDPs put a constraint on the KL divergence between the new and old policies, where the
KL divergence is second-order approximated.
For solving the surrogate objective at each iteration, the inverse of the Fisher information matrix is approximated.
Liu et al. (2022) decomposes the policy update step of CPO (Achiam et al., 2017) into two steps, where the first E-step computes a non-parametric form of the optimal policy and the second M-step distills it to a parametric policy. These methods often involve complex formulations and high computational costs.
In contrast, SEditor is a conceptually simple and computationally efficient first-order method.
[Figure] The line of works closest to ours is safety layer (Dalal et al., 2018; Pham et al., 2018;
Cheng et al., 2019; Li et al., 2021).
Although they also transform unsafe actions into safe ones, their safety layers assume simple safety models which can be solved in closed forms or by inner-level optimization ( e.g. , quadratic programming (Amos and Kolter, 2017)).
We extend this idea to more general safe RL scenarios without assuming a particular safety model.
Our SE can be also treated as a teacher under the teacher-student framework, where UM is the student whose actions are corrected by SE.
However, existing works (Turchetta et al., 2020; Langlois and Everitt, 2021) under this framework more or less rely on various heuristics and task-specific rules, or have access to the simulator’s internal state.
In complex realistic environments, such assumptions are invalidated.
Recovery RL (Thananjeyan et al., 2021) also decomposes policy learning across two policies.
It makes a hard switch between a task policy and a recovery policy by comparing a pre-trained constraint critic with a manual threshold.
It requires an offline dataset of demonstrations to learn the constraint critic and fix it during online exploration.
This offline training requirement and the hard switch scheme reduce the flexibility of Recovery RL.
DESTA (Mguni et al., 2021) fully decouples utility from safety by introducing two separate agents.
The safety agent can decide at which states it takes control of the system while the task agent can only maximize utility at the remaining states.
There is hardly any communication or cooperation between the two agents. In contrast, SEditor lets UM and SE cooperate with a “propose-and-edit” strategy, instead of switching between them. Flet-Berliac and Basu (2022) also adopts a two-policy design.
However, their two policies are adversarial. More specifically, one of their policies tries to intentionally maximize risks by behaving unsafely, to shrink the feasibility region of the agent ’s value function. Their overall policy will bias towards being conservative.
Our SE minimizes risks, and UM and SE always cooperate to resolve the conflict between utility and safety.  Limitations and Conclusions While SEditor is able to maintain an extremely low constraint violation rate in expectation, oscillation does happen occasionally on the training curves around the target threshold (Figure 3).
In general, if no oracle safety model can be accessed by the agent, avoiding this oscillation issue would be very difficult.
On one hand, our tasks always generate new random environment layouts for new episodes, and in theory the agent has to generalize its safety knowledge ( e.g. , one obstacle being dangerous at this location usually implies it being also dangerous even when the background changes).
On the other hand, neural networks have the notorious issue of catastrophic forgetting, meaning that while the agent learns some new safety knowledge, it might forget that already learned.
Finally, from a practical point of view, as the violation target reaches 0, it takes an exponential number of samples to evaluate if the agent truly can meet the target.
These challenging issues are currently ignored by
SEditor and would be interesting future directions.
In summary, we have introduced SEditor, for general safe RL scenarios, that makes a safety editor policy transform preliminary actions output by a utility maximizer policy into safe actions.
In problems where a utility maximizing action is often safe already, this architecture leads to faster policy learning.
We train this two-policy framework with simple primal-dual optimization, resulting in an efficient first-order approach.
On 14 safe RL tasks with very harsh constraint violation rates,
SEditor achieves an outstanding overall SWU score.
We hope that SEditor can serve as a preliminary step towards safe RL with harsh constraint budgets. NeurIPS Checklist
For all authors... (a) Do the main claims made in the abstract and introduction accurately reflect the paper’s contributions and scope?
[Yes] (b) Did you describe the limitations of your work?
[Yes
] Section(c) Did you discuss any potential negative societal impacts of your work?
[N/A]
The paper is about RL safety which strives to provide positive societal impacts.
(d) Have you read the ethics review guidelines and ensured that your paper conforms to them?
[Yes] 2.
If you are including theoretical results... (a) Did you state the full set of assumptions of all theoretical results?
[N/A] No theoretical results.
(b) Did you include complete proofs of all theoretical results?
[N/A] 3.
If you ran experiments... (a) Did you include the code, data, and instructions needed to reproduce the main experimental results (either in the supplemental material or as a URL)?
[Yes] (b) Did you specify all the training details (e.g., data splits, hyperparameters, how they were chosen)?
[Yes] Appendix H (c) Did you report error bars (e.g., with respect to the random seed after running experiments multiple times)?
[Yes] For example, Figure 3 and Figure(d) Did you include the total amount of compute and the type of resources used (e.g., type of GPUs, internal cluster, or cloud provider)?
[Yes] Appendix H 4.
If you are using existing assets (e.g., code, data, models) or curating/releasing new assets... (a) If your work uses existing assets, did you cite the creators?
[Yes] See Section 4, Appendix A.1, and Appendix A.2.
(b) Did you mention the license of the assets?
[N/A] (c) Did you include any new assets either in the supplemental material or as a URL?
[Yes] (d) Did you discuss whether and how consent was obtained from people whose data you’re using/curating?
[N/A]
The data are open-sourced.
(e) Did you discuss whether the data you are using/curating contains personally identifiable information or offensive content?
[N/A] We only use virtual simulators.
If you used crowdsourcing or conducted research with human subjects... (a)
(b) Did you describe any potential participant risks, with links to Institutional Review Board (IRB) approvals, if applicable?
[N/A] (c) Did you include the estimated hourly wage paid to participants and the total amount spent on participant compensation?
[N/A]
[Figure]
[Figure]
[Table]
A Task details A.1 Safety Gym
In Safety Gym (Ray et al., 2019) environments, a robot with lidar sensors navigates through cluttered environments to achieve tasks.
There are three types of tasks (Figure 6) for a robot:
1) G OAL : reaching a goal location while avoiding hazard zones and vases.
2) B UTTON : hitting one goal button out of several buttons while avoiding gremlins and hazard zones.
3) P USH : pushing a box to a goal location while avoiding pillars and hazard zones.
Each task has two levels, where level 2 has more obstacles and a larger map size than level 1.
In total there are 3 × 2 = 6 tasks for a robot.
We use the P OINT and C AR robots in our experiments.
We customized the environment so that the robot has a natural lidar of 64 bins.
The natural lidar contains more information of object shapes in the environment than the default pseudo lidar.
We found that rich shape information is necessary for the agent to achieve a harsh constraint threshold.
A separate lidar vector of length 64 is produced for each obstacle type or goal.
All lidar vectors and the robot status vector ( e.g. , acceleration, velocity, rotations) are concatenated together to produce a flattened observation vector.
A summary of the observation dimensions is in Table 2.
Whenever an obstacle is in contact with the robot, a constraint reward of − 1 is given.
The utility reward is calculated as the decrement of the distance between the robot (G OAL and B UTTON ) or box (P USH ) and the goal at every step.
An episode terminates when the goal is achieved, or after 1000 time steps.
We define a success as achieving the goal before timeout.
The map layout is randomized at the beginning of each episode.
We emphasize that the agent has no prior knowledge of which states are unsafe, thus path planning with known obstacles does not apply here.
Our customized Safety Gym is available at https://github.com/hnyu/safety-gym .
A.2 Safe Racing
The agent’s observation includes a bird’s-eye view image ( 96 × 96 ) and a car status vector (length 11) consisting of ABS sensor, wheel angles, speed, angular velocity, and the remaining tile portion.
The action space is [ − 1 , 1] 3 .
Based on the code https://github.com/NotAnyMike/gym/ blob/master/gym/envs/box2d/car_racing.py , we modify the original unconstrained car rac- ing task (Brockman et al., 2016) to add obstacles on the track.
A bird’s-eye view of the two safe racing tasks is illustrated in Figure 7.
We set the obstacle density ( #Obstacles#TrackTiles ) to 10% for S AFE R ACING O BSTACLE .
B Training with 10M Environment Steps
In Figure 3, one might wonder if SEditor only improves sample efficiency of the Lagrangian SAC, but doesn’t really improve the final performance on the 12 Safety Gym tasks.
To answer this question, we report here the SWU score comparison between SAC-actor2x-Lag and SEditor with 10M steps on Safety Gym.
The SWU scores are calculated based on the performance of unconstrained SAC at 10M steps (and thus are not directly comparable to those in Table 1).
[Table]
[Table]
We observe that both methods have saturated at 10M steps.
Training more steps somewhat decreases but not closes the gap between SAC-actor2x-Lag and SEditor regarding the final performance.
C Experiment on the Unmodified P OINT G OAL
Since we have modified the Safety Gym environments to pursue a much (98%) lower constraint violation threshold, one might be curious to see if SEditor also performs well on the original unmodified tasks.
As a representative experiment, we compare SEditor (averaged over 4 random seeds) with the results reported in Ray et al.
and Stooke et al.
(2020) on the unmodified
P OINT G
OAL 1.
We observe that all methods can satisfy the constraint threshold well; the difference residues in their utility performance.
We list their (rough) utility scores at different environment steps below:
[Table]
It’s unsurprising that SEditor did pretty well under such a much higher cost limit.
We also observe that without P-control, SEditor’s cost curve is similar to the ones of K P = 0 in Stooke et al.
(2020), which is expected: the initial cost was high and then quickly dropped to the limit.
Our cost stabilized at about 3M steps while Stooke et al.
(2020) stabilized at about 10M steps (with K I = 1 × 10 − 2 ).
D Ablation Study Results
Figure 8 and 9 show the comparison results between SEditor and its two variants SEditor-L2 and
SEditor-overwrite, as introduced in Section 4.
All three approaches share a common training setting except the changes to the action distance function d ( a, ˆ a ) or the editing function h
(ˆ a, ∆ a ) .
We notice that Figure 8 and 9 have opposite results, where SEditor-L2 is comparable to SEditor in the former while SEditor-overwrite is comparable to SEditor in the latter.
This suggests a hypothesis that the magnitude of ∆ a output by SE is usually small in Safety Gym but larger in the safe racing tasks, because SEditor-overwrite removes the inductive bias of ˆ a being close to h (ˆ a, ∆ a ) .
To verify this hypothesis, we record the output ∆ a when evaluating the trained models of SEditor on two representative tasks P OINT P USH 1 and S AFE R ACING O BSTACLE .
For either task, we plot the empirical distribution of ∆ a over 100 episodes (each episode has 1000 steps).
The plotted distributions are in Figure 10.
It is clear that on P OINT P USH 1, the population of ∆ a is more centered towards 0 .
On S AFE R ACING O BSTACLE , the population tends to distribute on the two extremes of ± 1 .
This somewhat explains why the L2 distance can be a good proxy for the utility Q closeness on
Safety Gym but not on the safe racing tasks.
[Figure]
[Figure]
[Figure]
[Figure]
[Table]
E Hinge Loss vs. L2 distance
In Figure 9, SEditor-L2 is especially bad compared to SEditor, indicating that the L2 distance is not a good choice for measuring the difference between the utility action ˆ a and the edited action a , when we actually attempt to compare their state-action values.
For further analysis, we evaluate a trained
SEditor model on S AFE R ACING O BSTACLE , and inspect the following inference results during an episode:
a) The action proposed by UM π ϕ , also known as the utility action ˆ a ∈ [ − 1 , 1] 3 ; b) The edited action a ∈ [ − 1 , 1] 3 by SE π ψ as the output to the environment; c) The hinge loss of their utility state-action values (Eq.
8); d) The squared L2 distance (per dimension) of the two actions 13 ∥
ˆ a − a ∥
We select 7 key frames of the episode and visualize their corresponding inference results in Figure 11.
The semantics of the action space is listed in Table 5.
In the first frame, the car just gets back on track from outside and there is an obstacle in front of it.
The utility action ˆ a steers left while the edited action a steers right due to safety concern.
This causes their squared L2 distance to be quite large.
However, Q ( s, a ; θ ) is no worse than Q ( s, ˆ a ; θ ) , and thus in this case π ψ of SEditor only needs to focus on maximizing the constraint reward, while π ψ of SEditor-L2 has to make compromises.
The second frame is where the hinge loss is positive because ˆ a commands acceleration while a does not, resulting in a potential decrease of the utility return.
(The front tires of the car are already steered all the way to the right, thus both actions turn left.)
Overall, SE is more cautious and wants to slow down when passing the obstacle.
For frames 2 and 3, ˆ a and a are similar, as the car is temporarily free from constraint violation.
Frame 4 is an example where a subtle difference in the L2 distance results in a large hinge loss.
The car is driving near the border of the track, and at any time it could go off-track and miss the next utility reward (a reward is given if the car touches a track tile).
Thus ˆ a turns all the way to the left to make sure that the off-track scenario will not happen.
However, because there is an obstacle in front, a makes the steering less extreme.
Since the car is at the critical point of being on-track, even a small difference in steering results in a large hinge loss.
In comparison, the left-front and left-rear tires are already on the track in frame 5, and even though a wants to turn right a little bit to avoid the obstacle, the utility return is not affected and the hinge loss is still zero.
Frame 6 is an example where both the L distance and hinge loss are small.
F Action Editing Function
In Section 3, our motivation for an additive action editing function is to ensure an easier optimization landscape for SE π ψ .
We hypothesize that if each proposed ˆ a is “mostly” safe, then there is an inductive bias for π ψ to output ∆ a → 0 .
In Section 4 on the Safety Gym tasks, we did observe that
SEditor-overwrite (directly using ∆ a as the final action) is worse than SEditor.
To further show why an additive editing function is beneficial, when evaluating SEditor we visualize the function surface of Eq.
7 (b) w.r.t.
∆ a given a sampled action proposal ˆ a in Figure 12.
It is clear that compared to an overwriting editing function, an additive editing function always has a much larger set of optimal ∆ a .
Furthermore, this set almost always covers those ∆ a that are close to 0.
Thus the additive editing function does provide a very good inductive bias for SE π ψ .
G Parameterization
We set λ
= softplus ( λ 0 ) to enforce the Lagrangian multiplier λ ≥ 0 , where λ 0 is a real-valued variable.
Thus Eq.
6 (b) becomes
[Equation]
(10) as unconstrained optimization solved by typical SGD.
We parameterize both policies π ϕ and π ψ as Beta distribution policies (Chou et al., 2017).
The advantage of Beta over Normal (Haarnoja et al., 2018) is that it natively has a bounded support of [0 , 1] for a continuous action space.
It avoids using squashing functions like tanh which could have numerical issues when computing the inverse mapping.
With PyTorch, we can use the reparameterization trick for the Beta distribution to enable gradient computation in Eq.
Generally speaking,
Eq.
7 is re-written as
[Equation]
where ϵ 1 and ϵ 2 are sampled from two fixed noise distributions.
Then gradients can be easily computed for ϕ and ψ .
H Hyperparameters and Compute
In this section, we list the key hyperparameters used by the baselines and SEditor.
A summary for the
Safety Gym experiments is in Table 6.
For the safe racing experiments, we only list the differences with Table 6 in Table 7.
For the remaining implementation details, we refer the reader to the source code https://github.com/hnyu/seditor .
With the model hyperparameters and training configurations above, a single job (one random seed) of each compared method in Section 4 takes up to 6 hours training on any of the Safety Gym tasks and up to 20 hours training on either safe racing task, on a single machine of Intel(R) Core(TM) i9-7960X CPU@2.80GHz with 32 CPU cores and one RTX 2080Ti GPU.
In practice, we use our internal cluster with similar hardware to launch multiple jobs in parallel
[Table]
[Table]
I Success and Failure Modes
Finally, we show example success and failure modes of SEditor on different tasks in Figureand Figure 14, respectively.
We briefly analyze the failure case of each episode in Figure 14.
In
C AR G OAL 2, the robot faced a crowded set of obstacles in front of it, making its decision very difficult considering the safety requirement.
It took quite some time to drive back and forth, before committing to a path through the two vases in its right front (the fourth frame).
However, when passing a vase, the robot incorrectly estimated its shape and the distance to the vase.
Even though the majority of its body passed, its left rear tire still hit the vase (the last two frames).
In P OINT B UTTON 2, the robot sped too much in the beginning of the episode, and collided into an oncoming gremlin due to inertia (the floor is slippery!).
It did not learn a precise prediction model of the gremlin’s dynamics.
In
C AR P USH 2, the robot spent too much time getting the box away from the pillar and did not achieve the goal in time.
These failure cases might be just due to insufficient exploration in similar scenarios.
In S AFE R ACING O BSTACLE , the robot learned to take a shortcut for most sharp turns, essentially sacrificing some utility rewards for being safer (skipping obstacles).
The reason is that during every sharp turn with a certain speed, the car’s state is quite unstable.
It requires very precise control to avoid obstacles during this period, which has not been learned by our approach.
[Figure]
[Figure]
J
Pseudocode
Algorithm 1:
Input: Learning rate α
Initialize: Randomize θ , ϕ , and ψ ; reset the replay buffer D ← ∅ for each training iteration do //
Rollout begins
Reset the rollout batch B r ← ∅ for each rollout step do
Action proposal by UM: ˆ a ∼ π ϕ
(ˆ a | s ) Action editing by SE:
∆ a ∼ π ψ (∆ a | s, ˆ a ) Output action a =
h
(ˆ a, ∆ a )
Environment transition s ′ ∼ P ( s ′ | s, a ) Add the transition to the rollout batch
B r ← B r (cid:83) { ( s, a, s ′ , r ( s, a ) , r c ( s, a )) } end
Store the rollout batch in the buffer D ← D (cid:83) B r //
Training begins
Estimate the gradient of the Lagrangian multiplier λ by evaluating Eq.
9 on B r
Update the multiplier by Eq. 10: λ 0 ← λ 0 − α Λ π ψ ◦ ϕ
Sample a training batch
B from the replay buffer D for gradient steps below Perform one gradient step on the critic parameters θ by TD backup (Eq. 5) on Q and Q c
Perform one gradient step on UM: ϕ ← ϕ + α ∆ ϕ
(gradient of Eq.
7, a)
Perform one gradient step on SE: ψ ← ψ + α ∆ ψ (gradient of Eq.
7, b)
Update other parameters such as entropy weight, target critic network, etc.
end