-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce minimize arrival time objective
- Loading branch information
1 parent
858b8b6
commit 31ac6ad
Showing
11 changed files
with
165 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#[cfg(test)] | ||
#[path = "../../../tests/unit/solver/objectives/minimize_arrival_time_test.rs"] | ||
mod minimize_arrival_time_test; | ||
|
||
use super::*; | ||
use rosomaxa::prelude::*; | ||
|
||
/// An objective function which prefers solution with less total arrival time. | ||
#[derive(Default)] | ||
pub struct MinimizeArrivalTime {} | ||
|
||
impl Objective for MinimizeArrivalTime { | ||
type Solution = InsertionContext; | ||
|
||
fn fitness(&self, solution: &Self::Solution) -> f64 { | ||
if solution.solution.routes.is_empty() { | ||
0. | ||
} else { | ||
let total: f64 = solution | ||
.solution | ||
.routes | ||
.iter() | ||
.filter_map(|route_ctx| route_ctx.route.tour.end()) | ||
.map(|end| end.schedule.arrival) | ||
.sum(); | ||
|
||
total / solution.solution.routes.len() as f64 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
vrp-core/tests/unit/solver/objectives/minimize_arrival_time_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::*; | ||
use crate::helpers::models::domain::*; | ||
use crate::helpers::models::solution::*; | ||
use crate::solver::objectives::MinimizeArrivalTime; | ||
|
||
fn create_test_insertion_ctx(routes: &[f64]) -> InsertionContext { | ||
let mut insertion_ctx = create_empty_insertion_context(); | ||
let problem = insertion_ctx.problem.clone(); | ||
|
||
routes.iter().for_each(|arrival| { | ||
let mut route_ctx = create_route_context_with_activities(problem.fleet.as_ref(), "v1", vec![]); | ||
route_ctx.route_mut().tour.all_activities_mut().last().unwrap().schedule.arrival = *arrival; | ||
|
||
insertion_ctx.solution.routes.push(route_ctx); | ||
}); | ||
|
||
insertion_ctx | ||
} | ||
|
||
#[test] | ||
fn can_properly_estimate_empty_solution() { | ||
let empty = create_empty_insertion_context(); | ||
let non_empty = create_test_insertion_ctx(&[10.]); | ||
|
||
let result = MinimizeArrivalTime::default().total_order(&empty, &non_empty); | ||
|
||
assert_eq!(result, Ordering::Less); | ||
} | ||
|
||
parameterized_test! {can_properly_estimate_solutions, (left, right, expected), { | ||
can_properly_estimate_solutions_impl(left, right, expected); | ||
}} | ||
|
||
can_properly_estimate_solutions! { | ||
case_01: (&[10.], &[10.], Ordering::Equal), | ||
case_02: (&[10.], &[11.], Ordering::Less), | ||
case_03: (&[10.], &[9.], Ordering::Greater), | ||
case_04: (&[10.], &[10., 10.], Ordering::Equal), | ||
case_05: (&[10.], &[10., 9.], Ordering::Greater), | ||
case_06: (&[10.], &[10., 11.], Ordering::Less), | ||
} | ||
|
||
fn can_properly_estimate_solutions_impl(left: &[f64], right: &[f64], expected: Ordering) { | ||
let left = create_test_insertion_ctx(left); | ||
let right = create_test_insertion_ctx(right); | ||
|
||
let result = MinimizeArrivalTime::default().total_order(&left, &right); | ||
|
||
assert_eq!(result, expected); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters