Skip to content

Commit af278b0

Browse files
Adjust code style
1 parent 6c43506 commit af278b0

File tree

21 files changed

+84
-327
lines changed

21 files changed

+84
-327
lines changed

.rustfmt.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
max_width=120
1+
max_width=120
2+
use_small_heuristics="Max"
3+
# fn_single_line=true

src/construction/constraints/pipeline.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ impl ConstraintPipeline {
127127
/// Checks whether all hard route constraints are fulfilled.
128128
/// Returns result of first failed constraint or empty value.
129129
pub fn evaluate_hard_route(&self, ctx: &RouteContext, job: &Arc<Job>) -> Option<RouteConstraintViolation> {
130-
self.hard_route_constraints
131-
.iter()
132-
.find_map(|c| c.evaluate_job(ctx, job))
130+
self.hard_route_constraints.iter().find_map(|c| c.evaluate_job(ctx, job))
133131
}
134132

135133
/// Checks whether all activity route constraints are fulfilled.
@@ -139,24 +137,16 @@ impl ConstraintPipeline {
139137
route_ctx: &RouteContext,
140138
activity_ctx: &ActivityContext,
141139
) -> Option<ActivityConstraintViolation> {
142-
self.hard_activity_constraints
143-
.iter()
144-
.find_map(|c| c.evaluate_activity(route_ctx, activity_ctx))
140+
self.hard_activity_constraints.iter().find_map(|c| c.evaluate_activity(route_ctx, activity_ctx))
145141
}
146142

147143
/// Checks soft route constraints and aggregates associated actual and penalty costs.
148144
pub fn evaluate_soft_route(&self, ctx: &RouteContext, job: &Arc<Job>) -> Cost {
149-
self.soft_route_constraints
150-
.iter()
151-
.map(|c| c.estimate_job(ctx, job))
152-
.sum()
145+
self.soft_route_constraints.iter().map(|c| c.estimate_job(ctx, job)).sum()
153146
}
154147

155148
/// Checks soft route constraints and aggregates associated actual and penalty costs.
156149
pub fn evaluate_soft_activity(&self, route_ctx: &RouteContext, activity_ctx: &ActivityContext) -> Cost {
157-
self.soft_activity_constraints
158-
.iter()
159-
.map(|c| c.estimate_activity(route_ctx, activity_ctx))
160-
.sum()
150+
self.soft_activity_constraints.iter().map(|c| c.estimate_activity(route_ctx, activity_ctx)).sum()
161151
}
162152
}

