                                    Safety-Polarized and Prioritized Reinforcement Learning



Algorithm 1 Safety-Polarized Optimal action Masks with                  5. Deep Q-Learning with Safety Polarization

Prioritized Experience Replay (SPOM PER)                                   and Safety Prioritized Experience Replay

 1: Require: a polarization function fpol , safety prioritiza-

    tion exponent α, importance sampling exponent θ                     In this section, we mainly focus on how we combine modern

 2: Initialize: a safety prioritized replay buffer D, Q-                deep Q-learning algorithms to solve practical safety-critical

    network Q, ψ-network ψ, along with target networks Q̄               RL tasks. In Section 5.1, we focus on a practical implemen-

    and ψ̄                                                              tation that transfers our proposed masking strategy using

 3: for each time step do                                               the polarization function. In Section 5.2, we adopt a tech-

 4:    Take action at based on π(st ) (cf. Equation (17))               nique inspired by prioritized experience replay to further

       combined with any exploration strategy, e.g., ϵ-                 help maximize safety in long-horizon sparse cost signal

       greedy                                                           scenarios.

 5:    Store the collected sample (st , at , rt , st+1 ) into D

       with maximal priority pt = maxi<t pi                             5.1. Safety Polarization for Q-functions

 6:    for each update step do                                          To fully implement our learned action masking in a deep RL

 7:       Sample a mini-batch of data (si , ai , ri , si+1 ) from       algorithm, we define the gating operator as

          replay buffer according to their priorities i ∼ P (i)                                  (

          (cf. Equation (19))                                                                      0, x ≤ η

                                                                                       Γη (x) =

 8:       Compute importance sampling weight based wi                                              1, otherwise.

          (cf. Equation (20))

 9:       Compute                                             Our action masking Cζt (s) defined in Equation 12 can be

                                                              viewed as adding −∞ to Qt at the masked action while

          aQ

           i+1 = arg max Q(si+1 , a) + fpol (1 − ψ(si+1 , a))

                                                              adding 0 to Qt at other action. Specifically, for actions

                     a∈A                                      where Γζt (s) (ψ(s, a)) = 1, we penalize Qt during training.

                                                              We implement the following class of polarization function to

10:       Compute Q targets yiQ = ri + γ Q̄(si+1 , aQi+1 )    combine with the learned Q-function. Formally, we define

11:       Minimize MSE loss between Q(si , ai ) and yiQ       the polarization function class as follows:

12:       Compute aψi+1 = arg mina∈A ψ(si+1 , a)              Definition 5.1. Define the polarization function class Fpol

13:       Compute ψ targets                                   as the set of all polarization functions fpol : [0, 1] →

                                                              [−∞,  0] which is a monotonically increasing function satis-

              yiψ = max{I[si ∈ Su ], γ ψ̄(si+1 , aψ

                                                  i+1 )}      fying fpol (1) = 0, fpol (0) := limx→0+ fpol (x) = −∞.

         and ψ-TD errors δi = yiψ − ψ(si , ai )                         Examples of polarization functions include c·log(x), 1− x1c

14:      Minimize weighted MSE loss between ψ(si , ai )                 for a constant value c > 0. Together with the gating operator

         and yiψ based on wi (cf. Equation (20))                        Γη and our learned optimal action mask Cζt (s), we derive

15:      Update transition priority pi based on ψ-TD errors             our learned MaxSafe policy can be rewritten as

16:      Update target networks Q̄ and ψ̄                                πt (s) = arg max Qt (s, a)

17:   end for                                                                        a∈Cζ (s)

18: end for

                                                                                                                          

                                                                         = arg max Qt (s, a) + fpol (1 − Γζ(s) (ψt (s, a))) . (16)

                                                                                 a∈A



                                                                        During implementation, we further get rid of the hard mask-

                                                                        ing rule induced by the gating operator Γ, and compose the

 (3) κ > 0 is small enough to identify the gap between

                                                                        Q-function Qt with the SA-REF ψt through f as follows:

     Cζ (s) and A \ Cζ (s), i.e., ∀b ∈ Cζ (s), e ∈ A \ Cζ (s),

                                                                            πt (s) := arg max Qt (s, a) + f (1 − ψt (s, a)).       (17)

                                                                                           a∈A

                    ψ ∗ (s, b) + 2κ < ψ ∗ (s, e),           (15)

                                                                        Intuitively, if a state-action pair (s, a) is measured to be

                                                                        unsafe, 1 − ψt (s, a) will be close to 0, then f (1 − ψt (s, a))

                                                                        will be close to −∞ and the action a is likely not to be

then the ψt update in Equation (11) will converge to the                chosen at state s since Qt (s, a) + fpol (1 − ψt (s, a)) is very

optimal SA-REF ψ ∗ , and our safe Q-learning update in                  low. Therefore the actions below the state-dependent thresh-

Equation (13) will converge to Q∗ζ . Thus, the learned pol-             old ζt (s) are more likely to be selected. The choice of the

icy πt (s) will converge to an optimal policy πζ∗ under our             polarization function fpol provides a balance between explo-

M AX S AFE objective (2).                                               ration and safety: we focus more on safety if fpol converges

                                                                        faster to −∞ as x → 0+, we encourages more exploration

The proof of Theorem 4.4 is deferred to Appendix B.4.                   otherwise.



                                                                    5

