mahf 0.1.0

A framework for modular construction and evaluation of metaheuristics.
Documentation
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
//! Common selection components.

use color_eyre::Section;
use eyre::{ensure, ContextCompat, WrapErr};
use rand::{seq::SliceRandom, Rng};
use serde::{Deserialize, Serialize};

use crate::{
    component::ExecResult,
    components::{
        selection::{functional as f, selection, Selection},
        Component,
    },
    population::IntoSingleRef,
    problems::SingleObjectiveProblem,
    state::random::Random,
    Individual, Problem, State,
};

/// Selects all individuals once.
#[derive(Clone, Serialize, Deserialize)]
pub struct All;

impl All {
    pub fn from_params() -> Self {
        Self
    }

    pub fn new<P: Problem>() -> Box<dyn Component<P>> {
        Box::new(Self::from_params())
    }
}

impl<P: Problem> Selection<P> for All {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        _rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        Ok(population.iter().collect())
    }
}

impl<P: Problem> Component<P> for All {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Selects no individual.
#[derive(Clone, Serialize, Deserialize)]
pub struct None;

impl None {
    pub fn from_params() -> Self {
        Self
    }

    pub fn new<P: Problem>() -> Box<dyn Component<P>> {
        Box::new(Self::from_params())
    }
}

impl<P: Problem> Selection<P> for None {
    fn select<'a>(
        &self,
        _population: &'a [Individual<P>],
        _rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        Ok(Vec::new())
    }
}

impl<P: Problem> Component<P> for None {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Select the single individual in the population `num_selected` times.
///
/// This is useful for cloning the current individual in a single-solution-based metaheuristic
/// and then performing some operation on each cloned individual e.g. for generating multiple neighbors.
///
/// # Errors
///
/// The component returns an `Err` if the population does not contain exactly one individual.
#[derive(Clone, Serialize, Deserialize)]
pub struct CloneSingle {
    /// Number of times the single individual is cloned.
    pub num_selected: u32,
}

impl CloneSingle {
    pub fn from_params(num_selected: u32) -> Self {
        Self { num_selected }
    }

    pub fn new<P: Problem>(num_selected: u32) -> Box<dyn Component<P>> {
        Box::new(Self::from_params(num_selected))
    }
}

impl<P: Problem> Selection<P> for CloneSingle {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        _rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        let single = population.into_single_ref()?;
        Ok(std::iter::repeat(single)
            .take(self.num_selected as usize)
            .collect())
    }
}

impl<P: Problem> Component<P> for CloneSingle {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Selects `num_selected` random individuals with replacement.
#[derive(Clone, Serialize, Deserialize)]
pub struct FullyRandom {
    /// Number of selected individuals.
    pub num_selected: u32,
}

impl FullyRandom {
    pub fn from_params(num_selected: u32) -> Self {
        Self { num_selected }
    }

    pub fn new<P: Problem>(num_selected: u32) -> Box<dyn Component<P>> {
        Box::new(Self::from_params(num_selected))
    }
}

impl<P: Problem> Selection<P> for FullyRandom {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        let mut selection = Vec::new();
        for _ in 0..self.num_selected {
            selection.push(population.choose(rng).unwrap());
        }
        Ok(selection)
    }
}

impl<P: Problem> Component<P> for FullyRandom {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Selects `num_selected` random individuals without replacement.
///
/// # Errors
///
/// Returns an `Err` if there is not a sufficient amount of solutions to choose from.
#[derive(Clone, Serialize, Deserialize)]
pub struct RandomWithoutRepetition {
    /// Number of selected individuals.
    pub num_selected: u32,
}

impl RandomWithoutRepetition {
    pub fn from_params(num_selected: u32) -> Self {
        Self { num_selected }
    }

