Skip to content

Commit 867929c

Browse files
authored
Fix: Auto-increment seed across batch_run iterations
When using batch_run() with a single seed value and multiple iterations, all iterations were using the same seed, producing identical results instead of independent replications. This defeats the purpose of running multiple iterations. This commit modifies _model_run_func to automatically increment the seed for each iteration (seed, seed+1, seed+2, ...) when a numeric seed is provided. This ensures: - Each iteration produces different random outcomes - Results remain reproducible (same base seed → same sequence) - Backward compatibility with seed arrays (no modification if seed is already an iterable passed via parameters) - Unchanged behavior when no seed is specified (each iteration gets random seed from OS) The fix only applies when: 1. A 'seed' parameter exists in kwargs 2. The seed value is not None 3. The iteration number is > 0 4. The seed is a single numeric value (int/float, not bool)
1 parent e1f9780 commit 867929c

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

mesa/batchrunner.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ def _model_run_func(
170170
Return model_data, agent_data from the reporters
171171
"""
172172
run_id, iteration, kwargs = run
173+
174+
# Handle seed uniqueness across iterations
175+
if 'seed' in kwargs and kwargs['seed'] is not None and iteration > 0:
176+
seed_value = kwargs['seed']
177+
if isinstance(seed_value, (int, float)) and not isinstance(seed_value, bool):
178+
kwargs = kwargs.copy()
179+
kwargs['seed'] = int(seed_value) + iteration
180+
173181
model = model_cls(**kwargs)
174182
while model.running and model.steps <= max_steps:
175183
model.step()

0 commit comments

Comments
 (0)