From eac17bcf939115a122eab5a097abacff486e730c Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 17 Oct 2025 21:46:08 +0200 Subject: [PATCH] feat: add CloserThanTerminatingCondition --- env/src/terminating/mod.rs | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/env/src/terminating/mod.rs b/env/src/terminating/mod.rs index c63ac89ecee48d52151336f4a0c9fa647511e411..2ede8a214f2a1f50d6acfb03b9503874f2998c69 100644 --- a/env/src/terminating/mod.rs +++ b/env/src/terminating/mod.rs @@ -1,3 +1,5 @@ +use nalgebra::{allocator::Allocator, DefaultAllocator, Dim, OVector}; + use crate::{binary_string::BinaryString, local_search::{LocalSearchCandidate, LocalSearchStats}}; pub trait TerminatingCondition @@ -59,6 +61,59 @@ where } } +pub struct CloserThanTerminatingCondition { + target: T, + max_distance: f64, + remember_match: bool, + matched: bool, +} + +impl CloserThanTerminatingCondition { + pub fn new(target: T, max_distance: f64) -> Self { + Self { + target, + max_distance, + remember_match: false, + matched: false, + } + } + + pub fn new_remembered(target: T, max_distance: f64) -> Self { + Self { + target, + max_distance, + remember_match: true, + matched: false, + } + } + + pub fn reset_match(self: &mut Self) { + self.matched = false; + } +} + +impl TerminatingCondition, TResult> for CloserThanTerminatingCondition> +where + D: Dim, + DefaultAllocator: Allocator, + TResult: Clone, +{ + fn should_terminate( + self: &mut Self, + candidate: &LocalSearchCandidate, TResult>, + _: &LocalSearchStats, TResult>, + _: usize + ) -> bool { + let matched = (&candidate.pos - &self.target).magnitude() < self.max_distance; + + if matched && self.remember_match { + self.matched = true; + } + + matched || self.matched + } +} + pub struct NoBetterForCyclesTerminatingCondition { cycles: usize }