use crate::aco::pheromone::Pheromone;
use std::time::{Duration, Instant};
pub trait TerminationCondition<P: Pheromone> {
fn init(&mut self, pheromone: &P);
fn update_and_check(&mut self, pheromone: &P) -> bool;
}
pub struct IterationCond {
curr_iteration: usize,
iterations_limit: usize,
}
impl IterationCond {
pub fn new(iterations_limit: usize) -> Self {
Self {
curr_iteration: 0,
iterations_limit,
}
}
}
impl<P: Pheromone> TerminationCondition<P> for IterationCond {
fn init(&mut self, _pheromone: &P) {
self.curr_iteration = 0;
}
fn update_and_check(&mut self, _pheromone: &P) -> bool {
self.curr_iteration += 1;
self.curr_iteration > self.iterations_limit
}
}
pub struct TimeCond {
start_time: Instant,
duration: Duration,
}
impl TimeCond {
pub fn new(duration: Duration) -> Self {
Self {
start_time: Instant::now(),
duration,
}
}
}
impl<P: Pheromone> TerminationCondition<P> for TimeCond {
fn init(&mut self, _pheromone: &P) {
self.start_time = Instant::now()
}
fn update_and_check(&mut self, _pheromone: &P) -> bool {
let curr_duration = Instant::now() - self.start_time;
curr_duration > self.duration
}
}