optirs-nas 0.3.2

OptiRS Neural Architecture Search and hyperparameter optimization
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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
//! Hyperparameter optimization for Neural Architecture Search.
//!
//! This module provides hyperparameter optimization capabilities for both
//! the NAS process itself and the discovered architectures.
//!
//! Every strategy [`HyperparameterOptimizer`] dispatches to is a real algorithm:
//!
//! | [`OptimizationStrategy`] | implementation |
//! |---|---|
//! | `Random` | prior sampling, honoring each parameter's [`DistributionType`] |
//! | `Grid` | exhaustive mixed-radix enumeration ([`grid`]) |
//! | `TPE` | two adaptive Parzen estimators over a good/bad quantile split ([`tpe`]) |
//! | `Bayesian` | GP-free kernel-regression surrogate + acquisition maximization ([`surrogate`]) |
//! | `Evolutionary` | tournament selection, crossover and bounded mutation ([`evolution`]) |
//!
//! `Grid`, `Bayesian`, `TPE` and `Evolutionary` previously all returned
//! `self.random_search()`, so selecting any of them silently performed random
//! search. The model-based strategies now require a random *initial design* before
//! they can be fitted (which is part of the published algorithms); that phase is
//! reported through [`HyperparameterOptimizer::last_strategy_used`] instead of
//! being hidden, and [`ParticleSwarm`](OptimizationStrategy::ParticleSwarm),
//! `SuccessiveHalving`, `Hyperband` and `BOHB` — which have no implementation
//! here — return an error rather than quietly becoming random search.

pub mod evolution;
pub mod grid;
pub mod support;
pub mod surrogate;
pub mod tpe;

use crate::error::{OptimError, Result};
use scirs2_core::numeric::Float;
use scirs2_core::random::Random;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

use support::StrategyRng;

/// Hyperparameter optimizer for NAS and discovered architectures
#[derive(Debug)]
pub struct HyperparameterOptimizer<T: Float> {
    /// Search space definition
    search_space: HyperparameterSpace<T>,

    /// Optimization strategy
    strategy: OptimizationStrategy,

    /// Evaluation history
    evaluation_history: Vec<HyperparameterEvaluation<T>>,

    /// Current best configuration
    best_config: Option<HyperparameterConfiguration<T>>,

    /// Optimization state
    state: OptimizerState<T>,

    /// RNG shared by every stochastic strategy, so a seeded optimizer is
    /// reproducible end to end.
    rng: StrategyRng,

    /// Number of points per continuous grid axis.
    grid_resolution: usize,

    /// TPE tunables.
    tpe_config: tpe::TpeConfig,

    /// Surrogate (Bayesian) tunables.
    surrogate_config: surrogate::SurrogateConfig,

    /// Evolutionary tunables.
    evolution_config: evolution::EvolutionConfig,

    /// Acquisition function used by the Bayesian strategy.
    acquisition: AcquisitionFunction,

    /// Which strategy actually produced the most recent suggestion. Model-based
    /// strategies need a random initial design first; this records honestly which
    /// of the two ran instead of leaving the caller to assume.
    last_strategy_used: OptimizationStrategy,
}

/// Hyperparameter search space definition
#[derive(Debug, Clone)]
pub struct HyperparameterSpace<T: Float> {
    /// Individual hyperparameter ranges
    parameters: HashMap<String, ParameterRange<T>>,

    /// Parameter dependencies
    dependencies: Vec<ParameterDependency>,

    /// Constraints on parameter combinations
    constraints: Vec<HyperparameterConstraint<T>>,

    /// Categorical parameters
    categorical_parameters: HashMap<String, Vec<String>>,
}

/// Range specification for a continuous hyperparameter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParameterRange<T: Float> {
    /// Parameter name
    pub name: String,

    /// Minimum value
    pub min_value: T,

    /// Maximum value
    pub max_value: T,

    /// Distribution type for sampling
    pub distribution: DistributionType,

    /// Whether to use log scale
    pub log_scale: bool,

    /// Discrete values (if applicable)
    pub discrete_values: Option<Vec<T>>,
}

