scirs2-cluster 0.4.1

Clustering algorithms module for SciRS2 (scirs2-cluster)
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
//! Parameter generation strategies for hyperparameter optimization
//!
//! This module implements various parameter generation strategies including
//! grid search, random search, Bayesian optimization, and evolutionary approaches.

use scirs2_core::ndarray::Array2;
use scirs2_core::random::{rng, Rng, RngExt, SeedableRng};
use std::collections::HashMap;

use crate::error::{ClusteringError, Result};

use super::bayesian_optimization::BayesianOptimizer;
use super::config::*;

/// Parameter combination generator for different search strategies
pub struct ParameterGenerator {
    config: TuningConfig,
}

impl ParameterGenerator {
    /// Create a new parameter generator
    pub fn new(config: &TuningConfig) -> Self {
        Self {
            config: config.clone(),
        }
    }

    /// Generate parameter combinations based on search strategy
    pub fn generate_combinations(
        &self,
        search_space: &SearchSpace,
    ) -> Result<Vec<HashMap<String, f64>>> {
        match &self.config.strategy {
            SearchStrategy::GridSearch => self.generate_grid_combinations(search_space),
            SearchStrategy::RandomSearch { n_trials } => {
                self.generate_random_combinations(search_space, *n_trials)
            }
            SearchStrategy::BayesianOptimization {
                n_initial_points,
                acquisition_function,
            } => self.generate_bayesian_combinations(
                search_space,
                *n_initial_points,
                acquisition_function,
            ),
            SearchStrategy::EnsembleSearch {
                strategies,
                weights,
            } => self.generate_ensemble_combinations(search_space, strategies, weights),
            SearchStrategy::EvolutionarySearch {
                population_size,
                n_generations,
                mutation_rate,
                crossover_rate,
            } => self.generate_evolutionary_combinations(
                search_space,
                *population_size,
                *n_generations,
                *mutation_rate,
                *crossover_rate,
            ),
            SearchStrategy::SMBO {
                surrogate_model,
                acquisition_function,
            } => {
                self.generate_smbo_combinations(search_space, surrogate_model, acquisition_function)
            }
            SearchStrategy::MultiObjective {
                objectives,
                strategy,
            } => self.generate_multi_objective_combinations(search_space, objectives, strategy),
            SearchStrategy::AdaptiveSearch {
                initial_strategy, ..
            } => match initial_strategy.as_ref() {
                SearchStrategy::RandomSearch { n_trials } => {
                    self.generate_random_combinations(search_space, *n_trials)
                }
                SearchStrategy::GridSearch => self.generate_grid_combinations(search_space),
                _ => self.generate_random_combinations(search_space, self.config.max_evaluations),
            },
        }
    }

    /// Generate grid search combinations
    pub fn generate_grid_combinations(
        &self,
        search_space: &SearchSpace,
    ) -> Result<Vec<HashMap<String, f64>>> {
        let mut combinations = Vec::new();
        let mut param_names = Vec::new();
        let mut param_values = Vec::new();

        for (name, param) in &search_space.parameters {
            param_names.push(name.clone());
            match param {
                HyperParameter::Integer { min, max } => {
                    let values: Vec<f64> = (*min..=*max).map(|x| x as f64).collect();
                    param_values.push(values);
                }
                HyperParameter::Float { min, max } => {
                    let n_steps = 10;
                    let step = (max - min) / (n_steps as f64 - 1.0);
                    let values: Vec<f64> = (0..n_steps).map(|i| min + i as f64 * step).collect();
                    param_values.push(values);
                }
                HyperParameter::Categorical { choices } => {
                    let values: Vec<f64> = (0..choices.len()).map(|i| i as f64).collect();
                    param_values.push(values);
                }
                HyperParameter::Boolean => {
                    param_values.push(vec![0.0, 1.0]);
                }
                HyperParameter::LogUniform { min, max } => {
                    let n_steps = 10;
                    let log_min = min.ln();
                    let log_max = max.ln();
                    let step = (log_max - log_min) / (n_steps as f64 - 1.0);
                    let values: Vec<f64> = (0..n_steps)
                        .map(|i| (log_min + i as f64 * step).exp())
                        .collect();
                    param_values.push(values);
                }
                HyperParameter::IntegerChoices { choices } => {
                    let values: Vec<f64> = choices.iter().map(|&x| x as f64).collect();
                    param_values.push(values);
                }
            }
        }

        self.generate_cartesian_product(
            &param_names,
            &param_values,
            &mut combinations,
            Vec::new(),
            0,
        );

        Ok(combinations)
    }

