tslime 0.1.1

A lightweight terminal screensaver simulating slime mold growth patterns
Documentation
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
//! Parameter space explorer for discovering optimal presets.
//!
//! Runs headless simulations with various parameter combinations and
//! evaluates them using pattern metrics to find parameters that produce
//! specific emergent behaviors.

use crate::exploration::metrics::PatternMetrics;
use crate::render::palette::RgbColor;
use crate::simulation::config::{
    DiffusionKernel, InitMode, SimConfig, SpeciesConfig, TerrainType, Wind,
};
use crate::simulation::Simulation;
use rand::prelude::*;
use rand_xoshiro::Xoshiro256PlusPlus;

/// Parameter set explored by the optimizer.
#[derive(Debug, Clone, Copy)]
pub struct ExplorationParams {
    // Core agent parameters
    /// Sensor angle in degrees (5-90).
    pub sensor_angle: f32,
    /// Sensor distance in pixels (1-50).
    pub sensor_distance: f32,
    /// Rotation angle in degrees (5-90).
    pub rotation_angle: f32,
    /// Agent step size (0.5-5.0).
    pub step_size: f32,
    /// Trail decay factor (0.5-0.99).
    pub decay_factor: f32,
    /// Amount of pheromone deposited (1-20).
    pub deposit_amount: f32,
    /// Total agent population.
    pub population: usize,

    // Extended parameters
    /// Diffusion algorithm (Mean3x3 or Gaussian).
    pub diffusion_kernel: DiffusionKernel,
    /// Wind horizontal component (-1.0 to 1.0), None if disabled.
    pub wind_dx: Option<f32>,
    /// Wind vertical component (-1.0 to 1.0), None if disabled.
    pub wind_dy: Option<f32>,
    /// Terrain type for steering bias.
    pub terrain: TerrainType,
    /// Terrain effect strength (0.1-5.0).
    pub terrain_strength: f32,
    /// Agent initialization mode.
    pub init_mode: InitMode,
}

impl ExplorationParams {
    /// Create random parameters within valid ranges.
    pub fn random(rng: &mut impl Rng) -> Self {
        // 30% chance of wind
        let (wind_dx, wind_dy) = if rng.gen_bool(0.3) {
            (
                Some(rng.gen_range(-0.5..0.5)),
                Some(rng.gen_range(-0.5..0.5)),
            )
        } else {
            (None, None)
        };

        // Randomly select terrain type (40% none, 20% each other)
        let terrain = match rng.gen_range(0..5) {
            0 | 1 => TerrainType::None,
            2 => TerrainType::Smooth,
            3 => TerrainType::Turbulent,
            _ => TerrainType::Mixed,
        };

        // Randomly select init mode (60% Random, 40% others)
        let init_mode = match rng.gen_range(0..10) {
            0..=5 => InitMode::Random,
            6 => InitMode::CentralBurst,
            7 => InitMode::Circle,
            8 => InitMode::Gradient,
            _ => InitMode::WaveFront,
        };

        Self {
            sensor_angle: rng.gen_range(5.0..90.0),
            sensor_distance: rng.gen_range(1.0..50.0),
            rotation_angle: rng.gen_range(5.0..90.0),
            step_size: rng.gen_range(0.5..5.0),
            decay_factor: rng.gen_range(0.5..0.99),
            deposit_amount: rng.gen_range(1.0..20.0),
            population: rng.gen_range(5000..100000),
            diffusion_kernel: if rng.gen_bool(0.5) {
                DiffusionKernel::Mean3x3
            } else {
                DiffusionKernel::Gaussian
            },
            wind_dx,
            wind_dy,
            terrain,
            terrain_strength: rng.gen_range(0.5..3.0),
            init_mode,
        }
    }