    pub fn new<P: Problem>(num_selected: u32) -> Box<dyn Component<P>> {
        Box::new(Self::from_params(num_selected))
    }
}

impl<P: Problem> Selection<P> for RandomWithoutRepetition {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        let num_selected = self.num_selected as usize;
        ensure!(
            population.len() > num_selected,
            "the population does not contain enough individuals to sample without replacement"
        );
        let selection = population.choose_multiple(rng, num_selected).collect();
        Ok(selection)
    }
}

impl<P: Problem> Component<P> for RandomWithoutRepetition {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Selects `num_selected` individuals using the roulette-wheel method with replacement.
///
/// The weights for individuals are calculated using
/// [`proportional_weights`] with the `offset` as argument.
///
/// [`proportional_weights`]: f::proportional_weights
///
/// # Errors
///
/// Returns an `Err` if the population contains individuals with infinite objective value.
#[derive(Clone, Serialize, Deserialize)]
pub struct RouletteWheel {
    /// Number of selected individuals.
    pub num_selected: u32,
    /// The `offset` weight to prevent the worst individual having a weight of 0.
    pub offset: f64,
}

impl RouletteWheel {
    pub fn from_params(num_selected: u32, offset: f64) -> Self {
        Self {
            num_selected,
            offset,
        }
    }

    pub fn new<P: SingleObjectiveProblem>(num_selected: u32, offset: f64) -> Box<dyn Component<P>> {
        Box::new(Self::from_params(num_selected, offset))
    }
}

impl<P: SingleObjectiveProblem> Selection<P> for RouletteWheel {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        let weights = f::proportional_weights(population, self.offset, false)
            .wrap_err("population contains invalid objective values")
            .note("roulette wheel does not work with infinite objective values")?;
        let selection = f::sample_population_weighted(population, &weights, self.num_selected, rng)
            .wrap_err("sampling from population failed")?;
        Ok(selection)
    }
}

impl<P: SingleObjectiveProblem> Component<P> for RouletteWheel {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Selects `num_selected` individuals using stochastic universal sampling with replacement.
///
/// Note that the population is not sorted by fitness, but individuals are weighted "in place".
///
/// The weights for individuals are calculated using
/// [`proportional_weights`] with the `offset` as argument.
///
/// [`proportional_weights`]: f::proportional_weights
///
/// # Errors
///
/// Returns an `Err` if the population contains individuals with infinite objective value.
#[derive(Clone, Serialize, Deserialize)]
pub struct StochasticUniversalSampling {
    /// Number of selected individuals.
    pub num_selected: u32,
    /// The `offset` weight to prevent the worst individual having a weight of 0.
    pub offset: f64,
}

impl StochasticUniversalSampling {
    pub fn from_params(num_selected: u32, offset: f64) -> Self {
        Self {
            num_selected,
            offset,
        }
    }

    pub fn new<P: SingleObjectiveProblem>(num_selected: u32, offset: f64) -> Box<dyn Component<P>> {
        Box::new(Self::from_params(num_selected, offset))
    }
}

impl<P: SingleObjectiveProblem> Selection<P> for StochasticUniversalSampling {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        let weights = f::proportional_weights(population, self.offset, false)
            .wrap_err("population contains invalid objective values")
            .note("stochastic universal sampling does not work with infinite objective values")?;

        // Calculate the distance between selection points and the random start point
        let weights_total = weights.iter().sum();
        let gaps = weights_total / self.num_selected as f64;
        let start = rng.gen::<f64>() * gaps;
        let mut distance = start;

        // Select the individuals for which the selection point falls within their fitness range
        let mut selection = Vec::new();
        let mut sum_weights = weights[0];
        let mut i: usize = 0;
        while distance < weights_total {
            while sum_weights < distance {
                i += 1;
                sum_weights += weights[i];
            }
            selection.push(&population[i]);
            distance += gaps;
        }
        Ok(selection)
    }
}

