E2CFD: Towards Effective and Efficient Cost Function Design for Safe Reinforcement Learning via Large Language Model

Zepeng Wang, Chao Ma, Linjiang Zhou, Libing Wu, Lei Yang, Xiaochuan Shi, Guojun PengarXiv 2024 · DOI

Introduction

Safe Reinforcement Learning (Safe RL) is a critical subfield of artificial intelligence that focuses on training agents to maximize cumulative rewards while strictly adhering to safety constraints. This is particularly vital in real-world applications such as autonomous driving, industrial robotics, and healthcare, where a single violation can have catastrophic consequences. However, defining "safety" in a way that an RL agent can understand is notoriously difficult.

Traditionally, safety is encoded via cost functions that penalize undesirable behaviors. Designing these functions is a tedious, expert-heavy process prone to "reward hacking," where an agent finds a mathematical loophole to satisfy the function while still performing dangerously. Moreover, different tasks require different safety thresholds—some may tolerate occasional violations, while others demand a "zero-violation" policy. There is a clear need for a more automated, robust, and generalizable approach to cost function design.

The E2CFD Framework

We present the Effective and Efficient Cost Function Design ($E^2$CFD) framework, which leverages the semantic reasoning and code-generation capabilities of Large Language Models (LLMs) to automate the design of cost functions. Unlike previous methods that require deep access to environment internals, $E^2$CFD operates in a "gray-box" scenario, requiring only high-level natural language descriptions and basic code snippets.

The E2CFD framework overview, illustrating the transition from natural language task descriptions to an optimized cost function through initialization and evolutionary phases.

The framework operates in two distinct phases:

  1. Initialization Phase: The LLM acts as a "cost function architect." It receives a prompt containing the task description, safety requirements, and existing environment code. From this, it generates a set of candidate Base Functions. To ensure these functions are usable, the Error Code Filtering (ECF) module performs automated syntax tests and a quick manual review to filter out non-compliant or broken code.
  2. Evolutionary Phase: Since the first generated functions are rarely optimal, $E^2$CFD employs an iterative refinement process. The Fast Performance Evaluation (FPE) module runs short, early-stage training bursts to "score" the candidate functions. This avoids the computational burden of full training cycles while providing enough signal to create a weighted Output Function that best aligns with the task's ultimate safety and performance goals.

Experimental Evaluation

The framework was rigorously tested using the StaticPointGoal task from the Safety Gym benchmark. In this environment, a "Point" robot must navigate a 2D space to reach a goal while avoiding fixed hazardous areas. This benchmark is a standard proving ground for testing how well RL agents handle safety constraints in the presence of obstacles.

Results and Discussion

Our results demonstrate that $E^2$CFD significantly outperforms traditional Safe RL baselines and human-designed cost functions.

Training performance comparison on the StaticPointGoal task. (a) Average episode return and (b) average episode cost over total environment interactions, comparing E2CFD against standard Safe RL baselines.

As shown in the training curves above, $E^2$CFD (magenta line) achieves a high average return comparable to standard PPO while rapidly driving the average episode cost down to near-zero levels. In "zero-violation" scenarios, $E^2$CFD was the closest to achieving a true zero-cost policy compared to established algorithms like PPO-Lag and CPO.

Furthermore, the framework addresses the "expert bottleneck." Human-designed cost functions often exhibit high variance in performance, as experts may have different interpretations of safety or struggle to tune weights correctly. $E^2$CFD provides a consistent, high-performing alternative that matches or exceeds the performance of carefully hand-tuned functions. The FPE module also ensures high efficiency, finding optimal cost functions with a significant reduction in total training time compared to standard evolutionary approaches.

Conclusion and Future Work

$E^2$CFD represents a significant step toward making Safe RL more accessible and robust. By leveraging the semantic reasoning capabilities of LLMs and an efficient evolutionary feedback loop, the framework automates a previously tedious manual process.

Future research will focus on developing more sophisticated and automated prompting strategies to further reduce the need for human-in-the-loop reviews and scaling the framework to handle complex "multi-constraint" scenarios, where agents must balance multiple, sometimes competing, safety requirements simultaneously.

Summary updated

BibTeX
@misc{wang2024e,
  author = {Zepeng Wang and Chao Ma and Linjiang Zhou and Libing Wu and Lei Yang and Xiaochuan Shi and Guojun Peng},
  title = {{E2CFD: Towards Effective and Efficient Cost Function Design for Safe Reinforcement Learning via Large Language Model}},
  year = {2024},
  eprint = {2407.05580},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.05580},
}
Figure