/// Type of probability distribution for parameter sampling
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DistributionType {
    /// Uniform distribution
    Uniform,
    /// Normal (Gaussian) distribution
    Normal,
    /// Log-normal distribution
    LogNormal,
    /// Beta distribution
    Beta,
    /// Exponential distribution
    Exponential,
}

/// Dependency between parameters
#[derive(Debug, Clone)]
pub struct ParameterDependency {
    /// Dependent parameter name
    pub dependent: String,

    /// Parent parameter name
    pub parent: String,

    /// Dependency type
    pub dependency_type: DependencyType,

    /// Condition for activation
    pub condition: DependencyCondition,
}

/// Type of parameter dependency
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DependencyType {
    /// Parameter is only active when condition is met
    Conditional,
    /// Parameter value is derived from parent
    Derived,
    /// Parameter range depends on parent value
    RangeDependent,
}

/// Condition for parameter dependency
#[derive(Debug, Clone)]
pub enum DependencyCondition {
    /// Parent equals specific value
    Equals(String),
    /// Parent greater than value
    GreaterThan(f64),
    /// Parent less than value
    LessThan(f64),
    /// Parent in range
    InRange(f64, f64),
    /// Parent in set of values
    InSet(Vec<String>),
}

/// Constraint on hyperparameter combinations
#[derive(Debug, Clone)]
pub struct HyperparameterConstraint<T: Float> {
    /// Constraint name
    pub name: String,

    /// Parameters involved in constraint
    pub parameters: Vec<String>,

    /// Constraint type
    pub constraint_type: ConstraintType<T>,

    /// Violation penalty
    pub penalty: T,
}

/// Type of hyperparameter constraint
#[derive(Debug, Clone)]
pub enum ConstraintType<T: Float> {
    /// Linear constraint: sum(coeffs * params) <= bound
    Linear { coefficients: Vec<T>, bound: T },
    /// Nonlinear constraint with custom function
    NonLinear { function_name: String },
    /// Mutual exclusion: only one parameter can be active
    MutualExclusion,
    /// Ordering constraint: param1 <= param2 <= ... <= paramN
    Ordering,
}

/// Hyperparameter configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HyperparameterConfiguration<T: Float> {
    /// Configuration ID
    pub id: String,

    /// Parameter values
    pub parameters: HashMap<String, T>,

    /// Categorical parameter values
    pub categorical_parameters: HashMap<String, String>,

    /// Configuration score/performance
    pub score: Option<T>,

    /// Evaluation metadata
    pub metadata: HashMap<String, String>,
}

/// Evaluation result for a hyperparameter configuration
#[derive(Debug, Clone)]
pub struct HyperparameterEvaluation<T: Float> {
    /// Configuration that was evaluated
    pub configuration: HyperparameterConfiguration<T>,

    /// Performance metrics
    pub metrics: EvaluationMetrics<T>,

    /// Evaluation duration
    pub duration_seconds: f64,

    /// Success/failure status
    pub status: EvaluationStatus,

    /// Error message (if failed)
    pub error_message: Option<String>,
}

/// Performance metrics from hyperparameter evaluation
#[derive(Debug, Clone)]
pub struct EvaluationMetrics<T: Float> {
    /// Primary objective value
    pub primary_objective: T,

    /// Secondary objectives
    pub secondary_objectives: HashMap<String, T>,

    /// Validation score
    pub validation_score: Option<T>,

    /// Training time
    pub training_time: f64,

    /// Memory usage
    pub memory_usage: f64,

    /// Convergence information
    pub converged: bool,

    /// Number of iterations to convergence
    pub convergence_iterations: Option<u32>,
}

/// Status of hyperparameter evaluation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EvaluationStatus {
    /// Evaluation completed successfully
    Success,
    /// Evaluation failed due to error
    Failed,
    /// Evaluation was terminated early
    Terminated,
    /// Evaluation is still running
    Running,
    /// Evaluation timed out
    Timeout,
}