impl<P: SingleObjectiveProblem> Component<P> for StochasticUniversalSampling {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Selects `num_selected` individuals using deterministic Tournament selection of `size` with replacement.
#[derive(Clone, Serialize, Deserialize)]
pub struct Tournament {
    /// Number of selected individuals.
    pub num_selected: u32,
    /// Tournament size.
    pub size: u32,
}

impl Tournament {
    pub fn from_params(num_selected: u32, size: u32) -> Self {
        Self { num_selected, size }
    }

    pub fn new<P: SingleObjectiveProblem>(num_selected: u32, size: u32) -> Box<dyn Component<P>> {
        Box::new(Self::from_params(num_selected, size))
    }
}

impl<P: SingleObjectiveProblem> Selection<P> for Tournament {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        ensure!(
            population.len() >= self.size as usize,
            "population size must be equal to or greater than the tournament size"
        );
        let mut selection = Vec::new();
        for _ in 0..self.num_selected {
            // Choose `size` competitors in tournament and select the winner
            let winner = population
                .choose_multiple(rng, self.size as usize)
                .min_by_key(|&i| i.objective())
                .unwrap();
            selection.push(winner);
        }
        Ok(selection)
    }
}

impl<P: SingleObjectiveProblem> Component<P> for Tournament {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Selects `num_selected` solutions with replacement using linear ranking.
#[derive(Clone, Serialize, Deserialize)]
pub struct LinearRank {
    /// Number of selected individuals.
    pub num_selected: u32,
}

impl LinearRank {
    pub fn from_params(num_selected: u32) -> Self {
        Self { num_selected }
    }

    pub fn new<P: SingleObjectiveProblem>(num_selected: u32) -> Box<dyn Component<P>> {
        Box::new(Self::from_params(num_selected))
    }
}

impl<P: SingleObjectiveProblem> Selection<P> for LinearRank {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        let weights = f::reverse_rank(population);
        let selection = f::sample_population_weighted(population, &weights, self.num_selected, rng)
            .wrap_err("sampling from population failed")?;
        Ok(selection)
    }
}

impl<P: SingleObjectiveProblem> Component<P> for LinearRank {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}

/// Selects `num_selected` solutions with replacement using exponential ranking.
#[derive(Serialize, Deserialize, Clone)]
pub struct ExponentialRank {
    /// Number of selected individuals.
    pub num_selected: u32,
    /// Base of the exponent within (0,1).
    pub base: f64,
}

impl ExponentialRank {
    pub fn from_params(num_selected: u32, base: f64) -> ExecResult<Self> {
        ensure!(
            (f64::EPSILON..1.0).contains(&base),
            "the base of the exponent must be within (0, 1)"
        );
        Ok(Self { num_selected, base })
    }

    pub fn new<P: SingleObjectiveProblem>(
        num_selected: u32,
        base: f64,
    ) -> ExecResult<Box<dyn Component<P>>> {
        Ok(Box::new(Self::from_params(num_selected, base)?))
    }
}

impl<P: SingleObjectiveProblem> Selection<P> for ExponentialRank {
    fn select<'a>(
        &self,
        population: &'a [Individual<P>],
        rng: &mut Random,
    ) -> ExecResult<Vec<&'a Individual<P>>> {
        let ranking = f::reverse_rank(population);
        let max_rank = ranking.iter().max().cloned().unwrap_or(0);
        let factor = (self.base - 1.0) / (self.base.powi(max_rank as i32) - 1.0);
        let weights: Vec<_> = ranking
            .iter()
            .map(|i| factor * (self.base.powi((max_rank - i) as i32)))
            .collect();
        let selection = f::sample_population_weighted(population, &weights, self.num_selected, rng)
            .wrap_err("sampling from population failed")?;
        Ok(selection)
    }
}

impl<P: SingleObjectiveProblem> Component<P> for ExponentialRank {
    fn execute(&self, problem: &P, state: &mut State<P>) -> ExecResult<()> {
        selection(self, problem, state)
    }
}