    /// Create parameters biased toward a specific behavior.
    pub fn random_biased(rng: &mut impl Rng, behavior: PresetBehavior) -> Self {
        match behavior {
            PresetBehavior::Vortex => Self {
                // rotation_angle > sensor_angle for spiraling
                sensor_angle: rng.gen_range(5.0..30.0),
                sensor_distance: rng.gen_range(8.0..20.0),
                rotation_angle: rng.gen_range(40.0..90.0),
                step_size: rng.gen_range(0.8..2.0),
                decay_factor: rng.gen_range(0.8..0.95),
                deposit_amount: rng.gen_range(3.0..8.0),
                population: rng.gen_range(30000..80000),
                // Gaussian diffusion for smoother vortex patterns
                diffusion_kernel: if rng.gen_bool(0.7) {
                    DiffusionKernel::Gaussian
                } else {
                    DiffusionKernel::Mean3x3
                },
                // Light wind can enhance swirling
                wind_dx: if rng.gen_bool(0.4) {
                    Some(rng.gen_range(-0.3..0.3))
                } else {
                    None
                },
                wind_dy: if rng.gen_bool(0.4) {
                    Some(rng.gen_range(-0.3..0.3))
                } else {
                    None
                },
                terrain: TerrainType::None,
                terrain_strength: 1.0,
                init_mode: if rng.gen_bool(0.6) {
                    InitMode::Random
                } else {
                    InitMode::Circle
                },
            },
            PresetBehavior::Lightning => Self {
                // Fast, sparse, high contrast branching
                sensor_angle: rng.gen_range(5.0..20.0),
                sensor_distance: rng.gen_range(10.0..30.0),
                rotation_angle: rng.gen_range(10.0..30.0),
                step_size: rng.gen_range(2.0..5.0),
                decay_factor: rng.gen_range(0.5..0.75),
                deposit_amount: rng.gen_range(10.0..20.0),
                population: rng.gen_range(5000..20000),
                // Mean3x3 for sharper branching
                diffusion_kernel: DiffusionKernel::Mean3x3,
                // No wind for lightning (let it spread naturally)
                wind_dx: None,
                wind_dy: None,
                terrain: TerrainType::None,
                terrain_strength: 1.0,
                // CentralBurst creates explosive lightning effect
                init_mode: if rng.gen_bool(0.5) {
                    InitMode::CentralBurst
                } else {
                    InitMode::Random
                },
            },
            PresetBehavior::Crystal => Self {
                // Slow, stable, persistent structures
                sensor_angle: rng.gen_range(20.0..50.0),
                sensor_distance: rng.gen_range(15.0..40.0),
                rotation_angle: rng.gen_range(10.0..40.0),
                step_size: rng.gen_range(0.5..1.0),
                decay_factor: rng.gen_range(0.95..0.99),
                deposit_amount: rng.gen_range(2.0..6.0),
                population: rng.gen_range(15000..40000),
                // Gaussian for smooth crystal facets
                diffusion_kernel: DiffusionKernel::Gaussian,
                wind_dx: None,
                wind_dy: None,
                // Smooth terrain can create interesting crystal growth patterns
                terrain: if rng.gen_bool(0.3) {
                    TerrainType::Smooth
                } else {
                    TerrainType::None
                },
                terrain_strength: rng.gen_range(0.5..1.5),
                init_mode: InitMode::Random,
            },
            PresetBehavior::Blob => Self {
                // Short-sighted, sharp turns, fast decay for clustering
                sensor_angle: rng.gen_range(30.0..70.0),
                sensor_distance: rng.gen_range(1.0..8.0),
                rotation_angle: rng.gen_range(50.0..90.0),
                step_size: rng.gen_range(0.5..1.5),
                decay_factor: rng.gen_range(0.5..0.7),
                deposit_amount: rng.gen_range(5.0..15.0),
                population: rng.gen_range(20000..60000),
                // Mean3x3 for sharper blob edges
                diffusion_kernel: DiffusionKernel::Mean3x3,
                wind_dx: None,
                wind_dy: None,
                // Turbulent terrain breaks up patterns into blobs
                terrain: if rng.gen_bool(0.5) {
                    TerrainType::Turbulent
                } else {
                    TerrainType::None
                },
                terrain_strength: rng.gen_range(1.0..3.0),
                // RandomClusters naturally creates blobby patterns
                init_mode: if rng.gen_bool(0.4) {
                    InitMode::RandomClusters
                } else {
                    InitMode::Random
                },
            },
            PresetBehavior::Worm => Self {
                // Long-sighted, low population for snaking trails
                sensor_angle: rng.gen_range(10.0..25.0),
                sensor_distance: rng.gen_range(20.0..50.0),
                rotation_angle: rng.gen_range(20.0..45.0),
                step_size: rng.gen_range(1.0..2.5),
                decay_factor: rng.gen_range(0.88..0.96),
                deposit_amount: rng.gen_range(4.0..10.0),
                population: rng.gen_range(3000..15000),
                diffusion_kernel: DiffusionKernel::Mean3x3,
                // Light directional wind creates flowing worms
                wind_dx: if rng.gen_bool(0.5) {
                    Some(rng.gen_range(0.1..0.4))
                } else {
                    None
                },
                wind_dy: if rng.gen_bool(0.3) {
                    Some(rng.gen_range(-0.2..0.2))
                } else {
                    None
                },
                terrain: TerrainType::None,
                terrain_strength: 1.0,
                // Gradient or WaveFront creates aligned worm trails
                init_mode: match rng.gen_range(0..3) {
                    0 => InitMode::Gradient,
                    1 => InitMode::WaveFront,
                    _ => InitMode::Random,
                },
            },
            PresetBehavior::ChaosEdge => Self {
                // sensor_angle ≈ rotation_angle for edge-of-chaos
                sensor_angle: rng.gen_range(15.0..40.0),
                sensor_distance: rng.gen_range(5.0..20.0),
                rotation_angle: rng.gen_range(15.0..40.0),
                step_size: rng.gen_range(0.8..1.5),
                decay_factor: rng.gen_range(0.8..0.92),
                deposit_amount: rng.gen_range(3.0..8.0),
                population: rng.gen_range(30000..70000),
                diffusion_kernel: if rng.gen_bool(0.5) {
                    DiffusionKernel::Mean3x3
                } else {
                    DiffusionKernel::Gaussian
                },
                wind_dx: None,
                wind_dy: None,
                // Mixed terrain adds to chaotic dynamics
                terrain: if rng.gen_bool(0.4) {
                    TerrainType::Mixed
                } else {
                    TerrainType::None
                },
                terrain_strength: rng.gen_range(0.5..2.0),
                init_mode: InitMode::Random,
            },
        }
    }