/// Optimization strategies for hyperparameter search
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptimizationStrategy {
    /// Random search
    Random,
    /// Grid search
    Grid,
    /// Bayesian optimization
    Bayesian,
    /// Evolutionary algorithms
    Evolutionary,
    /// Particle swarm optimization
    ParticleSwarm,
    /// Tree-structured Parzen Estimator (TPE)
    TPE,
    /// Successive halving
    SuccessiveHalving,
    /// Hyperband
    Hyperband,
    /// BOHB (Bayesian Optimization and HyperBand)
    BOHB,
}

/// Internal state of the hyperparameter optimizer
#[derive(Debug)]
pub struct OptimizerState<T: Float> {
    /// Current iteration/generation
    pub iteration: u32,

    /// Number of evaluations performed
    pub num_evaluations: u32,

    /// Best score found so far
    pub best_score: Option<T>,

    /// Population (for evolutionary strategies)
    pub population: Vec<HyperparameterConfiguration<T>>,

    /// Gaussian process model (for Bayesian optimization)
    pub surrogate_model: Option<SurrogateModel<T>>,

    /// Early stopping information
    pub early_stopping: EarlyStoppingState<T>,
}

/// Surrogate model for Bayesian optimization
#[derive(Debug)]
pub struct SurrogateModel<T: Float> {
    /// Model type
    pub model_type: SurrogateModelType,

    /// Training data points
    pub training_data: Vec<(Vec<T>, T)>,

    /// Model hyperparameters
    pub hyperparameters: HashMap<String, f64>,

    /// Acquisition function
    pub acquisition_function: AcquisitionFunction,
}

/// Type of surrogate model
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SurrogateModelType {
    /// Nadaraya-Watson kernel regression — the surrogate this crate actually
    /// implements (see [`surrogate::KernelSurrogate`]). It needs no linear algebra
    /// and no kernel-hyperparameter fitting, which is why it is the default.
    KernelRegression,
    /// Gaussian Process. **Not implemented in this module**; kept because it is
    /// part of the public type and callers may record their own model choice.
    /// [`HyperparameterOptimizer`] never fits one.
    GaussianProcess,
    /// Random Forest. Not implemented here; see [`SurrogateModelType::GaussianProcess`].
    RandomForest,
    /// Neural Network. Not implemented here; see [`SurrogateModelType::GaussianProcess`].
    NeuralNetwork,
    /// Polynomial regression. Not implemented here; see
    /// [`SurrogateModelType::GaussianProcess`].
    Polynomial,
}

/// Acquisition function for Bayesian optimization
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AcquisitionFunction {
    /// Expected Improvement
    ExpectedImprovement,
    /// Upper Confidence Bound
    UpperConfidenceBound,
    /// Probability of Improvement
    ProbabilityOfImprovement,
    /// Entropy Search
    EntropySearch,
}

/// Early stopping state
#[derive(Debug)]
pub struct EarlyStoppingState<T: Float> {
    /// Whether early stopping is enabled
    pub enabled: bool,

    /// Patience (iterations without improvement)
    pub patience: u32,

    /// Current patience counter
    pub patience_counter: u32,

    /// Minimum improvement threshold
    pub min_improvement: T,

    /// Best score for early stopping comparison
    pub best_score_for_stopping: Option<T>,
}

impl<T: Float + Default + Clone + Send + Sync> HyperparameterOptimizer<T> {
    /// Create a new hyperparameter optimizer, seeded from OS entropy.
    pub fn new(search_space: HyperparameterSpace<T>, strategy: OptimizationStrategy) -> Self {
        Self::with_seed(search_space, strategy, scirs2_core::random::random::<u64>())
    }

