metaheurustics/algorithm/
mod.rs

1use ndarray::Array1;
2
3pub mod abco;
4pub mod acgwo;
5pub mod de;
6pub mod fa;
7pub mod ga;
8pub mod gwo;
9pub mod pso;
10pub mod sa;
11
12// Re-export common types and algorithms
13pub use self::abco::{ABCO, ABCOParams};
14pub use self::acgwo::{ACGWO, ACGWOParams};
15pub use self::de::{DE, DEParams, DEStrategy};
16pub use self::fa::FireflyAlgorithm;
17pub use self::ga::{GA, GAParams};
18pub use self::gwo::{GWO, GWOParams};
19pub use self::pso::{PSO, PSOParams};
20pub use self::sa::{SA, SAParams};
21
22/// Trait for objective functions to be optimized
23pub trait ObjectiveFunction {
24    /// Evaluate the objective function at a given point
25    fn evaluate(&self, x: &Array1<f64>) -> f64;
26    
27    /// Get the number of dimensions of the problem
28    fn dimensions(&self) -> usize;
29    
30    /// Get the bounds of the search space
31    fn bounds(&self) -> (Vec<f64>, Vec<f64>);
32}
33
34/// Parameters for optimization algorithms
35#[derive(Debug, Clone)]
36pub struct OptimizationParams {
37    /// Size of the population
38    pub population_size: usize,
39    /// Maximum number of iterations
40    pub max_iterations: usize,
41    /// Target value for early stopping
42    pub target_value: Option<f64>,
43}
44
45impl Default for OptimizationParams {
46    fn default() -> Self {
47        Self {
48            population_size: 50,
49            max_iterations: 1000,
50            target_value: None,
51        }
52    }
53}
54
55/// Result of an optimization run
56#[derive(Debug)]
57pub struct OptimizationResult {
58    /// Best solution found
59    pub best_solution: Array1<f64>,
60    /// Best fitness value found
61    pub best_fitness: f64,
62    /// Number of iterations performed
63    pub iterations: usize,
64    /// Number of function evaluations
65    pub evaluations: usize,
66}
67
68/// Helper function to find the index of the minimum value in an array
69pub(crate) fn argmin(array: &Array1<f64>) -> Option<usize> {
70    array.iter()
71        .enumerate()
72        .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap())
73        .map(|(index, _)| index)
74}