Skip to main content

rlevo_evolution/algorithms/metaheuristic/
woa.rs

1//! Whale Optimization Algorithm.
2//!
3//! Each whale chooses per generation between two behaviours, driven by a
4//! uniform random `p ∈ U[0, 1]`:
5//!
6//! - `p < 0.5` — **encircle / search**:
7//!     - `|A| < 1`: exploit the current best (`X ← X_best − A·|C·X_best − X|`),
8//!     - `|A| ≥ 1`: explore by pulling toward a random other whale
9//!       (`X ← X_rand − A·|C·X_rand − X|`).
10//! - `p ≥ 0.5` — **spiral bubble-net**:
11//!   `X ← |X_best − X|·exp(b·l)·cos(2π·l) + X_best`, `l ∈ U[−1, 1]`,
12//!   `b = 1` (canonical).
13//!
14//! `A = 2a·r − a`, `C = 2r`, with `a` linearly decreased from 2 to 0
15//! over the budget.
16//!
17//! The branches are realized as two boolean masks and three tensor
18//! candidates — no divergent kernel paths.
19//!
20//! # Candor
21//!
22//! Legacy comparator. The spiral bubble-net and encircle-best operators
23//! compose to a motion pattern that is equivalent in expectation to a
24//! weighted PSO update toward the current best (Camacho Villalón et al.
25//! 2020 review the structural similarities). Ship it for API coverage;
26//! prefer CMA-ES or LSHADE when available.
27//!
28//! # References
29//!
30//! - Mirjalili & Lewis (2016), *The Whale Optimization Algorithm*.
31
32use std::f32::consts::PI;
33use std::marker::PhantomData;
34
35use burn::tensor::{Distribution, Int, Tensor, TensorData, backend::Backend};
36use rand::Rng;
37use rand::RngExt;
38
39use crate::rng::{SeedPurpose, seed_stream};
40use crate::strategy::{Strategy, StrategyMetrics};
41
42/// Static configuration for [`WhaleOptimization`].
43#[derive(Debug, Clone)]
44pub struct WoaConfig {
45    /// Number of whales.
46    pub pop_size: usize,
47    /// Genome dimensionality.
48    pub genome_dim: usize,
49    /// Search-space bounds.
50    pub bounds: (f32, f32),
51    /// Budget pacing `a = 2·(1 − t/max_generations)`.
52    pub max_generations: usize,
53    /// Spiral shape constant (Mirjalili's canonical `b = 1`).
54    pub b: f32,
55}
56
57impl WoaConfig {
58    /// Default configuration for a given population size and genome dimensionality.
59    #[must_use]
60    pub fn default_for(pop_size: usize, genome_dim: usize) -> Self {
61        Self {
62            pop_size,
63            genome_dim,
64            bounds: (-5.12, 5.12),
65            max_generations: 500,
66            b: 1.0,
67        }
68    }
69}
70
71/// Generation state for [`WhaleOptimization`].
72#[derive(Debug, Clone)]
73pub struct WoaState<B: Backend> {
74    /// Current positions, shape `(pop_size, D)`.
75    pub positions: Tensor<B, 2>,
76    /// Host-side fitness cache.
77    pub fitness: Vec<f32>,
78    /// Best-so-far genome.
79    pub best_genome: Option<Tensor<B, 2>>,
80    /// Best-so-far fitness.
81    pub best_fitness: f32,
82    /// Generation counter.
83    pub generation: usize,
84}
85
86/// Whale Optimization Algorithm strategy.
87///
88/// # Example
89///
90/// ```no_run
91/// use burn::backend::NdArray;
92/// use rlevo_evolution::algorithms::metaheuristic::woa::{WhaleOptimization, WoaConfig};
93///
94/// let strategy = WhaleOptimization::<NdArray>::new();
95/// let params = WoaConfig::default_for(32, 10);
96/// let _ = (strategy, params);
97/// ```
98#[derive(Debug, Clone, Copy, Default)]
99pub struct WhaleOptimization<B: Backend> {
100    _backend: PhantomData<fn() -> B>,
101}
102
103impl<B: Backend> WhaleOptimization<B> {
104    /// Builds a new (stateless) strategy object.
105    #[must_use]
106    pub fn new() -> Self {
107        Self {
108            _backend: PhantomData,
109        }
110    }
111}
112
113impl<B: Backend> Strategy<B> for WhaleOptimization<B>
114where
115    B::Device: Clone,
116{
117    type Params = WoaConfig;
118    type State = WoaState<B>;
119    type Genome = Tensor<B, 2>;
120
121    fn init(&self, params: &WoaConfig, rng: &mut dyn Rng, device: &B::Device) -> WoaState<B> {
122        let (lo, hi) = params.bounds;
123        B::seed(device, rng.next_u64());
124        let positions = Tensor::<B, 2>::random(
125            [params.pop_size, params.genome_dim],
126            Distribution::Uniform(f64::from(lo), f64::from(hi)),
127            device,
128        );
129        WoaState {
130            positions,
131            fitness: Vec::new(),
132            best_genome: None,
133            best_fitness: f32::INFINITY,
134            generation: 0,
135        }
136    }
137
138    #[allow(clippy::many_single_char_names)]
139    fn ask(
140        &self,
141        params: &WoaConfig,
142        state: &WoaState<B>,
143        rng: &mut dyn Rng,
144        device: &B::Device,
145    ) -> (Tensor<B, 2>, WoaState<B>) {
146        // First call: evaluate initial whales so `tell` can record fitness.
147        if state.fitness.is_empty() {
148            return (state.positions.clone(), state.clone());
149        }
150
151        let pop_size = params.pop_size;
152        let genome_dim = params.genome_dim;
153
154        // Linear schedule for a.
155        #[allow(clippy::cast_precision_loss)]
156        let t = state.generation as f32;
157        #[allow(clippy::cast_precision_loss)]
158        let max_t = params.max_generations.max(1) as f32;
159        let a = 2.0 * (1.0 - (t / max_t).min(1.0));
160
161        // Per-whale scalars: A, C, p, l. Sample on host via the scope
162        // splitmix stream so the seed contract is fully reproducible.
163        let mut stream = seed_stream(rng.next_u64(), state.generation as u64, SeedPurpose::Other);
164        let mut rand_idx: Vec<i64> = Vec::with_capacity(pop_size);
165        let mut a_scalar: Vec<f32> = Vec::with_capacity(pop_size);
166        let mut c_scalar: Vec<f32> = Vec::with_capacity(pop_size);
167        let mut p_scalar: Vec<f32> = Vec::with_capacity(pop_size);
168        let mut l_scalar: Vec<f32> = Vec::with_capacity(pop_size);
169        let mut abs_a_lt_one: Vec<i64> = Vec::with_capacity(pop_size);
170        let mut p_lt_half: Vec<i64> = Vec::with_capacity(pop_size);
171        for i in 0..pop_size {
172            let r_a: f32 = stream.random::<f32>();
173            let r_c: f32 = stream.random::<f32>();
174            let p: f32 = stream.random::<f32>();
175            let l: f32 = 2.0 * stream.random::<f32>() - 1.0;
176            let a_val = 2.0 * a * r_a - a;
177            let c_val = 2.0 * r_c;
178            a_scalar.push(a_val);
179            c_scalar.push(c_val);
180            p_scalar.push(p);
181            l_scalar.push(l);
182            abs_a_lt_one.push(i64::from(a_val.abs() < 1.0));
183            p_lt_half.push(i64::from(p < 0.5));
184            // Pick a different index for the "search" branch.
185            let mut r = stream.random_range(0..pop_size);
186            if r == i {
187                r = (r + 1) % pop_size;
188            }
189            #[allow(clippy::cast_possible_wrap)]
190            rand_idx.push(r as i64);
191        }
192
193        let a_row = Tensor::<B, 1>::from_data(TensorData::new(a_scalar, [pop_size]), device)
194            .unsqueeze_dim::<2>(1)
195            .expand([pop_size, genome_dim]);
196        let c_row = Tensor::<B, 1>::from_data(TensorData::new(c_scalar, [pop_size]), device)
197            .unsqueeze_dim::<2>(1)
198            .expand([pop_size, genome_dim]);
199        let l_vec = Tensor::<B, 1>::from_data(TensorData::new(l_scalar, [pop_size]), device);
200        let rand_idx_t =
201            Tensor::<B, 1, Int>::from_data(TensorData::new(rand_idx, [pop_size]), device);
202        let x_rand = state.positions.clone().select(0, rand_idx_t);
203
204        let x_best = state
205            .best_genome
206            .as_ref()
207            .expect("best_genome populated after the first tell")
208            .clone()
209            .expand([pop_size, genome_dim]);
210
211        // Encircle toward X_best:  X_best − A · |C · X_best − X|
212        let enc_best = x_best.clone()
213            - a_row
214                .clone()
215                .mul((c_row.clone().mul(x_best.clone()) - state.positions.clone()).abs());
216        // Search toward X_rand:    X_rand − A · |C · X_rand − X|
217        let enc_rand =
218            x_rand.clone() - a_row.mul((c_row.mul(x_rand) - state.positions.clone()).abs());
219        // Spiral toward X_best:    |X_best − X| · exp(b·l) · cos(2π·l) + X_best
220        let dist = (x_best.clone() - state.positions.clone()).abs();
221        let factor = l_vec
222            .clone()
223            .mul_scalar(params.b)
224            .exp()
225            .mul(l_vec.mul_scalar(2.0 * PI).cos());
226        let factor_mat = factor.unsqueeze_dim::<2>(1).expand([pop_size, genome_dim]);
227        let spiral = dist.mul(factor_mat) + x_best;
228
229        // Compose: p < 0.5 ? (|A|<1 ? enc_best : enc_rand) : spiral.
230        let m_abs_a_lt_one =
231            Tensor::<B, 1, Int>::from_data(TensorData::new(abs_a_lt_one, [pop_size]), device)
232                .equal_elem(1)
233                .unsqueeze_dim::<2>(1)
234                .expand([pop_size, genome_dim]);
235        let m_p_lt_half =
236            Tensor::<B, 1, Int>::from_data(TensorData::new(p_lt_half, [pop_size]), device)
237                .equal_elem(1)
238                .unsqueeze_dim::<2>(1)
239                .expand([pop_size, genome_dim]);
240
241        let encircle = enc_rand.mask_where(m_abs_a_lt_one, enc_best);
242        let new_positions = spiral.mask_where(m_p_lt_half, encircle);
243
244        let (lo, hi) = params.bounds;
245        let new_positions = new_positions.clamp(lo, hi);
246
247        let mut next = state.clone();
248        next.positions.clone_from(&new_positions);
249        (new_positions, next)
250    }
251
252    fn tell(
253        &self,
254        _params: &WoaConfig,
255        population: Tensor<B, 2>,
256        fitness: Tensor<B, 1>,
257        mut state: WoaState<B>,
258        _rng: &mut dyn Rng,
259    ) -> (WoaState<B>, StrategyMetrics) {
260        let fitness_host = fitness.into_data().into_vec::<f32>().unwrap_or_default();
261        state.fitness.clone_from(&fitness_host);
262        state.positions.clone_from(&population);
263        let best_idx = argmin(&fitness_host);
264        if fitness_host[best_idx] < state.best_fitness {
265            state.best_fitness = fitness_host[best_idx];
266            let device = population.device();
267            #[allow(clippy::cast_possible_wrap)]
268            let idx = Tensor::<B, 1, Int>::from_data(
269                TensorData::new(vec![best_idx as i64], [1]),
270                &device,
271            );
272            state.best_genome = Some(population.select(0, idx));
273        }
274        state.generation += 1;
275        let m =
276            StrategyMetrics::from_host_fitness(state.generation, &fitness_host, state.best_fitness);
277        state.best_fitness = m.best_fitness_ever;
278        (state, m)
279    }
280
281    fn best(&self, state: &WoaState<B>) -> Option<(Tensor<B, 2>, f32)> {
282        state
283            .best_genome
284            .as_ref()
285            .map(|g| (g.clone(), state.best_fitness))
286    }
287}
288
289fn argmin(xs: &[f32]) -> usize {
290    let mut best_idx = 0usize;
291    let mut best = f32::INFINITY;
292    for (i, &v) in xs.iter().enumerate() {
293        if v < best {
294            best = v;
295            best_idx = i;
296        }
297    }
298    best_idx
299}
300
301#[cfg(test)]
302mod tests {
303    use super::*;
304    use crate::fitness::FromFitnessEvaluable;
305    use crate::strategy::EvolutionaryHarness;
306    use burn::backend::NdArray;
307    use rlevo_core::fitness::FitnessEvaluable;
308
309    type TestBackend = NdArray;
310
311    struct Sphere;
312    struct SphereFit;
313    impl FitnessEvaluable for SphereFit {
314        type Individual = Vec<f64>;
315        type Landscape = Sphere;
316        fn evaluate(&self, x: &Self::Individual, _: &Self::Landscape) -> f64 {
317            x.iter().map(|v| v * v).sum()
318        }
319    }
320
321    #[test]
322    fn woa_converges_on_sphere_d10() {
323        // WOA as "legacy comparator" per module-level candor note. We
324        // verify convergence direction — reaching within 1e-4 of the
325        // optimum on Sphere-D10 in 600 generations confirms the
326        // spiral/encircle composition functions correctly.
327        let device = Default::default();
328        let strategy = WhaleOptimization::<TestBackend>::new();
329        let params = WoaConfig::default_for(32, 10);
330        let fitness_fn = FromFitnessEvaluable::new(SphereFit, Sphere);
331        let mut harness = EvolutionaryHarness::<TestBackend, _, _>::new(
332            strategy, params, fitness_fn, 5, device, 600,
333        );
334        harness.reset();
335        while !harness.step(()).done {}
336        let best = harness.latest_metrics().unwrap().best_fitness_ever;
337        assert!(best < 1e-4, "WOA D10 best={best}");
338    }
339}