Skip to content

Commit

Permalink
Apply some refactoring with extra test
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Oct 19, 2023
1 parent 8eb47a0 commit 4a75a62
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
5 changes: 3 additions & 2 deletions vrp-pragmatic/src/construction/enablers/location_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::format::{CoordIndex, CustomLocationType, Location as ApiLocation};
use vrp_core::models::common::{Distance, Duration, Location, Profile};
use vrp_core::models::problem::TransportFallback;

/// A transport fallback for unknown location. Returns zero distance/duration for unknown type locations.
/// A transport fallback for only custom unknown location type.
/// Returns zero distance/duration for unknown type locations.
pub struct UnknownLocationFallback {
coord_index: Arc<CoordIndex>,
}
Expand All @@ -19,7 +20,7 @@ impl UnknownLocationFallback {

match (from, to) {
(Some(ApiLocation::Custom { r#type: CustomLocationType::Unknown }), _)
| (_, Some(ApiLocation::Custom { r#type: CustomLocationType::Unknown })) => Duration::default(),
| (_, Some(ApiLocation::Custom { r#type: CustomLocationType::Unknown })) => f64::default(),
_ => panic!("fallback is only for locations of custom unknown type"),
}
}
Expand Down
8 changes: 6 additions & 2 deletions vrp-pragmatic/src/format/coord_index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! A helper module for processing geo coordinates in problem and solution.

#[cfg(test)]
#[path = "../../tests/unit/format/coord_index_test.rs"]
mod coord_index_test;

use crate::format::problem::{Problem, VehicleBreak};
use crate::format::{CustomLocationType, Location};
use hashbrown::{HashMap, HashSet};
Expand Down Expand Up @@ -159,8 +163,8 @@ impl CoordIndex {
(self.flags & 0b0010) > 0
}

/// Returns true if problem has unknown.
pub fn has_unknown(&self) -> bool {
/// Returns true if problem has custom locations.
pub fn has_custom(&self) -> bool {
(self.flags & 0b0100) > 0
}
}
Expand Down
2 changes: 1 addition & 1 deletion vrp-pragmatic/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl std::fmt::Display for Location {
let value = match r#type {
CustomLocationType::Unknown => "unknown",
};
write!(f, "unknown={value}")
write!(f, "custom={value}")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion vrp-pragmatic/src/format/problem/fleet_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub(super) fn create_transport_costs(
return Err("amount of fleet profiles does not match matrix profiles".into());
}

if coord_index.has_unknown() {
if coord_index.has_custom() {
create_matrix_transport_cost_with_fallback(matrix_data, UnknownLocationFallback::new(coord_index))
} else {
create_matrix_transport_cost(matrix_data)
Expand Down
60 changes: 60 additions & 0 deletions vrp-pragmatic/tests/unit/format/coord_index_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::*;
use crate::format::problem::*;
use crate::helpers::*;

#[test]
fn can_use_index_with_coordinate_an_unknown_location_types() {
let unknown_location = Location::Custom { r#type: CustomLocationType::Unknown };
let problem = Problem {
plan: Plan {
jobs: vec![
create_delivery_job("job1", (1., 0.)),
create_delivery_job("job2", (2., 0.)),
Job {
deliveries: Some(vec![JobTask {
places: vec![JobPlace {
location: unknown_location.clone(),
duration: 0.,
times: None,
tag: None,
}],
demand: None,
order: None,
}]),
..create_job("job3")
},
],
..create_empty_plan()
},
fleet: create_default_fleet(),
..create_empty_problem()
};

let index = CoordIndex::new(&problem);

assert!(index.has_coordinates());
assert!(index.has_custom());
assert!(!index.has_indices());
assert_eq!(index.max_matrix_index(), 2);
assert_eq!(index.custom_locations_len(), 1);
// Location::Coordinate type
assert_eq!(index.get_by_loc(&(1., 0.).to_loc()), Some(0));
assert_eq!(index.get_by_loc(&(2., 0.).to_loc()), Some(1));
assert_eq!(index.get_by_loc(&(0., 0.).to_loc()), Some(2));
assert_eq!(index.get_by_idx(0), Some((1., 0.).to_loc()));
assert_eq!(index.get_by_idx(1), Some((2., 0.).to_loc()));
assert_eq!(index.get_by_idx(2), Some((0., 0.).to_loc()));
assert!(!index.is_special_index(0));
assert!(!index.is_special_index(1));
assert!(!index.is_special_index(2));
// Location::Custom
assert_eq!(index.get_by_loc(&unknown_location), Some(9));
assert_eq!(index.get_by_idx(9), Some(unknown_location));
assert!(index.is_special_index(9));
// out of range
assert_eq!(index.get_by_loc(&(3., 0.).to_loc()), None);
assert_eq!(index.get_by_idx(3), None);
assert_eq!(index.get_by_idx(8), None);
assert_eq!(index.get_by_idx(10), None);
assert!(!index.is_special_index(3));
}

0 comments on commit 4a75a62

Please sign in to comment.