From Text to Trajectory: Exploring Complex Constraint Representation and Decomposition in Safe Reinforcement Learning

Pusen Dong, Tianchen Zhu, Yue Qiu, Haoyi Zhou, Jianxin Li

TL;DR

Safe reinforcement learning (RL) typically relies on hand-crafted mathematical cost functions to enforce safety constraints. This paper introduces the Trajectory-level Textual Constraints Translator (TTCT), a framework that allows agents to learn directly from complex natural language constraints across entire trajectories. By leveraging multimodal contrastive learning and temporal credit assignment, TTCT translates textual rules into dense cost signals, achieving up to a 4x reduction in safety violations compared to traditional ground-truth cost functions.

What Problem Does This Paper Address?

Standard safe RL methods suffer from three major limitations: they require significant domain expertise to manually design mathematical cost functions, these functions lack the flexibility to generalize to new tasks, and they typically only handle simple, single-state constraints (e.g., "don't touch lava").

Real-world safety rules are rarely that simple; they are often temporal, conditional, and complex (e.g., "don't stay in the gas area for more than 5 minutes, or your gas mask will fail"). While using natural language solves the flexibility problem, it introduces two major challenges:

  1. Semantic Comprehension (Challenge 1): Determining if a full trajectory of historical states and actions violates an abstract textual rule.
  2. Cost Sparsity (Challenge 2): Dealing with sparse feedback, as text-based safety violations are usually only triggered at the very end of an episode, making it hard for the agent to deduce which early action caused the eventual failure.

Methods

The authors propose TTCT, an end-to-end framework consisting of two primary components:

  1. Text-Trajectory Alignment Component: This component maps trajectories and textual constraints into a shared embedding space. A Causal Transformer processes the sequence of state-action pairs to capture temporal dependencies, while a pre-trained language model (BERT) encodes the textual constraint. The system is optimized using a Multimodal Contrastive (MC) loss (similar to CLIP) alongside a novel Within-Trajectory (WT) loss, which maximizes embedding similarity at the exact step a violation occurs while minimizing it for preceding safe steps.
  2. Cost Assignment Component: To solve the sparse cost problem, TTCT introduces a temporal credit assignment mechanism. It uses an attention layer where the text embedding acts as a query and the historical trajectory states act as keys. This decomposes the single episodic violation cost into dense, step-by-step cost signals, effectively telling the agent exactly when and how much an action escalated its safety risk.
Figure 1: TTCT overview. TTCT consists of two training components: (1) the text-trajectory alignment component connects trajectory to text with multimodal architecture, and (2) the cost assignment component assigns a cost value to each state-action based on its impact on satisfying the constraint. When training RL policy, the text-trajectory alignment component is used to predict whether a trajectory violates a given constraint and the cost assignment component is used to assign non-violation cost.

Datasets

The framework was evaluated across two main simulation benchmarks and one transfer environment:

  • Hazard-World-Grid: A 2D grid world where the agent must collect reward objects while navigating hazards (lava, water, grass) based on textual rules.
  • SafetyGoal: A 3D robot navigation environment built within Safety-Gymnasium.
  • LavaWall: A modified grid environment used exclusively to test zero-shot transfer capabilities.

The authors generated a dataset of over 200 trajectory-level textual constraints categorized into four distinct difficulty levels: Quantitative (e.g., hit limits), Sequential (ordered actions), Relational (spatial distance), and Mathematical (numerical reasoning/HP tracking).

Figure 2: (a) One layout in Hazard-World-Grid [14], where orange tiles are lava, blue tiles are water and green tiles are grass. Agents need to collect reward objects in the grid while avoiding violating our designed textual constraint for the entire episode. (b) Robot navigation task SafetyGoal that is built in Safety-Gymnasium [37], where there are multiple types of objects in the environment. Agents need to reach the goal while avoiding violating our designed textual constraint for the entire episode. (c) LavaWall [40], a task has the same goal but different hazard objects compared to Hazard-World-Grid.

Key Insights and Ideas

  • The Dual Role of Text: Instead of treating text merely as a passive rulebook, TTCT uses it as a unified supervisory signal that actively shapes the trajectory representation during training.
  • Dense Signals from Sparse Rules: By using attention to map text constraints back to individual timesteps, the model successfully deduces when an agent started behaving dangerously, converting a binary end-of-episode failure into a smooth gradient of risk.
  • Zero-Shot Generalization: Because the alignment relies on semantic meaning rather than hardcoded environment features, the learned constraint translator can immediately adapt to environment shifts (like new hazard layouts) without any fine-tuning.

Key Results

  • Massive Violation Reduction: Policies trained using TTCT's predicted costs achieved up to 4.0x lower violation rates than those trained with standard, hand-designed ground-truth cost functions.
  • Superior Pareto Frontier: TTCT consistently pushed the Pareto frontier closer to the origin, demonstrating a much better trade-off between maximizing rewards and minimizing safety costs across multiple RL algorithms (PPO-Lagrangian, CPPO-PID, FOCOPS).
  • Robust Zero-Shot Transfer: When the TTCT model trained on Hazard-World-Grid was directly applied to the unseen LavaWall environment, the agent maintained a low violation rate comparable to an agent explicitly trained on that environment.

Limitations and Future Work

Despite its strong performance, the authors note a few limitations:

  • Not Absolute Zero: The safety violation rate, while significantly reduced, is still not absolute zero, which remains a hurdle for safety-critical real-world deployment.
  • Contextual Bottleneck: As trajectory lengths grow very large (beyond 200-300 steps), the model's performance (AUC) slightly degrades. This decline happens because the trajectory encoder struggles to capture global information over long horizons—a known limitation of standard transformer encoding capabilities.

Future work will focus on scaling TTCT to more complex environments and exploring meta-learning techniques to further improve the framework's generalization capabilities.

Summary updated

BibTeX
@inproceedings{dong2024from,
  author = {Pusen Dong and Tianchen Zhu and Yue Qiu and Haoyi Zhou and Jianxin Li},
  title = {{From Text to Trajectory: Exploring Complex Constraint Representation and Decomposition in Safe Reinforcement Learning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=MDpIQ9hQ7H},
}
Figure