                                      Safety-Polarized and Prioritized Reinforcement Learning



The proof of Lemma 3.1 can be found in Appendix B.1.                         Theorem 4.3. (Optimal SA-REF backward induction)

Grounded by Lemma 3.1, in the following section, we focus                    For tabular MDPs, with ψ0 = 0, the update

on designing an algorithm that learns Cζ (s) on the fly which

then guides the Q-function to select the action with optimal                 ψt+1 (s, a) = max{I[st ∈ Su ], Es′ ∼P (·|s,a) min

                                                                                                                           ′

                                                                                                                               ψt (s′ , a′ )}

                                                                                                                              a ∈A

reward among the safest actions and thus ensure maximal                                                                                       (10)

safety and reward to solve our M AX S AFE objective (2).                     will converge to the optimal SA-REF ψ ∗ as t → +∞.



                                                                             The proof of Theorem 4.3 is deferred to Appendix B.3.

4. Learning the Optimal Action Masks

In this section, we focus on learning state-dependent optimal                4.2. Safe Q-Learning with Optimal Action Masks

action masks Cζ (s). Ganai et al. (2023) proposed a reacha-                  With Theorem 4.3, we provide an update of ψt in the finite-

bility estimation function (REF) to capture the probability                  sample form and incorporate this update with the Q-learning

of constraint violation at state s under a given policy. In                  algorithm to obtain a safe Q-learning algorithm with theo-

our work, we extend the definition of REF to be state-action                 retical convergence-to-optimality guarantees.

dependent. As shown in Section 4.1, this extended REF can

serve as an estimate of the unsafe probability for a given                   The ψ update, starting from ψ0 = 0, goes as follows

state-action pair. It is then utilized to construct our action

                                                                                  ψt+1 (st , at ) = (1 − βt )ψt (st , at )+

masks, forming the foundation of our learning solution to                                                                                     (11)

the MaxSafe objective (2).                                                              βt max{I[st ∈ Su ], min

                                                                                                            ′

                                                                                                                ψt (st+1 , a′ )},

                                                                                                              a ∈A





4.1. State-Action Reachability Estimation Functions                          where 0 < βt ≤ 1 is the step size, each at ∼ πb (·|st ) is

                                                                             sampled from a behavior policy πb . Following Equation (9)

Definition 4.1. Given a policy π, we define the state-action                 we then define the learned optimal action masks as

reachability estimation function (SA-REF) ψ π : S × A →

[0, 1] as follows,                                                                      Cζt (s) := {b ∈ A | ψt (s, b) ≤ ζt (s)},              (12)

                        h                                 i

ψ π (s, a) := Eτ ∼(π,P ) max I[st ∈ Su ] s0 = s, a0 = a ,                    where for a small enough constant κ > 0, ζt (s) :=

                          st ∈τ                                              mina∈A ψt (s, a) + κ. Here κ is needed in order to tol-

                                                                   (6)       erate the stochastic approximation error during the learning

ψ π (s, a) represents the unsafe probability at state s and                  process. Note that the threshold function ζt (s) is state-

action a under the current policy π, that is, ψ π (s, a) =                   dependent, which is the key for the action masks Cζt to

Prτ ∼(π,P ) [∃st ∈ τ : st ∈ Su |s0 = s, a0 = a]. Expressing                  optimality.

in the form of Equation (6) provides a convenient way to                     We now propose our safe Q-learning update with optimal

compute ψ π (s, a) via backward induction.                                   action masks:

Lemma 4.2. We have the following Bellman backup for the                      Qt+1 (st , at ) ←(1 − βt )Qt (st , at )+

SA-REF ψ π :

                                                                                              βt (r(st , at ) + γ ′ max           Qt (st+1 , a′ )),

      ψ π (s, a) = max{I[s ∈ Su ],                                                                               a ∈Cζt (st+1 )

                                                                   (7)                                                                 (13)

                  Es′ ∼P (·|s,a),a′ ∼π(·|s′ ) [ψ π (s′ , a′ )]}.             where the difference from the original Q-learning algorithm

                                                                             is that the action maximizing Q at the next state s′ is chosen

The proof of Lemma 4.2 is in Appendix B.3. For conve-                        only from the learned optimal action mask during iteration

                                                 ∗

nience, we define the optimal SA-REF ψ ∗ := ψ π where π ∗                    t as Cζt (s′ ). Now the learned policy is of the form

is our optimal policy of M AX S AFE. Based on our SA-REF

formulation, we can rewrite the definition in Equation (3) as                               πt (s) := arg max Qt (s, a).                      (14)

                                                                                                           a∈Cζt (s)

                              ∗                      ∗

        ζ(s) = Ea∼π∗ (s) [ψ (s, a)] = min ψ (s, a)                 (8)

                                            a∈A                              The following convergence analysis is based on the fact that

since π ∗ reaches the minimum unsafe probability, and the                    Cζt (s′ ) will be the same as the optimal Cζ (s′ ) for t large

optimal action mask becomes                                                  enough and after that the Q-update (13) will stably converge.

                                                                             Theorem 4.4. For tabular MDPs, suppose the following

 Cζ (s) = {b ∈ A | ψ ∗ (s, b) ≤ ζ(s) = min ψ ∗ (s, a)}. (9)

                                              a∈A                            conditions hold:

Therefore, to find the optimal action mask Cζ , we would like

to learn the function ψ ∗ . Fortunately, this can be done via                (1) each state-action pair (s, a) is infinitely visited;

backward induction by mimicking π ∗ to take actions with                             P size sequenceP{βt }t≥0 satisfies 0 < βt ≤ 1

                                                                             (2) the step

the minimum unsafe probability as follows.                                       and t≥0 βt = +∞, t≥0 βt2 < +∞;



                                                                         4

