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
#[cfg(test)]
#[path = "../../tests/unit/termination/max_generation_test.rs"]
mod max_generation_test;
use super::*;
use std::marker::PhantomData;
pub struct MaxGeneration<C, O, S>
where
C: HeuristicContext<Objective = O, Solution = S>,
O: HeuristicObjective<Solution = S>,
S: HeuristicSolution,
{
limit: usize,
_marker: (PhantomData<C>, PhantomData<O>, PhantomData<S>),
}
impl<C, O, S> MaxGeneration<C, O, S>
where
C: HeuristicContext<Objective = O, Solution = S>,
O: HeuristicObjective<Solution = S>,
S: HeuristicSolution,
{
pub fn new(limit: usize) -> Self {
Self { limit, _marker: (Default::default(), Default::default(), Default::default()) }
}
}
impl<C, O, S> Termination for MaxGeneration<C, O, S>
where
C: HeuristicContext<Objective = O, Solution = S>,
O: HeuristicObjective<Solution = S>,
S: HeuristicSolution,
{
type Context = C;
type Objective = O;
fn is_termination(&self, heuristic_ctx: &mut Self::Context) -> bool {
heuristic_ctx.statistics().generation >= self.limit
}
fn estimate(&self, heuristic_ctx: &Self::Context) -> f64 {
(heuristic_ctx.statistics().generation as f64 / self.limit as f64).min(1.)
}
}