FactorSim: Generative Simulation via Factorized Representation

Fan-Yun Sun, Harini S I, Angela Yi, Yihan Zhou, Alex Zook, Jonathan Tremblay, Logan Cross, Jiajun Wu, Nick Haber

Overview

FactorSim is a novel framework that leverages Large Language Models (LLMs) to generate complete, runnable simulation environments directly from natural language descriptions. By structuring the code generation process around a factored Partially Observable Markov Decision Process (POMDP) and the Model-View-Controller (MVC) software pattern, FactorSim drastically reduces the context LLMs need to process at each step. This approach results in highly accurate simulations that can successfully train Reinforcement Learning (RL) agents for zero-shot transfer to ground-truth environments.

The Challenge of Generative Simulation

Training intelligent agents for robotics or complex games requires diverse, high-quality simulations. However, manually designing and coding these environments is expensive, rigid, and time-consuming.

While LLMs excel at writing isolated snippets of code, asking them to generate a full simulation from a single text prompt usually ends in failure. Simulations are complex systems with deeply intertwined logic, including physics, rendering, user input, and reward functions. When faced with this massive context, LLMs tend to get "lost in the middle"—they hallucinate non-existent functions, ignore specific instructions, or break existing code when trying to add new features. Previous research has successfully used LLMs to tweak parameters or write isolated reward functions, but generating the entire simulation dynamics and logic from scratch has remained an open challenge.

The FactorSim Methodology

To solve the context-overload problem, the authors introduce FactorSim, a framework that generates simulation code progressively by exploiting the inherent modularity of games and physics engines.

FactorSim Framework Overview

Instead of feeding the entire expanding codebase to the LLM for every update, FactorSim uses a Factored POMDP representation to isolate context. The process works as follows:

  1. Chain-of-Thought Decomposition: FactorSim breaks the initial natural language prompt into a step-by-step implementation plan.
  2. Context Selection via Factors: For each step, the system identifies only the relevant "factors" (specific state variables, like player position, and their associated functions). The LLM is only shown this highly restricted context, preventing it from interfering with unrelated code.
  3. Model-View-Controller (MVC) Pattern: To further simplify the LLM's job, FactorSim forces the model to write code in three distinct phases for each step:
  4. Controller: Code that handles user/agent input (e.g., key presses).
  5. Model: Code that updates the state transitions and reward logic (e.g., collision detection).
  6. View: Code that updates the rendering/observation function.

Datasets and Evaluation

The authors evaluated FactorSim on two distinct sets of tasks:

  • New Generative Simulation Benchmark: Adapted from the PyGame Learning Environment, this benchmark consists of 8 classic 2D RL games (Flappy Bird, Catcher, Snake, Pixelcopter, Pong, Puckworld, Waterworld, Monster Kong). The authors provided detailed text prompts for each and built automated system tests to verify the generated code.
  • GenSim Benchmark: An existing dataset of 50 robotic manipulation tasks in a physics engine (using the CLIPort framework) to test the generation of 3D spatial tasks.

Key Findings and Results

Context reduction proved to be the secret to complex code generation. Structurally isolating the context using POMDP factors prevents the LLM from hallucinating or breaking unrelated code, proving that applying classic software engineering principles to LLM prompts aligns perfectly with how simulations actually work.

Performance vs. Token Usage
  • 2D Game Generation & Token Efficiency: FactorSim (using GPT-4) outperformed all baselines (including AgentCoder and standard CoT with self-debugging) in 7 out of 8 games on automated system tests. For example, it scored a 78% pass rate on Flappy Bird, compared to just 33% for GPT-4 with self-debugging. Crucially, FactorSim achieved this high accuracy while using significantly fewer tokens than heavy iterative self-debugging methods, proving that smart decomposition is better than brute-force retries.
  • Zero-Shot Transfer: The accuracy of the generated simulation directly impacts the ability of an RL agent to generalize. PPO (Proximal Policy Optimization) agents trained on FactorSim-generated environments achieved much higher rewards when tested zero-shot on the ground-truth PyGame environments compared to agents trained on baseline-generated environments.
  • Robotics (GenSim): On complex robotic assembly tasks, FactorSim achieved a 92% syntax pass rate and a 33% task completion rate, vastly outperforming the best GenSim baseline (which only achieved an 8% task completion rate).
  • Human Evaluation: In a blind study of 320 evaluations, human players rated FactorSim games as significantly more playable and functional than those generated by the strongest baseline.

Limitations and Future Directions

The current framework is limited to single-agent environments, with multi-agent settings planned for future work. Additionally, while FactorSim handles 2D games and specific robotic tasks well, scaling to massive, complex 3D simulations might eventually exceed current context window limits, even with factorization.

Future directions include generating GPU-accelerated simulation code to speed up RL training and creating a closed-loop system where feedback from the RL agent's training process is used to automatically modify and refine the generated simulation for even better generalization.

Summary updated

BibTeX
@inproceedings{sun2024factorsim,
  author = {Fan-Yun Sun and Harini S I and Angela Yi and Yihan Zhou and Alex Zook and Jonathan Tremblay and Logan Cross and Jiajun Wu and Nick Haber},
  title = {{FactorSim: Generative Simulation via Factorized Representation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=wBzvYh3PRA},
}
Figure