    /// Create a fully reproducible optimizer.
    pub fn with_seed(
        search_space: HyperparameterSpace<T>,
        strategy: OptimizationStrategy,
        seed: u64,
    ) -> Self {
        Self {
            search_space,
            strategy,
            evaluation_history: Vec::new(),
            best_config: None,
            rng: Random::seed(seed),
            grid_resolution: grid::DEFAULT_GRID_RESOLUTION,
            tpe_config: tpe::TpeConfig::default(),
            surrogate_config: surrogate::SurrogateConfig::default(),
            evolution_config: evolution::EvolutionConfig::default(),
            acquisition: AcquisitionFunction::ExpectedImprovement,
            last_strategy_used: strategy,
            state: OptimizerState {
                iteration: 0,
                num_evaluations: 0,
                best_score: None,
                population: Vec::new(),
                surrogate_model: None,
                early_stopping: EarlyStoppingState {
                    enabled: false,
                    patience: 50,
                    patience_counter: 0,
                    min_improvement: T::from(0.001).unwrap_or_else(T::zero),
                    best_score_for_stopping: None,
                },
            },
        }
    }

    /// Generate the next hyperparameter configuration to evaluate.
    ///
    /// Returns an error for a strategy this module does not implement
    /// ([`OptimizationStrategy::ParticleSwarm`], `SuccessiveHalving`, `Hyperband`,
    /// `BOHB`) rather than silently performing random search, and for a search
    /// space that declares no parameters at all.
    ///
    /// The model-based strategies (`Bayesian`, `TPE`, `Evolutionary`) need a
    /// random initial design first. During that phase they sample from the prior
    /// and set [`HyperparameterOptimizer::last_strategy_used`] to
    /// [`OptimizationStrategy::Random`], so the caller can see which algorithm
    /// actually produced the suggestion.
    pub fn suggest_configuration(&mut self) -> Result<HyperparameterConfiguration<T>> {
        if self.search_space.is_empty() {
            return Err(OptimError::InvalidConfig(
                "hyperparameter search requires a non-empty search space".to_string(),
            ));
        }

        let (parameters, categorical_parameters, used) = match self.strategy {
            OptimizationStrategy::Random => {
                let (parameters, categorical) = self.sample_from_prior();
                (parameters, categorical, OptimizationStrategy::Random)
            }
            OptimizationStrategy::Grid => {
                let index = self.state.num_evaluations as usize;
                let (parameters, categorical) = grid::suggest(
                    &self.search_space,
                    index,
                    self.grid_resolution,
                    &mut self.rng,
                )?;
                (parameters, categorical, OptimizationStrategy::Grid)
            }
            OptimizationStrategy::TPE => {
                let scored = Self::scored_observations(&self.evaluation_history);
                match tpe::suggest(&self.search_space, &scored, &self.tpe_config, &mut self.rng) {
                    Ok((parameters, categorical)) => {
                        (parameters, categorical, OptimizationStrategy::TPE)
                    }
                    Err(_) if scored.len() < self.tpe_config.startup_trials.max(2) => {
                        // Documented initial design: TPE cannot fit two density
                        // estimators before it has observations.
                        drop(scored);
                        let (parameters, categorical) = self.sample_from_prior();
                        (parameters, categorical, OptimizationStrategy::Random)
                    }
                    Err(error) => return Err(error),
                }
            }
            OptimizationStrategy::Bayesian => {
                let scored = Self::scored_observations(&self.evaluation_history);
                match surrogate::suggest(
                    &self.search_space,
                    &scored,
                    self.acquisition,
                    &self.surrogate_config,
                    &mut self.rng,
                ) {
                    Ok((parameters, categorical)) => {
                        (parameters, categorical, OptimizationStrategy::Bayesian)
                    }
                    Err(_) if scored.len() < self.surrogate_config.startup_trials.max(2) => {
                        drop(scored);
                        let (parameters, categorical) = self.sample_from_prior();
                        (parameters, categorical, OptimizationStrategy::Random)
                    }
                    Err(error) => return Err(error),
                }
            }
            OptimizationStrategy::Evolutionary => {
                let population: Vec<(&HyperparameterConfiguration<T>, f64)> = self
                    .state
                    .population
                    .iter()
                    .filter_map(|config| config.score.and_then(|s| s.to_f64()).map(|s| (config, s)))
                    .collect();
                if population.is_empty() {
                    drop(population);
                    let (parameters, categorical) = self.sample_from_prior();
                    (parameters, categorical, OptimizationStrategy::Random)
                } else {
                    let (parameters, categorical) = evolution::breed(
                        &self.search_space,
                        &population,
                        &self.evolution_config,
                        &mut self.rng,
                    )?;
                    (parameters, categorical, OptimizationStrategy::Evolutionary)
                }
            }
            unimplemented => {
                return Err(OptimError::NotImplemented(format!(
                    "hyperparameter strategy {:?} is not implemented in optirs-nas; \
                     available strategies are Random, Grid, TPE, Bayesian and Evolutionary",
                    unimplemented
                )))
            }
        };

        self.last_strategy_used = used;
        Ok(HyperparameterConfiguration {
            id: format!("config_{}", self.state.num_evaluations),
            parameters,
            categorical_parameters,
            score: None,
            metadata: HashMap::from([("strategy".to_string(), format!("{:?}", used))]),
        })
    }

