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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use crate::construction::constraints::*;
use crate::construction::heuristics::{InsertionContext, RouteContext, SolutionContext};
use crate::models::problem::{Job, TargetConstraint, TargetObjective};
use rosomaxa::prelude::*;
use std::cmp::Ordering;
use std::ops::Deref;
use std::slice::Iter;
use std::sync::Arc;
pub type JobMergeFn = Arc<dyn Fn(Job, Job) -> Result<Job, i32> + Send + Sync>;
pub type RouteValueFn = Arc<dyn Fn(&RouteContext) -> f64 + Send + Sync>;
pub type SolutionValueFn = Arc<dyn Fn(&SolutionContext) -> f64 + Send + Sync>;
pub type EstimateValueFn = Arc<dyn Fn(&SolutionContext, &RouteContext, &Job, f64) -> f64 + Send + Sync>;
pub struct GenericValue {}
impl GenericValue {
pub fn new_constrained_objective(
threshold: Option<f64>,
job_merge_func: JobMergeFn,
route_value_func: RouteValueFn,
solution_value_func: SolutionValueFn,
estimate_value_func: EstimateValueFn,
state_key: i32,
) -> (TargetConstraint, TargetObjective) {
let objective = GenericValueObjective {
threshold,
state_key,
route_value_func: route_value_func.clone(),
solution_value_func: solution_value_func.clone(),
estimate_value_func,
};
let constraint = GenericValueConstraint {
constraints: vec![ConstraintVariant::SoftRoute(Arc::new(objective.clone()))],
job_merge_func,
route_value_func,
state_key,
keys: vec![state_key],
solution_value_func,
};
(Arc::new(constraint), Arc::new(objective))
}
}
struct GenericValueConstraint {
constraints: Vec<ConstraintVariant>,
job_merge_func: Arc<dyn Fn(Job, Job) -> Result<Job, i32> + Send + Sync>,
route_value_func: Arc<dyn Fn(&RouteContext) -> f64 + Send + Sync>,
solution_value_func: Arc<dyn Fn(&SolutionContext) -> f64 + Send + Sync>,
state_key: i32,
keys: Vec<i32>,
}
impl ConstraintModule for GenericValueConstraint {
fn accept_insertion(&self, solution_ctx: &mut SolutionContext, route_index: usize, _job: &Job) {
self.accept_route_state(solution_ctx.routes.get_mut(route_index).unwrap());
}
fn accept_route_state(&self, ctx: &mut RouteContext) {
let value = self.route_value_func.deref()(ctx);
ctx.state_mut().put_route_state(self.state_key, value);
}
fn accept_solution_state(&self, ctx: &mut SolutionContext) {
let value = self.solution_value_func.deref()(ctx);
ctx.state.insert(self.state_key, Arc::new(value));
}
fn merge(&self, source: Job, candidate: Job) -> Result<Job, i32> {
self.job_merge_func.deref()(source, candidate)
}
fn state_keys(&self) -> Iter<i32> {
self.keys.iter()
}
fn get_constraints(&self) -> Iter<ConstraintVariant> {
self.constraints.iter()
}
}
#[derive(Clone)]
struct GenericValueObjective {
threshold: Option<f64>,
state_key: i32,
route_value_func: Arc<dyn Fn(&RouteContext) -> f64 + Send + Sync>,
solution_value_func: Arc<dyn Fn(&SolutionContext) -> f64 + Send + Sync>,
estimate_value_func: Arc<dyn Fn(&SolutionContext, &RouteContext, &Job, f64) -> f64 + Send + Sync>,
}
impl SoftRouteConstraint for GenericValueObjective {
fn estimate_job(&self, solution_ctx: &SolutionContext, route_ctx: &RouteContext, job: &Job) -> f64 {
let value = route_ctx
.state
.get_route_state::<f64>(self.state_key)
.cloned()
.unwrap_or_else(|| self.route_value_func.deref()(route_ctx));
if value.is_finite() && self.threshold.map_or(true, |threshold| value > threshold) {
self.estimate_value_func.deref()(solution_ctx, route_ctx, job, value)
} else {
0.
}
}
}
impl Objective for GenericValueObjective {
type Solution = InsertionContext;
fn total_order(&self, a: &Self::Solution, b: &Self::Solution) -> Ordering {
let fitness_a = self.fitness(a);
let fitness_b = self.fitness(b);
if let Some(threshold) = self.threshold {
if fitness_a < threshold && fitness_b < threshold {
return Ordering::Equal;
}
if fitness_a < threshold {
return Ordering::Less;
}
if fitness_b < threshold {
return Ordering::Greater;
}
}
compare_floats(fitness_a, fitness_b)
}
fn fitness(&self, solution: &Self::Solution) -> f64 {
solution
.solution
.state
.get(&self.state_key)
.and_then(|s| s.downcast_ref::<f64>())
.cloned()
.unwrap_or_else(|| self.solution_value_func.deref()(&solution.solution))
}
}