    /// Mutate parameters slightly for local search.
    pub fn mutate(&self, rng: &mut impl Rng, mutation_strength: f32) -> Self {
        fn mutate_f32(v: f32, min: f32, max: f32, rng: &mut impl Rng, strength: f32) -> f32 {
            let delta = (max - min) * strength * rng.gen_range(-1.0..1.0);
            (v + delta).clamp(min, max)
        }

        // Occasionally flip diffusion kernel (probability scales with mutation strength, capped at 20%)
        let diffusion_kernel = if rng.gen_bool((mutation_strength * 0.5).min(0.2) as f64) {
            match self.diffusion_kernel {
                DiffusionKernel::Mean3x3 => DiffusionKernel::Gaussian,
                DiffusionKernel::Gaussian => DiffusionKernel::Mean3x3,
            }
        } else {
            self.diffusion_kernel
        };

        // Mutate wind (can enable/disable or adjust)
        let (wind_dx, wind_dy) = if rng.gen_bool((mutation_strength * 0.3).min(0.15) as f64) {
            // Toggle wind on/off or change values
            if self.wind_dx.is_some() && rng.gen_bool(0.3) {
                (None, None) // Disable wind
            } else {
                (
                    Some(mutate_f32(
                        self.wind_dx.unwrap_or(0.0),
                        -0.8,
                        0.8,
                        rng,
                        mutation_strength,
                    )),
                    Some(mutate_f32(
                        self.wind_dy.unwrap_or(0.0),
                        -0.8,
                        0.8,
                        rng,
                        mutation_strength,
                    )),
                )
            }
        } else {
            // Keep wind as is, but mutate values if present
            (
                self.wind_dx
                    .map(|v| mutate_f32(v, -0.8, 0.8, rng, mutation_strength)),
                self.wind_dy
                    .map(|v| mutate_f32(v, -0.8, 0.8, rng, mutation_strength)),
            )
        };

        // Occasionally change terrain type (probability scales with mutation strength, capped at 15%)
        let terrain = if rng.gen_bool((mutation_strength * 0.5).min(0.15) as f64) {
            match rng.gen_range(0..4) {
                0 => TerrainType::None,
                1 => TerrainType::Smooth,
                2 => TerrainType::Turbulent,
                _ => TerrainType::Mixed,
            }
        } else {
            self.terrain
        };

        // Occasionally change init mode (probability scales with mutation strength, capped at 10%)
        let init_mode = if rng.gen_bool((mutation_strength * 0.4).min(0.1) as f64) {
            match rng.gen_range(0..5) {
                0 => InitMode::Random,
                1 => InitMode::CentralBurst,
                2 => InitMode::Circle,
                3 => InitMode::Gradient,
                _ => InitMode::WaveFront,
            }
        } else {
            self.init_mode
        };

        Self {
            sensor_angle: mutate_f32(self.sensor_angle, 5.0, 90.0, rng, mutation_strength),
            sensor_distance: mutate_f32(self.sensor_distance, 1.0, 50.0, rng, mutation_strength),
            rotation_angle: mutate_f32(self.rotation_angle, 5.0, 90.0, rng, mutation_strength),
            step_size: mutate_f32(self.step_size, 0.5, 5.0, rng, mutation_strength),
            decay_factor: mutate_f32(self.decay_factor, 0.5, 0.99, rng, mutation_strength),
            deposit_amount: mutate_f32(self.deposit_amount, 1.0, 20.0, rng, mutation_strength),
            population: (self.population as f32
                * (1.0 + mutation_strength * rng.gen_range(-0.5..0.5)))
            .clamp(5000.0, 100000.0) as usize,
            diffusion_kernel,
            wind_dx,
            wind_dy,
            terrain,
            terrain_strength: mutate_f32(self.terrain_strength, 0.1, 5.0, rng, mutation_strength),
            init_mode,
        }
    }