    /// Which strategy produced the most recent suggestion. Differs from
    /// [`HyperparameterOptimizer::strategy`] exactly while a model-based strategy
    /// is still running its random initial design.
    pub fn last_strategy_used(&self) -> OptimizationStrategy {
        self.last_strategy_used
    }

    /// The configured strategy.
    pub fn strategy(&self) -> OptimizationStrategy {
        self.strategy
    }

    /// Number of points per continuous grid axis.
    pub fn grid_resolution(&self) -> usize {
        self.grid_resolution
    }

    /// Set the number of points per continuous grid axis.
    pub fn set_grid_resolution(&mut self, resolution: usize) {
        self.grid_resolution = resolution.max(1);
    }

    /// Total number of distinct grid points, or `None` if the product overflows.
    pub fn grid_size(&self) -> Option<usize> {
        grid::grid_size(&self.search_space, self.grid_resolution)
    }

    /// Choose the acquisition function used by the `Bayesian` strategy.
    pub fn set_acquisition_function(&mut self, acquisition: AcquisitionFunction) {
        self.acquisition = acquisition;
    }

    /// Override the TPE tunables.
    pub fn set_tpe_config(&mut self, config: tpe::TpeConfig) {
        self.tpe_config = config;
    }

    /// Override the Bayesian surrogate tunables.
    pub fn set_surrogate_config(&mut self, config: surrogate::SurrogateConfig) {
        self.surrogate_config = config;
    }

    /// Override the evolutionary tunables.
    pub fn set_evolution_config(&mut self, config: evolution::EvolutionConfig) {
        self.evolution_config = config;
    }

    /// The search space this optimizer explores.
    pub fn get_search_space(&self) -> &HyperparameterSpace<T> {
        &self.search_space
    }

    /// The evolutionary population (fittest first), which is now genuinely read by
    /// [`OptimizationStrategy::Evolutionary`] rather than being write-only state.
    pub fn population(&self) -> &[HyperparameterConfiguration<T>] {
        &self.state.population
    }

    /// The fitted surrogate's recorded training data, or `None` before the
    /// Bayesian strategy has run.
    pub fn surrogate_model(&self) -> Option<&SurrogateModel<T>> {
        self.state.surrogate_model.as_ref()
    }

    /// Scored observations from the evaluation history, newest last.
    fn scored_observations(
        history: &[HyperparameterEvaluation<T>],
    ) -> Vec<(&HyperparameterConfiguration<T>, f64)> {
        history
            .iter()
            .filter(|evaluation| matches!(evaluation.status, EvaluationStatus::Success))
            .filter_map(|evaluation| {
                evaluation
                    .configuration
                    .score
                    .and_then(|score| score.to_f64())
                    .map(|score| (&evaluation.configuration, score))
            })
            .collect()
    }

