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

// Copyright 2018 Stefan Kroboth
//
// copied, modified, or distributed except according to those terms.

//! # Rastrigin test function
//!
//! Defined as
//!
//! f(x_1, x_2, ..., x_n) = a * n + \sum_{i=1}^{n} \left[ x_i^2 - a * cos(2 * pi * x_i) \right]
//!
//! where x_i \in [-5.12, 5.12] and a = 10
//!
//! 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;

/// Rastrigin test function
///
/// Defined as
///
/// f(x_1, x_2, ..., x_n) = a * n + \sum_{i=1}^{n} \left[ x_i^2 - a * cos(2 * pi * x_i) \right]
///
/// where x_i \in [-5.12, 5.12] and a = 10
///
/// The global minimum is at f(x_1, x_2, ..., x_n) = f(0, 0, ..., 0) = 0.
pub fn rastrigin<T: Float + FromPrimitive + Sum>(param: &[T]) -> T {
rastrigin_a(param, T::from_f64(10.0).unwrap())
}

/// Rastrigin test function
///
/// The same as rastrigin; however, it allows to set the parameter a.
pub fn rastrigin_a<T: Float + FromPrimitive + Sum>(param: &[T], a: T) -> T {
a * T::from_usize(param.len()).unwrap()
+ param
.iter()
.map(|&x| x.powi(2) - a * (T::from_f64(2.0 * PI).unwrap() * x).cos())
.sum()
}

mod tests {
// use super::{rastrigin, rastrigin_a};
// use std;

#[test]
fn test_rastrigin_optimum() {
assert!(::rastrigin(&[0.0_f32, 0.0_f32]).abs() < ::std::f32::EPSILON);
assert!(::rastrigin(&[0.0_f64, 0.0_f64]).abs() < ::std::f64::EPSILON);
}

#[test]
fn test_parameter_a() {
assert!(
::rastrigin(&[0.0_f32, 0.0_f32]).abs()
== ::rastrigin_a(&[0.0_f32, 0.0_f32], 10.0).abs()
);
}
}