                                     Safety-Polarized and Prioritized Reinforcement Learning



5.2. Safety Prioritized Experience Replay                              5.3. Practical Implementation

Prioritized Experience Replay (PER) enhances off-policy                For large-scale tasks with discrete action space A based on

RL by assigning priorities to transitions based on their TD            DQN (Mnih et al., 2015), we use neural networks Q and

errors, emphasizing those with the highest learning potential.         ψ to approximate the optimal Q-function and the SA-REF,

This approach improves sample efficiency and accelerates               respectively (the corresponding target networks are Q̄ and

convergence, particularly in scenarios with sparse reward              ψ̄). We select the Sigmoid function as the activation for

signals. In our work, to address the challenge of sparse cost          the output layer of the ψ-network to ensure that its output

signals, we leverage PER to enhance the learning of the ψ              remains bounded within the range [0, 1]. The pseudo-code

network. The motivation behind this approach is similar                is presented in Algorithm 1.

to that of PER in traditional Q-learning, where transitions

                                                                       When computing the targets for Q and ψ updates, we use a

with larger TD errors are prioritized to accelerate learning.

                                                                       double-Q-learning-style implementation (Van Hasselt et al.,

Furthermore, importance sampling is employed to mitigate

                                                                       2015). Specifically, at lines 9 and 10 of Algorithm 1, the

the distribution shift introduced by the prioritization process.

                                                                       target for Q is computed by querying the action aQi+1 , which

Specifically, we introduce a prioritization technique tailored         combines the max Q-value with the safety polarization func-

for the ψ network. The priority of a transition i is deter-            tion penalty at the next state si+1 . This is done using the

mined by the ψ-TD error, defined as                                    current policy π, composed of the current Q and ψ. Sim-

                                                                       ilarly, the target for ψ (computed at lines 12 and 13 of

 δi = max{I[si ∈ Su ], min ψt (si+1 , a)} − ψt (si , ai ),             Algorithm 1) follows the same principle. Note that for the

                           a∈A

                                                         (18)          ψ target at line 12, we multiply ψ̄ by the discount factor γ

where the priority pi is calculated as pi = |δi | + ϵ. Here, ϵ         to reduce the long-term variance in ψ backward induction.

is a small positive constant added to ensure that transitions          This approach is analogous to the multiplication of Q̄ by γ

are not excluded from sampling when their error becomes                in DQN.

zero. The sampling probability for each transition is then             For the polarization function fpol used in our experiments,

computed using the prioritization heuristic:                           we select 10 · log(x) which demonstrates the best empirical

                                                                       performance. Additionally, we conduct an ablation study to

                               pα                                      analyze the effects of different polarization functions (see

                      P (i) = PN i            ,            (19)

                                        α

                                   k=1 pk                              Section 6.3).



where α ∈ [0, 1] controls the degree of prioritization. Unlike

                                                                       6. Experiments

traditional PER in Q-learning, our prioritization scheme

is explicitly tied to the learned ψ network, reflecting the            6.1. Experiment Setup

specific requirements of safety-critical scenarios.

                                                                       Benchmarks. Our evaluation adopts the following four

Since safety prioritized experience replay introduces sam-             tasks: TwoWay, Merge, Roundabout, and Intersection.

pling bias by altering the transition distribution, we correct         These tasks are from the highway-env environment (Leurent,

this bias using importance-sampling (IS) weights during the            2018; Leurent & Mercat, 2019), designed for simulated au-

ψ update. The IS weight for a sampled transition i is defined          tonomous driving with diverse objectives that require intri-

as:                                                                    cate behaviors to safely achieve the corresponding goals.

                                      θ

                             1     1                                   Additionally, our evaluation includes classical safe control

                    wi =       ·           ,              (20)         tasks such as Adaptive Cruise Control (ACC) (Anderson

                            N P (i)

                                                                       et al., 2020) and Circle (Achiam et al., 2017). We highlight

where N is the size of the replay buffer, and θ ∈ [0, 1]               that the cost functions used for the CMDP-based methods

controls the degree of correction. Then the update of our ψ            are designed as c = 1 when a crash happens, and 0 oth-

network will become                                                    erwise. More details about the environments, e.g., state

                                                                       spaces, action spaces, reward functions, etc., can be found

  ψt (st , at ) ←(1 − wt βt )ψt (st , at )+                            in Appendix C.1.

                wt βt max{I[st ∈ Su ], min ψt (st+1 , a′ )}.

                                       ′  a ∈A                         Baselines. Our base unconstrained RL is DQN which,

                                                        (21)           in our implementation, uses the double Q-learning tech-

By incorporating safety-prioritized experience replay, our             nique (Van Hasselt et al., 2015) by default. Other safe RL

approach effectively enhances the learning of the ψ network,           baselines include the following:

particularly in environments with long-horizon tasks and

sparse cost signals. This integration improves both safety

and sample efficiency.                                                    • Reward Shaping. The reward is shaped to −10 when



                                                                   6

