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
/// This is the Sphere function.
///
/// The function is borrowed from [here](https://en.wikipedia.org/wiki/Test_functions_for_optimization).
/// Although the function accepts a vector with an arbitrary number of inputs, this is what it looks like in 2D ![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Sphere_function_in_3D.pdf/page1-800px-Sphere_function_in_3D.pdf.jpg)
pub struct Sphere {}

impl Sphere {
    /// The bounds of the canonical sphere optimization problem are infinite.
    pub const BOUNDS: (f64, f64) = (-f64::INFINITY, f64::INFINITY);

    /// The global minimum is constant and zero
    pub const MINIMUM: f64 = 0.0;

    /// Function for evaluating
    pub fn f(x: Vec<f64>) -> f64 {
        let mut f = 0f64;
        for i in 0..x.len() {
            f -= x[i] * x[i];
        }
        f
    }

    /// This function returns the minimizer (argument that will return the global minimum
    pub fn minimizer(n: usize) -> Vec<f64> {
        vec![0.0; n]
    }
}

/// This is the Rastrigin function.
///
/// The function is borrowed from [here](https://en.wikipedia.org/wiki/Test_functions_for_optimization).
/// Although the function accepts a vector with an arbitrary number of inputs, this is what it looks like in 2D ![](https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Rastrigin_function.png/800px-Rastrigin_function.png)
pub struct Rastrigin {}

impl Rastrigin {
    /// The bounds of the canonical sphere optimization problem are infinite.
    pub const BOUNDS: (f64, f64) = (-5.12, 5.12);

    /// The global minimum is constant and zero
    pub const MINIMUM: f64 = 0.0;

    /// Function for evaluating
    pub fn f(x: Vec<f64>) -> f64 {
        let a = 10.0;
        let n = x.len() ;
        let mut fx = a*(n as f64);

        for i in 0..n {
            fx += x[i].powi(2) - a*(2.0*x[i]*std::f64::consts::PI).cos();
        }
        fx
    }

    /// This function returns the minimizer (argument that will return the global minimum
    pub fn minimizer(n: usize) -> Vec<f64> {
        vec![0.0; n]
    }
}

/// This is the Rosenbrock function.
///
/// The function is borrowed from [here](https://en.wikipedia.org/wiki/Test_functions_for_optimization).
/// Although the function accepts a vector with an arbitrary number of inputs, this is what it looks like in 2D ![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/Rosenbrock%27s_function_in_3D.pdf/page1-800px-Rosenbrock%27s_function_in_3D.pdf.jpg)
pub struct Rosenbrock {}

impl Rosenbrock {
    /// The bounds of the canonical sphere optimization problem are infinite.
    pub const BOUNDS: (f64, f64) = (-f64::INFINITY, f64::INFINITY);

    /// The global minimum is constant and zero
    pub const MINIMUM: f64 = 0.0;

    /// Function for evaluating
    pub fn f(x: Vec<f64>) -> f64 {
        let n = x.len();
        let mut fx = 0.0;
        for i in 0..(n-1) {
            fx += 100.0*(x[i+1] - x[i].powi(2)).powi(2) + (1.0 - x[i]).powi(2);
        }
        fx
    }

    /// This function returns the minimizer (argument that will return the global minimum
    pub fn minimizer(n: usize) -> Vec<f64> {
        vec![1.0; n]
    }
}

/// This is the Ackley function.
///
/// The function is borrowed from [here](https://en.wikipedia.org/wiki/Test_functions_for_optimization).
/// Although the function accepts a vector with an arbitrary number of inputs, this is what it looks like in 2D ![](https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Ackley%27s_function.pdf/page1-800px-Ackley%27s_function.pdf.jpg)
pub struct Ackley {}

impl Ackley {
    /// The bounds of the canonical sphere optimization problem are infinite.
    pub const BOUNDS: (f64, f64) = (-5.0, 5.0);

    /// The global minimum is constant and zero
    pub const MINIMUM: f64 = 0.0;

    /// Function for evaluating
    pub fn f(x: Vec<f64>) -> f64 {
        let n=x.len();
        let mut fx = 0.0;
        let mut square_sum = 0.0;
        let mut cosine_sum = 0.0;
        for i in 0..n {
            square_sum += x[i].powi(2);
            cosine_sum += (2.0*std::f64::consts::PI*x[i]).cos();
        }
        fx += -20.0*(-0.2*(0.5*square_sum).sqrt()).exp();
        fx -= (cosine_sum/(n as f64)).exp();
        fx + std::f64::consts::E + 20.0
    }

    /// This function returns the minimizer (argument that will return the global minimum
    pub fn minimizer(n: usize) -> Vec<f64> {
        vec![0.0; n]
    }
}

#[cfg(test)]
mod two_d_tests {
    use super::*;

    const D: usize = 2;

    #[test]
    fn test_sphere() {
        assert_eq!(Sphere::f(Sphere::minimizer(D)), Sphere::MINIMUM)
    }

    #[test]
    fn test_rastrigin() {
        assert_eq!(Rastrigin::f(Rastrigin::minimizer(D)), Rastrigin::MINIMUM)
    }

    #[test]
    fn test_rosenbrock() {
        assert_eq!(Rosenbrock::f(Rosenbrock::minimizer(D)), Rosenbrock::MINIMUM)
    }

    #[test]
    fn test_ackley() {
        assert_eq!(Ackley::f(Ackley::minimizer(D)), Ackley::MINIMUM)
    }
}



#[cfg(test)]
mod high_d_tests {
    use super::*;

    const D: usize = 100;

    #[test]
    fn test_sphere() {
        assert_eq!(Sphere::f(Sphere::minimizer(D)), Sphere::MINIMUM)
    }

    #[test]
    fn test_rastrigin() {
        assert_eq!(Rastrigin::f(Rastrigin::minimizer(D)), Rastrigin::MINIMUM)
    }

    #[test]
    fn test_rosenbrock() {
        assert_eq!(Rosenbrock::f(Rosenbrock::minimizer(D)), Rosenbrock::MINIMUM)
    }

    #[test]
    fn test_ackley() {
        assert_eq!(Ackley::f(Ackley::minimizer(D)), Ackley::MINIMUM)
    }
}