Skip to content

Commit

Permalink
Apply refactoring on objectives definition
Browse files Browse the repository at this point in the history
  • Loading branch information
reinterpretcat committed Dec 12, 2023
1 parent 5f8b827 commit f4bda1c
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 38 deletions.
4 changes: 2 additions & 2 deletions vrp-pragmatic/src/format/problem/goal_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn get_objective_features(
objectives
} else {
let mut objectives = vec![
vec![Objective::MinimizeUnassignedJobs { breaks: Some(1.) }],
vec![Objective::MinimizeUnassigned { breaks: Some(1.) }],
vec![Objective::MinimizeTours],
vec![Objective::MinimizeCost],
];
Expand Down Expand Up @@ -155,7 +155,7 @@ fn get_objective_features(
}),
-1,
),
Objective::MinimizeUnassignedJobs { breaks } => create_minimize_unassigned_jobs_feature(
Objective::MinimizeUnassigned { breaks } => create_minimize_unassigned_jobs_feature(
"min_unassigned",
Arc::new({
let break_value = *breaks;
Expand Down
22 changes: 3 additions & 19 deletions vrp-pragmatic/src/format/problem/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,11 @@ pub struct VehicleOptionalBreakPlace {

/// Vehicle break policy.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum VehicleOptionalBreakPolicy {
/// Allows to skip break if actual tour schedule doesn't intersect with vehicle time window.
#[serde(rename(deserialize = "skip-if-no-intersection", serialize = "skip-if-no-intersection"))]
SkipIfNoIntersection,
/// Allows to skip break if vehicle arrives before break's time window end.
#[serde(rename(deserialize = "skip-if-arrival-before-end", serialize = "skip-if-arrival-before-end"))]
SkipIfArrivalBeforeEnd,
}

Expand Down Expand Up @@ -557,59 +556,49 @@ pub struct Fleet {

/// Specifies objective function types.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(tag = "type")]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum Objective {
/// An objective to minimize total cost as linear combination of total time and distance.
#[serde(rename(deserialize = "minimize-cost", serialize = "minimize-cost"))]
MinimizeCost,

/// An objective to minimize total distance.
#[serde(rename(deserialize = "minimize-distance", serialize = "minimize-distance"))]
MinimizeDistance,

/// An objective to minimize total duration.
#[serde(rename(deserialize = "minimize-duration", serialize = "minimize-duration"))]
MinimizeDuration,

/// An objective to minimize total tour amount.
#[serde(rename(deserialize = "minimize-tours", serialize = "minimize-tours"))]
MinimizeTours,

/// An objective to maximize total tour amount.
#[serde(rename(deserialize = "maximize-tours", serialize = "maximize-tours"))]
MaximizeTours,

/// An objective to maximize value of served jobs.
#[serde(rename(deserialize = "maximize-value", serialize = "maximize-value"))]
MaximizeValue {
/// Specifies a weight of skipped breaks.
#[serde(skip_serializing_if = "Option::is_none")]
breaks: Option<f64>,
},

/// An objective to minimize amount of unassigned jobs.
#[serde(rename(deserialize = "minimize-unassigned", serialize = "minimize-unassigned"))]
MinimizeUnassignedJobs {
MinimizeUnassigned {
/// A skipped break weight to increase/decrease break is importance.
/// Default is 1.
#[serde(skip_serializing_if = "Option::is_none")]
breaks: Option<f64>,
},

/// An objective to minimize sum of arrival times from all routes.
#[serde(rename(deserialize = "minimize-arrival-time", serialize = "minimize-arrival-time"))]
MinimizeArrivalTime,

/// An objective to balance max load across all tours.
#[serde(rename(deserialize = "balance-max-load", serialize = "balance-max-load"))]
BalanceMaxLoad {
/// A relative load in single tour before balancing takes place.
#[serde(skip_serializing_if = "Option::is_none")]
options: Option<BalanceOptions>,
},

/// An objective to balance activities across all tours.
#[serde(rename(deserialize = "balance-activities", serialize = "balance-activities"))]
BalanceActivities {
/// An options which can be used to specify minimum activity amount in a tour before
/// it considered for balancing.
Expand All @@ -618,7 +607,6 @@ pub enum Objective {
},

/// An objective to balance distance across all tours.
#[serde(rename(deserialize = "balance-distance", serialize = "balance-distance"))]
BalanceDistance {
/// An options which can be used to specify minimum distance of a tour before
/// it considered for balancing.
Expand All @@ -627,7 +615,6 @@ pub enum Objective {
},

/// An objective to balance duration across all tours.
#[serde(rename(deserialize = "balance-duration", serialize = "balance-duration"))]
BalanceDuration {
/// An options which can be used to specify minimum duration of a tour before
/// it considered for balancing.
Expand All @@ -636,18 +623,15 @@ pub enum Objective {
},

/// An objective to control how tours are built.
#[serde(rename(deserialize = "compact-tour", serialize = "compact-tour"))]
CompactTour {
/// Options used to relax objective's impact.
options: CompactOptions,
},

/// An objective to control order of job activities in the tour.
#[serde(rename(deserialize = "tour-order", serialize = "tour-order"))]
TourOrder,

/// An objective to prefer jobs to be served as soon as possible.
#[serde(rename(deserialize = "fast-service", serialize = "fast-service"))]
FastService {
/// An objective tolerance specifies how different objective values have to be
/// to consider them different. Relative distance metric is used.
Expand Down
2 changes: 1 addition & 1 deletion vrp-pragmatic/src/validation/objectives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn check_e1601_duplicate_objectives(objectives: &[&Objective]) -> Result<(), For
MinimizeTours => acc.entry("minimize-tours"),
MaximizeTours => acc.entry("maximize-tours"),
MaximizeValue { .. } => acc.entry("maximize-value"),
MinimizeUnassignedJobs { .. } => acc.entry("minimize-unassigned"),
MinimizeUnassigned { .. } => acc.entry("minimize-unassigned"),
MinimizeArrivalTime => acc.entry("minimize-arrival-time"),
BalanceMaxLoad { .. } => acc.entry("balance-max-load"),
BalanceActivities { .. } => acc.entry("balance-activities"),
Expand Down
4 changes: 2 additions & 2 deletions vrp-pragmatic/tests/features/breaks/interval_break_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::format::problem::Objective::{MinimizeCost, MinimizeUnassignedJobs};
use crate::format::problem::Objective::{MinimizeCost, MinimizeUnassigned};
use crate::format::problem::*;
use crate::format_time;
use crate::helpers::*;

fn create_test_objectives() -> Option<Vec<Vec<Objective>>> {
Some(vec![vec![MinimizeUnassignedJobs { breaks: Some(10.) }], vec![MinimizeCost]])
Some(vec![vec![MinimizeUnassigned { breaks: Some(10.) }], vec![MinimizeCost]])
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion vrp-pragmatic/tests/features/fleet/basic_multi_shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn can_prefer_first_days_with_minimize_arrival_time_objective() {
..create_empty_plan()
},
objectives: Some(vec![
vec![Objective::MinimizeUnassignedJobs { breaks: None }],
vec![Objective::MinimizeUnassigned { breaks: None }],
vec![Objective::MinimizeArrivalTime],
vec![Objective::MinimizeCost],
]),
Expand Down
4 changes: 2 additions & 2 deletions vrp-pragmatic/tests/features/priorities/basic_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ fn create_test_limit() -> Option<VehicleLimits> {
fn create_order_objective(is_constrained: bool) -> Vec<Vec<Objective>> {
if is_constrained {
vec![
vec![Objective::MinimizeUnassignedJobs { breaks: None }],
vec![Objective::MinimizeUnassigned { breaks: None }],
vec![Objective::MinimizeTours],
vec![Objective::MinimizeCost],
]
} else {
vec![
vec![Objective::MinimizeUnassignedJobs { breaks: None }],
vec![Objective::MinimizeUnassigned { breaks: None }],
vec![Objective::MinimizeTours],
vec![Objective::TourOrder],
vec![Objective::MinimizeCost],
Expand Down
2 changes: 1 addition & 1 deletion vrp-pragmatic/tests/features/priorities/basic_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ parameterized_test! {can_prefer_jobs_with_more_value, objectives, {

can_prefer_jobs_with_more_value! {
case01: Some(vec![
vec![MinimizeUnassignedJobs { breaks: None }],
vec![MinimizeUnassigned { breaks: None }],
vec![MaximizeValue { breaks: None }],
vec![MinimizeCost],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn can_compact_tour() {
..create_default_fleet()
},
objectives: Some(vec![
vec![MinimizeUnassignedJobs { breaks: None }],
vec![MinimizeUnassigned { breaks: None }],
vec![MinimizeTours],
vec![CompactTour { options: CompactOptions { job_radius: 2, threshold: 2, distance: 0. } }],
vec![MinimizeCost],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn can_balance_activities_with_threshold_impl(threshold: Option<f64>, expected_l
..create_default_fleet()
},
objectives: Some(vec![
vec![MinimizeUnassignedJobs { breaks: None }],
vec![MinimizeUnassigned { breaks: None }],
vec![BalanceActivities { options: Some(BalanceOptions { threshold }) }],
vec![MinimizeCost],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn can_balance_max_load() {
..create_default_fleet()
},
objectives: Some(vec![
vec![MinimizeUnassignedJobs { breaks: None }],
vec![MinimizeUnassigned { breaks: None }],
vec![BalanceMaxLoad { options: None }],
vec![MinimizeCost],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn can_balance_duration() {
..create_default_fleet()
},
objectives: Some(vec![
vec![MinimizeUnassignedJobs { breaks: None }],
vec![MinimizeUnassigned { breaks: None }],
vec![BalanceDuration { options: None }],
vec![MinimizeCost],
]),
Expand Down
4 changes: 2 additions & 2 deletions vrp-pragmatic/tests/helpers/problem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::format::problem::Objective::{MinimizeCost, MinimizeUnassignedJobs};
use crate::format::problem::Objective::{MinimizeCost, MinimizeUnassigned};
use crate::format::problem::*;
use crate::format::{CoordIndex, Location};
use crate::format_time;
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn create_default_matrix_profiles() -> Vec<MatrixProfile> {
}

pub fn create_min_jobs_cost_objective() -> Option<Vec<Vec<Objective>>> {
Some(vec![vec![MinimizeUnassignedJobs { breaks: None }], vec![MinimizeCost]])
Some(vec![vec![MinimizeUnassigned { breaks: None }], vec![MinimizeCost]])
}

pub fn create_empty_plan() -> Plan {
Expand Down
8 changes: 4 additions & 4 deletions vrp-pragmatic/tests/unit/validation/objectives_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn can_detect_missing_cost_objective_impl(objectives: Option<Vec<Vec<Objective>>
fn can_detect_missing_value_jobs() {
let problem = Problem {
objectives: Some(vec![
vec![MinimizeUnassignedJobs { breaks: None }],
vec![MinimizeUnassigned { breaks: None }],
vec![MaximizeValue { breaks: None }],
vec![MinimizeCost],
]),
Expand All @@ -114,7 +114,7 @@ fn can_detect_missing_value_jobs() {
#[test]
fn can_detect_missing_order_jobs() {
let problem = Problem {
objectives: Some(vec![vec![MinimizeUnassignedJobs { breaks: None }], vec![TourOrder], vec![MinimizeCost]]),
objectives: Some(vec![vec![MinimizeUnassigned { breaks: None }], vec![TourOrder], vec![MinimizeCost]]),
..create_empty_problem()
};
let coord_index = CoordIndex::new(&problem);
Expand Down Expand Up @@ -186,11 +186,11 @@ parameterized_test! {can_detect_missing_value_objective, (objectives, expected),

can_detect_missing_value_objective! {
case01: (Some(vec![
vec![MinimizeUnassignedJobs { breaks: None }],
vec![MinimizeUnassigned { breaks: None }],
vec![MinimizeCost],
]), Some("E1607".to_string())),
case02: (Some(vec![
vec![MinimizeUnassignedJobs { breaks: None }],
vec![MinimizeUnassigned { breaks: None }],
vec![MaximizeValue { breaks: None }],
vec![MinimizeCost],
]), None),
Expand Down

0 comments on commit f4bda1c

Please sign in to comment.