genetic_algorithm_fn/
test_functions.rs

1/// The inner part of the 3d hartman function that is loop over for
2/// each compution
3fn hartman_inner_function(idx_i: usize, x: f64, y: f64, z: f64) -> f64 {
4    // A matrix, represented by the columns always use with on dimension of `x`
5    let a_x = [3.0, 0.1, 3.0, 0.1];
6    let a_y = [10.0, 10.0, 10.0, 10.0];
7    let a_z = [30.0, 35.0, 30.0, 35.0];
8    // P matrix, represented by the columns always use with on dimension of `x`
9    let p_x = [0.3689, 0.4699, 0.1091, 0.0381];
10    let p_y = [0.1170, 0.4387, 0.8732, 0.5743];
11    let p_z = [0.2673, 0.7470, 0.5547, 0.8828];
12    // final computation
13    -(a_x[idx_i] * (x - p_x[idx_i]).powi(2)
14        + a_y[idx_i] * (y - p_y[idx_i]).powi(2)
15        + a_z[idx_i] * (z - p_z[idx_i]).powi(2))
16}
17/// The hartman function taken from https://www.sfu.ca/~ssurjano/hart3.html
18pub fn hartman_3_dimensional(x: f64, y: f64, z: f64) -> f64 {
19    -(1.0 * hartman_inner_function(0, x, y, z).exp()
20        + 1.2 * hartman_inner_function(1, x, y, z).exp()
21        + 3.0 * hartman_inner_function(2, x, y, z).exp()
22        + 3.2 * hartman_inner_function(3, x, y, z).exp())
23}
24
25mod tests {
26    use super::*;
27    mod test_full_function {
28        use super::*;
29        #[test]
30        fn origin() {
31            assert_eq!(hartman_3_dimensional(0.0, 0.0, 0.0), -0.06797411659013469)
32        }
33        #[test]
34        fn global_minima() {
35            assert_eq!(
36                hartman_3_dimensional(0.114614, 0.555649, 0.852547),
37                -3.8627797869493365
38            )
39        }
40    }
41    mod test_inner_function {
42        use super::*;
43        #[test]
44        fn origin_iteration_0() {
45            assert_eq!(
46                hartman_inner_function(0, 0.114614, 0.555649, 0.852547),
47                -12.393535091668001
48            )
49        }
50        #[test]
51        fn origin_iteration_1() {
52            assert_eq!(
53                hartman_inner_function(1, 0.114614, 0.555649, 0.852547),
54                -0.5392994225046004
55            )
56        }
57        #[test]
58        fn origin_iteration_2() {
59            assert_eq!(
60                hartman_inner_function(2, 0.114614, 0.555649, 0.852547),
61                -3.6698626508680015
62            )
63        }
64        #[test]
65        fn origin_iteration_3() {
66            assert_eq!(
67                hartman_inner_function(3, 0.114614, 0.555649, 0.852547),
68                -0.036097577544599975
69            )
70        }
71    }
72}