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
// Copyright 2018-2024 argmin developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! # Ackley test function
//!
//! Defined as
//!
//! `f(x_1, x_2, ..., x_n) = - a * exp( -b \sqrt{\frac{1}{d}\sum_{i=1}^n x_i^2 ) -
//! exp( \frac{1}{d} cos(c * x_i) ) + a + exp(1)`
//!
//! where `x_i \in [-32.768, 32.768]` and usually `a = 10`, `b = 0.2` and `c = 2*pi`
//!
//! The global minimum is at `f(x_1, x_2, ..., x_n) = f(0, 0, ..., 0) = 0`.

use num::{Float, FromPrimitive};
use std::f64::consts::PI;
use std::iter::Sum;

/// Ackley test function
///
/// Defined as
///
/// `f(x_1, x_2, ..., x_n) = - a * exp( -b \sqrt{\frac{1}{d}\sum_{i=1}^n x_i^2 ) -
/// exp( \frac{1}{d} cos(c * x_i) ) + a + exp(1)`
///
/// where `x_i \in [-32.768, 32.768]` and usually `a = 10`, `b = 0.2` and `c = 2*pi`
///
/// The global minimum is at `f(x_1, x_2, ..., x_n) = f(0, 0, ..., 0) = 0`.
pub fn ackley<T>(param: &[T]) -> T
where
    T: Float + FromPrimitive + Sum,
{
    ackley_abc(
        param,
        T::from_f64(20.0).unwrap(),
        T::from_f64(0.2).unwrap(),
        T::from_f64(2.0 * PI).unwrap(),
    )
}

/// Ackley test function
///
/// The same as `ackley`; however, it allows to set the parameters a, b and c.
pub fn ackley_abc<T>(param: &[T], a: T, b: T, c: T) -> T
where
    T: Float + FromPrimitive + Sum,
{
    let num1 = T::from_f64(1.0).unwrap();
    let n = T::from_usize(param.len()).unwrap();
    -a * (-b * ((num1 / n) * param.iter().map(|x| x.powi(2)).sum()).sqrt()).exp()
        - ((num1 / n) * param.iter().map(|x| (c * *x).cos()).sum()).exp()
        + a
        + num1.exp()
}

/// Derivative of Ackley test function
///
/// Calls `ackley_abc_derivative` with `a = 10`, `b = 0.2` and `c = 2*pi`
pub fn ackley_derivative<T>(param: &[T]) -> Vec<T>
where
    T: Float + FromPrimitive + Sum,
{
    ackley_abc_derivative(
        param,
        T::from_f64(20.0).unwrap(),
        T::from_f64(0.2).unwrap(),
        T::from_f64(2.0 * PI).unwrap(),
    )
}

/// Derivative of Ackley test function
///
/// The same as `ackley_derivative`; however, it allows to set the parameters a, b and c.
pub fn ackley_abc_derivative<T>(param: &[T], a: T, b: T, c: T) -> Vec<T>
where
    T: Float + FromPrimitive + Sum,
{
    let d = T::from_usize(param.len()).unwrap();
    let n0 = T::from_f64(0.0).unwrap();
    let eps = T::epsilon();

    let norm = param.iter().map(|x| x.powi(2)).sum::<T>().sqrt();

    let f1 = (c * (param.iter().map(|&x| (c * x).cos()).sum::<T>() / d).exp()) / d;
    let f2 = if norm <= eps {
        n0
    } else {
        (a * b * (-b * norm / d.sqrt()).exp()) / (d.sqrt() * norm)
    };

    param
        .iter()
        .map(|&x| ((c * x).sin() * f1) + x * f2)
        .collect()
}

/// Derivative of Ackley test function
///
/// Calls `ackley_abc_derivative_const` with `a = 10`, `b = 0.2` and `c = 2*pi`
///
/// This is the const generics version, which requires the number of parameters to be known
/// at compile time.
pub fn ackley_derivative_const<const N: usize, T>(param: &[T; N]) -> [T; N]
where
    T: Float + FromPrimitive + Sum,
{
    ackley_abc_derivative_const(
        param,
        T::from_f64(20.0).unwrap(),
        T::from_f64(0.2).unwrap(),
        T::from_f64(2.0 * PI).unwrap(),
    )
}

