Offline Safe Reinforcement Learning Using Trajectory Classification

Ze Gong, Akshat Kumar, Pradeep Varakantham

Introduction

In safety-critical domains such as autonomous driving and robotics, reinforcement learning (RL) agents cannot be trained through real-world trial-and-error due to the catastrophic risks of unsafe actions. Offline Safe RL addresses this by training policies exclusively on pre-collected datasets. However, traditional methods often struggle with instability, myopic safety constraints, and over-conservatism. This paper introduces TraC (Contrastive Trajectory Classification), a novel approach that reframes safe RL as a trajectory classification problem, bypassing the complexities of standard min-max optimization.

The Challenge of Offline Safe RL

Existing offline safe RL algorithms typically rely on step-by-step cost constraints and Lagrangian min-max optimization. This approach faces three primary hurdles:

  1. Instability: Min-max optimization is notoriously difficult to tune and prone to divergence.
  2. Myopic Safety: Enforcing constraints at every time step can ignore the broader trajectory-level context of safety.
  3. Performance Trade-offs: Agents often become overly conservative, sacrificing significant reward to ensure safety, or fail to find a safe policy entirely.

The TraC Approach

TraC discards the standard RL optimization playbook in favor of a two-phase classification strategy.

1. Contrastive Dataset Construction

The algorithm first partitions the offline dataset into "desirable" and "undesirable" buckets based on a cost threshold.

  • Desirable: The top-performing safe trajectories based on cumulative reward.
  • Undesirable: All unsafe trajectories and the lowest-performing safe trajectories.
Figure 1: Illustration of partitioning offline data into desirable and undesirable sets based on a cost threshold (e.g., cost return < 40).

2. Contrastive Trajectory Classification

Instead of isolated state-action pairs, TraC optimizes the policy over entire trajectories. It uses a custom loss function inspired by regret-based preference models—similar to those used in Reinforcement Learning from Human Feedback (RLHF). The policy is trained to assign high probability scores to desirable trajectories and low scores to undesirable ones. To prevent distributional drift, the loss is regularized using a reference policy pre-trained via simple Behavior Cloning.

Evaluation and Results

The method was evaluated on the DSRL benchmark, encompassing 38 continuous control tasks across three simulators: SafetyGymnasium, BulletSafetyGym, and MetaDrive.

TraC consistently outperformed competitive baselines such as CDT, CPQ, and COptiDICE. A key strength of TraC is its ability to maintain high rewards while strictly adhering to safety constraints. For example, in BulletGym, TraC achieved a normalized reward of 0.61 with a safe normalized cost of 0.52. In contrast, baselines like CDT often violated safety (cost > 1.0), while others like BEAR-Lag were overly conservative with lower rewards.

Figure 3: Comparison of normalized reward vs. normalized cost across different methods. TraC (rightmost) shows a superior cluster of safe (green), high-reward outcomes compared to baselines.

Key Insights

  • Learning from Failure: Ablation studies demonstrate that training only on safe data is insufficient. The agent requires "undesirable" trajectories to explicitly learn which behaviors to avoid.
  • Robustness: TraC is highly robust to the specific percentages used for data partitioning, maintaining stable performance across various hyperparameter settings.
  • Trajectory Context: By evaluating whole sequences, the model captures global safety constraints more effectively than step-by-step penalties.

Conclusion

By reframing offline safe RL as a binary classification problem, TraC provides a stable and effective alternative to traditional constrained optimization. Its success across diverse benchmarks and its theoretical ties to preference-based learning suggest that trajectory classification is a powerful paradigm for aligning RL agents with complex safety requirements.

Summary updated

BibTeX
@inproceedings{gong2025offline,
  author = {Ze Gong and Akshat Kumar and Pradeep Varakantham},
  title = {{Offline Safe Reinforcement Learning Using Trajectory Classification}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aaai.v39i16.33855},
}
Figure