    /// Generate cartesian product of parameter values
    fn generate_cartesian_product(
        &self,
        param_names: &[String],
        param_values: &[Vec<f64>],
        combinations: &mut Vec<HashMap<String, f64>>,
        current: Vec<f64>,
        index: usize,
    ) {
        if index == param_names.len() {
            let mut combination = HashMap::new();
            for (i, name) in param_names.iter().enumerate() {
                combination.insert(name.clone(), current[i]);
            }
            combinations.push(combination);
            return;
        }

        for &value in &param_values[index] {
            let mut new_current = current.clone();
            new_current.push(value);
            self.generate_cartesian_product(
                param_names,
                param_values,
                combinations,
                new_current,
                index + 1,
            );
        }
    }

    /// Generate random search combinations
    pub fn generate_random_combinations(
        &self,
        search_space: &SearchSpace,
        n_trials: usize,
    ) -> Result<Vec<HashMap<String, f64>>> {
        let mut combinations = Vec::new();
        let mut rng = match self.config.random_seed {
            Some(seed) => scirs2_core::random::rngs::StdRng::seed_from_u64(seed),
            None => scirs2_core::random::rngs::StdRng::seed_from_u64(42),
        };

        for _ in 0..n_trials {
            let mut combination = HashMap::new();

            for (name, param) in &search_space.parameters {
                let value = match param {
                    HyperParameter::Integer { min, max } => rng.random_range(*min..=*max) as f64,
                    HyperParameter::Float { min, max } => rng.random_range(*min..=*max),
                    HyperParameter::Categorical { choices } => {
                        rng.random_range(0..choices.len()) as f64
                    }
                    HyperParameter::Boolean => {
                        if rng.random_range(0.0..1.0) < 0.5 {
                            1.0
                        } else {
                            0.0
                        }
                    }
                    HyperParameter::LogUniform { min, max } => {
                        let log_min = min.ln();
                        let log_max = max.ln();
                        let log_value = rng.random_range(log_min..=log_max);
                        log_value.exp()
                    }
                    HyperParameter::IntegerChoices { choices } => {
                        let idx = rng.random_range(0..choices.len());
                        choices[idx] as f64
                    }
                };

                combination.insert(name.clone(), value);
            }

            combinations.push(combination);
        }

        Ok(combinations)
    }

    /// Generate Bayesian optimization combinations
    pub fn generate_bayesian_combinations(
        &self,
        search_space: &SearchSpace,
        n_initial_points: usize,
        acquisition_function: &AcquisitionFunction,
    ) -> Result<Vec<HashMap<String, f64>>> {
        let mut combinations = Vec::new();
        let parameter_names: Vec<String> = search_space.parameters.keys().cloned().collect();

        let mut bayesian_optimizer = BayesianOptimizer::new(
            parameter_names.clone(),
            acquisition_function.clone(),
            self.config.random_seed,
        );

        // Generate initial random points
        let initial_points = self.generate_random_combinations(search_space, n_initial_points)?;
        combinations.extend(initial_points);

        // Generate remaining points using Bayesian optimization
        let remaining_points = self.config.max_evaluations.saturating_sub(n_initial_points);

        for _ in 0..remaining_points {
            bayesian_optimizer.update_observations(&combinations);
            let next_point = bayesian_optimizer.optimize_acquisition_function(search_space)?;
            combinations.push(next_point);
        }

        Ok(combinations)
    }

    /// Generate ensemble search combinations
    pub fn generate_ensemble_combinations(
        &self,
        search_space: &SearchSpace,
        strategies: &[SearchStrategy],
        weights: &[f64],
    ) -> Result<Vec<HashMap<String, f64>>> {
        let mut all_combinations = Vec::new();
        let total_evaluations = self.config.max_evaluations;

        let weight_sum: f64 = weights.iter().sum();
        let normalized_weights: Vec<f64> = weights.iter().map(|w| w / weight_sum).collect();

        for (strategy, &weight) in strategies.iter().zip(normalized_weights.iter()) {
            let n_evaluations = (total_evaluations as f64 * weight) as usize;

            let strategy_combinations = match strategy {
                SearchStrategy::RandomSearch { .. } => {
                    self.generate_random_combinations(search_space, n_evaluations)?
                }
                SearchStrategy::GridSearch => {
                    let grid_combinations = self.generate_grid_combinations(search_space)?;
                    grid_combinations.into_iter().take(n_evaluations).collect()
                }
                _ => self.generate_random_combinations(search_space, n_evaluations)?,
            };

            all_combinations.extend(strategy_combinations);
        }

        let mut rng = match self.config.random_seed {
            Some(seed) => scirs2_core::random::rngs::StdRng::seed_from_u64(seed),
            None => scirs2_core::random::rngs::StdRng::seed_from_u64(42),
        };

        use scirs2_core::random::seq::SliceRandom;
        all_combinations.shuffle(&mut rng);

        Ok(all_combinations)
    }