    /// Record evaluation result
    pub fn record_evaluation(&mut self, evaluation: HyperparameterEvaluation<T>) {
        self.state.num_evaluations += 1;

        // Update best configuration
        if let Some(score) = evaluation.configuration.score {
            let is_better = match self.best_config.as_ref().and_then(|best| best.score) {
                Some(best) => score > best,
                None => true,
            };
            if is_better {
                self.best_config = Some(evaluation.configuration.clone());
                self.state.best_score = Some(score);
            }
        }

        // Update early stopping state
        if self.state.early_stopping.enabled {
            self.update_early_stopping(&evaluation);
        }

        // Store evaluation
        self.evaluation_history.push(evaluation);

        // Update strategy-specific state
        self.update_strategy_state();
    }

    /// Check if optimization should stop early
    pub fn should_stop_early(&self) -> bool {
        if !self.state.early_stopping.enabled {
            return false;
        }

        self.state.early_stopping.patience_counter >= self.state.early_stopping.patience
    }

    /// Get the best configuration found so far
    pub fn get_best_configuration(&self) -> Option<&HyperparameterConfiguration<T>> {
        self.best_config.as_ref()
    }

    /// Get optimization statistics
    pub fn get_statistics(&self) -> OptimizationStatistics<T> {
        let scores: Vec<T> = self
            .evaluation_history
            .iter()
            .filter_map(|eval| eval.configuration.score)
            .collect();

        let mean_score = if scores.is_empty() {
            T::zero()
        } else {
            scores.iter().fold(T::zero(), |acc, &x| acc + x)
                / T::from(scores.len()).unwrap_or_else(|| T::one())
        };

        OptimizationStatistics {
            num_evaluations: self.state.num_evaluations,
            best_score: self.state.best_score,
            mean_score: Some(mean_score),
            num_successful_evaluations: self
                .evaluation_history
                .iter()
                .filter(|eval| matches!(eval.status, EvaluationStatus::Success))
                .count() as u32,
            convergence_iteration: self.get_convergence_iteration(),
        }
    }

    // Private methods for different optimization strategies

    /// Draw one value per parameter from its declared prior. Shared by the
    /// `Random` strategy and by the initial-design phase of the model-based
    /// strategies.
    ///
    /// Unlike the previous version this honors every [`DistributionType`] (the old
    /// code had a `_ => uniform` arm covering four of five variants), respects
    /// `discrete_values`, and contains no `expect`: a value that cannot be
    /// converted falls back to the range's own bound rather than aborting.
    fn sample_from_prior(&mut self) -> (HashMap<String, T>, HashMap<String, String>) {
        let mut parameters = HashMap::new();
        let mut categorical_parameters = HashMap::new();

        for name in support::sorted_parameter_names(&self.search_space) {
            let Some(range) = self.search_space.parameters.get(&name).cloned() else {
                continue;
            };
            let raw =
                support::snap_to_discrete(&range, support::sample_parameter(&range, &mut self.rng));
            parameters.insert(
                name,
                scirs2_core::numeric::NumCast::from(raw).unwrap_or(range.min_value),
            );
        }

        for name in support::sorted_categorical_names(&self.search_space) {
            let Some(categories) = self.search_space.categorical_parameters.get(&name).cloned()
            else {
                continue;
            };
            if categories.is_empty() {
                continue;
            }
            let idx = self.rng.gen_range(0..categories.len());
            categorical_parameters.insert(name, categories[idx].clone());
        }

        (parameters, categorical_parameters)
    }

    fn update_early_stopping(&mut self, evaluation: &HyperparameterEvaluation<T>) {
        if let Some(score) = evaluation.configuration.score {
            if let Some(best_score) = self.state.early_stopping.best_score_for_stopping {
                if score > best_score + self.state.early_stopping.min_improvement {
                    self.state.early_stopping.best_score_for_stopping = Some(score);
                    self.state.early_stopping.patience_counter = 0;
                } else {
                    self.state.early_stopping.patience_counter += 1;
                }
            } else {
                self.state.early_stopping.best_score_for_stopping = Some(score);
            }
        }
    }

