use std::fmt::Debug;
use solverforge_core::domain::PlanningSolution;
use super::Acceptor;
pub struct StepCountingHillClimbingAcceptor<S: PlanningSolution> {
step_count_limit: u64,
steps_since_improvement: u64,
best_score: Option<S::Score>,
}
impl<S: PlanningSolution> Debug for StepCountingHillClimbingAcceptor<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StepCountingHillClimbingAcceptor")
.field("step_count_limit", &self.step_count_limit)
.field("steps_since_improvement", &self.steps_since_improvement)
.finish()
}
}
impl<S: PlanningSolution> Clone for StepCountingHillClimbingAcceptor<S> {
fn clone(&self) -> Self {
Self {
step_count_limit: self.step_count_limit,
steps_since_improvement: self.steps_since_improvement,
best_score: self.best_score,
}
}
}
impl<S: PlanningSolution> StepCountingHillClimbingAcceptor<S> {
pub fn new(step_count_limit: u64) -> Self {
Self {
step_count_limit,
steps_since_improvement: 0,
best_score: None,
}
}
}
impl<S: PlanningSolution> Default for StepCountingHillClimbingAcceptor<S> {
fn default() -> Self {
Self::new(100)
}
}
impl<S: PlanningSolution> Acceptor<S> for StepCountingHillClimbingAcceptor<S> {
fn is_accepted(&mut self, last_step_score: &S::Score, move_score: &S::Score) -> bool {
if move_score > last_step_score {
return true;
}
self.steps_since_improvement < self.step_count_limit
}
fn phase_started(&mut self, initial_score: &S::Score) {
self.best_score = Some(*initial_score);
self.steps_since_improvement = 0;
}
fn step_ended(&mut self, step_score: &S::Score) {
let improved = match &self.best_score {
Some(best) => step_score > best,
None => true,
};
if improved {
self.best_score = Some(*step_score);
self.steps_since_improvement = 0;
} else {
self.steps_since_improvement += 1;
}
}
fn phase_ended(&mut self) {
self.best_score = None;
self.steps_since_improvement = 0;
}
}
#[cfg(test)]
#[path = "step_counting_tests.rs"]
mod tests;