Skip to main content

rosomaxa/termination/
max_generation.rs

1#[cfg(test)]
2#[path = "../../tests/unit/termination/max_generation_test.rs"]
3mod max_generation_test;
4
5use super::*;
6use std::marker::PhantomData;
7
8/// A termination criteria which is in terminated state when maximum amount of generations is exceeded.
9pub struct MaxGeneration<C, O, S>
10where
11    C: HeuristicContext<Objective = O, Solution = S>,
12    O: HeuristicObjective<Solution = S>,
13    S: HeuristicSolution,
14{
15    limit: usize,
16    _marker: (PhantomData<C>, PhantomData<O>, PhantomData<S>),
17}
18
19impl<C, O, S> MaxGeneration<C, O, S>
20where
21    C: HeuristicContext<Objective = O, Solution = S>,
22    O: HeuristicObjective<Solution = S>,
23    S: HeuristicSolution,
24{
25    /// Creates a new instance of `MaxGeneration`.
26    pub fn new(limit: usize) -> Self {
27        Self { limit, _marker: (Default::default(), Default::default(), Default::default()) }
28    }
29}
30
31impl<C, O, S> Termination for MaxGeneration<C, O, S>
32where
33    C: HeuristicContext<Objective = O, Solution = S>,
34    O: HeuristicObjective<Solution = S>,
35    S: HeuristicSolution,
36{
37    type Context = C;
38    type Objective = O;
39
40    fn is_termination(&self, heuristic_ctx: &mut Self::Context) -> bool {
41        heuristic_ctx.statistics().generation >= self.limit
42    }
43
44    fn estimate(&self, heuristic_ctx: &Self::Context) -> Float {
45        (heuristic_ctx.statistics().generation as Float / self.limit as Float).min(1.)
46    }
47}