    /// Convert to SimConfig for simulation.
    pub fn to_sim_config(&self) -> SimConfig {
        // Wind requires both components present and a non-negligible magnitude
        let wind = match (self.wind_dx, self.wind_dy) {
            (Some(dx), Some(dy)) if dx.abs() > 0.001 || dy.abs() > 0.001 => Some(Wind::new(dx, dy)),
            _ => None,
        };

        SimConfig {
            sensor_angle: self.sensor_angle,
            sensor_distance: self.sensor_distance,
            rotation_angle: self.rotation_angle,
            step_size: self.step_size,
            decay_factor: self.decay_factor,
            deposit_amount: self.deposit_amount,
            species_configs: vec![SpeciesConfig {
                name: "explorer".to_string(),
                count: self.population,
                sensor_angle: self.sensor_angle,
                rotation_angle: self.rotation_angle,
                step_size: self.step_size,
                deposit_amount: self.deposit_amount,
                color: RgbColor::from_hex(0xffffff),
                trail_modulation: None,
            }],
            diffusion_kernel: self.diffusion_kernel,
            wind,
            terrain: self.terrain,
            terrain_strength: self.terrain_strength,
            ..Default::default()
        }
    }

    /// Format as Rust code for preset definition.
    pub fn to_rust_code(&self, name: &str) -> String {
        let diffusion_str = match self.diffusion_kernel {
            DiffusionKernel::Mean3x3 => "DiffusionKernel::Mean3x3",
            DiffusionKernel::Gaussian => "DiffusionKernel::Gaussian",
        };

        let wind_str = match (self.wind_dx, self.wind_dy) {
            (Some(dx), Some(dy)) if dx.abs() > 0.001 || dy.abs() > 0.001 => {
                format!("Some(Wind::new({:.2}, {:.2}))", dx, dy)
            }
            _ => "None".to_string(),
        };

        let terrain_str = match self.terrain {
            TerrainType::None => "TerrainType::None",
            TerrainType::Smooth => "TerrainType::Smooth",
            TerrainType::Turbulent => "TerrainType::Turbulent",
            TerrainType::Mixed => "TerrainType::Mixed",
        };

        let init_str = match self.init_mode {
            InitMode::Random => "InitMode::Random",
            InitMode::CentralBurst => "InitMode::CentralBurst",
            InitMode::Circle => "InitMode::Circle",
            InitMode::Gradient => "InitMode::Gradient",
            InitMode::WaveFront => "InitMode::WaveFront",
            InitMode::Spiral => "InitMode::Spiral",
            InitMode::RandomClusters => "InitMode::RandomClusters",
            InitMode::Food => "InitMode::Food",
            InitMode::Petri => "InitMode::Petri",
            InitMode::Constellation => "InitMode::Constellation",
            InitMode::FoodConstellation => "InitMode::FoodConstellation",
        };

        format!(
            r#"Preset::{name} => Self {{
    sensor_angle: {:.1},
    sensor_distance: {:.1},
    rotation_angle: {:.1},
    step_size: {:.2},
    decay_factor: {:.2},
    deposit_amount: {:.1},
    diffusion_kernel: {},
    wind: {},
    terrain: {},
    terrain_strength: {:.2},
    // population: {}, init_mode: {}
    ...
}}"#,
            self.sensor_angle,
            self.sensor_distance,
            self.rotation_angle,
            self.step_size,
            self.decay_factor,
            self.deposit_amount,
            diffusion_str,
            wind_str,
            terrain_str,
            self.terrain_strength,
            self.population,
            init_str,
        )
    }
}