    /// Generate evolutionary search combinations
    pub fn generate_evolutionary_combinations(
        &self,
        search_space: &SearchSpace,
        population_size: usize,
        n_generations: usize,
        mutation_rate: f64,
        crossover_rate: f64,
    ) -> Result<Vec<HashMap<String, f64>>> {
        let mut all_combinations = Vec::new();
        let mut rng = match self.config.random_seed {
            Some(seed) => scirs2_core::random::rngs::StdRng::seed_from_u64(seed),
            None => scirs2_core::random::rngs::StdRng::seed_from_u64(42),
        };

        // Generate initial population
        let mut population = self.generate_random_combinations(search_space, population_size)?;
        all_combinations.extend(population.clone());

        // Evolve population
        for _generation in 0..n_generations {
            let mut new_population = Vec::new();

            while new_population.len() < population_size {
                // Tournament selection
                let parent1 = self.tournament_selection(&population, &mut rng);
                let parent2 = self.tournament_selection(&population, &mut rng);

                // Crossover
                let mut offspring = if rng.random_range(0.0..1.0) < crossover_rate {
                    self.crossover(&parent1, &parent2, &mut rng)
                } else {
                    parent1.clone()
                };

                // Mutation
                if rng.random_range(0.0..1.0) < mutation_rate {
                    self.mutate(&mut offspring, search_space, &mut rng)?;
                }

                new_population.push(offspring);
            }

            population = new_population;
            all_combinations.extend(population.clone());
        }

        Ok(all_combinations)
    }

    /// Tournament selection for evolutionary algorithm
    fn tournament_selection(
        &self,
        population: &[HashMap<String, f64>],
        rng: &mut scirs2_core::random::rngs::StdRng,
    ) -> HashMap<String, f64> {
        let tournament_size = 3.min(population.len());
        let mut best = &population[rng.random_range(0..population.len())];

        for _ in 1..tournament_size {
            let candidate = &population[rng.random_range(0..population.len())];
            // In practice, we'd need fitness scores to compare
            // For now, just return the first candidate
            best = candidate;
        }

        best.clone()
    }

    /// Crossover operation for evolutionary algorithm
    fn crossover(
        &self,
        parent1: &HashMap<String, f64>,
        parent2: &HashMap<String, f64>,
        rng: &mut scirs2_core::random::rngs::StdRng,
    ) -> HashMap<String, f64> {
        let mut offspring = HashMap::new();

        for (key, value1) in parent1 {
            if let Some(value2) = parent2.get(key) {
                // Uniform crossover
                let offspring_value = if rng.random_range(0.0..1.0) < 0.5 {
                    *value1
                } else {
                    *value2
                };
                offspring.insert(key.clone(), offspring_value);
            } else {
                offspring.insert(key.clone(), *value1);
            }
        }

        offspring
    }

    /// Mutation operation for evolutionary algorithm
    fn mutate(
        &self,
        individual: &mut HashMap<String, f64>,
        search_space: &SearchSpace,
        rng: &mut scirs2_core::random::rngs::StdRng,
    ) -> Result<()> {
        for (param_name, param_def) in &search_space.parameters {
            if let Some(value) = individual.get_mut(param_name) {
                // Gaussian mutation with parameter-dependent variance
                let mutation_strength = match param_def {
                    HyperParameter::Float { min, max } => (max - min) * 0.1,
                    HyperParameter::Integer { min, max } => (*max - *min) as f64 * 0.1,
                    _ => 0.1,
                };

                let noise = rng.random_range(-mutation_strength..mutation_strength);
                let new_value = *value + noise;

                // Clip to parameter bounds
                *value = match param_def {
                    HyperParameter::Float { min, max } => new_value.clamp(*min, *max),
                    HyperParameter::Integer { min, max } => {
                        (new_value.round() as i64).clamp(*min, *max) as f64
                    }
                    HyperParameter::LogUniform { min, max } => new_value.clamp(*min, *max),
                    _ => new_value,
                };
            }
        }

        Ok(())
    }

