Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use std::fmt::Debug;

use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::ScoreLevel;
use solverforge_core::Score;

use super::Acceptor;
use crate::heuristic::r#move::MoveTabuSignature;
Expand All @@ -12,15 +14,27 @@ use crate::heuristic::r#move::MoveTabuSignature;
/// Maintains a circular buffer of recent scores and accepts moves that
/// are better than a score from N steps ago.
///
/// The buffer is filled with the initial score when the phase starts and
/// re-anchored to the best score only when a dominating level of that best
/// score changes: every hard and medium level (the hard level of a hard/soft
/// score, the hard and medium levels of a hard/medium/soft score, the hard
/// levels of a bendable score), so that entries from before a feasibility
/// change cannot admit moves that keep the new level but ruin the soft ones.
/// A score with only soft levels never re-anchors. Improvements in
/// the lowest level alone leave the buffer as it is: re-anchoring on those
/// would leave every entry equal to the best, after which only moves at
/// least as good as the best could ever be accepted and the search would
/// degrade into hill climbing.
///
pub(crate) struct LateAcceptanceAcceptor<S: PlanningSolution> {
// Size of the late acceptance list.
late_acceptance_size: usize,
// Circular buffer of historical scores.
score_history: Vec<Option<S::Score>>,
// Current index in the buffer.
current_index: usize,
// Best score seen so far in this phase; the buffer resets to it whenever a
// new best is committed, re-anchoring the acceptance window.
// Best score seen so far in this phase; the buffer is re-anchored to it
// when one of its dominating levels changes.
best_score: Option<S::Score>,
}

Expand Down Expand Up @@ -110,12 +124,27 @@ impl<S: PlanningSolution> Acceptor<S> for LateAcceptanceAcceptor<S> {
self.score_history[self.current_index] = Some(*step_score);
self.current_index = (self.current_index + 1) % self.late_acceptance_size;

// Re-anchor the whole window on a new best so the acceptance bar does
// not still quote pre-improvement scores.
let new_best = self.best_score.is_none_or(|best| step_score > &best);
if new_best {
// Re-anchor the window only when a dominating level of the best score
// changes; see the type documentation for why lowest-level
// improvements must leave the history alone.
let re_anchor = match self.best_score {
Some(best) if step_score > &best => dominating_level_changed(&best, step_score),
Some(_) => false,
None => true,
};
if self.best_score.is_none_or(|best| step_score > &best) {
self.best_score = Some(*step_score);
}
if re_anchor {
self.score_history.fill(Some(*step_score));
}
}
}

/// Whether any hard or medium level differs between two scores.
fn dominating_level_changed<Sc: Score>(previous: &Sc, next: &Sc) -> bool {
(0..Sc::levels_count()).any(|level| {
Sc::level_label(level) != ScoreLevel::Soft
&& previous.level_number(level) != next.level_number(level)
})
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::SoftScore;
use solverforge_core::score::{BendableScore, HardSoftScore, SoftScore};
use std::any::Any;

use super::*;
Expand All @@ -22,6 +22,40 @@ impl PlanningSolution for DummySolution {
}
}

#[derive(Clone, Debug)]
struct BendableSolution {
score: Option<BendableScore<1, 2>>,
}

impl PlanningSolution for BendableSolution {
type Score = BendableScore<1, 2>;

fn score(&self) -> Option<Self::Score> {
self.score
}

fn set_score(&mut self, score: Option<Self::Score>) {
self.score = score;
}
}

#[derive(Clone, Debug)]
struct HardSoftSolution {
score: Option<HardSoftScore>,
}

impl PlanningSolution for HardSoftSolution {
type Score = HardSoftScore;

fn score(&self) -> Option<Self::Score> {
self.score
}

fn set_score(&mut self, score: Option<Self::Score>) {
self.score = score;
}
}

fn signature(
descriptor_index: usize,
variable_name: &'static str,
Expand Down Expand Up @@ -112,6 +146,68 @@ fn late_acceptance_uses_late_score_threshold() {
assert!(!acceptor.is_accepted(&SoftScore::of(-10), &SoftScore::of(-15), None));
}

#[test]
fn late_acceptance_keeps_history_after_lowest_level_improvement() {
let mut acceptor = LateAcceptanceAcceptor::<DummySolution>::new(5);
acceptor.phase_started(&SoftScore::of(-10));
acceptor.step_ended(&SoftScore::of(-8), None);

// The late entry is still the initial -10, so a move worse than the new
// best -8 but better than -10 is accepted. Re-anchoring on the best would
// reject it and leave nothing but hill climbing.
assert!(acceptor.is_accepted(&SoftScore::of(-8), &SoftScore::of(-9), None));
assert!(!acceptor.is_accepted(&SoftScore::of(-8), &SoftScore::of(-11), None));
}

#[test]
fn late_acceptance_keeps_history_after_soft_only_improvement() {
let mut acceptor = LateAcceptanceAcceptor::<HardSoftSolution>::new(5);
acceptor.phase_started(&HardSoftScore::of(0, -10));
acceptor.step_ended(&HardSoftScore::of(0, -8), None);

assert!(acceptor.is_accepted(&HardSoftScore::of(0, -8), &HardSoftScore::of(0, -9), None));
}

#[test]
fn late_acceptance_re_anchors_when_a_dominating_level_changes() {
let mut acceptor = LateAcceptanceAcceptor::<HardSoftSolution>::new(5);
acceptor.phase_started(&HardSoftScore::of(-1, -10));
acceptor.step_ended(&HardSoftScore::of(0, -20), None);

// Every late entry is now the feasible best: a move that keeps the hard
// level but worsens the soft level below it is no longer admitted by a
// stale infeasible entry.
assert!(!acceptor.is_accepted(&HardSoftScore::of(0, -20), &HardSoftScore::of(0, -25), None));
assert!(acceptor.is_accepted(&HardSoftScore::of(0, -20), &HardSoftScore::of(0, -20), None));
}

#[test]
fn late_acceptance_keeps_history_after_bendable_soft_improvement() {
let mut acceptor = LateAcceptanceAcceptor::<BendableSolution>::new(5);
acceptor.phase_started(&BendableScore::of([0], [-10, -10]));
// The first soft level improves: a soft level, so the history stays.
acceptor.step_ended(&BendableScore::of([0], [-8, -10]), None);

assert!(acceptor.is_accepted(
&BendableScore::of([0], [-8, -10]),
&BendableScore::of([0], [-9, -10]),
None
));
}

#[test]
fn late_acceptance_re_anchors_when_a_bendable_hard_level_changes() {
let mut acceptor = LateAcceptanceAcceptor::<BendableSolution>::new(5);
acceptor.phase_started(&BendableScore::of([-1], [-10, -10]));
acceptor.step_ended(&BendableScore::of([0], [-20, -20]), None);

assert!(!acceptor.is_accepted(
&BendableScore::of([0], [-20, -20]),
&BendableScore::of([0], [-25, -20]),
None
));
}

#[test]
fn tabu_search_blocks_recent_entities_and_allows_aspiration() {
let mut acceptor =
Expand Down
Loading