sefar/benchmarks/
functions.rs

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
use crate::core::problem::Problem;
//use crate::core::genome::Genome;

#[derive(Debug, Clone)]
pub struct SumAbsFunction {}
impl Problem for SumAbsFunction {
    #[cfg(not(feature = "parallel"))]
    fn objectivefunction(&mut self, genome: &[f64]) -> f64 {
        let fitness = genome.iter().fold(0.0f64, |sum, g| sum + g.abs());
        fitness
    }

    #[cfg(feature = "parallel")]
    fn objectivefunction(&self, genome: &[f64]) -> f64 {
        let fitness = genome.iter().fold(0.0f64, |sum, g| sum + g.abs());
        fitness
    }
}

///
/// Sphere benchmark function (F1).
/// Fi(X) = Sum(|X|)
/// where X = {x1, x2, ..... xd}, and 'd' is the problem dimension.
///
#[derive(Debug, Clone)]
pub struct Sphere {}

impl Problem for Sphere {
    ///
    /// Define the objective function. The later is called in sequential mode.
    ///
    #[cfg(not(feature = "parallel"))]
    fn objectivefunction(&mut self, genome: &[f64]) -> f64 {
        let fitness = genome.iter().fold(0.0f64, |sum, g| sum + g.powi(2));
        fitness
    }

    #[cfg(feature = "parallel")]
    fn objectivefunction(&self, genome: &[f64]) -> f64 {
        let fitness = genome.iter().fold(0.0f64, |sum, g| sum + g.powi(2));
        fitness
    }
}

///
/// F2 = Sum(x_i) + Prod(x_i) = Sum(X) + Prod(X).
/// Search space : [-100.0, 100.0];
/// Problem dimension : 30;
/// Optimal value = vec![0.0; 30];
#[derive(Debug, Clone)]
pub struct F2 {}
impl Problem for F2 {
    #[cfg(not(feature = "parallel"))]
    fn objectivefunction(&mut self, genome: &[f64]) -> f64 {
        let sum = genome.iter().fold(0.0f64, |sum, g| sum + g.abs());
        let prod = genome.iter().fold(1.0f64, |prod, g| prod * f64::abs(*g));
        sum + prod
    }

    #[cfg(feature = "parallel")]
    fn objectivefunction(&self, genome: &[f64]) -> f64 {
        let sum = genome.iter().fold(0.0f64, |sum, g| sum + g.abs());
        let prod = genome.iter().fold(1.0f64, |prod, g| prod * f64::abs(*g));
        sum + prod
    }
}