Auto-Formulating Dynamic Programming Problems with Large Language Models

Chenyu Zhou, Jingyuan Yang, Linwei Xin, Yitian Chen, Ziyan He, Dongdong GearXiv 2025

TL;DR

While Large Language Models (LLMs) have gotten remarkably good at formulating linear programming problems, they still stumble on Dynamic Programming (DP) due to its complex sequential reasoning and stochastic nature. This paper introduces DP-Bench, a new textbook-level benchmark, and DualReflect, a novel data generation pipeline that combines forward and backward problem generation. Using this pipeline, the authors trained DPLM, a specialized 7B-parameter model that outperforms models 100x its size—including DeepSeek-R1 and OpenAI's o1—on hard DP formulation tasks.

What Problem Does This Paper Address?

Automating mathematical problem formulation is a holy grail for creating fully autonomous decision-support systems. While recent frameworks successfully use LLMs to translate natural language descriptions into Linear Programming (LP) or Mixed-Integer Programming (MIP) models, Dynamic Programming (DP) has remained a stubborn bottleneck.

DP problems are uniquely challenging for LLMs for two main reasons:

  1. Mathematical Complexity: DP requires multi-period decision-making with sequential, often stochastic, transitions. Models must carefully reason about the chronological order of events, immediate costs, and future system states.
  2. Linguistic Nuance: Operations research problems are typically embedded in text-rich, business-oriented scenarios. A phrase like "end-of-period capacity of 3 units" implies a completely different state constraint than simply having a "capacity of 3 units" when demand is random. General LLMs consistently miss these subtle cues.

Furthermore, there is a severe scarcity of high-quality training data. Unlike computer science DP problems (which are structured like LeetCode puzzles), operations research DP problems lack standardized datasets and benchmarks.

Methods

The authors developed a complete training and data synthesis pipeline to build the Dynamic Programming Language Model (DPLM), using Qwen-2.5-7B-Instruct as their base model.

DualReflect Pipeline:
[Forward Generation]  --> Problem First --> RAG Solution Generator --> Filter/Vote
[Backward Generation] --> Code First    --> Perturb Data           --> Reflected CoT

1. DualReflect Data Generation

To scale up a tiny seed set of 91 textbook problems into a massive training set, the authors designed DualReflect, a bidirectional synthetic data generation pipeline:

  • Forward Generation (Diversity): The LLM generates a new problem description based on a real-world scenario template first, and then attempts to solve it using a RAG-based solution generator. This introduces highly diverse problem formulations but lacks strict correctness guarantees.
  • Backward Generation (Reliability): The pipeline starts with a known, executable code solution from the seed data, perturbs its parameters (e.g., transition probabilities, cost coefficients), ensures it runs without errors, and then prompts the LLM to write a natural language problem matching that exact code.

2. Reflected Chain-of-Thought (CoT)

During backward generation, if the LLM's generated problem description fails to yield the correct answer when solved independently, the system triggers a Reflected CoT mechanism. The model is shown its failed code alongside the correct reference code and is prompted to identify its logical discrepancies, iteratively refining its reasoning (up to 6 attempts). This recovered roughly 20.8% of complex problems that would otherwise be discarded.

3. Two-Stage Training Pipeline

  • Stage 1: Supervised Fine-Tuning (SFT): The base model is "cold-started" on 113K synthetic trajectories generated by DualReflect.
  • Stage 2: Reinforcement Learning (RL): The model is aligned using Group Relative Policy Optimization (GRPO). The reward function gives partial credit (0.2) if the generated code is executable and well-typed, and full credit (0.8) only if it returns the exact correct numerical answer.

Datasets

  • DP-Bench (New): The first standardized benchmark for OR-style DP problems. It contains 132 textbook-level problems divided into 90 Easy instances (adapted from Winston, 2004) and 42 Hard instances (adapted from Puterman, 2005). Every problem features a closed-ended, specific numerical answer used to verify formulation accuracy.
  • DualReflect Synthetic Dataset: A dataset of 113K training samples (70K forward-generated, 34K backward-generated without reflection, and 8K hard-recovered via Reflected CoT).
  • Healthcare-OOD1 & OOD2 (New): Two out-of-domain evaluation datasets based on clinical decision-making to test how well the model generalizes beyond standard textbook contexts.

Key Insights and Ideas

  • The Forward-Backward Trade-Off: Backward generation is superior in low-data regimes because starting from code guarantees mathematical correctness. However, it suffers from diminishing returns at scale because it is constrained by the structure of the original seed data. Forward generation, despite being error-prone, becomes vital at scale because it injects structural diversity. A 50/50 hybrid mix hits the empirical "sweet spot."
  • Structural RAG Over Semantic RAG: When retrieving few-shot examples to guide the LLM, matching surface-level semantics (e.g., pairing an inventory problem with another inventory problem) can fail catastrophically if one is finite-horizon and the other is infinite-horizon. Filtering candidates by structural labels (e.g., action-dependent transitions, optimal stopping) yields far better generation accuracy.
  • SFT is the Foundation for Small Models: For a 7B model, RL-only training fails because the model lacks the capacity to discover high-quality DP reasoning paths from scratch. SFT provides a necessary structured lesson plan, while RL acts as a personalized coach to refine those skills.

Key Results

  • Beating the Giants on Hard Problems: The final model, DPLM-7B-SFT-GRPO, achieved 40.5% accuracy on the hard subset of DP-Bench, outperforming DeepSeek-R1 (28.6%), OpenAI's o1 (31.0%), and its own teacher model, GPT-4o (19.0%).
  • Macro-Average Superiority: On the overall macro-average score, DPLM scored 53.6%, surpassing DeepSeek-R1's 51.0%.
Model Parameters Easy (%) Hard (%) Macro (%)
DeepSeek-R1 671B 73.3 28.6 51.0
OpenAI o1 ~300B 48.9 31.0 39.9
GPT-4o ~200B 47.8 19.0 33.4
DPLM-7B-SFT-GRPO (Ours) 7B 66.7 40.5 53.6
  • Scaling with Stronger Teachers: When the authors swapped the data-generation teacher to DeepSeek-V3.2 and applied inference-time majority voting ($k=5$), DPLM's performance surged to 78.9% on easy and 54.8% on hard problems, achieving an overall micro-accuracy of 71.2%.

Limitations and Future Work

  • The Curse of Dimensionality: As problems become more realistic and structurally complex, the state space explodes. Even when DPLM correctly formulates the model and selects the right algorithm (like value iteration), the generated Python code can struggle to converge or hit computational limits.
  • Easy Problem Shortfall: While DPLM dominates on hard problems, it still slightly lags behind DeepSeek-R1 on easy problems due to the limited coverage of specialized domain knowledge in its initial seed dataset.
  • Future Direction: The authors suggest that instead of using the LLM to both formulate and execute pure Python loops, a more practical path is to use DPLM strictly as an auto-formulation tool and connect its mathematical output directly to specialized Approximate Dynamic Programming (ADP) or commercial RL solvers.

Summary updated

BibTeX
@misc{zhou2025auto,
  author = {Chenyu Zhou and Jingyuan Yang and Linwei Xin and Yitian Chen and Ziyan He and Dongdong Ge},
  title = {{Auto-Formulating Dynamic Programming Problems with Large Language Models}},
  year = {2025},
  eprint = {2507.11737},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2507.11737},
}
Figure