/// Derivative of Ackley test function
///
/// The same as `ackley_derivative`; however, it allows to set the parameters a, b and c.
///
/// This is the const generics version, which requires the number of parameters to be known
/// at compile time.
pub fn ackley_abc_derivative_const<const N: usize, T>(param: &[T; N], a: T, b: T, c: T) -> [T; N]
where
    T: Float + FromPrimitive + Sum,
{
    let d = T::from_usize(param.len()).unwrap();
    let n0 = T::from_f64(0.0).unwrap();
    let eps = T::epsilon();

    let norm = param.iter().map(|x| x.powi(2)).sum::<T>().sqrt();

    let f1 = (c * (param.iter().map(|&x| (c * x).cos()).sum::<T>() / d).exp()) / d;
    let f2 = if norm <= eps {
        n0
    } else {
        (a * b * (-b * norm / d.sqrt()).exp()) / (d.sqrt() * norm)
    };

    let mut out = [n0; N];
    param
        .iter()
        .zip(out.iter_mut())
        .map(|(&x, o)| *o = ((c * x).sin() * f1) + x * f2)
        .count();

    out
}

/// Hessian of Ackley test function
///
/// Calls `ackley_abc_hessian` with `a = 10`, `b = 0.2` and `c = 2*pi`
pub fn ackley_hessian<T>(param: &[T]) -> Vec<Vec<T>>
where
    T: Float + FromPrimitive + Sum + std::fmt::Debug,
{
    ackley_abc_hessian(
        param,
        T::from_f64(20.0).unwrap(),
        T::from_f64(0.2).unwrap(),
        T::from_f64(2.0 * PI).unwrap(),
    )
}

/// Hessian of Ackley test function
///
/// The same as `ackley_hessian`; however, it allows to set the parameters a, b and c.
pub fn ackley_abc_hessian<T>(param: &[T], a: T, b: T, c: T) -> Vec<Vec<T>>
where
    T: Float + FromPrimitive + Sum,
{
    let du = param.len();
    assert!(du >= 1);
    let d = T::from_usize(du).unwrap();
    let n0 = T::from_f64(0.0).unwrap();
    let eps = T::epsilon();

    // rename for convenience
    let x = param;

    let sqsum = param.iter().map(|x| x.powi(2)).sum::<T>();
    let norm = sqsum.sqrt();
    let nexp = (-b * norm / d.sqrt()).exp();

    let f1 = -c.powi(2) * (x.iter().map(|&x| (c * x).cos()).sum::<T>() / d).exp();
    let f2 = (a * b.powi(2) * nexp) / (d * sqsum);
    let f3 = (a * b * nexp) / (d.sqrt() * norm.powi(3));
    let f4 = (a * b * nexp) / (d.sqrt() * norm);
    let f5 = (a * b.powi(2) * nexp) / (d * sqsum);
    let f6 = (a * b * nexp) / (d.sqrt() * norm.powi(3));

    let mut out = vec![vec![n0; du]; du];
    for i in 0..du {
        for j in 0..du {
            if i == j {
                out[i][j] = (c * x[i]).sin().powi(2) * f1 / d.powi(2)
                    - (c * x[i]).cos() * f1 / d
                    - if norm <= eps {
                        n0
                    } else {
                        x[i].powi(2) * (f2 + f3) - f4
                    };
            } else {
                let tmp = (c * x[i]).sin() * (c * x[j]).sin() * f1 / d.powi(2)
                    + x[i] * x[j] * if norm <= eps { n0 } else { -f5 - f6 };
                out[i][j] = tmp;
                out[j][i] = tmp;
            };
        }
    }

    out
}

/// Hessian of Ackley test function
///
/// Calls `ackley_abc_hessian` with `a = 10`, `b = 0.2` and `c = 2*pi`
///
/// This is the const generics version, which requires the number of parameters to be known
/// at compile time.
pub fn ackley_hessian_const<const N: usize, T>(param: &[T; N]) -> [[T; N]; N]
where
    T: Float + FromPrimitive + Sum,
{
    ackley_abc_hessian_const(
        param,
        T::from_f64(20.0).unwrap(),
        T::from_f64(0.2).unwrap(),
        T::from_f64(2.0 * PI).unwrap(),
    )
}

