Agent Conversation: claude-opus-4.6
Task: safe-rl
Safe RL: Constraint-Handling Mechanism Design
Objective
Design a constraint-handling mechanism for safe reinforcement learning. Your code goes in custom_lag.py, a subclass of PPO registered as CustomLag. Reference implementations (PPOLag using Lagrange multiplier, CPPOPID using PID controller) are provided as read-only.
Background
Safe RL aims to maximize reward while satisfying safety constraints (keeping episode cost below a limit). The key challenge is how to adaptively balance reward and cost: the Lagrangian approach converts the constrained problem to an unconstrained dual problem via a multiplier lambda, while PID methods use control theory for more responsive constraint satisfaction. You must design: (1) a multiplier update rule in _update(), and (2) an advantage combination formula in _compute_adv_surrogate().
Evaluation
Evaluated on 3 Safety-Gymnasium environments to test generalization:
- SafetyPointGoal1-v0: point robot navigating to goals while avoiding hazards
- SafetyCarGoal1-v0: car robot (non-holonomic) navigating to goals while avoiding hazards
- SafetyPointButton1-v0: point robot pressing goal buttons while avoiding hazards
Metrics: episode reward (higher is better) and episode cost (lower is better, target <= 25.0). Each environment trains for 2M steps.
Baselines
- naive: no constraint handling (pure PPO, ignores cost)
- ppo_lag: Lagrangian multiplier updated via Adam optimizer
- pid_lag: PID controller for multiplier update
omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py [EDITABLE — lines 20–20, lines 48–70 only]
1: """Custom Lagrangian-based safe PPO for MLS-Bench.
2:
3: EDITABLE section: imports + constraint handling methods.
4: FIXED sections: algorithm registration, learn() with metrics reporting.
5: """
6:
7: from __future__ import annotations
8:
9: import time
10:
11: import numpy as np
12: import torch
13:
14: from omnisafe.algorithms import registry
15: from omnisafe.algorithms.on_policy.base.ppo import PPO
16:
17: # ===================================================================
18: # EDITABLE: Custom imports
19: # ===================================================================
20:
21:
22: # ===================================================================
23: # FIXED: Algorithm class definition
24: # ===================================================================
25: @registry.register
26: class CustomLag(PPO):
27: """Custom Lagrangian-based safe RL algorithm.
28:
29: Extends PPO with constraint handling for safe reinforcement learning.
30: The agent must design:
31: 1. _init: Initialize constraint handler state (call super()._init() first)
32: 2. _init_log: Register logging keys (call super()._init_log() first)
33: 3. _updaTool Input
1{
2 "op": "replace",
3 "filename": "omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py",
4 "start_line": 20,
5 "end_line": 20,
6 "content": "from collections import deque"
7}OK: Replaced lines 20..20 with 1 line(s) in omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py. Editable range: 20–20, 48–70.
[Current file: omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py | editable: 20–20, 48–70 | total: 149 lines]
20 from collections import deque
...
48 def _init(self) -> None:
49 super()._init()
50 self._cost_limit: float = self._cfgs.lagrange_cfgs.cost_limit
... (17 more lines) ...
68 Agent should incorporate self._lagrangian_multiplier to penalize cost.
69 """
70 return adv_rTool Input
1{
2 "content": " def _init(self) -> None:\n super()._init()\n self._cost_limit: float = self._cfgs.lagrange_cfgs.cost_limit\n self._lambda_lr: float = self._cfgs.lagrange_cfgs.lambda_lr\n\n # --- Novel: Adaptive PID with Gain Scheduling, Anti-Windup,\n # Predictive Lookahead, and Asymmetric Advantage Combination ---\n # Base PID gains\n self._base_kp: float = 0.1\n self._base_ki: float = 0.01\n self._base_kd: float = 0.01\n\n # PID state\n self._integral: float = 0.0\n self._integral_max: float = 10.0\n self._smoothed_delta: float = 0.0\n self._cost_ema: float = 0.0\n self._cost_history: deque = deque(maxlen=10)\n self._cost_history.append(0.0)\n\n self._lagrangian_multiplier: float = 0.0\n self._epoch_count: int = 0\n\n def _init_log(self) -> None:\n super()._init_log()\n self._logger.register_key('Metrics/LagrangeMultiplier', min_and_max=True)\n\n def _update(self) -> None:\n Jc = self._logger.get_stats('Metrics/EpCost')[0]\n assert not np.isnan(Jc), 'cost is nan'\n\n delta = float(Jc - self._cost_limit)\n\n # 1) Adaptive gain scheduling: amplify gains proportionally to violation\n violation_ratio = max(0.0, delta / max(self._cost_limit, 1.0))\n gain_scale = 1.0 + 2.0 * violation_ratio\n kp = self._base_kp * gain_scale\n ki = self._base_ki * gain_scale\n kd = self._base_kd\n\n # 2) Smoothed proportional term (EMA of delta)\n self._smoothed_delta = 0.9 * self._smoothed_delta + 0.1 * delta\n p_term = kp * self._smoothed_delta\n\n # 3) Integral with anti-windup and asymmetric rates\n # Fast wind-up (violating), slow wind-down (satisfying) -> safety bias\n if delta > 0:\n self._integral += delta * ki\n else:\n self._integral += delta * ki * 0.5\n self._integral = max(0.0, min(self._integral, self._integral_max))\n\n # 4) Derivative via EMA of cost\n self._cost_ema = 0.9 * self._cost_ema + 0.1 * float(Jc)\n d_raw = self._cost_ema - self._cost_history[0]\n d_term = kd * max(0.0, d_raw)\n self._cost_history.append(self._cost_ema)\n\n # 5) Predictive lookahead: penalize upward cost trends\n if len(self._cost_history) >= 3:\n lookback = min(5, len(self._cost_history) - 1)\n trend = self._cost_ema - self._cost_history[-1 - lookback]\n predict_term = 0.02 * max(0.0, trend)\n else:\n predict_term = 0.0\n\n self._lagrangian_multiplier = max(0.0,\n p_term + self._integral + d_term + predict_term)\n self._epoch_count += 1\n\n super()._update()\n self._logger.store({'Metrics/LagrangeMultiplier': self._lagrangian_multiplier})\n\n def _compute_adv_surrogate(self, adv_r: torch.Tensor, adv_c: torch.Tensor) -> torch.Tensor:\n \"\"\"Novel asymmetric advantage combination.\n\n Key idea: penalize cost-increasing actions (adv_c > 0) more heavily\n than rewarding cost-decreasing actions. This creates a natural safety\n bias that reduces constraint violations without being overly conservative.\n \"\"\"\n penalty = self._lagrangian_multiplier\n # Asymmetric scaling: 30% extra penalty on cost-increasing directions\n asymmetry = 0.3\n cost_weight = penalty * (1.0 + asymmetry * (adv_c > 0).float())\n return (adv_r - cost_weight * adv_c) / (1.0 + penalty)",
3 "end_line": 70,
4 "filename": "omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py",
5 "op": "replace",
6 "start_line": 48
7}OK: Replaced lines 48..70 with 85 line(s) in omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py. Editable range: 20–20, 48–132.
[Current file: omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py | editable: 20–20, 48–132 | total: 211 lines]
20 from collections import deque
...
48 def _init(self) -> None:
49 super()._init()
50 self._cost_limit: float = self._cfgs.lagrange_cfgs.cost_limit
... (79 more lines) ...
130 asymmetry = 0.3
131 cost_weight = penalty * (1.0 + asymmetry * (adv_c > 0).float())
132 return (adv_r - cost_weight * adv_c) / (1.0 + penalty)[Test #1] (2 tests remaining; call submit(n=N) to choose which test result to submit as final) ## Seed 42 ### SafetyPointGoal1-v0 (scripts/point_goal.sh) Training metrics (last epochs): TRAIN_METRICS epoch=95 ep_ret=-0.0437 ep_cost=14.7600 ep_len=1000.0 TRAIN_METRICS epoch=96 ep_ret=-0.0690 ep_cost=16.9800 ep_len=1000.0 TRAIN_METRICS epoch=97 ep_ret=-0.0390 ep_cost=14.1800 ep_len=1000.0 TRAIN_METRICS epoch=98 ep_ret=0.0309 ep_cost=9.1600 ep_len=1000.0 TRAIN_METRICS epoch=99 ep_ret=0.0999 ep_cost=9.6600 ep_len=1000.0 Final evaluation: TEST_METRICS ep_ret=0.0999 ep_cost=9.6600 ep_len=1000.0 ### SafetyCarGoal1-v0 (scripts/car_goal.sh) Training metrics (last epochs): TRAIN_METRICS epoch=95 ep_ret=0.8159 ep_cost=9.0500 ep_len=1000.0 TRAIN_METRICS epoch=96 ep_ret=0.9796 ep_cost=10.3500 ep_len=1000.0 TRAIN_METRICS epoch=97 ep_ret=1.1371 ep_cost=12.1300 ep_len=1000.0 TRAIN_METRICS epoch=98 ep_ret=1.1037 ep_cost=10.9300 ep_len=1000.0 TRAIN_METRICS epoch=99 ep_ret=1.0575 ep_cost=11.6400 ep
Tool Input
1{
2 "content": " def _init(self) -> None:\n super()._init()\n self._cost_limit: float = self._cfgs.lagrange_cfgs.cost_limit\n self._lambda_lr: float = self._cfgs.lagrange_cfgs.lambda_lr\n\n # --- Novel: Adaptive PID with Gain Scheduling, Anti-Windup,\n # Predictive Lookahead, and Asymmetric Advantage Combination ---\n # Base PID gains – tuned more aggressively for tighter constraint satisfaction\n self._base_kp: float = 0.15\n self._base_ki: float = 0.02\n self._base_kd: float = 0.015\n\n # PID state\n self._integral: float = 0.0\n self._integral_max: float = 10.0\n self._smoothed_delta: float = 0.0\n self._cost_ema: float = 0.0\n self._cost_history: deque = deque(maxlen=10)\n self._cost_history.append(0.0)\n\n # Safety margin: target slightly below cost_limit for robustness\n self._effective_limit: float = self._cost_limit * 0.8 # 20% margin\n\n self._lagrangian_multiplier: float = 0.0\n self._epoch_count: int = 0\n\n def _init_log(self) -> None:\n super()._init_log()\n self._logger.register_key('Metrics/LagrangeMultiplier', min_and_max=True)\n\n def _update(self) -> None:\n Jc = self._logger.get_stats('Metrics/EpCost')[0]\n assert not np.isnan(Jc), 'cost is nan'\n\n # Use effective limit (with safety margin) for tighter constraint satisfaction\n delta = float(Jc - self._effective_limit)\n\n # 1) Adaptive gain scheduling: amplify gains proportionally to violation\n violation_ratio = max(0.0, delta / max(self._cost_limit, 1.0))\n # Quadratic scaling for stronger response to large violations\n gain_scale = 1.0 + 3.0 * violation_ratio + 2.0 * violation_ratio ** 2\n kp = self._base_kp * gain_scale\n ki = self._base_ki * gain_scale\n kd = self._base_kd\n\n # 2) Smoothed proportional term (EMA of delta)\n self._smoothed_delta = 0.85 * self._smoothed_delta + 0.15 * delta\n p_term = kp * self._smoothed_delta\n\n # 3) Integral with anti-windup and asymmetric rates\n # Fast wind-up (violating), slow wind-down (satisfying) -> safety bias\n if delta > 0:\n self._integral += delta * ki\n else:\n # Slower wind-down: only decrease integral at 1/3 rate\n self._integral += delta * ki * 0.33\n self._integral = max(0.0, min(self._integral, self._integral_max))\n\n # 4) Derivative via EMA of cost\n self._cost_ema = 0.85 * self._cost_ema + 0.15 * float(Jc)\n d_raw = self._cost_ema - self._cost_history[0]\n d_term = kd * max(0.0, d_raw)\n self._cost_history.append(self._cost_ema)\n\n # 5) Predictive lookahead: penalize upward cost trends\n if len(self._cost_history) >= 3:\n lookback = min(5, len(self._cost_history) - 1)\n trend = self._cost_ema - self._cost_history[-1 - lookback]\n predict_term = 0.03 * max(0.0, trend)\n else:\n predict_term = 0.0\n\n self._lagrangian_multiplier = max(0.0,\n p_term + self._integral + d_term + predict_term)\n self._epoch_count += 1\n\n super()._update()\n self._logger.store({'Metrics/LagrangeMultiplier': self._lagrangian_multiplier})\n\n def _compute_adv_surrogate(self, adv_r: torch.Tensor, adv_c: torch.Tensor) -> torch.Tensor:\n \"\"\"Novel asymmetric advantage combination with focused cost penalty.\n\n Key ideas:\n 1. Asymmetric penalty: cost-increasing actions (adv_c > 0) are penalized\n more heavily than cost-decreasing actions are rewarded.\n 2. Rescaled normalization to preserve learning signal.\n \"\"\"\n penalty = self._lagrangian_multiplier\n # Asymmetric scaling: 50% extra penalty on cost-increasing directions\n asymmetry = 0.5\n cost_weight = penalty * (1.0 + asymmetry * (adv_c > 0).float())\n return (adv_r - cost_weight * adv_c) / (1.0 + penalty)",
3 "end_line": 132,
4 "filename": "omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py",
5 "op": "replace",
6 "start_line": 48
7}OK: Replaced lines 48..132 with 92 line(s) in omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py. Editable range: 20–20, 48–139.
[Current file: omnisafe/omnisafe/algorithms/on_policy/naive_lagrange/custom_lag.py | editable: 20–20, 48–139 | total: 218 lines]
20 from collections import deque
...
48 def _init(self) -> None:
49 super()._init()
50 self._cost_limit: float = self._cfgs.lagrange_cfgs.cost_limit
... (86 more lines) ...
137 asymmetry = 0.5
138 cost_weight = penalty * (1.0 + asymmetry * (adv_c > 0).float())
139 return (adv_r - cost_weight * adv_c) / (1.0 + penalty)[Test #2] (0 tests remaining; call submit(n=N) to choose which test result to submit as final) [NOTE] This was your last test. You MUST now call submit(n=X) to choose which test result to submit as your final answer. ## Seed 42 ### SafetyPointGoal1-v0 (scripts/point_goal.sh) Training metrics (last epochs): TRAIN_METRICS epoch=95 ep_ret=-0.0437 ep_cost=14.7600 ep_len=1000.0 TRAIN_METRICS epoch=96 ep_ret=-0.0690 ep_cost=16.9800 ep_len=1000.0 TRAIN_METRICS epoch=97 ep_ret=-0.0390 ep_cost=14.1800 ep_len=1000.0 TRAIN_METRICS epoch=98 ep_ret=0.0309 ep_cost=9.1600 ep_len=1000.0 TRAIN_METRICS epoch=99 ep_ret=0.0999 ep_cost=9.6600 ep_len=1000.0 Final evaluation: TEST_METRICS ep_ret=0.0999 ep_cost=9.6600 ep_len=1000.0 ### SafetyCarGoal1-v0 (scripts/car_goal.sh) Training metrics (last epochs): TRAIN_METRICS epoch=95 ep_ret=0.8159 ep_cost=9.0500 ep_len=1000.0 TRAIN_METRICS epoch=96 ep_ret=0.9796 ep_cost=10.3500 ep_len=1000.0 TRAIN_METRICS epoch=97 ep_ret=1.1371 ep_cost=12.1300 ep_len=1000.0
Tool Input
1{
2 "n": 2
3}[submit] Submitting result from test #2 as final. ## Seed 42 ### SafetyPointGoal1-v0 (scripts/point_goal.sh) Training metrics (last epochs): TRAIN_METRICS epoch=95 ep_ret=-0.0437 ep_cost=14.7600 ep_len=1000.0 TRAIN_METRICS epoch=96 ep_ret=-0.0690 ep_cost=16.9800 ep_len=1000.0 TRAIN_METRICS epoch=97 ep_ret=-0.0390 ep_cost=14.1800 ep_len=1000.0 TRAIN_METRICS epoch=98 ep_ret=0.0309 ep_cost=9.1600 ep_len=1000.0 TRAIN_METRICS epoch=99 ep_ret=0.0999 ep_cost=9.6600 ep_len=1000.0 Final evaluation: TEST_METRICS ep_ret=0.0999 ep_cost=9.6600 ep_len=1000.0 ### SafetyCarGoal1-v0 (scripts/car_goal.sh) Training metrics (last epochs): TRAIN_METRICS epoch=95 ep_ret=0.8159 ep_cost=9.0500 ep_len=1000.0 TRAIN_METRICS epoch=96 ep_ret=0.9796 ep_cost=10.3500 ep_len=1000.0 TRAIN_METRICS epoch=97 ep_ret=1.1371 ep_cost=12.1300 ep_len=1000.0 TRAIN_METRICS epoch=98 ep_ret=1.1037 ep_cost=10.9300 ep_len=1000.0 TRAIN_METRICS epoch=99 ep_ret=1.0575 ep_cost=11.6400 ep_len=1000.0 Final evaluation: TEST_METRICS