swarm 0.1.3

Fast blackbox optimisation tool.
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
use std::cmp::Ordering;

use rand::{rngs::StdRng, Rng, SeedableRng};

use crate::{
    error::{Result, SwarmError},
    initialisation::Initialisation,
    OptimiserResult, Solution, Variable,
};

#[cfg(feature = "parallel")]
use rayon::prelude::*;

#[derive(Debug, Copy, Clone)]
pub struct SbxParams {
    // Simulated Binary Crossover
    pub prob: f64,
    pub eta: f64,
}
impl SbxParams {
    /// Instantiate Simulated Binary Crossover parameters.
    ///
    /// # Arguments
    /// * `prob` - The probability of applying crossover.
    /// * `eta` - The distribution index for crossover.
    ///
    /// # Returns
    /// A new `SbxParams` instance.
    pub fn new(prob: f64, eta: f64) -> Self {
        Self { prob, eta }
    }
}
impl Default for SbxParams {
    fn default() -> Self {
        Self {
            prob: 0.9199,
            eta: 0.1057,
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub struct PmParams {
    // Polynomial Mutation
    pub prob: f64,
    pub eta: f64,
}
impl PmParams {
    /// Instantiate Polynomial Mutation parameters.
    ///
    /// # Arguments
    /// * `prob` - The probability of applying mutation.
    /// * `eta` - The distribution index for mutation.
    ///
    /// # Returns
    /// A new `PmParams` instance.
    pub fn new(prob: f64, eta: f64) -> Self {
        Self { prob, eta }
    }
}
impl Default for PmParams {
    fn default() -> Self {
        Self {
            prob: 1.0,
            eta: 20.0,
        }
    }
}

#[derive(Debug, Clone)]
struct Individual {
    x: Vec<f64>,
    f: Vec<f64>,
    g: Option<Vec<f64>>,
    // The total constraint violation, calculated once per evaluation.
    // A value of 0.0 means the individual is feasible.
    constraint_violation: f64,
    // Transient data for survival selection
    rank: usize,
    crowding_distance: f64,
}

impl Individual {
    /// Instantiate a new individual.
    ///
    /// # Arguments
    /// * `x` - The list of variable values for this individual.
    ///
    /// # Returns
    /// A new `Individual` instance.
    fn new(x: Vec<f64>) -> Self {
        Self {
            x,
            f: vec![],
            g: None,
            constraint_violation: 0.0,
            rank: 0,
            crowding_distance: 0.0,
        }
    }

    /// Evaluates the individual using the objective function and calculates its fitness.
    ///
    /// # Arguments
    /// * `func` - The objective function to evaluate.
    ///
    /// # Returns
    /// A tuple containing the objective values `f` and optional constraint violations `g`.
    fn evaluate<F: FnMut(&[f64]) -> (Vec<f64>, Option<Vec<f64>>)>(&mut self, func: &mut F) {
        let (f, g) = func(&self.x);
        self.f = f;
        self.g = g;
        self.constraint_violation = match &self.g {
            Some(constraints) => constraints.iter().filter(|&&c| c > 0.0).sum(),
            None => 0.0,
        };
    }

    /// Evaluates the individual (PARALLEL VERSION).
    #[cfg(feature = "parallel")]
    fn evaluate_par<F: Fn(&[f64]) -> (Vec<f64>, Option<Vec<f64>>) + Sync + Send>(
        &mut self,
        func: &F,
    ) {
        let (f, g) = func(&self.x);
        self.f = f;
        self.g = g;
        self.constraint_violation = match &self.g {
            Some(constraints) => constraints.iter().filter(|&&c| c > 0.0).sum(),
            None => 0.0,
        };
    }
}

/// The main NSGA-II function (SERIAL VERSION).
/// 
/// # Arguments
/// * `func` - The objective function to evaluate.
/// * `vars` - The list of variable bounds for this problem.
/// * `max_iter` - The maximum number of iterations to run.
/// * `pop_size` - The population size.
/// * `crossover_params` - The crossover parameters.
/// * `mutation_params` - The mutation parameters.
/// * `initialisation` - The initialisation method.
/// * `seed` - The random seed.
/// 
/// # Returns
/// A `Result` containing the optimiser result `OptimiserResult`.
pub fn nsga<F>(
    func: &mut F,
    vars: &[Variable],
    max_iter: usize,
    pop_size: usize,
    crossover_params: &SbxParams,
    mutation_params: &PmParams,
    initialisation: Initialisation,
    seed: Option<u64>,
) -> Result<OptimiserResult>
where
    F: FnMut(&[f64]) -> (Vec<f64>, Option<Vec<f64>>),
{
    // Checks
    if pop_size < 4 || pop_size % 2 != 0 {
        return Err(SwarmError::FailedToMeetCondition(
            "Population size must be an even number >= 4.".to_string(),
        ));
    }

    let mut rng = seed.map_or_else(StdRng::from_entropy, StdRng::seed_from_u64);

    // Initialisation
    let mut population: Vec<Individual> = initialisation
        .generate_samples(pop_size, vars, &mut rng)
        .into_iter()
        .map(Individual::new)
        .collect();

    // Evaluate the initial population
    for ind in &mut population {
        ind.evaluate(func);
    }

    // Assign rank and crowding to the initial population for the first selection
    survival_selection(&mut population, pop_size);

    // Optimisation Loop
    for _ in 0..max_iter {
        // Create Offspring
        let n_offsprings = pop_size / 2;
        let parents = selection(&population, n_offsprings, &mut rng);
        let mut offspring = crossover(&parents, vars, crossover_params, &mut rng);
        mutate(&mut offspring, vars, mutation_params, &mut rng);

        // Evaluate Offspring
        for ind in &mut offspring {
            ind.evaluate(func);
        }

        // Survival of the Fittest
        // Combine parent and offspring populations
        population.append(&mut offspring);

        // Select the next generation
        survival_selection(&mut population, pop_size);
    }

    // Final Result
    // The final population contains the best solutions. The first front is the Pareto front.
    let solutions = population
        .into_iter()
        .filter(|ind| ind.rank == 0) // Filter for the first non-dominated front
        .map(|ind| Solution {
            x: ind.x,
            f: ind.f,
            g: ind.g,
        })
        .collect();

    Ok(OptimiserResult::new(solutions, max_iter))
}

/// The main NSGA-II function (PARALLEL VERSION).
/// This version is compiled only when the "parallel" feature is enabled.
#[cfg(feature = "parallel")]
pub fn nsga_par<F>(
    func: &F, // Takes an immutable, thread-safe closure
    vars: &[Variable],
    max_iter: usize,
    pop_size: usize,
    crossover_params: &SbxParams,
    mutation_params: &PmParams,
    initialisation: Initialisation,
    seed: Option<u64>,
) -> Result<OptimiserResult>
where
    F: Fn(&[f64]) -> (Vec<f64>, Option<Vec<f64>>) + Sync + Send,
{
    if pop_size < 4 || pop_size % 2 != 0 {
        return Err(SwarmError::FailedToMeetCondition(
            "Population size must be an even number >= 4.".to_string(),
        ));
    }
    let mut rng = seed.map_or_else(StdRng::from_entropy, StdRng::seed_from_u64);

    // Initialisation (Parallel)
    let mut population: Vec<Individual> = initialisation
        .generate_samples(pop_size, vars, &mut rng)
        .into_par_iter() // Parallel iterator
        .map(|x| {
            let mut ind = Individual::new(x);
            ind.evaluate_par(func);
            ind
        })
        .collect();

    survival_selection(&mut population, pop_size);

    // Optimisation Loop
    for _ in 0..max_iter {
        let n_parent_pairs = pop_size / 2;
        let parents = selection(&population, n_parent_pairs, &mut rng);
        let mut offspring = crossover(&parents, vars, crossover_params, &mut rng);
        mutate(&mut offspring, vars, mutation_params, &mut rng);

        // Evaluate Offspring (Parallel)
        offspring.par_iter_mut().for_each(|ind| {
            ind.evaluate_par(func);
        });

        population.append(&mut offspring);
        survival_selection(&mut population, pop_size);
    }

    let solutions = population
        .into_iter()
        .filter(|ind| ind.rank == 0)
        .map(|ind| Solution {
            x: ind.x,
            f: ind.f,
            g: ind.g,
        })
        .collect();
    Ok(OptimiserResult::new(solutions, max_iter))
}

/// Performs survival selection on a combined population to select the next generation.
///
/// # Arguments
/// * `population` - The population to select from.
/// * `n_survive` - The number of individuals to select.
fn survival_selection(population: &mut Vec<Individual>, n_survive: usize) {
    // 1. Perform non-dominated sorting to get the fronts
    let fronts = non_dominated_sort(population);

    // 2. Assign rank and calculate crowding distance for each front
    for (rank_idx, front_indices) in fronts.iter().enumerate() {
        for &pop_idx in front_indices {
            population[pop_idx].rank = rank_idx;
        }
        // This is the corrected call, passing the whole population and the relevant indices
        crowding_distance_assignment(population, front_indices);
    }

    // 3. Sort the entire population based on rank (primary) and crowding distance (secondary)
    population.sort_by(|a, b| {
        a.rank.cmp(&b.rank).then_with(|| {
            b.crowding_distance
                .partial_cmp(&a.crowding_distance)
                .unwrap_or(Ordering::Equal)
        })
    });

    // 4. Truncate the population to the desired size
    population.truncate(n_survive);
}

/// Sorts the population into non-dominated fronts.
///
/// # Arguments
/// * `population` - The population to sort.
///
/// # Returns
/// A vector of vectors, where each inner vector contains the indices of the individuals in a front.
fn non_dominated_sort(population: &[Individual]) -> Vec<Vec<usize>> {
    let n = population.len();
    let mut fronts: Vec<Vec<usize>> = Vec::new();
    let mut dominated_by_count = vec![0; n];
    let mut dominates_list: Vec<Vec<usize>> = vec![Vec::new(); n];

    for i in 0..n {
        for j in (i + 1)..n {
            if dominates(&population[i], &population[j]) {
                dominates_list[i].push(j);
                dominated_by_count[j] += 1;
            } else if dominates(&population[j], &population[i]) {
                dominates_list[j].push(i);
                dominated_by_count[i] += 1;
            }
        }
        if dominated_by_count[i] == 0 {
            if fronts.is_empty() {
                fronts.push(Vec::new());
            }
            fronts[0].push(i);
        }
    }

    let mut current_front_idx = 0;
    while current_front_idx < fronts.len() {
        let mut next_front = Vec::new();
        for &i in &fronts[current_front_idx] {
            for &j in &dominates_list[i] {
                dominated_by_count[j] -= 1;
                if dominated_by_count[j] == 0 {
                    next_front.push(j);
                }
            }
        }
        if !next_front.is_empty() {
            fronts.push(next_front);
        }
        current_front_idx += 1;
    }

    fronts
}

/// Calculates and assigns crowding distance to all individuals within a single front.
///
/// # Arguments
/// * `population` - The population to calculate crowding distance for.
/// * `front_indices` - The indices of the individuals in the front.
fn crowding_distance_assignment(population: &mut [Individual], front_indices: &[usize]) {
    let n = front_indices.len();
    if n == 0 {
        return;
    }

    // Set initial crowding distance to 0 for all individuals in the front
    for &idx in front_indices {
        population[idx].crowding_distance = 0.0;
    }

    let n_obj = population[front_indices[0]].f.len();

    for m in 0..n_obj {
        // Create a mutable copy of the indices to sort them
        let mut sorted_indices = front_indices.to_vec();

        // Sort the indices based on the m-th objective value of the individuals they point to
        sorted_indices.sort_by(|&a, &b| {
            population[a].f[m]
                .partial_cmp(&population[b].f[m])
                .unwrap_or(Ordering::Equal)
        });

        // Assign infinite distance to the boundary points
        let first_idx = sorted_indices[0];
        let last_idx = sorted_indices[n - 1];
        population[first_idx].crowding_distance = f64::INFINITY;
        population[last_idx].crowding_distance = f64::INFINITY;

        let f_min = population[first_idx].f[m];
        let f_max = population[last_idx].f[m];
        let range = f_max - f_min;

        if range.abs() < 1e-9 {
            continue;
        } // Avoid division by zero

        // Add distance for intermediate points
        for i in 1..(n - 1) {
            let prev_idx = sorted_indices[i - 1];
            let next_idx = sorted_indices[i + 1];
            let current_idx = sorted_indices[i];

            let numerator = population[next_idx].f[m] - population[prev_idx].f[m];
            population[current_idx].crowding_distance += numerator / range;
        }
    }
}

/// Selects parents from the population using a binary tournament.
///
/// # Arguments
/// * `population` - The population to select from.
/// * `n_select` - The number of parents to select.
/// * `rng` - A mutable random number generator.
///
/// # Returns
/// A vector of pairs of selected parents.
fn selection(
    population: &[Individual],
    n_select: usize,
    rng: &mut StdRng,
) -> Vec<(Individual, Individual)> {
    let mut parents = Vec::with_capacity(n_select);
    for _ in 0..n_select {
        let p1 = tournament(population, rng);
        let p2 = tournament(population, rng);
        parents.push((p1.clone(), p2.clone()));
    }
    parents
}

/// Performs a single binary tournament selection.
///
/// # Arguments
/// * `population` - The population to select from.
/// * `rng` - A mutable random number generator.
///
/// # Returns
/// A reference to the selected individual.
fn tournament<'a>(population: &'a [Individual], rng: &mut StdRng) -> &'a Individual {
    let i1 = rng.gen_range(0..population.len());
    let i2 = rng.gen_range(0..population.len());
    let ind1 = &population[i1];
    let ind2 = &population[i2];

    if dominates(ind1, ind2) {
        ind1
    } else if dominates(ind2, ind1) {
        ind2
    } else if ind1.crowding_distance > ind2.crowding_distance {
        ind1
    } else {
        ind2
    }
}

/// The core dominance check, implementing the Constrained Dominance principle.
///
/// # Arguments
/// * `a` - The first individual to compare.
/// * `b` - The second individual to compare.
///
/// # Returns
/// A boolean indicating whether `a` dominates `b`.
fn dominates(a: &Individual, b: &Individual) -> bool {
    // Principle 1: If one is feasible and the other is not, the feasible one dominates.
    if a.constraint_violation == 0.0 && b.constraint_violation > 0.0 {
        return true;
    }
    if a.constraint_violation > 0.0 && b.constraint_violation == 0.0 {
        return false;
    }

    // Principle 2: If both are infeasible, the one with the smaller violation dominates.
    if a.constraint_violation > 0.0 && b.constraint_violation > 0.0 {
        return a.constraint_violation < b.constraint_violation;
    }

    // Principle 3: If both are feasible, use standard Pareto dominance.
    let a_is_better = a.f.iter().zip(&b.f).any(|(v_a, v_b)| v_a < v_b);
    let b_is_better = a.f.iter().zip(&b.f).any(|(v_a, v_b)| v_b < v_a);

    a_is_better && !b_is_better
}

/// Creates a new offspring population using Simulated Binary Crossover (SBX).
///
/// # Arguments
/// * `parents` - A slice of tuples, each containing two parent individuals.
/// * `vars` - A slice defining the bounds for each variable in the search space.
/// * `params` - Parameters controlling the crossover process.
/// * `rng` - A mutable random number generator.
///
/// # Returns
/// A vector of new offspring individuals.
fn crossover(
    parents: &[(Individual, Individual)],
    vars: &[Variable],
    params: &SbxParams,
    rng: &mut StdRng,
) -> Vec<Individual> {
    let mut offspring = Vec::with_capacity(parents.len() * 2);

    for (p1, p2) in parents {
        let mut c1 = Individual::new(p1.x.clone());
        let mut c2 = Individual::new(p2.x.clone());

        if rng.gen::<f64>() > params.prob {
            offspring.push(c1);
            offspring.push(c2);
            continue;
        }

        for (i, var) in vars.iter().enumerate() {
            let (x1, x2) = (p1.x[i], p2.x[i]);
            let (min, max) = (var.0, var.1);

            if (x1 - x2).abs() < 1e-9 {
                continue;
            }

            let (y1, y2) = if x1 < x2 { (x1, x2) } else { (x2, x1) };

            let rand = rng.gen::<f64>();
            let beta = if rand <= 0.5 {
                (2.0 * rand).powf(1.0 / (params.eta + 1.0))
            } else {
                (1.0 / (2.0 * (1.0 - rand))).powf(1.0 / (params.eta + 1.0))
            };

            let mut off1 = 0.5 * ((y1 + y2) - beta * (y2 - y1));
            let mut off2 = 0.5 * ((y1 + y2) + beta * (y2 - y1));

            off1 = off1.clamp(min, max);
            off2 = off2.clamp(min, max);

            if rng.gen::<f64>() < 0.5 {
                c1.x[i] = off1;
                c2.x[i] = off2;
            } else {
                c1.x[i] = off2;
                c2.x[i] = off1;
            }
        }
        offspring.push(c1);
        offspring.push(c2);
    }
    offspring
}

/// Modifies an offspring population using Polynomial Mutation.
///
/// # Arguments
/// * `offspring` - A mutable slice of `Individual` representing the offspring population.
/// * `vars` - A slice defining the bounds for each variable in the search space.
/// * `params` - Parameters controlling the mutation process.
/// * `rng` - A mutable random number generator.
fn mutate(
    offspring: &mut [Individual],
    vars: &[Variable],
    params: &PmParams,
    rng: &mut StdRng,
) {
    let prob_per_var = params.prob / vars.len() as f64;

    for ind in offspring {
        for (i, var) in vars.iter().enumerate() {
            if rng.gen::<f64>() > prob_per_var {
                continue;
            }

            let (x, min, max) = (ind.x[i], var.0, var.1);
            let delta1 = (x - min) / (max - min);
            let delta2 = (max - x) / (max - min);

            let rand = rng.gen::<f64>();
            let mut_pow = 1.0 / (params.eta + 1.0);

            let deltaq = if rand < 0.5 {
                (2.0 * rand + (1.0 - 2.0 * rand) * (1.0 - delta1).powf(params.eta + 1.0))
                    .powf(mut_pow)
                    - 1.0
            } else {
                1.0 - (2.0 * (1.0 - rand)
                    + 2.0 * (rand - 0.5) * (1.0 - delta2).powf(params.eta + 1.0))
                .powf(mut_pow)
            };

            ind.x[i] = (x + deltaq * (max - min)).clamp(min, max);
        }
    }
}