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
#[cfg(test)]
#[path = "../../../tests/unit/refinement/termination/max_generation_test.rs"]
mod max_generation_test;

use crate::refinement::termination::Termination;
use crate::refinement::{Individuum, RefinementContext};

/// Stops when maximum amount of generations is exceeded.
pub struct MaxGeneration {
    limit: usize,
}

impl MaxGeneration {
    /// Creates a new instance of [`MaxGeneration`].
    pub fn new(limit: usize) -> Self {
        Self { limit }
    }
}

impl Default for MaxGeneration {
    fn default() -> Self {
        Self::new(2000)
    }
}

impl Termination for MaxGeneration {
    fn is_termination(&self, refinement_ctx: &mut RefinementContext, _: (&Individuum, bool)) -> bool {
        refinement_ctx.generation >= self.limit
    }
}