use std::fmt::Debug;
use std::marker::PhantomData;
use crate::heuristic::r#move::Move;
use solverforge_core::domain::PlanningSolution;
pub trait LocalSearchForager<S, M>: Send + Debug
where
S: PlanningSolution,
M: Move<S>,
{
fn step_started(&mut self, best_score: S::Score, last_step_score: S::Score);
fn add_move_index(&mut self, index: usize, score: S::Score);
fn is_quit_early(&self) -> bool;
fn pick_move_index(&mut self) -> Option<(usize, S::Score)>;
}
mod improving;
pub use improving::{FirstBestScoreImprovingForager, FirstLastStepScoreImprovingForager};
pub struct AcceptedCountForager<S>
where
S: PlanningSolution,
{
accepted_count_limit: usize,
accepted_moves: Vec<(usize, S::Score)>,
_phantom: PhantomData<fn() -> S>,
}
impl<S> AcceptedCountForager<S>
where
S: PlanningSolution,
{
pub fn new(accepted_count_limit: usize) -> Self {
assert!(
accepted_count_limit > 0,
"AcceptedCountForager: accepted_count_limit must be > 0, got 0"
);
Self {
accepted_count_limit,
accepted_moves: Vec::new(),
_phantom: PhantomData,
}
}
}
impl<S> Clone for AcceptedCountForager<S>
where
S: PlanningSolution,
{
fn clone(&self) -> Self {
Self {
accepted_count_limit: self.accepted_count_limit,
accepted_moves: Vec::new(), _phantom: PhantomData,
}
}
}
impl<S> Debug for AcceptedCountForager<S>
where
S: PlanningSolution,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AcceptedCountForager")
.field("accepted_count_limit", &self.accepted_count_limit)
.field("accepted_count", &self.accepted_moves.len())
.finish()
}
}
impl<S, M> LocalSearchForager<S, M> for AcceptedCountForager<S>
where
S: PlanningSolution,
M: Move<S>,
{
fn step_started(&mut self, _best_score: S::Score, _last_step_score: S::Score) {
self.accepted_moves.clear();
}
fn add_move_index(&mut self, index: usize, score: S::Score) {
self.accepted_moves.push((index, score));
}
fn is_quit_early(&self) -> bool {
self.accepted_moves.len() >= self.accepted_count_limit
}
fn pick_move_index(&mut self) -> Option<(usize, S::Score)> {
if self.accepted_moves.is_empty() {
return None;
}
let mut best_idx = 0;
let mut best_score = self.accepted_moves[0].1;
for (i, &(_, score)) in self.accepted_moves.iter().enumerate().skip(1) {
if score > best_score {
best_idx = i;
best_score = score;
}
}
Some(self.accepted_moves.swap_remove(best_idx))
}
}
pub struct FirstAcceptedForager<S>
where
S: PlanningSolution,
{
accepted_move: Option<(usize, S::Score)>,
_phantom: PhantomData<fn() -> S>,
}
impl<S> Debug for FirstAcceptedForager<S>
where
S: PlanningSolution,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FirstAcceptedForager")
.field("has_move", &self.accepted_move.is_some())
.finish()
}
}
impl<S> FirstAcceptedForager<S>
where
S: PlanningSolution,
{
pub fn new() -> Self {
Self {
accepted_move: None,
_phantom: PhantomData,
}
}
}
impl<S> Clone for FirstAcceptedForager<S>
where
S: PlanningSolution,
{
fn clone(&self) -> Self {
Self {
accepted_move: None, _phantom: PhantomData,
}
}
}
impl<S> Default for FirstAcceptedForager<S>
where
S: PlanningSolution,
{
fn default() -> Self {
Self::new()
}
}
impl<S, M> LocalSearchForager<S, M> for FirstAcceptedForager<S>
where
S: PlanningSolution,
M: Move<S>,
{
fn step_started(&mut self, _best_score: S::Score, _last_step_score: S::Score) {
self.accepted_move = None;
}
fn add_move_index(&mut self, index: usize, score: S::Score) {
if self.accepted_move.is_none() {
self.accepted_move = Some((index, score));
}
}
fn is_quit_early(&self) -> bool {
self.accepted_move.is_some()
}
fn pick_move_index(&mut self) -> Option<(usize, S::Score)> {
self.accepted_move.take()
}
}
pub struct BestScoreForager<S>
where
S: PlanningSolution,
{
accepted_moves: Vec<(usize, S::Score)>,
_phantom: PhantomData<fn() -> S>,
}
impl<S> BestScoreForager<S>
where
S: PlanningSolution,
{
pub fn new() -> Self {
Self {
accepted_moves: Vec::new(),
_phantom: PhantomData,
}
}
}
impl<S> Default for BestScoreForager<S>
where
S: PlanningSolution,
{
fn default() -> Self {
Self::new()
}
}
impl<S> Debug for BestScoreForager<S>
where
S: PlanningSolution,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BestScoreForager")
.field("accepted_count", &self.accepted_moves.len())
.finish()
}
}
impl<S> Clone for BestScoreForager<S>
where
S: PlanningSolution,
{
fn clone(&self) -> Self {
Self {
accepted_moves: Vec::new(),
_phantom: PhantomData,
}
}
}
impl<S, M> LocalSearchForager<S, M> for BestScoreForager<S>
where
S: PlanningSolution,
M: Move<S>,
{
fn step_started(&mut self, _best_score: S::Score, _last_step_score: S::Score) {
self.accepted_moves.clear();
}
fn add_move_index(&mut self, index: usize, score: S::Score) {
self.accepted_moves.push((index, score));
}
fn is_quit_early(&self) -> bool {
false }
fn pick_move_index(&mut self) -> Option<(usize, S::Score)> {
if self.accepted_moves.is_empty() {
return None;
}
let mut best_idx = 0;
let mut best_score = self.accepted_moves[0].1;
for (i, &(_, score)) in self.accepted_moves.iter().enumerate().skip(1) {
if score > best_score {
best_idx = i;
best_score = score;
}
}
Some(self.accepted_moves.swap_remove(best_idx))
}
}
#[cfg(test)]
#[path = "forager_tests.rs"]
mod tests;