Abstract
Model-based Reinforcement Learning (RL) typically relies on neural network world models that are often computationally expensive and lack interpretability. This paper introduces Code World Models (CWMs), which leverage Large Language Models (LLMs) to generate executable Python-based environment simulators. To overcome the challenges of generating complex, bug-free code, the authors propose GIF-MCTS (Generate, Improve, and Fix with Monte Carlo Tree Search). This method enables iterative refinement of code through specialized LLM actions guided by tree search. CWMs offer significant speedups in planning—up to seven orders of magnitude faster than direct LLM querying—while maintaining high accuracy across diverse RL benchmarks.
Introduction
In model-based RL, an agent uses a world model to simulate the outcomes of potential actions. While neural world models are standard, they often require massive amounts of data and function as "black boxes." LLMs provide a promising alternative due to their extensive world knowledge, but using them directly as simulators is slow and prone to hallucinations.
The proposed solution is to use LLMs to write Python code that serves as the world model. Code is precise, interpretable, and executes nearly instantaneously. However, writing a complete, bug-free simulator for complex physics environments in a single pass is beyond the capabilities of current LLMs.
Methods: GIF-MCTS
The core of this approach is GIF-MCTS, a search strategy that treats code generation as a tree search problem. Each node in the search tree represents a version of the Python program. The system utilizes the Upper Confidence Bound for Trees (UCT) formula to navigate the search space, prioritizing code versions that show promise in predicting ground-truth trajectories.
The LLM can transition between nodes using three specialized actions:
- Generate: Completes the program by adding new blocks of code.
- Improve: Refines the logic of a running program that makes incorrect predictions, using specific failure cases as feedback.
- Fix: Targets syntax or runtime errors by providing the LLM with traceback information for debugging.
Evaluation and Results
The authors evaluated GIF-MCTS on three benchmarks: the new Code World Models Benchmark (CWMB) featuring 18 RL environments, the APPS coding benchmark, and the RTFM grid-world.
Key Performance Metrics
- Coding Proficiency: GIF-MCTS achieved 28.3% strict accuracy on the APPS Competition split, outperforming existing methods like WorldCoder and LATS.
- Efficiency: Planning with generated CWMs is 4 to 7 orders of magnitude faster than querying an LLM for next-state predictions.
- Sample Efficiency: By leveraging natural language descriptions, the system can build accurate models from just a few example trajectories, a feat difficult for traditional RL agents.
Discussion and Insights
A critical finding was the importance of the Fix action; debugging based on error messages proved significantly more efficient than repeated zero-shot generation attempts. Furthermore, shifting from "predicting pixels" to "generating logic" results in models that are human-readable and mathematically exact.
Limitations
Despite its success, the framework currently assumes deterministic and fully observable environments. It also relies heavily on high-quality natural language descriptions and can be "brittle" if environment dynamics change, as the code would need to be regenerated. High-dimensional continuous control tasks, such as "Humanoid" physics, remain challenging due to their mathematical complexity.
Conclusion
Generating Code World Models via GIF-MCTS represents a significant step toward interpretable and efficient model-based RL. By combining the reasoning capabilities of LLMs with the precision of executable code and the systematic exploration of MCTS, this approach creates robust simulators that empower faster and more reliable agent planning.
Summary updated