1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::construction::constraints::{ConstraintModule, ConstraintVariant, SoftRouteConstraint};
use crate::construction::heuristics::{RouteContext, SolutionContext};
use crate::models::common::Cost;
use crate::models::problem::Job;
use std::ops::Deref;
use std::slice::Iter;
use std::sync::Arc;
pub struct FleetUsageConstraintModule {
state_keys: Vec<i32>,
constraints: Vec<ConstraintVariant>,
}
impl ConstraintModule for FleetUsageConstraintModule {
fn accept_insertion(&self, _solution_ctx: &mut SolutionContext, _route_index: usize, _job: &Job) {}
fn accept_route_state(&self, _ctx: &mut RouteContext) {}
fn accept_solution_state(&self, _ctx: &mut SolutionContext) {}
fn merge(&self, source: Job, _candidate: Job) -> Result<Job, i32> {
Ok(source)
}
fn state_keys(&self) -> Iter<i32> {
self.state_keys.iter()
}
fn get_constraints(&self) -> Iter<ConstraintVariant> {
self.constraints.iter()
}
}
impl FleetUsageConstraintModule {
pub fn new_minimized() -> Self {
Self::new_with_cost(Box::new(|_| 1E12))
}
pub fn new_maximized() -> Self {
Self::new_with_cost(Box::new(|_| -1E12))
}
pub fn new_earliest() -> Self {
Self::new_with_cost(Box::new(|route_ctx| {
route_ctx.route.actor.detail.time.start
}))
}
fn new_with_cost(extra_cost_fn: Box<dyn Fn(&RouteContext) -> Cost + Send + Sync>) -> Self {
Self {
state_keys: vec![],
constraints: vec![ConstraintVariant::SoftRoute(Arc::new(FleetCostSoftRouteConstraint { extra_cost_fn }))],
}
}
}
struct FleetCostSoftRouteConstraint {
extra_cost_fn: Box<dyn Fn(&RouteContext) -> Cost + Send + Sync>,
}
impl SoftRouteConstraint for FleetCostSoftRouteConstraint {
fn estimate_job(&self, _: &SolutionContext, route_ctx: &RouteContext, _job: &Job) -> Cost {
if route_ctx.route.tour.job_count() == 0 {
self.extra_cost_fn.deref()(route_ctx)
} else {
0.
}
}
}