Skip to content

[Feature Request] PPO-Lagrangian for constrained RL on Safety-Gymnasium #4785

Backlog

Description

@vishalkotak

Motivation

TorchRL has had a Safety-Gymnasium wrapper since #3689, so envs now report a cost next to the reward. But there's no algorithm in the library that uses it, so you can't train a policy under a cost limit without writing the whole thing yourself. PPO-Lagrangian is the standard baseline for this. It's what the Safety Gym paper used, and OmniSafe and SafePO both ship it, so it'd be a good first constrained-RL algorithm for TorchRL.

Solution

A new loss that builds on clipped PPO:

  • The cost gets its own critic and its own GAE, using the existing GAE with the keys pointed at the cost.
  • The policy is trained on the reward advantage minus lambda times the cost advantage, divided by 1 + lambda.
  • Lambda is raised while the episode cost is over the limit and lowered (never below zero) while it's under, once per collected batch.

Everything else (collector, buffer, optimizers, logging) stays as in the current PPO script. I'd also add a sota script on SafetyPointGoal1, checked against OmniSafe's published numbers, plus tests.

One small change is needed first: the reward-summing transform only accepts reward keys, so it can't track episode cost, which lives in the observation spec. I'm sending that fix as its own PR, linked to this issue.

I have a working prototype of the loss with tests locally, and I'll open the PR once the design looks right to you.

Alternatives

A standalone multiplier module that works with the existing PPO loss as is, instead of a new loss class. It's lighter, but then the cost critic and its loss have to live in the training script. I went with the loss class because it keeps the training loop the same as PPO's. Happy to switch if you'd prefer the other way.

Later, the same multiplier could back a PID-Lagrangian variant (Stooke et al. 2020), which oscillates less, and SAC-Lagrangian for off-policy training.

Additional context

@theap06, since you added the safety-gymnasium wrapper, I'd like your thoughts on the design before I open the PR.

Checklist

  • I have checked that there is no similar issue in the repo (required)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions