surface_lib/calibration/
config.rs

1use crate::calibration::types::FixedParameters;
2use serde::Deserialize;
3
4/// CMA-ES specific configuration parameters
5#[derive(Debug, Clone, Deserialize)]
6pub struct CmaEsConfig {
7    /// Random seed for reproducibility
8    pub seed: Option<u64>,
9    /// Whether to evaluate the population in parallel
10    pub parallel_eval: bool,
11    /// Verbosity level (0=silent, 1=minimal, 2=normal)
12    pub verbosity: u8,
13    /// Number of IPOP restarts (0 = no IPOP)
14    pub ipop_restarts: usize,
15    /// Factor to increase population size in IPOP restarts
16    pub ipop_increase_factor: f64,
17    /// Max function evaluations per run (0=unlimited)
18    pub max_evaluations: usize,
19    /// Initial coordinate-wise standard deviation
20    pub sigma0: f64,
21    /// Number of BIPOP restarts (0 = no BIPOP)
22    pub bipop_restarts: usize,
23    /// Enable L-BFGS-B refinement after CMA-ES?
24    pub lbfgsb_enabled: bool,
25    /// Max iterations for L-BFGS-B
26    pub lbfgsb_max_iterations: usize,
27    /// Total function evaluations budget
28    pub total_evals_budget: usize,
29    /// Whether to use advanced sub-run budgeting logic
30    pub use_subrun_budgeting: bool,
31    /// Use mini CMA-ES on refinement
32    pub mini_cmaes_on_refinement: bool,
33}
34
35impl Default for CmaEsConfig {
36    fn default() -> Self {
37        Self {
38            seed: Some(123456),
39            parallel_eval: true,
40            verbosity: 0, // Silent by default for library use
41            ipop_restarts: 0,
42            ipop_increase_factor: 2.0,
43            max_evaluations: 100000,
44            sigma0: 0.3,
45            bipop_restarts: 5,
46            lbfgsb_enabled: true,
47            lbfgsb_max_iterations: 200,
48            total_evals_budget: 200000,
49            use_subrun_budgeting: false,
50            mini_cmaes_on_refinement: true,
51        }
52    }
53}
54
55#[derive(Debug, Clone, Deserialize)]
56pub struct AdaptiveBoundsConfig {
57    pub enabled: bool,
58    pub max_iterations: usize,
59    pub proximity_threshold: f64,
60    pub expansion_factor: f64,
61}
62
63impl Default for AdaptiveBoundsConfig {
64    fn default() -> Self {
65        Self {
66            enabled: false,
67            max_iterations: 3,
68            proximity_threshold: 0.1, // 10% from edge
69            expansion_factor: 0.25,   // expand by 25%
70        }
71    }
72}
73
74/// Main configuration struct for optimization
75#[derive(Debug, Deserialize, Clone)]
76pub struct OptimizationConfig {
77    #[serde(default = "default_max_iterations")]
78    pub max_iterations: usize,
79
80    #[serde(default = "default_tolerance")]
81    pub tolerance: f64,
82
83    #[serde(default)]
84    pub fixed_params: FixedParameters,
85
86    /// Population size for genetic algorithms
87    #[serde(default = "default_pop_size")]
88    pub pop_size: usize,
89
90    /// Maximum generations for evolutionary algorithms
91    #[serde(default = "default_max_gen")]
92    pub max_gen: usize,
93
94    /// Objective tolerance
95    #[serde(default = "default_obj_tol")]
96    pub obj_tol: f64,
97
98    /// Covariance matrix adaptation parameter
99    #[serde(default = "default_alpha_cov")]
100    pub alpha_cov: f64,
101
102    /// Step size adaptation parameter
103    #[serde(default = "default_alpha_sigma")]
104    pub alpha_sigma: f64,
105
106    /// Target success rate
107    #[serde(default = "default_target_sr")]
108    pub target_sr: f64,
109
110    /// CMA-ES specific configuration
111    #[serde(default)]
112    pub cmaes: CmaEsConfig,
113
114    /// Adaptive bounds configuration
115    #[serde(default)]
116    pub adaptive_bounds: AdaptiveBoundsConfig,
117}
118
119impl Default for OptimizationConfig {
120    fn default() -> Self {
121        Self {
122            max_iterations: default_max_iterations(),
123            tolerance: default_tolerance(),
124            fixed_params: FixedParameters::default(),
125            pop_size: default_pop_size(),
126            max_gen: default_max_gen(),
127            obj_tol: default_obj_tol(),
128            alpha_cov: default_alpha_cov(),
129            alpha_sigma: default_alpha_sigma(),
130            target_sr: default_target_sr(),
131            cmaes: CmaEsConfig::default(),
132            adaptive_bounds: AdaptiveBoundsConfig::default(),
133        }
134    }
135}
136
137impl OptimizationConfig {
138    /// Default configuration for production calibration with high accuracy
139    pub fn production() -> Self {
140        Self {
141            max_iterations: 1000,
142            tolerance: 1e-8,
143            fixed_params: FixedParameters::default(),
144            pop_size: 25,
145            max_gen: 50,
146            obj_tol: 1e-8,
147            alpha_cov: 0.2,
148            alpha_sigma: 0.5,
149            target_sr: 0.2,
150            cmaes: CmaEsConfig {
151                verbosity: 0, // Slightly more verbose for production monitoring
152                max_evaluations: 100000,
153                total_evals_budget: 200000,
154                ..CmaEsConfig::default()
155            },
156            adaptive_bounds: AdaptiveBoundsConfig::default(),
157        }
158    }
159
160    /// Fast configuration for development and testing
161    pub fn fast() -> Self {
162        Self {
163            max_iterations: 1000,
164            tolerance: 1e-6,
165            fixed_params: FixedParameters::default(),
166            pop_size: 30,
167            max_gen: 50,
168            obj_tol: 1e-6,
169            alpha_cov: 0.2,
170            alpha_sigma: 0.5,
171            target_sr: 0.2,
172            cmaes: CmaEsConfig {
173                verbosity: 2,
174                max_evaluations: 10000,
175                total_evals_budget: 20000,
176                bipop_restarts: 2,
177                ..CmaEsConfig::default()
178            },
179            adaptive_bounds: AdaptiveBoundsConfig::default(),
180        }
181    }
182
183    /// High-precision configuration for research and backtesting
184    pub fn research() -> Self {
185        Self {
186            max_iterations: 10000,
187            tolerance: 1e-9,
188            fixed_params: FixedParameters::default(),
189            pop_size: 100,
190            max_gen: 200,
191            obj_tol: 1e-9,
192            alpha_cov: 0.15,
193            alpha_sigma: 0.3,
194            target_sr: 0.15,
195            cmaes: CmaEsConfig {
196                verbosity: 1,
197                max_evaluations: 500000,
198                total_evals_budget: 1000000,
199                bipop_restarts: 5,
200                ipop_restarts: 3,
201                ..CmaEsConfig::default()
202            },
203            adaptive_bounds: AdaptiveBoundsConfig::default(),
204        }
205    }
206
207    /// Minimal configuration for quick validation and debugging
208    pub fn minimal() -> Self {
209        Self {
210            max_iterations: 100,
211            tolerance: 1e-4,
212            fixed_params: FixedParameters::default(),
213            pop_size: 10,
214            max_gen: 20,
215            obj_tol: 1e-4,
216            alpha_cov: 0.3,
217            alpha_sigma: 0.7,
218            target_sr: 0.3,
219            cmaes: CmaEsConfig {
220                verbosity: 0,
221                max_evaluations: 1000,
222                total_evals_budget: 2000,
223                bipop_restarts: 1,
224                ..CmaEsConfig::default()
225            },
226            adaptive_bounds: AdaptiveBoundsConfig::default(),
227        }
228    }
229}
230
231fn default_max_iterations() -> usize {
232    5000
233}
234
235fn default_tolerance() -> f64 {
236    1e-6
237}
238
239fn default_pop_size() -> usize {
240    50
241}
242
243fn default_max_gen() -> usize {
244    100
245}
246
247fn default_obj_tol() -> f64 {
248    1e-8
249}
250
251fn default_alpha_cov() -> f64 {
252    0.2
253}
254
255fn default_alpha_sigma() -> f64 {
256    0.5
257}
258
259fn default_target_sr() -> f64 {
260    0.2
261}