Organizations: ICN2 – Catalan Institute of Nanoscience and Nanotechnology, Barcelona, Spain · Artificial Intelligence Research Institute (IIIA-CSIC), Barcelona, Spain · Universitat Politècnica de Catalunya (UPC), Barcelona, Spain
Can an LLM learn how an optimizer behaves -- and use that knowledge to control it? We extend Code World Models (CWMs), LLM-synthesized Python programs that predict environment dynamics, from deterministic games to stochastic combinatorial optimization. Given suboptimal trajectories of (1+1)-RLSk, the LLM synthesizes a simulator of the optimizer's dynamics; greedy planning over this simulator then selects the mutation strength k at each step. On \lo{} and \onemax{}, CWM-greedy performs within 6% of the theoretically optimal policy -- without ever seeing optimal-policy trajectories. On \jump{k}, where a deceptive valley causes all adaptive baselines to fail (0% success rate), CWM-greedy achieves 100% success rate -- without any collection policy using oracle knowledge of the gap parameter. On the NK-Landscape, where no closed-form model exists, CWM-greedy outperforms all baselines across fifteen independently generated instances (36.94 vs.\ 36.32; p<0.001) when the prompt includes empirical transition statistics. The CWM also outperforms DQN in sample efficiency (200 offline trajectories vs.\ 500 online episodes), success rate (100% vs.\ 58%), and generalization (k=3: 78% vs.\ 0%). Robustness experiments confirm stable synthesis across 5 independent runs.
Figures & tables
Figure 1: Overview of the CWM approach. Offline : trajectory data and a problem description are assembled into a prompt; an LLM synthesizes a Python world model in a single API call ( ∼ € 0.04). Online : the greedy planner queries the CWM at each step to select k∗ . Bottom : key results across four benchmarks.
Aspect
Original CWM
This work
Environments
18 Gymnasium games
4 EA benchmarks
Dynamics
Deterministic
Stochastic combinatorial
LLM input
Env. docs + trajectories
Math / empirical stats + traj.
Synthesis
GIF-MCTS (iterative)
Single-shot + refinement
Planning
MCTS (5,000 rollouts)
Greedy 1-step lookahead
Baselines
Model-free RL
Adaptive rules & DQN
Table 1: Original CWM framework [ 3 ] vs. this work.
LeadingOnes
OneMax
Jump k
NK
Landscape
Unimodal
Unimodal
Deceptive valley
Rugged / epistatic
Optimal k∗
Known (closed)
Known (closed)
Unknown
Unknown
Key challenge
Smooth k ramp
Sharp policy cliff
Valley crossing
No math model
CWM input
Math description
Math description
Math + empirical table
Empirical table
Budget
0.8n2=2,000
5nlnn=978
10,000
10,000
What CWM learns
k↓ as i↑
Cliff at n/2
k↑ at edge
Transition lookup
Table 2: Benchmark progression: each problem adds a new challenge for CWM-based parameter control.
Figure 2: LeadingOnes : CWM score heatmap over (fitness i , parameter k ). Black stars ( ⋆ ) mark the optimal k∗(i)=⌊n/(i+1)⌋ , snapped to the nearest column when k∗ is not in the displayed set. Stars appear only at k values that are optimal for at least one fitness level; columns without stars (e.g. k=20,25,30,40 ) are never greedy-optimal for any i .
Figure 3: Jump k ( k=2 ): CWM score heatmap over (fitness, parameter k ). Black stars ( ⋆ ) mark the greedy-optimal k∗(i) , snapped to the nearest displayed column. At the valley edge (fitness =n ), the CWM correctly predicts that only k=2 leads to improvement—opposite to all adaptive baselines, which decrease k during stagnation.
Policy
Mean
SR%
Data
optimal
1,346
100%
—
CWM-greedy
1,342
100%
200 offline traj.
stagnation
1,506
99%
— (heuristic)
DQN (500 ep.)
5,210
58%
500 online ep.
DQN (200 ep.)
10,000
0%
200 online ep.
Table 5: CWM vs. DQN on Jump k ( n=50 , k=2 , 100 eval. runs).
Figure 4: Left: CWM vs. DQN on Jump k : 100% success rate vs. 58%. Right: DQN learning curves ( k=2 ). Training beyond 500 episodes degrades performance—success rate plateaus at ∼50% while steps decrease only marginally, revealing overfitting to the ϵ -greedy exploration policy.
Policy
Mean best
Std
Max
Mean rank
CWM-greedy
36.94
1.03
39.12
1.0
static_5
36.32
1.04
38.96
2.5
random_k
36.31
1.00
39.06
2.5
fifth_rule
35.36
0.99
38.91
4.5
stagnation
35.32
0.93
38.91
5.1
self_adjusting
35.22
0.95
39.06
6.1
Table 6: NK-Landscape results ( n=50 , K=2 , budget =10,000 , 100 runs × 15 instances). Std: across per-instance means; Max: best single run. CWM-greedy ranks first on every instance (paired t -test p<0.001 ).
Figure 6: NK CWM score heatmap ( K=2 ). The scores are instance-independent (determined by the CWM alone); the model is evaluated on fifteen landscape instances in Table 6 . No optimal overlay exists—the optimal policy is unknown. The CWM prefers small k at high fitness.
Problem
n
Optimal
CWM
RLS 1
CWM/Opt
LeadingOnes
50
983
1,052
1,220
1.07 ×
100
3,705
4,167
4,923
1.12 ×
200
15,761
16,156
20,210
1.02 ×
OneMax
50
185
188
192
1.02 ×
100
441
452
431
1.02 ×
200
1,031
1,020
1,037
0.99 ×
Table 7: Generalization across problem sizes. CWM trained on n=50 , 50 eval. runs.
CWM
DQN
EAα
k
Mean
SR
Mean
SR
Mean
SR
2
1,259
100%
5,023
58%
10,000
0%
3
21,377
78%
50,000
0%
50,000
0%
4
50,000
0%
50,000
0%
50,000
0%
Table 8: Generalization across Jump k values. CWM and DQN (best: 500 training episodes) trained on k=2 , 50 eval. runs.
Figure 7: Jump k generalization: CWM 78% on k=3 vs. DQN 0%.
Benchmark
τ
ρ
Exact
Note
LeadingOnes
0.784
0.916
60%
—
OneMax
0.614
0.939
82%
—
Jump k
0.898
0.948
—
valley-edge τ=1.0
NK
0.553
—
—
with empirical table
Table 9: CWM quality metrics. τ : Kendall’s rank correlation between CWM-predicted and true action rankings. ρ : Spearman correlation. Exact: fraction of states where CWM selects the same k∗ as the oracle.