    /// Generate SMBO (Sequential Model-Based Optimization) combinations
    pub fn generate_smbo_combinations(
        &self,
        search_space: &SearchSpace,
        surrogate_model: &SurrogateModel,
        acquisition_function: &AcquisitionFunction,
    ) -> Result<Vec<HashMap<String, f64>>> {
        let n_initial_points = 10.max(search_space.parameters.len() * 2);
        let mut combinations = Vec::new();

        let initial_points = self.generate_random_combinations(search_space, n_initial_points)?;
        combinations.extend(initial_points);

        let remaining_points = self.config.max_evaluations.saturating_sub(n_initial_points);

        for _iteration in 0..remaining_points {
            let next_point = match surrogate_model {
                SurrogateModel::GaussianProcess { .. } => {
                    let parameter_names: Vec<String> =
                        search_space.parameters.keys().cloned().collect();
                    let mut bayesian_optimizer = BayesianOptimizer::new(
                        parameter_names,
                        acquisition_function.clone(),
                        self.config.random_seed,
                    );
                    bayesian_optimizer.update_observations(&combinations);
                    bayesian_optimizer.optimize_acquisition_function(search_space)?
                }
                SurrogateModel::RandomForest { .. } => {
                    self.generate_rf_guided_point(search_space, &combinations)?
                }
                SurrogateModel::GradientBoosting { .. } => {
                    self.generate_gb_guided_point(search_space, &combinations)?
                }
            };

            combinations.push(next_point);
        }

        Ok(combinations)
    }

    /// Generate point guided by Random Forest surrogate model
    fn generate_rf_guided_point(
        &self,
        search_space: &SearchSpace,
        existing_combinations: &[HashMap<String, f64>],
    ) -> Result<HashMap<String, f64>> {
        if existing_combinations.is_empty() {
            return self
                .generate_random_combinations(search_space, 1)
                .map(|mut v| v.pop().unwrap_or_default());
        }

        let mut promising_point = HashMap::new();

        for (param_name, param_def) in &search_space.parameters {
            let values: Vec<f64> = existing_combinations
                .iter()
                .filter_map(|c| c.get(param_name))
                .copied()
                .collect();

            if values.is_empty() {
                continue;
            }

            let mean = values.iter().sum::<f64>() / values.len() as f64;
            let variance =
                values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64;

            let suggested_value = match param_def {
                HyperParameter::Float { min, max } => {
                    let mut rng = scirs2_core::random::rng();
                    let noise = rng.random_range(-variance.sqrt()..variance.sqrt());
                    (mean + noise).clamp(*min, *max)
                }
                HyperParameter::Integer { min, max } => {
                    let mut rng = scirs2_core::random::rng();
                    rng.random_range(*min..=*max) as f64
                }
                _ => mean,
            };

            promising_point.insert(param_name.clone(), suggested_value);
        }

        Ok(promising_point)
    }

    /// Generate point guided by Gradient Boosting surrogate model
    fn generate_gb_guided_point(
        &self,
        search_space: &SearchSpace,
        existing_combinations: &[HashMap<String, f64>],
    ) -> Result<HashMap<String, f64>> {
        // Simplified implementation - similar to Random Forest for now
        self.generate_rf_guided_point(search_space, existing_combinations)
    }

    /// Multi-objective optimization using Pareto frontier
    fn generate_multi_objective_combinations(
        &self,
        search_space: &SearchSpace,
        _objectives: &[EvaluationMetric],
        base_strategy: &SearchStrategy,
    ) -> Result<Vec<HashMap<String, f64>>> {
        let base_combinations = match base_strategy {
            SearchStrategy::RandomSearch { n_trials } => {
                self.generate_random_combinations(search_space, *n_trials)?
            }
            SearchStrategy::GridSearch => self.generate_grid_combinations(search_space)?,
            SearchStrategy::BayesianOptimization {
                n_initial_points,
                acquisition_function,
            } => self.generate_bayesian_combinations(
                search_space,
                *n_initial_points,
                acquisition_function,
            )?,
            _ => self.generate_random_combinations(search_space, self.config.max_evaluations)?,
        };

        let mut diverse_combinations = base_combinations;

        let additional_random = self.generate_random_combinations(
            search_space,
            (self.config.max_evaluations / 4).max(10),
        )?;
        diverse_combinations.extend(additional_random);

        Ok(diverse_combinations)
    }
}