/// Target behaviors for optimization.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PresetBehavior {
    /// Swirling vortex patterns with high angular momentum.
    Vortex,
    /// Fast dendritic branching patterns like lightning.
    Lightning,
    /// Slow, stable geometric crystal growth patterns.
    Crystal,
    /// Aggregating blob clusters with high fragmentation.
    Blob,
    /// Long snaking worm-like trails.
    Worm,
    /// Edge-of-chaos sensitive patterns with high variance.
    ChaosEdge,
}

impl PresetBehavior {
    /// Score the given metrics for this behavior.
    pub fn score(&self, metrics: &PatternMetrics) -> f32 {
        match self {
            PresetBehavior::Vortex => metrics.vortex_score(),
            PresetBehavior::Lightning => metrics.lightning_score(),
            PresetBehavior::Crystal => metrics.crystal_score(),
            PresetBehavior::Blob => metrics.blob_score(),
            PresetBehavior::Worm => metrics.worm_score(),
            PresetBehavior::ChaosEdge => metrics.chaos_score(),
        }
    }

    /// All behavior types.
    pub fn all() -> &'static [PresetBehavior] {
        &[
            PresetBehavior::Vortex,
            PresetBehavior::Lightning,
            PresetBehavior::Crystal,
            PresetBehavior::Blob,
            PresetBehavior::Worm,
            PresetBehavior::ChaosEdge,
        ]
    }
}

/// Result of evaluating a parameter set.
#[derive(Debug, Clone)]
pub struct EvaluationResult {
    /// The parameters that were evaluated.
    pub params: ExplorationParams,
    /// Computed pattern metrics.
    pub metrics: PatternMetrics,
    /// Scores for each behavior type.
    pub scores: Vec<(PresetBehavior, f32)>,
}

/// Explorer configuration.
#[derive(Debug, Clone)]
pub struct ExplorerConfig {
    /// Grid width for simulation.
    pub width: usize,
    /// Grid height for simulation.
    pub height: usize,
    /// Number of frames to simulate before measuring.
    pub warmup_frames: usize,
    /// Number of frames to measure over.
    pub measurement_frames: usize,
    /// Random seed.
    pub seed: u64,
}

impl Default for ExplorerConfig {
    fn default() -> Self {
        Self {
            width: 200,
            height: 200,
            warmup_frames: 100,
            measurement_frames: 50,
            seed: 42,
        }
    }
}

/// Parameter space explorer.
pub struct Explorer {
    config: ExplorerConfig,
    rng: Xoshiro256PlusPlus,
}

impl Explorer {
    /// Create a new explorer.
    pub fn new(config: ExplorerConfig) -> Self {
        let rng = Xoshiro256PlusPlus::seed_from_u64(config.seed);
        Self { config, rng }
    }

