Skip to content

Commit

Permalink
Avoid cloning insertion cost in evaluation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Oct 16, 2024
1 parent 6355721 commit 1e62f4c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion experiments/heuristic-research/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions vrp-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
6 changes: 3 additions & 3 deletions vrp-core/src/construction/heuristics/evaluators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions vrp-core/src/construction/heuristics/insertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand Down

0 comments on commit 1e62f4c

Please sign in to comment.