Skip to main content

graphmind_optimization/algorithms/
jaya.rs

1use crate::common::{Individual, OptimizationResult, Problem, SolverConfig};
2use ndarray::Array1;
3use rand::prelude::*;
4use rayon::prelude::*;
5
6pub struct JayaSolver {
7    pub config: SolverConfig,
8}
9
10impl JayaSolver {
11    pub fn new(config: SolverConfig) -> Self {
12        Self { config }
13    }
14
15    pub fn solve<P: Problem>(&self, problem: &P) -> OptimizationResult {
16        let mut rng = thread_rng();
17        let dim = problem.dim();
18        let (lower, upper) = problem.bounds();
19
20        let mut population: Vec<Individual> = (0..self.config.population_size)
21            .map(|_| {
22                let mut vars = Array1::zeros(dim);
23                for i in 0..dim {
24                    vars[i] = rng.gen_range(lower[i]..upper[i]);
25                }
26                let fitness = problem.fitness(&vars);
27                Individual::new(vars, fitness)
28            })
29            .collect();
30
31        let mut history = Vec::with_capacity(self.config.max_iterations);
32
33        for iter in 0..self.config.max_iterations {
34            if iter % 10 == 0 {
35                println!(
36                    "Jaya Solver: Iteration {}/{}",
37                    iter, self.config.max_iterations
38                );
39            }
40            let (best_idx, worst_idx) = self.find_best_worst(&population);
41            let best_vars = population[best_idx].variables.clone();
42            let worst_vars = population[worst_idx].variables.clone();
43            let best_fitness = population[best_idx].fitness;
44
45            history.push(best_fitness);
46
47            population = population
48                .into_par_iter()
49                .map(|mut ind| {
50                    let mut local_rng = thread_rng();
51                    let mut new_vars = Array1::zeros(dim);
52
53                    // Generate r1, r2 once per individual to match Python's vector op
54                    let r1: f64 = local_rng.gen();
55                    let r2: f64 = local_rng.gen();
56
57                    for j in 0..dim {
58                        let val = ind.variables[j] + r1 * (best_vars[j] - ind.variables[j].abs())
59                            - r2 * (worst_vars[j] - ind.variables[j].abs());
60
61                        new_vars[j] = val.clamp(lower[j], upper[j]);
62                    }
63
64                    let new_fitness = problem.fitness(&new_vars);
65                    if new_fitness < ind.fitness {
66                        ind.variables = new_vars;
67                        ind.fitness = new_fitness;
68                    }
69                    ind
70                })
71                .collect();
72        }
73
74        let (final_best_idx, _) = self.find_best_worst(&population);
75        let final_best = &population[final_best_idx];
76
77        OptimizationResult {
78            best_variables: final_best.variables.clone(),
79            best_fitness: final_best.fitness,
80            history,
81        }
82    }
83
84    fn find_best_worst(&self, population: &[Individual]) -> (usize, usize) {
85        let mut best_idx = 0;
86        let mut worst_idx = 0;
87        for (i, ind) in population.iter().enumerate() {
88            if ind.fitness < population[best_idx].fitness {
89                best_idx = i;
90            }
91            if ind.fitness > population[worst_idx].fitness {
92                worst_idx = i;
93            }
94        }
95        (best_idx, worst_idx)
96    }
97}