NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Anytime Safe Reinforcement Learning

Pol Mestres, Arnau Marzabal, Jorge Cortés · DOI

Abstract

Traditional safe reinforcement learning (RL) algorithms often prioritize asymptotic safety, ensuring that a policy is safe only after the training process has converged. However, in safety-critical applications such as autonomous driving or robotics, violations during the learning phase can be catastrophic. This paper presents Reinforcement Learning-based Safe Gradient Flow (RL-SGF), an algorithm designed to provide "anytime" safety guarantees. By formulating policy updates as a constrained optimization problem inspired by continuous-time gradient flows, RL-SGF ensures that the agent remains within safe boundaries at every iteration of the training process with high probability.

Introduction

Reinforcement Learning (RL) has demonstrated remarkable success in complex decision-making tasks. However, its standard paradigm of trial-and-error is often incompatible with real-world systems where safety is paramount. Most existing frameworks for safe RL utilize Constrained Markov Decision Processes (CMDPs), which seek to maximize a cumulative reward function while keeping a cost function below a predefined threshold.

A significant limitation of current CMDP solvers, such as primal-dual methods, is their focus on the safety of the final converged policy. During the training process, these agents frequently explore unsafe regions, leading to thousands of constraint violations. This paper addresses the challenge of "anytime safety," proposing a method that maintains feasibility from the very first update, even if training is interrupted prematurely.

Methodology: Reinforcement Learning-based Safe Gradient Flow (RL-SGF)

The proposed RL-SGF algorithm shifts the focus from penalizing safety violations to treating safety as a hard constraint within the parameter update space. The approach is grounded in the concept of gradient flows—continuous-time trajectories that stay within a feasible region of an optimization problem.

1. Quadratic Programming Updates

At each training step, RL-SGF updates the policy parameters by solving a convex Quadratically Constrained Quadratic Program (QCQP). This sub-problem utilizes local estimates of reward and safety gradients. The objective is to find a parameter update that maximizes reward improvement while ensuring the update vector does not push the policy into a "danger zone" where costs exceed the threshold.

2. Statistical Estimators and Confidence Bounds

Because the true reward and safety functions are typically unknown, RL-SGF relies on episodic data to generate unbiased estimates. To bridge the gap between estimation and safety, the authors provide explicit statistical bounds. By determining the necessary number of episodes ($N$), the algorithm can guarantee that a policy update is safe with a specific confidence level (e.g., 95% or 99%), accounting for the variance in the environment.

3. Adaptive Mechanisms

Unlike standard methods that use fixed step sizes or manually tuned penalty weights (dual variables), RL-SGF is inherently adaptive. It dynamically adjusts the step size and the relative weight of the safety constraint based on the local geometry of the parameter space. This is mathematically linked to the "Moving Balls Algorithm," ensuring that the policy eventually converges to a Karush-Kuhn-Tucker (KKT) point—a local optimum that satisfies all constraints.

4. Recovery from Unsafe Initializations

A unique feature of RL-SGF is its robust handling of unsafe starting points. If an agent begins in a state that violates constraints, the algorithm is designed to prioritize moving toward the safe region as rapidly as possible, suspending reward maximization until safety is restored.

Experimental Evaluation

The authors evaluated RL-SGF against two primary baselines: standard Primal-Dual (PD) methods and Constrained Policy Optimization (CPO). Testing was conducted in two simulated 2D navigation environments containing circular and rectangular obstacles:

  1. Single Integrator: A point-mass model with omnidirectional movement.
  2. Differential-Drive Robot: A non-holonomic model requiring complex orientation and velocity management.

Key Results

  • Unprecedented Safety: In the differential-drive robot task, RL-SGF maintained a safe policy for 99.83% of the training duration. In comparison, the Primal-Dual approach (initialized with $\lambda_0=0$) remained safe only 2.17% of the time.
  • Efficient Recovery: When initialized in an unsafe configuration, RL-SGF reached the feasible safe set significantly faster than both CPO and PD.
  • Performance Parity: Despite the strict safety constraints, RL-SGF achieved higher cumulative rewards than its competitors in the long run. This suggests that enforcing strict safety does not necessarily impede the agent's ability to learn high-performing behaviors; rather, it may guide the search more effectively through the feasible space.

Discussion and Future Work

While RL-SGF provides a robust framework for anytime safety, it faces challenges regarding sample complexity. The high-confidence safety guarantees require a substantial number of episodes per update, which may be prohibitive in some real-world settings. Additionally, the current implementation is on-policy, meaning data is discarded after each update, limiting its data efficiency.

Future research directions include extending RL-SGF to off-policy learning to improve sample efficiency and incorporating more sophisticated safety metrics, such as Value-at-Risk (VaR), to handle environments with high stochasticity. By refining these aspects, the anytime safety framework can be moved closer to deployment in physical, safety-critical autonomous systems.

Summary updated

BibTeX
@misc{mestres2025anytime,
  author = {Pol Mestres and Arnau Marzabal and Jorge Cortés},
  title = {{Anytime Safe Reinforcement Learning}},
  year = {2025},
  eprint = {2504.16417},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2504.16417},
}
Figure