Skip to content

Commit

Permalink
Refactor heuristic filter
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Sep 11, 2023
1 parent 4cb80e2 commit 15066ce
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
33 changes: 19 additions & 14 deletions vrp-core/src/solver/heuristic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::construction::heuristics::*;
use crate::models::common::{has_multi_dim_demand, MultiDimLoad, SingleDimLoad};
use crate::models::common::{has_multi_dim_demand, Dimensions, MultiDimLoad, SingleDimLoad, ValueDimension};
use crate::models::GoalContext;
use crate::rosomaxa::get_default_selection_size;
use crate::solver::search::*;
Expand Down Expand Up @@ -52,20 +52,26 @@ pub type TargetHeuristicGroup = HeuristicSearchGroup<RefinementContext, GoalCont
/// A type alias for evolution config builder.
pub type ProblemConfigBuilder = EvolutionConfigBuilder<RefinementContext, GoalContext, InsertionContext, String>;

/// A wrapper around heuristic filter function.
pub struct HeuristicFilter {
filter: Arc<dyn Fn(&str) -> bool + Send + Sync>,
/// A type to filter meta heuristics by name. Returns true if heuristic can be used.
pub type HeuristicFilterFn = Arc<dyn Fn(&str) -> bool + Send + Sync>;

/// A type to use a filtering by meta heuristics name.
/// The corresponding function returns true if heuristic can be used.
pub trait HeuristicFilter {
/// Gets heuristic filter.
fn get_heuristic_filter(&self) -> Option<HeuristicFilterFn>;

/// Sets heuristic filter.
fn set_heuristic_filter(&mut self, heuristic_filter: Arc<dyn Fn(&str) -> bool + Send + Sync>);
}

impl HeuristicFilter {
/// Creates a new instance of `HeuristicFilter`.
pub fn new<F: Fn(&str) -> bool + Send + Sync + 'static>(filter: F) -> Self {
Self { filter: Arc::new(filter) }
impl HeuristicFilter for Dimensions {
fn get_heuristic_filter(&self) -> Option<HeuristicFilterFn> {
self.get_value("heuristic_filter").cloned()
}

/// Checks whether heuristic (or method) is enabled.
pub fn is_enabled(&self, heuristic_name: &str) -> bool {
(self.filter)(heuristic_name)
fn set_heuristic_filter(&mut self, heuristic_filter: HeuristicFilterFn) {
self.set_value("heuristic_filter", heuristic_filter);
}
}

Expand Down Expand Up @@ -529,8 +535,7 @@ mod dynamic {
),
];

let heuristic_filter =
problem.extras.get(HEURISTIC_FILTER_KEY).and_then(|s| s.downcast_ref::<HeuristicFilter>());
let heuristic_filter = problem.extras.get_heuristic_filter();

recreates
.iter()
Expand All @@ -544,7 +549,7 @@ mod dynamic {
})
})
.chain(mutations)
.filter(|(_, name, _)| heuristic_filter.map_or(true, |filter| filter.is_enabled(name.as_str())))
.filter(|(_, name, _)| heuristic_filter.as_ref().map_or(true, |filter| (filter)(name.as_str())))
.collect::<Vec<_>>()
}

Expand Down
3 changes: 0 additions & 3 deletions vrp-core/src/solver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ pub mod search;

mod heuristic;

/// A key to store a filter for heuristic methods applied by dynamic hyper-heuristic.
pub const HEURISTIC_FILTER_KEY: &str = "heuristic_filter";

/// A key to store tabu list.
const TABU_LIST_KEY: i32 = 1;
/// A key to store solution order information.
Expand Down
7 changes: 2 additions & 5 deletions vrp-scientific/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ mod routing;
pub use self::routing::CoordIndex;

use vrp_core::models::Extras;
use vrp_core::solver::{HeuristicFilter, HEURISTIC_FILTER_KEY};
use vrp_core::solver::HeuristicFilter;

pub(crate) fn get_extras(coord_index: CoordIndex) -> Extras {
let mut extras = Extras::default();

extras.insert("coord_index".to_string(), Arc::new(coord_index));
extras.insert(
HEURISTIC_FILTER_KEY.to_string(),
Arc::new(HeuristicFilter::new(|name| name != "local_reschedule_departure")),
);
extras.set_heuristic_filter(Arc::new(|name| name != "local_reschedule_departure"));

extras
}
Expand Down

0 comments on commit 15066ce

Please sign in to comment.