-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiments.py
More file actions
1253 lines (1052 loc) · 40.9 KB
/
experiments.py
File metadata and controls
1253 lines (1052 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Predator-Prey Hydra Effect Experiments
======================================
HPC-ready experiment runner for investigating the Hydra effect in
predator-prey cellular automata.
Experimental Phases
-------------------
- **Phase 1**: Parameter sweep to find critical point (bifurcation + cluster analysis)
- **Phase 2**: Self-organization analysis (evolution toward criticality)
- **Phase 3**: Finite-size scaling at critical point
- **Phase 4**: Sensitivity analysis across parameter regimes
- **Phase 5**: Model extensions (directed hunting comparison)
Functions
---------
```python
run_single_simulation # Execute one simulation run and collect metrics.
run_phase1, run_phase2, run_phase3, run_phase4, run_phase5 # Phase-specific experiment runners.
```
Utilities
---------
```python
generate_unique_seed # Deterministic seed generation from parameters.
count_populations # Count species populations on grid.
get_evolved_stats # Statistics for evolved parameters.
average_pcfs # Average multiple PCF measurements.
save_results_jsonl, load_results_jsonl, save_results_npz # I/O functions for experiment results.
```
Command Line Usage
------------------
```bash
python experiments.py --phase 1 # Run phase 1
python experiments.py --phase 1 --dry-run # Estimate runtime
python experiments.py --phase all # Run all phases
python experiments.py --phase 1 --output results/ # Custom output
```
Programmatic Usage
------------------
```python
from experiments import run_single_simulation, run_phase1
from models.config import PHASE1_CONFIG
# Single simulation
result = run_single_simulation(
prey_birth=0.2,
prey_death=0.05,
predator_birth=0.8,
predator_death=0.1,
grid_size=100,
seed=42,
cfg=PHASE1_CONFIG,
)
# Full phase (writes to output directory)
import logging
results = run_phase1(PHASE1_CONFIG, Path("results/"), logging.getLogger())
```
"""
import argparse
import hashlib
import json
import logging
import os
import sys
import time
from dataclasses import asdict
from pathlib import Path
from typing import Dict, List, Tuple, Optional
import warnings
import numpy as np
from tqdm import tqdm
warnings.filterwarnings("ignore")
# Project imports
project_root = str(Path(__file__).parent.parent)
if project_root not in sys.path:
sys.path.insert(0, project_root)
from models.config import Config, get_phase_config, PHASE_CONFIGS
# Numba imports
try:
from models.numba_optimized import (
compute_all_pcfs_fast,
get_cluster_stats_fast,
warmup_numba_kernels,
set_numba_seed,
NUMBA_AVAILABLE,
)
USE_NUMBA = NUMBA_AVAILABLE
except ImportError:
USE_NUMBA = False
def warmup_numba_kernels(size, **kwargs):
pass
def set_numba_seed(seed):
pass
# =============================================================================
# Utility Functions
# =============================================================================
def generate_unique_seed(params: dict, rep: int) -> int:
"""
Create a deterministic seed from a dictionary of parameters and a repetition index.
This function serializes the input dictionary into a sorted JSON string,
appends the repetition count, and hashes the resulting string using SHA-256.
The first 8 characters of the hex digest are then converted to an integer
to provide a stable, unique seed for random number generators.
Parameters
----------
params : dict
A dictionary of configuration parameters. Keys are sorted to ensure
determinism regardless of insertion order.
rep : int
The repetition or iteration index, used to ensure different seeds
are generated for the same parameter set across multiple runs.
Returns
-------
int
A unique integer seed derived from the input parameters.
Examples
--------
>>> params = {'learning_rate': 0.01, 'batch_size': 32}
>>> generate_unique_seed(params, 1)
3432571217
>>> generate_unique_seed(params, 2)
3960013583
"""
identifier = json.dumps(params, sort_keys=True) + f"_{rep}"
return int(hashlib.sha256(identifier.encode()).hexdigest()[:8], 16)
def count_populations(grid: np.ndarray) -> Tuple[int, int, int]:
"""
Count the number of empty, prey, and predator cells in the simulation grid.
Parameters
----------
grid : np.ndarray
A 2D NumPy array representing the simulation environment, where:
- 0: Empty cell
- 1: Prey
- 2: Predator
Returns
-------
empty_count : int
Total number of cells with a value of 0.
prey_count : int
Total number of cells with a value of 1.
predator_count : int
Total number of cells with a value of 2.
Examples
--------
>>> grid = np.array([[0, 1], [2, 1]])
>>> count_populations(grid)
(1, 2, 1)
"""
return int(np.sum(grid == 0)), int(np.sum(grid == 1)), int(np.sum(grid == 2))
def get_evolved_stats(model, param: str) -> Dict:
"""
Get statistics of an evolved parameter from the model.
This function retrieves parameter values from the model's internal storage,
filters out NaN values, and calculates basic descriptive statistics.
Parameters
----------
model : object
The simulation model instance containing a `cell_params` attribute
with a `.get()` method.
param : str
The name of the parameter to calculate statistics for.
Returns
-------
stats : dict
A dictionary containing the following keys:
- 'mean': Arithmetic mean of valid values.
- 'std': Standard deviation of valid values.
- 'min': Minimum valid value.
- 'max': Maximum valid value.
- 'n': Count of non-NaN values.
If no valid data is found, all stats return NaN and n returns 0.
Examples
--------
>>> stats = get_evolved_stats(my_model, "speed")
>>> print(stats['mean'])
1.25
"""
arr = model.cell_params.get(param)
if arr is None:
return {"mean": np.nan, "std": np.nan, "min": np.nan, "max": np.nan, "n": 0}
valid = arr[~np.isnan(arr)]
if len(valid) == 0:
return {"mean": np.nan, "std": np.nan, "min": np.nan, "max": np.nan, "n": 0}
return {
"mean": float(np.mean(valid)),
"std": float(np.std(valid)),
"min": float(np.min(valid)),
"max": float(np.max(valid)),
"n": len(valid),
}
def average_pcfs(
pcf_list: List[Tuple[np.ndarray, np.ndarray, int]],
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
"""
Average multiple Pair Correlation Function (PCF) measurements and calculate standard error.
Parameters
----------
pcf_list : list of tuple
A list where each element is a tuple containing:
- distances (np.ndarray): The radial distances (r).
- pcf_values (np.ndarray): The correlation values g(r).
- count (int): Metadata or weight (not used in current calculation).
Returns
-------
distances : np.ndarray
The radial distances from the first entry in the list.
pcf_mean : np.ndarray
The element-wise mean of the PCF values across all measurements.
pcf_se : np.ndarray
The standard error of the mean for the PCF values.
Examples
--------
>>> data = [(np.array([0, 1]), np.array([1.0, 2.0]), 10),
... (np.array([0, 1]), np.array([1.2, 1.8]), 12)]
>>> dist, mean, se = average_pcfs(data)
>>> mean
array([1.1, 1.9])
"""
if len(pcf_list) == 0:
return np.array([]), np.array([]), np.array([])
distances = pcf_list[0][0]
pcfs = np.array([p[1] for p in pcf_list])
pcf_mean = np.mean(pcfs, axis=0)
pcf_se = np.std(pcfs, axis=0) / np.sqrt(len(pcfs))
return distances, pcf_mean, pcf_se
def save_results_jsonl(results: List[Dict], output_path: Path):
"""
Save a list of dictionaries to a file in JSON Lines (JSONL) format.
Each dictionary in the list is serialized into a single JSON string and
written as a new line. Non-serializable objects are converted to strings
using the default string representation.
Parameters
----------
results : list of dict
The collection of result dictionaries to be saved.
output_path : Path
The file system path (pathlib.Path) where the JSONL file will be created.
Returns
-------
None
Notes
-----
The file is opened in 'w' (write) mode, which will overwrite any existing
content at the specified path.
Examples
--------
>>> data = [{"id": 1, "score": 0.95}, {"id": 2, "score": 0.88}]
>>> save_results_jsonl(data, Path("results.jsonl"))
"""
with open(output_path, "w", encoding="utf-8") as f:
for result in results:
f.write(json.dumps(result, default=str) + "\n")
def save_results_npz(results: List[Dict], output_path: Path):
"""
Save simulation results to a compressed NumPy (.npz) binary file.
This function flattens a list of result dictionaries into a single
dictionary of NumPy arrays, prefixing keys with the run index to
maintain data separation. The resulting file is compressed to
reduce storage space.
Parameters
----------
results : list of dict
A list where each dictionary contains key-value pairs of
simulation data (e.g., arrays, lists, or scalars).
output_path : Path
The file system path (pathlib.Path) where the compressed
NPZ file will be saved.
Returns
-------
None
Notes
-----
The keys in the saved file follow the format 'run_{index}_{original_key}'.
Values are automatically converted to NumPy arrays if they are not
already.
Examples
--------
>>> results = [{"energy": [1, 2]}, {"energy": [3, 4]}]
>>> save_results_npz(results, Path("output.npz"))
"""
data = {}
for i, res in enumerate(results):
for key, val in res.items():
data[f"run_{i}_{key}"] = np.array(val)
np.savez_compressed(output_path, **data)
def load_results_jsonl(input_path: Path) -> List[Dict]:
"""
Load simulation results from a JSON Lines (JSONL) formatted file.
This function reads a file line-by-line, parsing each line as an
independent JSON object and aggregating them into a list of dictionaries.
Parameters
----------
input_path : Path
The file system path (pathlib.Path) to the JSONL file.
Returns
-------
results : list of dict
A list of dictionaries reconstructed from the file content.
Raises
------
FileNotFoundError
If the specified input path does not exist.
json.JSONDecodeError
If a line in the file is not valid JSON.
Examples
--------
>>> data = load_results_jsonl(Path("results.jsonl"))
>>> len(data)
2
"""
results = []
with open(input_path, "r", encoding="utf-8") as f:
for line in f:
results.append(json.loads(line.strip()))
return results
# =============================================================================
# Simulation Functionality
# =============================================================================
def run_single_simulation(
prey_birth: float,
prey_death: float,
predator_birth: float,
predator_death: float,
grid_size: int,
seed: int,
cfg: Config,
with_evolution: bool = False,
compute_pcf: Optional[bool] = None,
) -> Dict:
"""
Run a single Predator-Prey (PP) simulation and collect comprehensive metrics.
This function initializes a Cellular Automata model, executes a warmup phase
to reach steady state, and then performs a measurement phase to track
population dynamics, spatial clustering, and evolutionary changes.
Parameters
----------
prey_birth : float
The probability or rate of prey reproduction.
prey_death : float
The base probability or rate of prey mortality.
predator_birth : float
The probability or rate of predator reproduction upon consuming prey.
predator_death : float
The probability or rate of predator mortality.
grid_size : int
The side length of the square simulation grid.
seed : int
Random seed for ensuring reproducibility of the simulation run.
cfg : Config
A configuration object containing simulation hyperparameters (densities,
sampling rates, timing, etc.).
with_evolution : bool, optional
If True, enables the evolution of the 'prey_death' parameter within
the model (default is False).
compute_pcf : bool, optional
Explicit toggle for Pair Correlation Function calculation. If None,
it is determined by `cfg.pcf_sample_rate` (default is None).
Returns
-------
result : dict
A dictionary containing simulation results including:
- Input parameters and survival flags.
- Population mean and standard deviation for both species.
- Cluster statistics (number of clusters, sizes, largest fractions).
- Evolutionary statistics (mean, std, min, max, and final values).
- PCF data and spatial indices (segregation and clustering).
- Optional time series for populations and evolved parameters.
Notes
-----
The function relies on several external utilities: `count_populations`,
`get_evolved_stats`, `get_cluster_stats_fast`, `compute_all_pcfs_fast`,
and `average_pcfs`.
"""
from models.CA import PP
if USE_NUMBA:
set_numba_seed(seed)
if compute_pcf is None:
compute_pcf = cfg.collect_pcf and (np.random.random() < cfg.pcf_sample_rate)
# Initialize model
model = PP(
rows=grid_size,
cols=grid_size,
densities=cfg.densities,
neighborhood="moore", # NOTE: Default neighborhood
params={
"prey_birth": prey_birth,
"prey_death": prey_death,
"predator_death": predator_death,
"predator_birth": predator_birth,
},
seed=seed,
directed_hunting=cfg.directed_hunting,
)
if with_evolution:
model.evolve(
"prey_death",
sd=cfg.evolve_sd,
min_val=cfg.evolve_min,
max_val=cfg.evolve_max,
)
# Scale timing with grid size
warmup_steps = cfg.get_warmup_steps(grid_size)
measurement_steps = cfg.get_measurement_steps(grid_size)
# Warmup phase
for _ in range(warmup_steps):
model.update()
# Measurement phase: start collecting our mertics
prey_pops, pred_pops = [], [] # Prey populations and predator populations
evolved_means, evolved_stds = [], [] # Evolution stats over time
cluster_sizes_prey, cluster_sizes_pred = [], [] # Cluster sizes
largest_fractions_prey, largest_fractions_pred = (
[],
[],
) # Largest cluster fractions = size of largest cluster / total population
pcf_samples = {"prey_prey": [], "pred_pred": [], "prey_pred": []}
# Determine minimum count for analysis
min_count = int(cfg.min_density_for_analysis * (grid_size**2))
for step in range(measurement_steps):
model.update()
_, prey, pred = count_populations(model.grid)
prey_pops.append(prey)
pred_pops.append(pred)
# Track evolution
if with_evolution:
stats = get_evolved_stats(model, "prey_death")
evolved_means.append(stats["mean"])
evolved_stds.append(stats["std"])
# Cluster analysis (at end of measurement)
if step == measurement_steps - 1:
prey_survived = prey_pops[-1] > min_count
pred_survived = pred_pops[-1] > (min_count // 4)
if prey_survived:
prey_stats = get_cluster_stats_fast(model.grid, 1)
cluster_sizes_prey = prey_stats["sizes"].tolist()
largest_fractions_prey.append(prey_stats["largest_fraction"])
if pred_survived:
pred_stats = get_cluster_stats_fast(model.grid, 2)
cluster_sizes_pred = pred_stats["sizes"].tolist()
largest_fractions_pred.append(pred_stats["largest_fraction"])
# PCF requires both
if compute_pcf and prey_survived and pred_survived:
max_dist = min(grid_size / 2, cfg.pcf_max_distance)
pcf_data = compute_all_pcfs_fast(model.grid, max_dist, cfg.pcf_n_bins)
pcf_samples["prey_prey"].append(pcf_data["prey_prey"])
pcf_samples["pred_pred"].append(pcf_data["pred_pred"])
pcf_samples["prey_pred"].append(pcf_data["prey_pred"])
# Compile results
result = {
# Parameters
"prey_birth": prey_birth,
"prey_death": prey_death,
"predator_birth": predator_birth,
"predator_death": predator_death,
"grid_size": grid_size,
"with_evolution": with_evolution,
"seed": seed,
# Population dynamics
"prey_mean": float(np.mean(prey_pops)),
"prey_std": float(np.std(prey_pops)),
"pred_mean": float(np.mean(pred_pops)),
"pred_std": float(np.std(pred_pops)),
"prey_survived": prey_pops[-1] > min_count,
"pred_survived": pred_pops[-1] > (min_count // 4),
# Cluster statistics
"prey_n_clusters": len(cluster_sizes_prey),
"pred_n_clusters": len(cluster_sizes_pred),
"prey_cluster_sizes": cluster_sizes_prey,
"pred_cluster_sizes": cluster_sizes_pred,
# Order parameters
"prey_largest_fraction": (
float(np.mean(largest_fractions_prey)) if largest_fractions_prey else np.nan
),
"pred_largest_fraction": (
float(np.mean(largest_fractions_pred)) if largest_fractions_pred else np.nan
),
}
# Time series (if requested)
if cfg.save_timeseries:
subsample = cfg.timeseries_subsample
result["prey_timeseries"] = prey_pops[
::subsample
] # NOTE: Sample temporal data every 'subsample' steps
result["pred_timeseries"] = pred_pops[::subsample]
# Evolution statistics
if with_evolution and evolved_means:
valid_means = [v for v in evolved_means if not np.isnan(v)]
result["evolved_prey_death_mean"] = (
float(np.mean(valid_means)) if valid_means else np.nan
)
result["evolved_prey_death_std"] = (
float(np.mean([v for v in evolved_stds if not np.isnan(v)]))
if evolved_stds
else np.nan
)
result["evolved_prey_death_final"] = valid_means[-1] if valid_means else np.nan
result["evolved_prey_death_min"] = (
float(np.min(valid_means)) if valid_means else np.nan
)
result["evolved_prey_death_max"] = (
float(np.max(valid_means)) if valid_means else np.nan
)
result["evolve_sd"] = cfg.evolve_sd
if cfg.save_timeseries:
result["evolved_prey_death_timeseries"] = evolved_means[
:: cfg.timeseries_subsample
]
# PCF statistics
if pcf_samples["prey_prey"]:
dist, pcf_rr, _ = average_pcfs(pcf_samples["prey_prey"])
_, pcf_cc, _ = average_pcfs(pcf_samples["pred_pred"])
_, pcf_cr, _ = average_pcfs(pcf_samples["prey_pred"])
result["pcf_distances"] = dist.tolist()
result["pcf_prey_prey"] = pcf_rr.tolist()
result["pcf_pred_pred"] = pcf_cc.tolist()
result["pcf_prey_pred"] = pcf_cr.tolist()
# Short-range indices
short_mask = dist < 3.0
if np.any(short_mask):
result["segregation_index"] = float(np.mean(pcf_cr[short_mask]))
result["prey_clustering_index"] = float(np.mean(pcf_rr[short_mask]))
result["pred_clustering_index"] = float(np.mean(pcf_cc[short_mask]))
return result
# =============================================================================
# Experiment Phases
# =============================================================================
def run_phase1(cfg: Config, output_dir: Path, logger: logging.Logger) -> List[Dict]:
"""
Execute Phase 1 of the simulation: a parameter sweep to identify critical points.
This function performs a 1D sweep across varying prey mortality rates while
keeping other parameters fixed. It utilizes parallel execution via joblib
and saves results incrementally to a JSONL file to ensure data integrity
during long-running batches.
Parameters
----------
cfg : Config
Configuration object containing simulation hyperparameters, sweep
ranges, and execution settings (n_jobs, grid_size, etc.).
output_dir : Path
Directory where result files (JSONL) and metadata (JSON) will be stored.
logger : logging.Logger
Logger instance for tracking simulation progress and recording
operational metadata.
Returns
-------
all_results : list of dict
A list of dictionaries containing the metrics collected from every
individual simulation run in the sweep.
Notes
-----
The function performs the following steps:
1. Pre-warms Numba kernels for performance.
2. Generates a deterministic set of simulation jobs using unique seeds.
3. Executes simulations in parallel using a generator for memory efficiency.
4. Records metadata including a timestamp and a serialized snapshot of
the configuration.
"""
from joblib import Parallel, delayed
warmup_numba_kernels(cfg.grid_size, directed_hunting=cfg.directed_hunting)
prey_deaths = cfg.get_prey_deaths()
# Build job list
jobs = []
# Sweep through prey_death only (prey_birth is fixed)
for pd in prey_deaths:
for rep in range(cfg.n_replicates):
params = {"pd": pd}
seed = generate_unique_seed(params, rep)
jobs.append(
(
cfg.prey_birth,
pd,
cfg.predator_birth,
cfg.predator_death,
cfg.grid_size,
seed,
cfg,
False,
)
)
logger.info(f"Phase 1: {len(jobs):,} simulations")
logger.info(
f" Grid: {cfg.n_prey_death} prey_death values × {cfg.n_replicates} reps (prey_birth={cfg.prey_birth})"
)
# Run with incremental saving
output_jsonl = output_dir / "phase1_results.jsonl"
all_results = []
with open(output_jsonl, "w", encoding="utf-8") as f:
executor = Parallel(n_jobs=cfg.n_jobs, return_as="generator")
tasks = (delayed(run_single_simulation)(*job) for job in jobs)
for result in tqdm(executor(tasks), total=len(jobs), desc="Phase 1"):
f.write(json.dumps(result, default=str) + "\n")
f.flush()
all_results.append(result)
# Save metadata
meta = {
"phase": 1,
"description": "Parameter sweep for critical point",
"n_sims": len(all_results),
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"config": asdict(cfg),
}
with open(output_dir / "phase1_metadata.json", "w") as f:
json.dump(meta, f, indent=2, default=str)
logger.info(f"Phase 1 complete. Results: {output_jsonl}")
return all_results
def run_phase2(cfg: Config, output_dir: Path, logger: logging.Logger) -> List[Dict]:
"""
Execute Phase 2 of the simulation: self-organization and criticality analysis.
This phase tests the Self-Organized Criticality (SOC) hypothesis by
initializing simulations at different points in the parameter space and
observing whether evolutionary pressure drives the system toward a
common critical point, regardless of initial prey mortality rates.
Parameters
----------
cfg : Config
Configuration object containing simulation hyperparameters, evolution
settings, and execution constraints.
output_dir : Path
Directory where result files (JSONL) and metadata (JSON) will be stored.
logger : logging.Logger
Logger instance for tracking progress and evolutionary convergence.
Returns
-------
all_results : list of dict
A list of dictionaries containing metrics from the evolutionary
simulation runs.
Notes
-----
The function captures:
1. Convergence of 'prey_death' across multiple replicates.
2. Final steady-state population distributions.
3. Incremental saving of results to prevent data loss.
"""
from joblib import Parallel, delayed
warmup_numba_kernels(cfg.grid_size, directed_hunting=cfg.directed_hunting)
# Test at multiple prey_birth values
pb = 0.2
# Vary intial prey_death
initial_prey_deaths = np.linspace(
cfg.prey_death_range[0], cfg.prey_death_range[1], cfg.n_prey_death
)
jobs = []
for initial_pd in initial_prey_deaths:
for rep in range(cfg.n_replicates):
params = {"pb": pb, "initial_pd": initial_pd, "phase": 2}
seed = generate_unique_seed(params, rep)
jobs.append(
(
pb,
initial_pd,
cfg.predator_birth,
cfg.predator_death,
cfg.grid_size,
seed,
cfg,
True,
)
)
logger.info(f"Phase 2: {len(jobs):,} simulations")
logger.info(f" prey_birth value: {pb}")
logger.info(f" initial prey_death values: {len(initial_prey_deaths)}")
logger.info(f" Replicates: {cfg.n_replicates}")
output_jsonl = output_dir / "phase2_results.jsonl"
all_results = []
with open(output_jsonl, "w", encoding="utf-8") as f:
executor = Parallel(n_jobs=cfg.n_jobs, return_as="generator")
tasks = (delayed(run_single_simulation)(*job) for job in jobs)
for result in tqdm(executor(tasks), total=len(jobs), desc="Phase 2"):
f.write(json.dumps(result, default=str) + "\n")
f.flush()
all_results.append(result)
meta = {
"phase": 2,
"description": "Self-organization toward criticality",
"n_sims": len(all_results),
"initial_prey_deaths": initial_prey_deaths.tolist(),
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
}
with open(output_dir / "phase2_metadata.json", "w") as f:
json.dump(meta, f, indent=2, default=str)
logger.info(f"Phase 2 complete. Results: {output_jsonl}")
return all_results
def run_phase3(cfg: Config, output_dir: Path, logger: logging.Logger) -> List[Dict]:
"""
Phase 3: Finite-size scaling at critical point.
- Multiple grid sizes at (critical_prey_birth, critical_prey_death)
- Analyze cluster size cutoffs vs L
"""
from joblib import Parallel, delayed
# NOTE: Tuned to critical points from phase 1
pb = cfg.critical_prey_birth
pd = cfg.critical_prey_death
logger.info(f"Phase 3: FSS at critical point (pb={pb}, pd={pd})")
for L in cfg.grid_sizes:
warmup_numba_kernels(L, directed_hunting=cfg.directed_hunting)
jobs = []
for L in cfg.grid_sizes: # Sweep through grid sizes
for rep in range(cfg.n_replicates):
params = {"L": L, "phase": 3}
seed = generate_unique_seed(params, rep)
jobs.append(
(pb, pd, cfg.predator_birth, cfg.predator_death, L, seed, cfg, False)
)
logger.info(f" Grid sizes: {cfg.grid_sizes}")
logger.info(f" Total simulations: {len(jobs):,}")
output_jsonl = output_dir / "phase3_results.jsonl"
all_results = []
with open(output_jsonl, "w", encoding="utf-8") as f:
executor = Parallel(n_jobs=cfg.n_jobs, return_as="generator")
tasks = (delayed(run_single_simulation)(*job) for job in jobs)
for result in tqdm(executor(tasks), total=len(jobs), desc="Phase 3"):
f.write(json.dumps(result, default=str) + "\n")
f.flush()
all_results.append(result)
# Post-run metadata: postprocessing will fit cluster cutoffs vs L
meta = {
"phase": 3,
"description": "Finite-size scaling",
"critical_point": {"prey_birth": pb, "prey_death": pd},
"grid_sizes": cfg.grid_sizes,
"n_sims": len(all_results),
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
}
with open(output_dir / "phase3_metadata.json", "w") as f:
json.dump(meta, f, indent=2, default=str)
logger.info(f"Phase 3 complete. Results: {output_jsonl}")
return all_results
def run_phase4(cfg: Config, output_dir: Path, logger: logging.Logger) -> List[Dict]:
"""
Execute Phase 3 of the simulation: Finite-Size Scaling (FSS) analysis.
This phase investigates how spatial structures, specifically cluster size
cutoffs, scale with the system size (L) at the critical point identified
in Phase 1. This is essential for determining the universality class of
the phase transition.
Parameters
----------
cfg : Config
Configuration object containing critical point parameters, the list of
grid sizes to test, and execution settings.
output_dir : Path
Directory where result files (JSONL) and FSS metadata (JSON) will be
stored.
logger : logging.Logger
Logger instance for tracking progress across different grid sizes.
Returns
-------
all_results : list of dict
A list of dictionaries containing metrics and cluster statistics for
each grid size and replicate.
Notes
-----
The function performs the following:
1. Iterates through multiple grid sizes defined in `cfg.grid_sizes`.
2. Generates parallel jobs for each size using critical birth/death rates.
3. Saves results incrementally to allow for post-simulation analysis of
power-law exponents.
"""
from joblib import Parallel, delayed
import itertools
warmup_numba_kernels(cfg.grid_size, directed_hunting=cfg.directed_hunting)
# Define sweep values
prey_death_values = np.linspace(0.05, 0.95, 10) # 10 values for prey_death
other_param_values = np.linspace(0.0, 1.0, 11) # 11 values for the rest
# Logging
logger.info(f"Phase 4: Full 4D Parameter Sweep")
logger.info(f" prey_death: 10 values from 0.05 to 0.95")
logger.info(f" prey_birth, pred_birth, pred_death: 11 values each from 0 to 1")
logger.info(f" Grid Size: {cfg.grid_size}")
logger.info(f" Replicates: {cfg.n_replicates}")
# Build parameter grid
param_grid = itertools.product(
other_param_values, # prey_birth (11 values)
prey_death_values, # prey_death (10 values)
other_param_values, # predator_birth (11 values)
other_param_values, # predator_death (11 values)
)
jobs = []
for pb, pd, pred_b, pred_d in param_grid:
for rep in range(cfg.n_replicates):
params_id = {
"pb": pb,
"pd": pd,
"pred_b": pred_b,
"pred_d": pred_d,
"rep": rep,
}
seed = generate_unique_seed(params_id, rep)
jobs.append(
(
pb, # prey_birth
pd, # prey_death
pred_b, # predator_birth
pred_d, # predator_death
cfg.grid_size,
seed,
cfg,
False,
)
)
logger.info(
f" Total simulations: {len(jobs):,}"
) # 11 * 10 * 11 * 11 * n_reps = 13,310 * n_reps
output_jsonl = output_dir / "phase4_results.jsonl"
all_results = []
with open(output_jsonl, "w", encoding="utf-8") as f:
executor = Parallel(n_jobs=cfg.n_jobs, return_as="generator")
tasks = (delayed(run_single_simulation)(*job) for job in jobs)
for result in tqdm(executor(tasks), total=len(jobs), desc="Phase 4 (4D Sweep)"):
f.write(json.dumps(result, default=str) + "\n")
f.flush()
all_results.append(result)
# Save Metadata
meta = {
"phase": 4,
"description": "Global 4D Sensitivity Analysis",
"prey_death_values": prey_death_values.tolist(),
"other_param_values": other_param_values.tolist(),
"parameters_varied": [
"prey_birth",
"prey_death",
"predator_birth",
"predator_death",
],
"n_sims": len(all_results),
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"config": asdict(cfg),
}
with open(output_dir / "phase4_metadata.json", "w") as f:
json.dump(meta, f, indent=2, default=str)
logger.info(f"Phase 4 complete. Results: {output_jsonl}")
return all_results
def run_phase5(cfg: Config, output_dir: Path, logger: logging.Logger) -> List[Dict]:
"""
Execute Phase 5 of the simulation: Global 4D parameter sweep with directed hunting.
This phase performs a comprehensive sensitivity analysis by varying four key
parameters (prey birth/death and predator birth/death) while directed
hunting is enabled. The results allow for a direct comparison with Phase 4
to determine how predator search behavior shifts the system's critical