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
use Result;
use ;
/// Struct to hold the population as standard normal data points
///
/// # Example
/// ```rust
/// use haru_cmaes::fitness::PopulationZ;
/// use nalgebra::{DMatrix, Matrix3x4};
/// let static_matrix = Matrix3x4::new(
/// -0.1, 0.2, 0.3, -0.3,
/// 0.4, -0.2, -0.6, 0.6,
/// 0.7, -0.8, 0.9, -0.9,
/// );
/// let z = DMatrix::from_row_slice(3, 4, static_matrix.as_slice());
/// let pop_z = PopulationZ { z };
/// assert!(pop_z.z.shape() == (3, 4));
/// ```
/// Struct to hold the population as eigen rotated and scaled data points
///
/// # Example
/// ```rust
/// use haru_cmaes::fitness::PopulationY;
/// use nalgebra::{DMatrix, Matrix3x4};
/// let static_matrix = Matrix3x4::new(
/// 1.0, 2.0, 3.0, 3.5,
/// 4.0, 5.0, 6.0, 6.5,
/// 7.0, 8.0, 9.0, 9.5,
/// );
/// let y = DMatrix::from_row_slice(3, 4, static_matrix.as_slice());
/// let pop_y = PopulationY { y };
/// assert!(pop_y.y.shape() == (3, 4));
/// ```
/// Enum to specify optimization direction (minimization or maximization)
/// Structure to hold fitness values of a population
///
/// # Example
/// ```rust
/// use haru_cmaes::fitness::Fitness;
/// use nalgebra::DVector;
/// let fitness_values = DVector::from_vec(vec![0.1, 0.2, 0.3]);
/// let fitness = Fitness { values: fitness_values };
/// assert!(fitness.values.shape() == (3, 1));
/// ```
/// A trait for fitness evaluation
///
/// Implements the full evaluate and evaluator_dim methods for any user-defined objective function
/// Implement the `FitnessEvaluator` trait for `UserFitness` to allow it to be used as an objective function in the CMA-ES algorithm.
/// Generic fitness wrapper for user-defined objective function.
///
/// `UserFitness` allows users to define custom objective functions as simple closures
/// that take a single individual's vector and return a scalar fitness value.
///
/// The trait implementation automatically handles mapping over entire populations
/// and applying the optimization direction (min/max).
///
/// NOTE: Be sure to match the `obj_dim` with the dimension of the individuals in the population for correct evaluation.
///
/// # Example
///
/// ```rust
/// use haru_cmaes::fitness::{PopulationY, FitnessEvaluator, UserFitness, MinOrMax};
/// use nalgebra::{Matrix3x4, DMatrix};
///
/// let static_matrix = Matrix3x4::new(
/// 1.0, 2.0, 3.0, 3.5,
/// 4.0, 5.0, 6.0, 6.5,
/// 7.0, 8.0, 9.0, 9.5,
/// );
/// let y = DMatrix::from_row_slice(3, 4, static_matrix.as_slice());
/// let pop = PopulationY { y };
///
/// // Define a custom objective with a closure (sum of squares)
/// let objective_function = UserFitness::new(
/// |individual: &nalgebra::DVector<f32>| {
/// let mut sum = 0.0;
/// for i in 0..individual.len() {
/// sum += individual[i].powi(2);
/// }
/// sum
/// },
/// 4, // This needs to match the CMAES algorith initial parameter dimension.
/// MinOrMax::Min,
/// );
/// assert_eq!(objective_function.evaluator_dim().unwrap(), 4);
/// assert_eq!(objective_function.dir, MinOrMax::Min);
/// ```
,