src/construction/constraints/timing.rs

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ impl ConstraintModule for TimingConstraintModule {
3939
|(loc, dep), activity| {
4040
let mut a = activity.write().unwrap();
4141

42-
a.schedule.arrival = dep
43-
+ self
44-
.transport
45-
.duration(actor.vehicle.profile, loc, a.place.location, dep);
42+
a.schedule.arrival = dep + self.transport.duration(actor.vehicle.profile, loc, a.place.location, dep);
4643

4744
a.schedule.departure = a.schedule.arrival.max(a.place.time.start)
4845
+ self.activity.duration(
@@ -59,10 +56,7 @@ impl ConstraintModule for TimingConstraintModule {
5956
// update latest arrival and waiting states of non-terminate (jobs) activities
6057
let init = (
6158
actor.detail.time.end,
62-
actor
63-
.detail
64-
.end
65-
.unwrap_or(actor.detail.start.unwrap_or(panic!(OP_START_MSG))),
59+
actor.detail.end.unwrap_or(actor.detail.start.unwrap_or(panic!(OP_START_MSG))),
6660
0f64,
6761
);
6862
route.tour.all_activities().rev().fold(init, |acc, activity| {
@@ -73,12 +67,8 @@ impl ConstraintModule for TimingConstraintModule {
7367

7468
let (end_time, prev_loc, waiting) = acc;
7569
let potential_latest = end_time
76-
- self
77-
.transport
78-
.duration(actor.vehicle.profile, act.place.location, prev_loc, end_time)
79-
- self
80-
.activity
81-
.duration(actor.vehicle.as_ref(), actor.driver.as_ref(), act.deref(), end_time);
70+
- self.transport.duration(actor.vehicle.profile, act.place.location, prev_loc, end_time)
71+
- self.activity.duration(actor.vehicle.as_ref(), actor.driver.as_ref(), act.deref(), end_time);
8272

8373
let latest_arrival_time = act.place.time.end.min(potential_latest);
8474
let future_waiting = waiting + (act.place.time.start - act.schedule.arrival).max(0f64);
@@ -137,17 +127,11 @@ struct TimeHardActivityConstraint {
137127

138128
impl TimeHardActivityConstraint {
139129
fn fail(&self) -> Option<ActivityConstraintViolation> {
140-
Some(ActivityConstraintViolation {
141-
code: self.code,
142-
stopped: true,
143-
})
130+
Some(ActivityConstraintViolation { code: self.code, stopped: true })
144131
}
145132

146133
fn stop(&self) -> Option<ActivityConstraintViolation> {
147-
Some(ActivityConstraintViolation {
148-
code: self.code,
149-
stopped: false,
150-
})
134+
Some(ActivityConstraintViolation { code: self.code, stopped: false })
151135
}
152136

153137
fn success(&self) -> Option<ActivityConstraintViolation> {
@@ -173,9 +157,7 @@ impl HardActivityConstraint for TimeHardActivityConstraint {
173157

174158
if actor.detail.time.end < prev.place.time.start
175159
|| actor.detail.time.end < target.place.time.start
176-
|| next.map_or(false, |next| {
177-
actor.detail.time.end < next.read().unwrap().place.time.start
178-
})
160+
|| next.map_or(false, |next| actor.detail.time.end < next.read().unwrap().place.time.start)
179161
{
180162
return self.fail();
181163
}
@@ -190,30 +172,24 @@ impl HardActivityConstraint for TimeHardActivityConstraint {
190172
}
191173
(
192174
n.place.location,
193-
*state
194-
.get_activity_state(LATEST_ARRIVAL_KEY, next.unwrap())
195-
.unwrap_or(&n.place.time.end),
175+
*state.get_activity_state(LATEST_ARRIVAL_KEY, next.unwrap()).unwrap_or(&n.place.time.end),
196176
)
197177
} else {
198178
// open vrp
199179
(target.place.location, target.place.time.end.min(actor.detail.time.end))
200180
};
201181

202-
let arr_time_at_next = departure
203-
+ self
204-
.transport
205-
.duration(profile, prev.place.location, next_act_location, departure);
182+
let arr_time_at_next =
183+
departure + self.transport.duration(profile, prev.place.location, next_act_location, departure);
206184
if arr_time_at_next > latest_arr_time_at_next_act {
207185
return self.fail();
208186
}
209187
if target.place.time.start > latest_arr_time_at_next_act {
210188
return self.stop();
211189
}
212190

213-
let arr_time_at_target_act = departure
214-
+ self
215-
.transport
216-
.duration(profile, prev.place.location, target.place.location, departure);
191+
let arr_time_at_target_act =
192+
departure + self.transport.duration(profile, prev.place.location, target.place.location, departure);
217193

218194
let end_time_at_new_act = arr_time_at_target_act.max(target.place.time.start)
219195
+ self.activity.duration(
@@ -248,9 +224,7 @@ impl HardActivityConstraint for TimeHardActivityConstraint {
248224
}
249225

250226
let arr_time_at_next_act = end_time_at_new_act
251-
+ self
252-
.transport
253-
.duration(profile, target.place.location, next_act_location, end_time_at_new_act);
227+
+ self.transport.duration(profile, target.place.location, next_act_location, end_time_at_new_act);
254228

255229
if arr_time_at_next_act > latest_arr_time_at_next_act {
256230
self.stop()

src/construction/heuristics/evaluators.rs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ impl InsertionEvaluator {
2020

2121
/// Evaluates possibility to preform insertion from given insertion context.
2222
pub fn evaluate(&self, job: &Arc<Job>, ctx: &InsertionContext) -> InsertionResult {
23-
ctx.solution
24-
.routes
25-
.iter()
26-
.cloned()
27-
.chain(ctx.solution.registry.next().map(|a| RouteContext::new(a)))
28-
.fold(InsertionResult::make_failure(), |acc, route_ctx| {
23+
ctx.solution.routes.iter().cloned().chain(ctx.solution.registry.next().map(|a| RouteContext::new(a))).fold(
24+
InsertionResult::make_failure(),
25+
|acc, route_ctx| {
2926
if let Some(violation) = ctx.problem.constraint.evaluate_hard_route(&route_ctx, job) {
3027
return InsertionResult::choose_best_result(
3128
acc,
@@ -49,7 +46,8 @@ impl InsertionEvaluator {
4946
Job::Multi(multi) => Self::evaluate_multi(job, multi, ctx, &route_ctx, &progress),
5047
},
5148
)
52-
})
49+
},
50+
)
5351
}
5452

5553
fn evaluate_single(
@@ -71,22 +69,15 @@ impl InsertionEvaluator {
7169
_ => panic!("Unexpected route leg configuration."),
7270
};
7371

74-
let mut activity_ctx = ActivityContext {
75-
index,
76-
prev,
77-
target: activity.clone(),
78-
next: Some(next),
79-
};
72+
let mut activity_ctx = ActivityContext { index, prev, target: activity.clone(), next: Some(next) };
8073

8174
// 2. analyze service details
8275
single.places.iter().try_fold(out, |in1, detail| {
8376
// TODO check whether tw is empty
8477
// 3. analyze detail time windows
8578
detail.times.iter().try_fold(in1, |in2, time| {
8679
activity.write().unwrap().place = Place {
87-
location: detail
88-
.location
89-
.unwrap_or(activity_ctx.prev.read().unwrap().place.location),
80+
location: detail.location.unwrap_or(activity_ctx.prev.read().unwrap().place.location),
9081
duration: detail.duration,
9182
time: time.clone(),
9283
};
@@ -118,12 +109,7 @@ impl InsertionEvaluator {
118109

119110
if result.is_success() {
120111
activity.write().unwrap().place = result.place;
121-
InsertionResult::make_success(
122-
result.cost,
123-
job.clone(),
124-
vec![(activity, result.index)],
125-
route_ctx.clone(),
126-
)
112+
InsertionResult::make_success(result.cost, job.clone(), vec![(activity, result.index)], route_ctx.clone())
127113
} else {
128114
InsertionResult::make_failure_with_code(result.violation.unwrap().code)
129115
}
@@ -159,22 +145,13 @@ impl SingleContext {
159145
violation: None,
160146
index: 0,
161147
cost,
162-
place: Place {
163-
location: 0,
164-
duration: 0.0,
165-
time: TimeWindow { start: 0.0, end: 0.0 },
166-
},
148+
place: Place { location: 0, duration: 0.0, time: TimeWindow { start: 0.0, end: 0.0 } },
167149
}
168150
}
169151

170152
fn fail(violation: ActivityConstraintViolation, other: SingleContext) -> Result<Self, Self> {
171153
let stopped = violation.stopped;
172-
let ctx = Self {
173-
violation: Some(violation),
174-
index: other.index,
175-
cost: other.cost,
176-
place: other.place,
177-
};
154+
let ctx = Self { violation: Some(violation), index: other.index, cost: other.cost, place: other.place };
178155
if stopped {
179156
Result::Err(ctx)
180157
} else {
@@ -183,12 +160,7 @@ impl SingleContext {
183160
}
184161

185162
fn success(index: usize, cost: Cost, place: Place) -> Result<Self, Self> {
186-
Result::Ok(Self {
187-
violation: None,
188-
index,
189-
cost,
190-
place,
191-
})
163+
Result::Ok(Self { violation: None, index, cost, place })
192164
}
193165

194166
fn skip(other: SingleContext) -> Result<Self, Self> {

src/construction/states/models.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,7 @@ impl InsertionResult {
113113
activities: Vec<(TourActivity, usize)>,
114114
route_ctx: RouteContext,
115115
) -> Self {
116-
Self::Success(InsertionSuccess {
117-
cost,
118-
job,
119-
activities,
120-
context: route_ctx,
121-
})
116+
Self::Success(InsertionSuccess { cost, job, activities, context: route_ctx })
122117
}
123118

124119
/// Creates result which represents insertion failure.
@@ -163,39 +158,20 @@ impl RouteContext {
163158
fn create_start_activity(actor: &Arc<Actor>) -> TourActivity {
164159
Arc::new(RwLock::new(Activity {
165160
place: Place {
166-
location: actor
167-
.detail
168-
.start
169-
.unwrap_or_else(|| unimplemented!("Optional start is not yet implemented")),
161+
location: actor.detail.start.unwrap_or_else(|| unimplemented!("Optional start is not yet implemented")),
170162
duration: 0.0,
171-
time: TimeWindow {
172-
start: actor.detail.time.start,
173-
end: std::f64::MAX,
174-
},
175-
},
176-
schedule: Schedule {
177-
arrival: actor.detail.time.start,
178-
departure: actor.detail.time.start,
163+
time: TimeWindow { start: actor.detail.time.start, end: std::f64::MAX },
179164
},
165+
schedule: Schedule { arrival: actor.detail.time.start, departure: actor.detail.time.start },
180166
job: None,
181167
}))
182168
}
183169

184170
fn create_end_activity(actor: &Arc<Actor>) -> Option<TourActivity> {
185171
actor.detail.end.map(|location| {
186172
Arc::new(RwLock::new(Activity {
187-
place: Place {
188-
location,
189-
duration: 0.0,
190-
time: TimeWindow {
191-
start: 0.0,
192-
end: actor.detail.time.end,
193-
},
194-
},
195-
schedule: Schedule {
196-
arrival: actor.detail.time.end,
197-
departure: actor.detail.time.end,
198-
},
173+
place: Place { location, duration: 0.0, time: TimeWindow { start: 0.0, end: actor.detail.time.end } },
174+
schedule: Schedule { arrival: actor.detail.time.end, departure: actor.detail.time.end },
199175
job: None,
200176
}))
201177
})

src/construction/states/route.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ impl RouteState {
4040

4141
/// Gets value associated with key.
4242
pub fn get_activity_state<T: 'static>(&self, key: i32, activity: &TourActivity) -> Option<&T> {
43-
self.activity_states
44-
.get(&ActivityWithKey(activity.clone(), key))
45-
.and_then(|s| s.downcast_ref::<T>())
43+
self.activity_states.get(&ActivityWithKey(activity.clone(), key)).and_then(|s| s.downcast_ref::<T>())
4644
}
4745

4846
/// Puts value associated with key.
@@ -53,8 +51,7 @@ impl RouteState {
5351

5452
/// Puts value associated with key and specific activity.
5553
pub fn put_activity_state<T: 'static>(&mut self, key: i32, activity: &TourActivity, value: T) {
56-
self.activity_states
57-
.insert(ActivityWithKey(activity.clone(), key), Box::new(value));
54+
self.activity_states.insert(ActivityWithKey(activity.clone(), key), Box::new(value));
5855
self.keys.insert(key);
5956
}
6057

src/models/problem/costs.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ use crate::models::solution::Activity;
77
pub trait ActivityCost {
88
/// Returns cost to perform activity.
99
fn cost(&self, vehicle: &Vehicle, driver: &Driver, activity: &Activity, arrival: Timestamp) -> Cost {
10-
let waiting = if activity.place.time.start > arrival {
11-
activity.place.time.start - arrival
12-
} else {
13-
0.0
14-
};
10+
let waiting = if activity.place.time.start > arrival { activity.place.time.start - arrival } else { 0.0 };
1511
let service = self.duration(vehicle, driver, activity, arrival);
1612

1713
waiting * (driver.costs.per_waiting_time + vehicle.costs.per_waiting_time)
@@ -59,30 +55,16 @@ impl MatrixTransportCost {
5955
assert!(distances.iter().all(|d| (d.len() as f64).sqrt() as usize == size));
6056
assert!(durations.iter().all(|d| (d.len() as f64).sqrt() as usize == size));
6157

62-
MatrixTransportCost {
63-
durations,
64-
distances,
65-
size,
66-
}
58+
MatrixTransportCost { durations, distances, size }
6759
}
6860
}
6961

7062
impl TransportCost for MatrixTransportCost {
7163
fn duration(&self, profile: Profile, from: Location, to: Location, departure: Timestamp) -> Duration {
72-
self.durations
73-
.get(profile as usize)
74-
.unwrap()
75-
.get(from * self.size + to)
76-
.unwrap()
77-
.clone()
64+
self.durations.get(profile as usize).unwrap().get(from * self.size + to).unwrap().clone()
7865
}
7966

8067
fn distance(&self, profile: Profile, from: Location, to: Location, departure: Timestamp) -> Distance {
81-
self.distances
82-
.get(profile as usize)
83-
.unwrap()
84-
.get(from * self.size + to)
85-
.unwrap()
86-
.clone()
68+
self.distances.get(profile as usize).unwrap().get(from * self.size + to).unwrap().clone()
8769
}
8870
}

0 commit comments

Comments
 (0)