    /// Evaluate a single parameter set.
    pub fn evaluate(&self, params: &ExplorationParams) -> EvaluationResult {
        let sim_config = params.to_sim_config();
        let mut sim = Simulation::new(
            self.config.width,
            self.config.height,
            sim_config,
            self.config.seed,
            params.init_mode,
            0, // No trail history needed for metrics
        );

        // Warmup
        for _ in 0..self.config.warmup_frames {
            sim.update(1.0);
        }

        // Measure over multiple frames
        let mut prev_trail: Option<Vec<f32>> = None;
        let mut accumulated_metrics = PatternMetrics::default();
        let mut sample_count = 0;

        for _ in 0..self.config.measurement_frames {
            sim.update(1.0);

            let trail = sim.trail_map().current().to_vec();
            let agents = sim.agents();

            let metrics = PatternMetrics::compute(
                &trail,
                self.config.width,
                self.config.height,
                agents,
                prev_trail.as_deref(),
            );

            // Accumulate metrics
            accumulated_metrics.angular_momentum += metrics.angular_momentum;
            accumulated_metrics.heading_variance += metrics.heading_variance;
            accumulated_metrics.trail_fragmentation += metrics.trail_fragmentation;
            accumulated_metrics.trail_elongation += metrics.trail_elongation;
            accumulated_metrics.spatial_entropy += metrics.spatial_entropy;
            accumulated_metrics.temporal_stability += metrics.temporal_stability;
            accumulated_metrics.density_variance += metrics.density_variance;
            accumulated_metrics.mean_intensity += metrics.mean_intensity;
            accumulated_metrics.coverage += metrics.coverage;
            accumulated_metrics.branching_factor += metrics.branching_factor;
            accumulated_metrics.flow_coherence += metrics.flow_coherence;
            accumulated_metrics.spatial_concentration += metrics.spatial_concentration;
            accumulated_metrics.path_continuity += metrics.path_continuity;

            sample_count += 1;
            prev_trail = Some(trail);
        }

        // Average metrics
        let n = sample_count as f32;
        let avg_metrics = PatternMetrics {
            angular_momentum: accumulated_metrics.angular_momentum / n,
            heading_variance: accumulated_metrics.heading_variance / n,
            trail_fragmentation: accumulated_metrics.trail_fragmentation / sample_count as u32,
            trail_elongation: accumulated_metrics.trail_elongation / n,
            spatial_entropy: accumulated_metrics.spatial_entropy / n,
            temporal_stability: accumulated_metrics.temporal_stability / n,
            density_variance: accumulated_metrics.density_variance / n,
            mean_intensity: accumulated_metrics.mean_intensity / n,
            coverage: accumulated_metrics.coverage / n,
            branching_factor: accumulated_metrics.branching_factor / n,
            flow_coherence: accumulated_metrics.flow_coherence / n,
            spatial_concentration: accumulated_metrics.spatial_concentration / n,
            path_continuity: accumulated_metrics.path_continuity / n,
        };

        // Compute scores for all behaviors
        let scores: Vec<(PresetBehavior, f32)> = PresetBehavior::all()
            .iter()
            .map(|&b| (b, b.score(&avg_metrics)))
            .collect();

        EvaluationResult {
            params: *params,
            metrics: avg_metrics,
            scores,
        }
    }

    /// Run random search for a specific behavior.
    pub fn random_search(
        &mut self,
        behavior: PresetBehavior,
        iterations: usize,
        use_bias: bool,
    ) -> Vec<EvaluationResult> {
        let mut results = Vec::with_capacity(iterations);

        for i in 0..iterations {
            let params = if use_bias {
                ExplorationParams::random_biased(&mut self.rng, behavior)
            } else {
                ExplorationParams::random(&mut self.rng)
            };

            let result = self.evaluate(&params);

            if i % 10 == 0 {
                let score = behavior.score(&result.metrics);
                eprintln!("Iteration {}/{}: score = {:.4}", i + 1, iterations, score);
            }

            results.push(result);
        }

        // Sort by target behavior score (descending)
        results.sort_by(|a, b| {
            let sa = behavior.score(&a.metrics);
            let sb = behavior.score(&b.metrics);
            sb.partial_cmp(&sa).unwrap_or(std::cmp::Ordering::Equal)
        });

        results
    }

    /// Run hill-climbing optimization for a specific behavior.
    pub fn hill_climb(
        &mut self,
        behavior: PresetBehavior,
        initial: Option<ExplorationParams>,
        iterations: usize,
        restarts: usize,
    ) -> EvaluationResult {
        let mut best_result: Option<EvaluationResult> = None;

        for restart in 0..restarts {
            // Initialize
            let mut current = initial
                .unwrap_or_else(|| ExplorationParams::random_biased(&mut self.rng, behavior));
            let mut current_result = self.evaluate(&current);
            let mut current_score = behavior.score(&current_result.metrics);

            let mut no_improvement = 0;
            let mut mutation_strength = 0.2f32;

            for iter in 0..iterations {
                // Generate neighbor
                let neighbor = current.mutate(&mut self.rng, mutation_strength);
                let neighbor_result = self.evaluate(&neighbor);
                let neighbor_score = behavior.score(&neighbor_result.metrics);

                if neighbor_score > current_score {
                    current = neighbor;
                    current_result = neighbor_result;
                    current_score = neighbor_score;
                    no_improvement = 0;
                    mutation_strength = (mutation_strength * 0.95).max(0.05);
                } else {
                    no_improvement += 1;
                    if no_improvement > 10 {
                        mutation_strength = (mutation_strength * 1.1).min(0.5);
                        no_improvement = 0;
                    }
                }

                if iter % 20 == 0 {
                    eprintln!(
                        "Restart {}/{}, Iter {}/{}: score = {:.4}, mutation = {:.3}",
                        restart + 1,
                        restarts,
                        iter + 1,
                        iterations,
                        current_score,
                        mutation_strength
                    );
                }
            }

            // Update best
            if best_result.is_none()
                || behavior.score(&current_result.metrics)
                    > behavior.score(&best_result.as_ref().unwrap().metrics)
            {
                best_result = Some(current_result);
            }
        }

        best_result.expect("At least one restart should complete")
    }

