Skip to content

Commit

Permalink
Add some leftovers and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Jun 29, 2024
1 parent 74b71c1 commit 33574cf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
6 changes: 3 additions & 3 deletions vrp-core/examples/custom_objective.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use vrp_core::prelude::*;
// a property for the job priority: true if it is a high prio job
custom_dimension!(JobPriority typeof bool);
// a state property to keep track of solution fitness for the priority feature
custom_solution_state!(PriorityFitness typeof f64);
custom_solution_state!(PriorityFitness typeof Cost);

/// Provides a way to guide the search to achieve a goal of optimization considering priority jobs
/// assignment as the most important aspect.
struct PriorityObjective;

impl FeatureObjective for PriorityObjective {
fn fitness(&self, solution: &InsertionContext) -> f64 {
fn fitness(&self, solution: &InsertionContext) -> Cost {
// estimate global objective fitness: get the number of jobs with top priority in the solution

let solution_ctx = &solution.solution;
Expand Down Expand Up @@ -75,7 +75,7 @@ fn estimate_job_cost(job: &Job) -> Cost {

/// Estimates solution fitness: iterate over every inserted job in the solution,
/// estimate the cost of its insertion and sum it.
fn calculate_solution_fitness(solution_ctx: &SolutionContext) -> f64 {
fn calculate_solution_fitness(solution_ctx: &SolutionContext) -> Cost {
solution_ctx.routes.iter().flat_map(|route_ctx| route_ctx.route().tour.jobs()).map(estimate_job_cost).sum::<Cost>()
}

Expand Down
2 changes: 1 addition & 1 deletion vrp-core/src/construction/heuristics/insertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const COST_DIMENSION: usize = 6;
/// A size of a cost array used by `InsertionCost`.
type CostArray = [Cost; COST_DIMENSION];

/// A hierarchical cost of job's insertion.
/// A lexicographical cost of job's insertion.
#[derive(Clone, Default)]
pub struct InsertionCost {
data: TinyVec<CostArray>,
Expand Down
12 changes: 10 additions & 2 deletions vrp-core/src/models/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl TransportCost for ExampleTransportCost {
/// Creates an example jobs used in documentation tests.
fn create_example_jobs() -> GenericResult<Vec<Job>> {
Ok(vec![SingleBuilder::default()
.id("job1")
.location(1)?
.times(vec![TimeWindow::new(0., 100.)])?
.demand(Demand::delivery(1))
Expand All @@ -42,7 +43,14 @@ fn create_example_vehicles() -> GenericResult<Vec<Vehicle>> {
.id("v1")
.set_distance_cost(1.)
.capacity(SingleDimLoad::new(2))
.add_detail(VehicleDetailBuilder::default().set_start_location(0).build()?)
.add_detail(
VehicleDetailBuilder::default()
.set_start_location(0)
.set_start_time(0.)
.set_end_location(0)
.set_end_time(1000.)
.build()?,
)
.build()?])
}

Expand All @@ -62,7 +70,7 @@ fn create_example_goal_ctx(
];

GoalContextBuilder::with_features(features)?
.set_goal(&["min_jobs", "min_tours", "min_distance"], &["min_jobs", "min_tours", "min_distance"])?
.set_goal(&["min_jobs", "min_tours", "min_distance"], &["min_tours", "min_distance"])?
.build()
}

Expand Down
32 changes: 31 additions & 1 deletion vrp-core/tests/unit/models/common/load_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod single {
use crate::models::common::{Load, SingleDimLoad};
use crate::models::common::{Demand, Load, SingleDimLoad};

fn from_value(load: i32) -> SingleDimLoad {
SingleDimLoad::new(load)
Expand Down Expand Up @@ -46,6 +46,36 @@ mod single {
assert!(from_value(10).can_fit(&from_value(5)));
assert!(!from_value(5).can_fit(&from_value(10)));
}

#[test]
fn can_use_pudo_simple_ctors() {
let pickup = Demand::pickup(1);
assert_eq!(pickup.pickup.0, SingleDimLoad::new(1));
assert_eq!(pickup.pickup.1, SingleDimLoad::default());
assert_eq!(pickup.delivery.0, SingleDimLoad::default());
assert_eq!(pickup.delivery.1, SingleDimLoad::default());

let dropoff = Demand::delivery(1);
assert_eq!(dropoff.pickup.0, SingleDimLoad::default());
assert_eq!(dropoff.pickup.1, SingleDimLoad::default());
assert_eq!(dropoff.delivery.0, SingleDimLoad::new(1));
assert_eq!(dropoff.delivery.1, SingleDimLoad::default());
}

#[test]
fn can_use_pudo_demand_ctors() {
let pickup = Demand::pudo_pickup(1);
assert_eq!(pickup.pickup.0, SingleDimLoad::default());
assert_eq!(pickup.pickup.1, SingleDimLoad::new(1));
assert_eq!(pickup.delivery.0, SingleDimLoad::default());
assert_eq!(pickup.delivery.1, SingleDimLoad::default());

let dropoff = Demand::pudo_delivery(1);
assert_eq!(dropoff.pickup.0, SingleDimLoad::default());
assert_eq!(dropoff.pickup.1, SingleDimLoad::default());
assert_eq!(dropoff.delivery.0, SingleDimLoad::default());
assert_eq!(dropoff.delivery.1, SingleDimLoad::new(1));
}
}

mod multi {
Expand Down

0 comments on commit 33574cf

Please sign in to comment.