    /// Refresh the state each strategy reads between suggestions.
    ///
    /// * The evolutionary population becomes a real, bounded, elitist pool
    ///   (previously it was filled once with random configurations and never read).
    /// * The Bayesian surrogate's recorded training data is rebuilt from the scored
    ///   history, so `state.surrogate_model` stops being permanently `None`.
    fn update_strategy_state(&mut self) {
        self.state.iteration += 1;

        match self.strategy {
            OptimizationStrategy::Evolutionary => {
                let scored: Vec<HyperparameterConfiguration<T>> = self
                    .evaluation_history
                    .iter()
                    .filter(|evaluation| {
                        matches!(evaluation.status, EvaluationStatus::Success)
                            && evaluation.configuration.score.is_some()
                    })
                    .map(|evaluation| evaluation.configuration.clone())
                    .collect();
                self.state.population = scored;
                evolution::survivor_selection(&mut self.state.population, &self.evolution_config);
            }
            OptimizationStrategy::Bayesian => {
                let continuous = support::sorted_parameter_names(&self.search_space);
                let training_data: Vec<(Vec<T>, T)> = self
                    .evaluation_history
                    .iter()
                    .filter_map(|evaluation| {
                        let score = evaluation.configuration.score?;
                        let vector = continuous
                            .iter()
                            .map(|name| {
                                evaluation
                                    .configuration
                                    .parameters
                                    .get(name)
                                    .copied()
                                    .unwrap_or_else(T::zero)
                            })
                            .collect();
                        Some((vector, score))
                    })
                    .collect();
                let mut hyperparameters = HashMap::new();
                hyperparameters.insert("bandwidth".to_string(), self.surrogate_config.bandwidth);
                hyperparameters.insert("ucb_beta".to_string(), self.surrogate_config.ucb_beta);
                self.state.surrogate_model = Some(SurrogateModel {
                    model_type: surrogate::IMPLEMENTED_SURROGATE,
                    training_data,
                    hyperparameters,
                    acquisition_function: self.acquisition,
                });
            }
            _ => {}
        }
    }

    fn get_convergence_iteration(&self) -> Option<u32> {
        // Simple convergence detection based on improvement rate
        let window_size = 10;
        if self.evaluation_history.len() < window_size * 2 {
            return None;
        }

        let recent_scores: Vec<T> = self
            .evaluation_history
            .iter()
            .rev()
            .take(window_size)
            .filter_map(|eval| eval.configuration.score)
            .collect();

        let older_scores: Vec<T> = self
            .evaluation_history
            .iter()
            .rev()
            .skip(window_size)
            .take(window_size)
            .filter_map(|eval| eval.configuration.score)
            .collect();

        if recent_scores.len() == window_size && older_scores.len() == window_size {
            let recent_mean = recent_scores.iter().fold(T::zero(), |acc, &x| acc + x)
                / T::from(window_size).unwrap_or_else(|| T::one());
            let older_mean = older_scores.iter().fold(T::zero(), |acc, &x| acc + x)
                / T::from(window_size).unwrap_or_else(|| T::one());

            let improvement = recent_mean - older_mean;
            let threshold = T::from(0.001).unwrap_or_else(T::zero);

            if improvement < threshold {
                return Some(self.state.iteration - window_size as u32);
            }
        }

        None
    }
}

/// Statistics about the optimization process
#[derive(Debug, Clone)]
pub struct OptimizationStatistics<T: Float> {
    /// Total number of evaluations
    pub num_evaluations: u32,

    /// Best score achieved
    pub best_score: Option<T>,

    /// Mean score across all evaluations
    pub mean_score: Option<T>,

    /// Number of successful evaluations
    pub num_successful_evaluations: u32,

    /// Iteration at which convergence was detected
    pub convergence_iteration: Option<u32>,
}

impl<T: Float> HyperparameterSpace<T> {
    /// Create a new hyperparameter search space
    pub fn new() -> Self {
        Self {
            parameters: HashMap::new(),
            dependencies: Vec::new(),
            constraints: Vec::new(),
            categorical_parameters: HashMap::new(),
        }
    }

