From 1e62f4c18b72b3c6458192ceb35840a80147c300 Mon Sep 17 00:00:00 2001 From: reinterpretcat Date: Wed, 16 Oct 2024 20:27:16 +0200 Subject: [PATCH] Avoid cloning insertion cost in evaluation logic --- Cargo.toml | 1 + experiments/heuristic-research/Cargo.toml | 2 +- vrp-core/Cargo.toml | 1 + vrp-core/src/construction/heuristics/evaluators.rs | 6 +++--- vrp-core/src/construction/heuristics/insertions.rs | 11 ++++++++--- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 521cce45f..06fd04f89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ rand = { version = "0.8.5", features = ["small_rng"] } rayon = "1.10.0" rustc-hash = "2.0.0" paste = "1.0.15" +lazy_static = "1.5.0" # dev dependencies criterion = "0.5.1" diff --git a/experiments/heuristic-research/Cargo.toml b/experiments/heuristic-research/Cargo.toml index d3e107d24..fb65e2c8f 100644 --- a/experiments/heuristic-research/Cargo.toml +++ b/experiments/heuristic-research/Cargo.toml @@ -21,10 +21,10 @@ vrp-scientific.workspace = true serde.workspace = true serde_json.workspace = true +lazy_static.workspace = true plotters = "0.3.7" plotters-canvas = "0.3.0" itertools = "0.13.0" wasm-bindgen = "0.2.93" web-sys = { version = "0.3.70", features = ["HtmlCanvasElement", "console"] } -lazy_static = "1.5.0" diff --git a/vrp-core/Cargo.toml b/vrp-core/Cargo.toml index 1627c60fe..f9e1c8b40 100644 --- a/vrp-core/Cargo.toml +++ b/vrp-core/Cargo.toml @@ -18,6 +18,7 @@ rand.workspace = true rayon.workspace = true rustc-hash.workspace = true paste.workspace = true +lazy_static.workspace = true nohash-hasher = "0.2.0" tinyvec = { version = "1.8.0", features = ["alloc"] } diff --git a/vrp-core/src/construction/heuristics/evaluators.rs b/vrp-core/src/construction/heuristics/evaluators.rs index a6106740c..eb5bb3c03 100644 --- a/vrp-core/src/construction/heuristics/evaluators.rs +++ b/vrp-core/src/construction/heuristics/evaluators.rs @@ -307,14 +307,14 @@ fn analyze_insertion_in_route_leg( } let costs = eval_ctx.goal.estimate(&move_ctx) + &route_costs; - let other_costs = single_ctx.cost.clone().unwrap_or_else(InsertionCost::max_value); + let other_costs = single_ctx.cost.as_ref().unwrap_or(InsertionCost::max_value()); - match eval_ctx.result_selector.select_cost(&costs, &other_costs) { + match eval_ctx.result_selector.select_cost(&costs, other_costs) { // found better insertion Either::Left(_) => { single_ctx.violation = None; single_ctx.index = index; - single_ctx.cost = Some(costs.clone()); + single_ctx.cost = Some(costs); single_ctx.place = Some(target.place.clone()); } Either::Right(_) => continue, diff --git a/vrp-core/src/construction/heuristics/insertions.rs b/vrp-core/src/construction/heuristics/insertions.rs index 2aaae871b..8748784a2 100644 --- a/vrp-core/src/construction/heuristics/insertions.rs +++ b/vrp-core/src/construction/heuristics/insertions.rs @@ -7,6 +7,7 @@ use crate::models::common::Cost; use crate::models::problem::{Actor, Job, JobIdDimension}; use crate::models::solution::Activity; use crate::models::ViolationCode; +use lazy_static::lazy_static; use rosomaxa::prelude::*; use std::borrow::Borrow; use std::cmp::Ordering; @@ -82,6 +83,10 @@ const COST_DIMENSION: usize = 6; /// A size of a cost array used by `InsertionCost`. type CostArray = [Cost; COST_DIMENSION]; +lazy_static! { + static ref MAX_INSERTION_COST: InsertionCost = InsertionCost::new(&[Cost::MAX]); +} + /// A lexicographical cost of job's insertion. #[derive(Clone, Default)] pub struct InsertionCost { @@ -99,9 +104,9 @@ impl InsertionCost { self.data.iter().cloned() } - /// Returns highest* possible insertion cost. - pub fn max_value() -> Self { - Self::new(&[Cost::MAX]) + /// Returns a reference to the highest possible insertion cost. + pub fn max_value() -> &'static Self { + &MAX_INSERTION_COST } }