    /// Hybrid search: combines random search with hill-climbing refinement.
    /// First runs random search to find promising regions, then refines the best with hill-climbing.
    pub fn hybrid_search(
        &mut self,
        behavior: PresetBehavior,
        random_iterations: usize,
        hill_climb_iterations: usize,
        top_k: usize,
    ) -> EvaluationResult {
        eprintln!(
            "Phase 1: Random search ({} iterations)...",
            random_iterations
        );

        // Phase 1: Random search to find promising starting points
        let random_results = self.random_search(behavior, random_iterations, true);

        // Select top-k candidates
        let mut sorted = random_results;
        sorted.sort_by(|a, b| {
            behavior
                .score(&b.metrics)
                .partial_cmp(&behavior.score(&a.metrics))
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        let candidates: Vec<_> = sorted.into_iter().take(top_k).collect();

        eprintln!(
            "Phase 1 complete. Top {} scores: {:?}",
            top_k,
            candidates
                .iter()
                .map(|r| behavior.score(&r.metrics))
                .collect::<Vec<_>>()
        );

        // Phase 2: Hill-climb from each candidate
        eprintln!(
            "Phase 2: Hill-climbing refinement ({} iterations x {} candidates)...",
            hill_climb_iterations, top_k
        );

        let mut best_overall: Option<EvaluationResult> = None;

        for (i, candidate) in candidates.iter().enumerate() {
            eprintln!("  Refining candidate {}/{}...", i + 1, top_k);
            let refined =
                self.hill_climb(behavior, Some(candidate.params), hill_climb_iterations, 1);
            let refined_score = behavior.score(&refined.metrics);

            eprintln!(
                "    Candidate {}: initial={:.4}, refined={:.4}",
                i + 1,
                behavior.score(&candidate.metrics),
                refined_score
            );

            if best_overall.is_none()
                || refined_score > behavior.score(&best_overall.as_ref().unwrap().metrics)
            {
                best_overall = Some(refined);
            }
        }

        best_overall.expect("At least one candidate should be refined")
    }

    /// Find optimal parameters for all behaviors using the basic hill-climb method.
    pub fn optimize_all(
        &mut self,
        iterations_per_behavior: usize,
    ) -> Vec<(PresetBehavior, EvaluationResult)> {
        let mut results = Vec::new();

        for &behavior in PresetBehavior::all() {
            eprintln!("\n=== Optimizing {:?} ===", behavior);
            let result = self.hill_climb(behavior, None, iterations_per_behavior, 3);
            eprintln!(
                "Best {:?} score: {:.4}",
                behavior,
                behavior.score(&result.metrics)
            );
            eprintln!(
                "Parameters:\n{}",
                result.params.to_rust_code(&format!("{:?}", behavior))
            );
            results.push((behavior, result));
        }

        results
    }

    /// Find optimal parameters for all behaviors using the hybrid search method.
    /// This is more thorough than optimize_all, combining random exploration with refinement.
    pub fn optimize_all_hybrid(
        &mut self,
        random_iterations: usize,
        hill_climb_iterations: usize,
        top_k: usize,
    ) -> Vec<(PresetBehavior, EvaluationResult)> {
        let mut results = Vec::new();

        for &behavior in PresetBehavior::all() {
            eprintln!("\n=== Hybrid optimization for {:?} ===", behavior);
            let result =
                self.hybrid_search(behavior, random_iterations, hill_climb_iterations, top_k);
            let final_score = behavior.score(&result.metrics);

            eprintln!("Best {:?} score: {:.4}", behavior, final_score);
            eprintln!(
                "Parameters:\n{}",
                result.params.to_rust_code(&format!("{:?}", behavior))
            );

            // Print key metrics for analysis
            eprintln!("Key metrics:");
            eprintln!("  angular_momentum: {:.4}", result.metrics.angular_momentum);
            eprintln!("  heading_variance: {:.4}", result.metrics.heading_variance);
            eprintln!(
                "  trail_fragmentation: {}",
                result.metrics.trail_fragmentation
            );
            eprintln!("  trail_elongation: {:.4}", result.metrics.trail_elongation);
            eprintln!("  flow_coherence: {:.4}", result.metrics.flow_coherence);
            eprintln!(
                "  spatial_concentration: {:.4}",
                result.metrics.spatial_concentration
            );
            eprintln!("  path_continuity: {:.4}", result.metrics.path_continuity);
            eprintln!("  coverage: {:.4}", result.metrics.coverage);

            results.push((behavior, result));
        }

        // Summary
        eprintln!("\n=== SUMMARY ===");
        for (behavior, result) in &results {
            let score = behavior.score(&result.metrics);
            eprintln!(
                "{:?}: score={:.4}, coverage={:.2}, frag={}, elongation={:.2}",
                behavior,
                score,
                result.metrics.coverage,
                result.metrics.trail_fragmentation,
                result.metrics.trail_elongation
            );
        }

        results
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_exploration_params_random() {
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);
        let params = ExplorationParams::random(&mut rng);

        assert!(params.sensor_angle >= 5.0 && params.sensor_angle <= 90.0);
        assert!(params.decay_factor >= 0.5 && params.decay_factor <= 0.99);
        assert!(params.population >= 5000 && params.population <= 100000);
    }

    #[test]
    fn test_exploration_params_mutate() {
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);
        let params = ExplorationParams::random(&mut rng);
        let mutated = params.mutate(&mut rng, 0.1);

        // Should be different but still valid
        assert!(mutated.sensor_angle >= 5.0 && mutated.sensor_angle <= 90.0);
        assert!(mutated.decay_factor >= 0.5 && mutated.decay_factor <= 0.99);
    }

    #[test]
    fn test_explorer_evaluate() {
        let config = ExplorerConfig {
            width: 100,
            height: 100,
            warmup_frames: 10,
            measurement_frames: 5,
            seed: 42,
        };
        let explorer = Explorer::new(config);
        let params = ExplorationParams {
            sensor_angle: 22.5,
            sensor_distance: 9.0,
            rotation_angle: 45.0,
            step_size: 1.0,
            decay_factor: 0.9,
            deposit_amount: 5.0,
            population: 10000,
            diffusion_kernel: DiffusionKernel::Mean3x3,
            wind_dx: None,
            wind_dy: None,
            terrain: TerrainType::None,
            terrain_strength: 1.0,
            init_mode: InitMode::Random,
        };

        let result = explorer.evaluate(&params);

        assert!(!result.scores.is_empty());
        assert!(result.metrics.coverage >= 0.0);
    }

    #[test]
    fn test_to_sim_config() {
        let params = ExplorationParams {
            sensor_angle: 30.0,
            sensor_distance: 15.0,
            rotation_angle: 45.0,
            step_size: 1.5,
            decay_factor: 0.85,
            deposit_amount: 5.0,
            population: 25000,
            diffusion_kernel: DiffusionKernel::Gaussian,
            wind_dx: Some(0.2),
            wind_dy: Some(0.1),
            terrain: TerrainType::Smooth,
            terrain_strength: 1.5,
            init_mode: InitMode::CentralBurst,
        };

        let config = params.to_sim_config();
        assert_eq!(config.sensor_angle, 30.0);
        assert_eq!(config.rotation_angle, 45.0);
        assert_eq!(config.total_population(), 25000);
        assert_eq!(config.diffusion_kernel, DiffusionKernel::Gaussian);
        assert!(config.wind.is_some());
        assert_eq!(config.terrain, TerrainType::Smooth);
    }

    #[test]
    fn test_extended_params_random() {
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);
        let params = ExplorationParams::random(&mut rng);

        // Check extended params have valid values
        assert!(params.terrain_strength >= 0.1 && params.terrain_strength <= 5.0);
        // init_mode and terrain can be any valid variant
    }

    #[test]
    fn test_extended_params_biased() {
        let mut rng = Xoshiro256PlusPlus::seed_from_u64(42);

        // Test each behavior has valid extended params
        for behavior in PresetBehavior::all() {
            let params = ExplorationParams::random_biased(&mut rng, *behavior);
            assert!(params.terrain_strength >= 0.1 && params.terrain_strength <= 5.0);
        }
    }
}