cisat/problems/
ackley.rs

1//! This is an example problem for optimizing the Ackley function
2
3use super::super::{
4    utilities::randomness::{random_gaussian_vector, random_uniform_vector},
5    utilities::Solution,
6};
7use std::{cmp::Ordering, ops::Sub};
8
9/// Specifically, we optimize Ackley in 5 dimensions
10const NUMBER_OF_DIMENSIONS: usize = 5;
11
12#[derive(Clone, Debug)]
13/// This contains solutions for the Ackley problem
14pub struct Ackley {
15    /// This contains direct objective function values
16    objective_function_value: Vec<f64>,
17    /// This contains a single quality scalar derived from objective function values
18    quality_scalar: f64,
19    /// This contains the parameters
20    x: Vec<f64>,
21}
22
23impl Solution for Ackley {
24    const NUMBER_OF_MOVE_OPERATORS: usize = 1;
25    const NUMBER_OF_OBJECTIVES: usize = 1;
26
27    fn new() -> Ackley {
28        let mut solution = Ackley {
29            objective_function_value: vec![0.0; Ackley::NUMBER_OF_OBJECTIVES],
30            x: random_uniform_vector(NUMBER_OF_DIMENSIONS, -10.0, 10.0),
31            quality_scalar: 0.0,
32        };
33        solution.evaluate();
34        solution
35    }
36
37    fn apply_move_operator(&mut self, _move_index: usize, temperature: f64) {
38        let perturbation_arg = random_uniform_vector(
39            self.x.len(),
40            -std::f64::consts::PI / 2.0,
41            std::f64::consts::PI / 2.0,
42        );
43        for i in 0..self.x.len() {
44            self.x[i] += perturbation_arg[i].tan() * temperature;
45        }
46        self.evaluate();
47    }
48
49    fn get_quality_scalar(&self) -> f64 {
50        self.quality_scalar
51    }
52}
53
54impl Ackley {
55    /// This function offers some functionality for evaluation
56    fn evaluate(&mut self) {
57        let n = self.x.len();
58        let mut fx = 0.0;
59        let mut square_sum = 0.0;
60        let mut cosine_sum = 0.0;
61        for xi in self.x.to_vec() {
62            square_sum += xi.powi(2);
63            cosine_sum += (2.0 * std::f64::consts::PI * xi).cos();
64        }
65        fx += -20.0 * (-0.2 * (0.5 * square_sum).sqrt()).exp();
66        fx -= (cosine_sum / (n as f64)).exp();
67        fx += std::f64::consts::E + 20.0;
68        self.objective_function_value = vec![fx; 1];
69        self.quality_scalar = 20.0 + std::f64::consts::E - fx;
70    }
71}
72
73impl PartialEq for Ackley {
74    fn eq(&self, other: &Self) -> bool {
75        self.quality_scalar == other.quality_scalar
76    }
77}
78
79impl Eq for Ackley {}
80
81impl PartialOrd for Ackley {
82    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
83        self.quality_scalar.partial_cmp(&other.quality_scalar)
84    }
85}
86
87impl Ord for Ackley {
88    fn cmp(&self, other: &Self) -> Ordering {
89        self.partial_cmp(other).unwrap()
90    }
91}
92
93impl Sub for Ackley {
94    type Output = f64;
95
96    fn sub(self, rhs: Self) -> Self::Output {
97        self.quality_scalar - rhs.quality_scalar
98    }
99}