From e1b41b4117f573e8a6345cc1807edd0f273694f6 Mon Sep 17 00:00:00 2001 From: Stefan Lapers Date: Tue, 10 Nov 2026 13:48:03 +0100 Subject: [PATCH] fix(solver): late acceptance re-anchors only on a dominating level change LateAcceptanceAcceptor refilled its whole history with every new best score. After the first improvement every entry equals the best, so a move is accepted only when it scores at least the best, and each further best refills again: from then on the acceptor is hill climbing with sideways moves, not late acceptance, and searches that rely on accepting worse moves to leave a local optimum settle early. The history is now re-anchored to the best score only when one of its dominating levels changes (the hard level of a hard/soft score; hard and medium for hard/medium/soft), which keeps the guard against stale entries admitting moves that hold a newly reached level but ruin the lower ones. Improvements in the lowest level alone leave the history as it is, and a single-level score never re-anchors, as the published algorithm has it. Tests: two cases that the previous code fails (a lowest-level improvement keeps the late threshold, for a single-level and a hard/soft score) and one that both pass (a hard-level change re-anchors). --- .../localsearch/acceptor/late_acceptance.rs | 37 ++++++++++--- .../src/phase/localsearch/acceptor/tests.rs | 54 ++++++++++++++++++- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/crates/solverforge-solver/src/phase/localsearch/acceptor/late_acceptance.rs b/crates/solverforge-solver/src/phase/localsearch/acceptor/late_acceptance.rs index c8e18361..697989ee 100644 --- a/crates/solverforge-solver/src/phase/localsearch/acceptor/late_acceptance.rs +++ b/crates/solverforge-solver/src/phase/localsearch/acceptor/late_acceptance.rs @@ -3,6 +3,7 @@ use std::fmt::Debug; use solverforge_core::domain::PlanningSolution; +use solverforge_core::Score; use super::Acceptor; use crate::heuristic::r#move::MoveTabuSignature; @@ -12,6 +13,17 @@ 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 (the hard level of a hard/soft score; the hard and medium +/// levels of a hard/medium/soft score), so that entries from before a +/// feasibility change cannot admit moves that keep the new level but ruin +/// the lower ones. A single-level score 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 { // Size of the late acceptance list. late_acceptance_size: usize, @@ -19,8 +31,8 @@ pub(crate) struct LateAcceptanceAcceptor { score_history: Vec>, // 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, } @@ -110,12 +122,25 @@ impl Acceptor for LateAcceptanceAcceptor { 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 level above the lowest differs between two scores. +fn dominating_level_changed(previous: &Sc, next: &Sc) -> bool { + (0..Sc::levels_count().saturating_sub(1)) + .any(|level| previous.level_number(level) != next.level_number(level)) +} diff --git a/crates/solverforge-solver/src/phase/localsearch/acceptor/tests.rs b/crates/solverforge-solver/src/phase/localsearch/acceptor/tests.rs index 7f75d12c..0d2cdbf9 100644 --- a/crates/solverforge-solver/src/phase/localsearch/acceptor/tests.rs +++ b/crates/solverforge-solver/src/phase/localsearch/acceptor/tests.rs @@ -1,5 +1,5 @@ use solverforge_core::domain::PlanningSolution; -use solverforge_core::score::SoftScore; +use solverforge_core::score::{HardSoftScore, SoftScore}; use std::any::Any; use super::*; @@ -22,6 +22,23 @@ impl PlanningSolution for DummySolution { } } +#[derive(Clone, Debug)] +struct HardSoftSolution { + score: Option, +} + +impl PlanningSolution for HardSoftSolution { + type Score = HardSoftScore; + + fn score(&self) -> Option { + self.score + } + + fn set_score(&mut self, score: Option) { + self.score = score; + } +} + fn signature( descriptor_index: usize, variable_name: &'static str, @@ -112,6 +129,41 @@ 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::::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::::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::::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 tabu_search_blocks_recent_entities_and_allows_aspiration() { let mut acceptor =