/// Hessian of Ackley test function
///
/// The same as `ackley_hessian`; however, it allows to set the parameters a, b and c.
pub fn ackley_abc_hessian_const<const N: usize, T>(param: &[T; N], a: T, b: T, c: T) -> [[T; N]; N]
where
    T: Float + FromPrimitive + Sum,
{
    assert!(N >= 1);
    let d = T::from_usize(N).unwrap();
    let n0 = T::from_f64(0.0).unwrap();
    let eps = T::epsilon();

    // rename for convenience
    let x = param;

    let sqsum = param.iter().map(|x| x.powi(2)).sum::<T>();
    let norm = sqsum.sqrt();
    let nexp = (-b * norm / d.sqrt()).exp();

    let f1 = -c.powi(2) * (x.iter().map(|&x| (c * x).cos()).sum::<T>() / d).exp();
    let f2 = (a * b.powi(2) * nexp) / (d * sqsum);
    let f3 = (a * b * nexp) / (d.sqrt() * norm.powi(3));
    let f4 = (a * b * nexp) / (d.sqrt() * norm);
    let f5 = (a * b.powi(2) * nexp) / (d * sqsum);
    let f6 = (a * b * nexp) / (d.sqrt() * norm.powi(3));

    let mut out = [[n0; N]; N];
    for i in 0..N {
        for j in 0..N {
            if i == j {
                out[i][j] = (c * x[i]).sin().powi(2) * f1 / d.powi(2)
                    - (c * x[i]).cos() * f1 / d
                    - if norm <= eps {
                        n0
                    } else {
                        x[i].powi(2) * (f2 + f3) - f4
                    };
            } else {
                let tmp = (c * x[i]).sin() * (c * x[j]).sin() * f1 / d.powi(2)
                    + x[i] * x[j] * if norm <= eps { n0 } else { -f5 - f6 };
                out[i][j] = tmp;
                out[j][i] = tmp;
            };
        }
    }

    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use finitediff::FiniteDiff;
    use proptest::prelude::*;
    use std::{f32, f64};

    #[test]
    fn test_ackley_optimum() {
        assert_relative_eq!(
            ackley(&[0.0_f32, 0.0_f32, 0.0_f32]),
            0.0,
            epsilon = f32::EPSILON * 10_f32
        );
        assert_relative_eq!(
            ackley(&[0.0_f64, 0.0_f64, 0.0_f64]),
            0.0,
            epsilon = f64::EPSILON * 5_f64
        );

        let deriv = ackley_derivative(&[0.0_f64, 0.0_f64, 0.0_f64]);
        for i in 0..3 {
            assert_relative_eq!(deriv[i], 0.0, epsilon = f64::EPSILON * 3_f64);
        }

        let deriv = ackley_derivative_const(&[0.0_f64, 0.0_f64, 0.0_f64]);
        for i in 0..3 {
            assert_relative_eq!(deriv[i], 0.0, epsilon = f64::EPSILON * 3_f64);
        }
    }

    proptest! {
        #[test]
        fn test_parameters(a in -5.0..5.0, b in -5.0..5.0, c in -5.0..5.0) {
            let param = [a, b, c];
            assert_relative_eq!(
                ackley(&param),
                ackley_abc(&param, 20.0, 0.2, 2.0 * PI),
                epsilon = f64::EPSILON
            );

            let deriv1 = ackley_derivative(&param);
            let deriv2 = ackley_abc_derivative(&param, 20.0, 0.2, 2.0 * PI);
            for i in 0..3 {
                assert_relative_eq!(deriv1[i], deriv2[i], epsilon = f64::EPSILON);
            }

            let deriv1 = ackley_derivative_const(&param);
            let deriv2 = ackley_abc_derivative_const(&param, 20.0, 0.2, 2.0 * PI);
            for i in 0..3 {
                assert_relative_eq!(deriv1[i], deriv2[i], epsilon = f64::EPSILON);
            }

            let hessian1 = ackley_hessian(&param);
            let hessian2 = ackley_abc_hessian(&param, 20.0, 0.2, 2.0 * PI);
            for i in 0..3 {
                for j in 0..3 {
                    assert_relative_eq!(hessian1[i][j], hessian2[i][j], epsilon = f64::EPSILON);
                }
            }

            let hessian1 = ackley_hessian_const(&param);
            let hessian2 = ackley_abc_hessian_const(&param, 20.0, 0.2, 2.0 * PI);
            for i in 0..3 {
                for j in 0..3 {
                    assert_relative_eq!(hessian1[i][j], hessian2[i][j], epsilon = f64::EPSILON);
                }
            }
        }
    }

    proptest! {
        #[test]
        fn test_ackley_derivative_finitediff(a in -5.0..5.0,
                                             b in -5.0..5.0,
                                             c in -5.0..5.0,
                                             d in -5.0..5.0,
                                             e in -5.0..5.0,
                                             f in -5.0..5.0,
                                             g in -5.0..5.0,
                                             h in -5.0..5.0) {
            let param = [a, b, c, d, e, f, g, h];
            let derivative = ackley_derivative(&param);
            let derivative_fd = Vec::from(param).central_diff(&|x| ackley(&x));
            // println!("1: {derivative:?} at {a}/{b}");
            // println!("2: {derivative_fd:?} at {a}/{b}");
            for i in 0..derivative.len() {
                assert_relative_eq!(
                    derivative[i],
                    derivative_fd[i],
                    epsilon = 1e-5,
                    max_relative = 1e-2
                );
            }
        }
    }

    proptest! {
        #[test]
        fn test_ackley_derivative_const_finitediff(a in -5.0..5.0,
                                                   b in -5.0..5.0,
                                                   c in -5.0..5.0,
                                                   d in -5.0..5.0,
                                                   e in -5.0..5.0,
                                                   f in -5.0..5.0,
                                                   g in -5.0..5.0,
                                                   h in -5.0..5.0) {
            let param = [a, b, c, d, e, f, g, h];
            let derivative = ackley_derivative_const(&param);
            let derivative_fd = Vec::from(param).central_diff(&|x| ackley(&x));
            // println!("1: {derivative:?} at {a}/{b}");
            // println!("2: {derivative_fd:?} at {a}/{b}");
            for i in 0..derivative.len() {
                assert_relative_eq!(
                    derivative[i],
                    derivative_fd[i],
                    epsilon = 1e-5,
                    max_relative = 1e-2
                );
            }
        }
    }

    proptest! {
        #[test]
        fn test_ackley_hessian_finitediff(a in -5.0..5.0,
                                          b in -5.0..5.0,
                                          c in -5.0..5.0,
                                          d in -5.0..5.0,
                                          e in -5.0..5.0,
                                          f in -5.0..5.0,
                                          g in -5.0..5.0,
                                          h in -5.0..5.0) {
            let param = [a, b, c, d, e, f, g, h];
            let hessian = ackley_hessian(&param);
            let hessian_fd = Vec::from(param).central_hessian(&|x| ackley_derivative(&x));
            // println!("1: {hessian:?} at {a}/{b}/{c}/{d}/{e}/{f}/{g}/{h}");
            // println!("2: {hessian_fd:?} at {a}/{b}/{c}/{d}/{e}/{f}/{g}/{h}");
            for i in 0..hessian.len() {
                for j in 0..hessian.len() {
                    assert_relative_eq!(
                        hessian[i][j],
                        hessian_fd[i][j],
                        epsilon = 1e-5,
                        max_relative = 1e-2
                    );
                }
            }
        }
    }

    proptest! {
        #[test]
        fn test_ackley_hessian_const_finitediff(a in -5.0..5.0,
                                                b in -5.0..5.0,
                                                c in -5.0..5.0,
                                                d in -5.0..5.0,
                                                e in -5.0..5.0,
                                                f in -5.0..5.0,
                                                g in -5.0..5.0,
                                                h in -5.0..5.0) {
            let param = [a, b, c, d, e, f, g, h];
            let hessian = ackley_hessian_const(&param);
            let hessian_fd = Vec::from(param).central_hessian(&|x| ackley_derivative(&x));
            // println!("1: {hessian:?} at {a}/{b}/{c}/{d}/{e}/{f}/{g}/{h}");
            // println!("2: {hessian_fd:?} at {a}/{b}/{c}/{d}/{e}/{f}/{g}/{h}");
            for i in 0..hessian.len() {
                for j in 0..hessian.len() {
                    assert_relative_eq!(
                        hessian[i][j],
                        hessian_fd[i][j],
                        epsilon = 1e-5,
                        max_relative = 1e-2
                    );
                }
            }
        }
    }
}