~ruther/ctu-fee-eoa

eac17bcf939115a122eab5a097abacff486e730c — Rutherther a month ago 3a92d73
feat: add CloserThanTerminatingCondition
1 files changed, 55 insertions(+), 0 deletions(-)

M env/src/terminating/mod.rs
M env/src/terminating/mod.rs => env/src/terminating/mod.rs +55 -0
@@ 1,3 1,5 @@
use nalgebra::{allocator::Allocator, DefaultAllocator, Dim, OVector};

use crate::{binary_string::BinaryString, local_search::{LocalSearchCandidate, LocalSearchStats}};

pub trait TerminatingCondition<TInput, TResult>


@@ 59,6 61,59 @@ where
    }
}

pub struct CloserThanTerminatingCondition<T> {
    target: T,
    max_distance: f64,
    remember_match: bool,
    matched: bool,
}

impl<T> CloserThanTerminatingCondition<T> {
    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<D, TResult> TerminatingCondition<OVector<f64, D>, TResult> for CloserThanTerminatingCondition<OVector<f64, D>>
where
    D: Dim,
    DefaultAllocator: Allocator<D>,
    TResult: Clone,
{
    fn should_terminate(
        self: &mut Self,
        candidate: &LocalSearchCandidate<OVector<f64, D>, TResult>,
        _: &LocalSearchStats<OVector<f64, D>, 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
}