use std::fmt::Debug;
use std::marker::PhantomData;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::Score;
use solverforge_scoring::Director;
use crate::heuristic::r#move::Move;
use super::decision::{
is_first_fit_improvement, select_best_fit, select_first_feasible, select_first_fit,
ScoredChoiceTracker,
};
use super::evaluation::evaluate_trial_move;
use super::Placement;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConstructionChoice {
KeepCurrent,
Select(usize),
}
pub trait ConstructionForager<S, M>: Send + Debug
where
S: PlanningSolution,
M: Move<S>,
{
fn pick_move_index<D: Director<S>>(
&self,
placement: &Placement<S, M>,
score_director: &mut D,
) -> ConstructionChoice;
}
pub struct FirstFitForager<S, M> {
_phantom: PhantomData<fn() -> (S, M)>,
}
impl<S, M> Clone for FirstFitForager<S, M> {
fn clone(&self) -> Self {
*self
}
}
impl<S, M> Copy for FirstFitForager<S, M> {}
impl<S, M> Default for FirstFitForager<S, M> {
fn default() -> Self {
Self::new()
}
}
impl<S, M> Debug for FirstFitForager<S, M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FirstFitForager").finish()
}
}
impl<S, M> FirstFitForager<S, M> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<S, M> ConstructionForager<S, M> for FirstFitForager<S, M>
where
S: PlanningSolution,
M: Move<S>,
{
fn pick_move_index<D: Director<S>>(
&self,
placement: &Placement<S, M>,
score_director: &mut D,
) -> ConstructionChoice {
let mut first_doable = None;
let baseline_score = placement
.keep_current_legal()
.then(|| score_director.calculate_score());
for (idx, m) in placement.moves.iter().enumerate() {
if !m.is_doable(score_director) {
continue;
}
if let Some(baseline_score) = baseline_score {
let candidate_score = evaluate_trial_move(score_director, m);
if is_first_fit_improvement(baseline_score, candidate_score) {
first_doable = Some(idx);
break;
}
} else {
first_doable = Some(idx);
break;
}
}
select_first_fit(first_doable)
}
}
pub struct BestFitForager<S, M> {
_phantom: PhantomData<fn() -> (S, M)>,
}
impl<S, M> Clone for BestFitForager<S, M> {
fn clone(&self) -> Self {
*self
}
}
impl<S, M> Copy for BestFitForager<S, M> {}
impl<S, M> Default for BestFitForager<S, M> {
fn default() -> Self {
Self::new()
}
}
impl<S, M> Debug for BestFitForager<S, M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BestFitForager").finish()
}
}
impl<S, M> BestFitForager<S, M> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<S, M> ConstructionForager<S, M> for BestFitForager<S, M>
where
S: PlanningSolution,
M: Move<S>,
{
fn pick_move_index<D: Director<S>>(
&self,
placement: &Placement<S, M>,
score_director: &mut D,
) -> ConstructionChoice {
let baseline_score = placement
.keep_current_legal()
.then(|| score_director.calculate_score());
let mut tracker = ScoredChoiceTracker::default();
for (idx, m) in placement.moves.iter().enumerate() {
if !m.is_doable(score_director) {
continue;
}
let score = evaluate_trial_move(score_director, m);
tracker.consider(idx, score);
}
select_best_fit(tracker, baseline_score)
}
}
pub struct FirstFeasibleForager<S, M> {
_phantom: PhantomData<fn() -> (S, M)>,
}
impl<S, M> Clone for FirstFeasibleForager<S, M> {
fn clone(&self) -> Self {
*self
}
}
impl<S, M> Copy for FirstFeasibleForager<S, M> {}
impl<S, M> Default for FirstFeasibleForager<S, M> {
fn default() -> Self {
Self::new()
}
}
impl<S, M> Debug for FirstFeasibleForager<S, M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FirstFeasibleForager").finish()
}
}
impl<S, M> FirstFeasibleForager<S, M> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<S, M> ConstructionForager<S, M> for FirstFeasibleForager<S, M>
where
S: PlanningSolution,
M: Move<S>,
{
fn pick_move_index<D: Director<S>>(
&self,
placement: &Placement<S, M>,
score_director: &mut D,
) -> ConstructionChoice {
let baseline_score = placement
.keep_current_legal()
.then(|| score_director.calculate_score());
let mut fallback_tracker = ScoredChoiceTracker::default();
let mut first_feasible = None;
for (idx, m) in placement.moves.iter().enumerate() {
if !m.is_doable(score_director) {
continue;
}
let score = evaluate_trial_move(score_director, m);
if score.is_feasible() {
first_feasible = Some(idx);
break;
}
fallback_tracker.consider(idx, score);
}
select_first_feasible(first_feasible, fallback_tracker, baseline_score)
}
}
pub struct WeakestFitForager<S, M> {
strength_fn: fn(&M, &S) -> i64,
_phantom: PhantomData<fn() -> S>,
}
impl<S, M> Clone for WeakestFitForager<S, M> {
fn clone(&self) -> Self {
*self
}
}
impl<S, M> Copy for WeakestFitForager<S, M> {}
impl<S, M> Debug for WeakestFitForager<S, M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WeakestFitForager").finish()
}
}
impl<S, M> WeakestFitForager<S, M> {
pub fn new(strength_fn: fn(&M, &S) -> i64) -> Self {
Self {
strength_fn,
_phantom: PhantomData,
}
}
pub(crate) fn strength(&self, mov: &M, solution: &S) -> i64 {
(self.strength_fn)(mov, solution)
}
}
impl<S, M> ConstructionForager<S, M> for WeakestFitForager<S, M>
where
S: PlanningSolution,
S::Score: Score,
M: Move<S>,
{
fn pick_move_index<D: Director<S>>(
&self,
placement: &Placement<S, M>,
score_director: &mut D,
) -> ConstructionChoice {
let mut best_idx: Option<usize> = None;
let mut min_strength: Option<i64> = None;
for (idx, m) in placement.moves.iter().enumerate() {
if !m.is_doable(score_director) {
continue;
}
let strength = self.strength(m, score_director.working_solution());
let is_weaker = match min_strength {
None => true,
Some(best) => strength < best,
};
if is_weaker {
best_idx = Some(idx);
min_strength = Some(strength);
}
}
let Some(best_idx) = best_idx else {
return ConstructionChoice::KeepCurrent;
};
if !placement.keep_current_legal() {
return ConstructionChoice::Select(best_idx);
}
let baseline_score = score_director.calculate_score();
let candidate_score = evaluate_trial_move(score_director, &placement.moves[best_idx]);
if candidate_score > baseline_score {
ConstructionChoice::Select(best_idx)
} else {
ConstructionChoice::KeepCurrent
}
}
}
pub struct StrongestFitForager<S, M> {
strength_fn: fn(&M, &S) -> i64,
_phantom: PhantomData<fn() -> S>,
}
impl<S, M> Clone for StrongestFitForager<S, M> {
fn clone(&self) -> Self {
*self
}
}
impl<S, M> Copy for StrongestFitForager<S, M> {}
impl<S, M> Debug for StrongestFitForager<S, M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StrongestFitForager").finish()
}
}
impl<S, M> StrongestFitForager<S, M> {
pub fn new(strength_fn: fn(&M, &S) -> i64) -> Self {
Self {
strength_fn,
_phantom: PhantomData,
}
}
pub(crate) fn strength(&self, mov: &M, solution: &S) -> i64 {
(self.strength_fn)(mov, solution)
}
}
impl<S, M> ConstructionForager<S, M> for StrongestFitForager<S, M>
where
S: PlanningSolution,
S::Score: Score,
M: Move<S>,
{
fn pick_move_index<D: Director<S>>(
&self,
placement: &Placement<S, M>,
score_director: &mut D,
) -> ConstructionChoice {
let mut best_idx: Option<usize> = None;
let mut max_strength: Option<i64> = None;
for (idx, m) in placement.moves.iter().enumerate() {
if !m.is_doable(score_director) {
continue;
}
let strength = self.strength(m, score_director.working_solution());
let is_stronger = match max_strength {
None => true,
Some(best) => strength > best,
};
if is_stronger {
best_idx = Some(idx);
max_strength = Some(strength);
}
}
let Some(best_idx) = best_idx else {
return ConstructionChoice::KeepCurrent;
};
if !placement.keep_current_legal() {
return ConstructionChoice::Select(best_idx);
}
let baseline_score = score_director.calculate_score();
let candidate_score = evaluate_trial_move(score_director, &placement.moves[best_idx]);
if candidate_score > baseline_score {
ConstructionChoice::Select(best_idx)
} else {
ConstructionChoice::KeepCurrent
}
}
}
#[cfg(test)]
mod tests;