    /// Continuous parameter ranges, keyed by name.
    pub fn parameter_ranges(&self) -> &HashMap<String, ParameterRange<T>> {
        &self.parameters
    }

    /// Categorical parameter option lists, keyed by name.
    pub fn categorical_options(&self) -> &HashMap<String, Vec<String>> {
        &self.categorical_parameters
    }

    /// Declared parameter dependencies.
    pub fn dependencies(&self) -> &[ParameterDependency] {
        &self.dependencies
    }

    /// Declared cross-parameter constraints.
    pub fn constraints(&self) -> &[HyperparameterConstraint<T>] {
        &self.constraints
    }

    /// Whether the space declares no parameters at all.
    pub fn is_empty(&self) -> bool {
        self.parameters.is_empty() && self.categorical_parameters.is_empty()
    }

    /// Add a continuous parameter to the search space
    pub fn add_parameter(&mut self, name: String, range: ParameterRange<T>) {
        self.parameters.insert(name, range);
    }

    /// Add a categorical parameter to the search space
    pub fn add_categorical_parameter(&mut self, name: String, categories: Vec<String>) {
        self.categorical_parameters.insert(name, categories);
    }

    /// Add a dependency between parameters
    pub fn add_dependency(&mut self, dependency: ParameterDependency) {
        self.dependencies.push(dependency);
    }

    /// Add a constraint on parameter combinations
    pub fn add_constraint(&mut self, constraint: HyperparameterConstraint<T>) {
        self.constraints.push(constraint);
    }

    /// Validate a configuration against the search space
    pub fn validate_configuration(&self, config: &HyperparameterConfiguration<T>) -> bool {
        // Check parameter ranges
        for (name, value) in &config.parameters {
            if let Some(range) = self.parameters.get(name) {
                if *value < range.min_value || *value > range.max_value {
                    return false;
                }
            }
        }

        // Check categorical parameters
        for (name, value) in &config.categorical_parameters {
            if let Some(categories) = self.categorical_parameters.get(name) {
                if !categories.contains(value) {
                    return false;
                }
            }
        }

        // Check constraints
        for constraint in &self.constraints {
            if !self.check_constraint(constraint, config) {
                return false;
            }
        }

        true
    }

    fn check_constraint(
        &self,
        constraint: &HyperparameterConstraint<T>,
        config: &HyperparameterConfiguration<T>,
    ) -> bool {
        match &constraint.constraint_type {
            ConstraintType::Linear {
                coefficients,
                bound,
            } => {
                let mut sum = T::zero();
                for (i, param_name) in constraint.parameters.iter().enumerate() {
                    if let Some(value) = config.parameters.get(param_name) {
                        sum = sum + coefficients[i] * *value;
                    }
                }
                sum <= *bound
            }
            ConstraintType::MutualExclusion => {
                let active_count = constraint
                    .parameters
                    .iter()
                    .filter(|param| config.parameters.contains_key(*param))
                    .count();
                active_count <= 1
            }
            ConstraintType::Ordering => {
                let mut values = Vec::new();
                for param_name in &constraint.parameters {
                    if let Some(value) = config.parameters.get(param_name) {
                        values.push(*value);
                    }
                }

                for i in 1..values.len() {
                    if values[i - 1] > values[i] {
                        return false;
                    }
                }
                true
            }
            ConstraintType::NonLinear { .. } => {
                // Custom constraint functions would be implemented here
                true
            }
        }
    }
}

impl<T: Float> Default for HyperparameterSpace<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Float + fmt::Debug> fmt::Display for HyperparameterConfiguration<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "Configuration: {}", self.id)?;
        for (name, value) in &self.parameters {
            writeln!(f, "  {}: {:?}", name, value)?;
        }
        for (name, value) in &self.categorical_parameters {
            writeln!(f, "  {}: {}", name, value)?;
        }
        if let Some(score) = self.score {
            writeln!(f, "  Score: {:?}", score)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests;