-
Notifications
You must be signed in to change notification settings - Fork 7
/
RtSolverRoutePlanner.java
270 lines (230 loc) · 8.44 KB
/
RtSolverRoutePlanner.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (C) 2013-2016 Rinde van Lon, iMinds-DistriNet, KU Leuven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.rinde.logistics.pdptw.mas.route;
import static com.google.common.collect.Lists.newLinkedList;
import java.io.Serializable;
import java.util.Deque;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import com.github.rinde.rinsim.central.GlobalStateObject;
import com.github.rinde.rinsim.central.Measurable;
import com.github.rinde.rinsim.central.SimSolverBuilder;
import com.github.rinde.rinsim.central.Solver;
import com.github.rinde.rinsim.central.SolverTimeMeasurement;
import com.github.rinde.rinsim.central.SolverUser;
import com.github.rinde.rinsim.central.Solvers.SolveArgs;
import com.github.rinde.rinsim.central.rt.RealtimeSolver;
import com.github.rinde.rinsim.central.rt.RtSimSolver;
import com.github.rinde.rinsim.central.rt.RtSimSolver.EventType;
import com.github.rinde.rinsim.central.rt.RtSimSolverBuilder;
import com.github.rinde.rinsim.central.rt.RtSolverUser;
import com.github.rinde.rinsim.central.rt.RtStAdapters;
import com.github.rinde.rinsim.core.model.pdp.PDPModel.VehicleState;
import com.github.rinde.rinsim.core.model.pdp.Parcel;
import com.github.rinde.rinsim.event.Event;
import com.github.rinde.rinsim.event.Listener;
import com.github.rinde.rinsim.pdptw.common.PDPRoadModel;
import com.github.rinde.rinsim.pdptw.common.RouteFollowingVehicle;
import com.github.rinde.rinsim.util.StochasticSupplier;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
/**
*
* @author Rinde van Lon
*/
public final class RtSolverRoutePlanner extends AbstractRoutePlanner
implements RtSolverUser, Measurable {
Deque<Parcel> route;
Optional<PDPRoadModel> pdpRoadModel;
Optional<RtSimSolver> simSolver;
private final RealtimeSolver solver;
RtSolverRoutePlanner(RealtimeSolver s) {
route = newLinkedList();
solver = s;
simSolver = Optional.absent();
pdpRoadModel = Optional.absent();
}
@Override
protected void afterInit() {
pdpRoadModel = Optional.of((PDPRoadModel) roadModel.get());
}
@Override
protected void doUpdate(Set<Parcel> onMap, long time) {
if (onMap.isEmpty()
&& pdpModel.get().getContents(vehicle.get()).isEmpty()) {
route.clear();
} else {
final Set<Parcel> toRemove = new LinkedHashSet<Parcel>();
// remove all parcels which are no longer assigned to this routeplanner
for (final Parcel p : route) {
if (!onMap.contains(p)
&& !pdpModel.get().getParcelState(p).isPickedUp()
&& !pdpModel.get().getParcelState(p).isTransitionState()) {
toRemove.add(p);
}
}
LOGGER.trace("route {}", route);
route.removeAll(toRemove);
LOGGER.trace("to remove: {}, remaining route: {}", toRemove, route);
// when diversion is allowed we can just change course
@Nullable
final Parcel destination =
pdpRoadModel.get().isVehicleDiversionAllowed()
? null
: pdpRoadModel.get().getDestinationToParcel(vehicle.get());
if (pdpModel.get().getVehicleState(vehicle.get()) != VehicleState.IDLE
|| destination != null) {
// vehicle is picking up or delivering -> make sure that first item in
// route is the parcel that is being serviced
final Parcel next = destination == null
? pdpModel.get().getVehicleActionInfo(vehicle.get()).getParcel()
: destination;
if (!route.peek().equals(next)) {
// remove first occurrence
route.removeFirstOccurrence(next);
route.addFirst(next);
LOGGER.trace("moved {} to first position in route", next);
}
}
final Iterable<Parcel> newRoute =
RouteFollowingVehicle.delayAdjuster().adjust(route,
(RouteFollowingVehicle) vehicle.get());
LOGGER.trace("adjusted route: {}", newRoute);
route.clear();
Iterables.addAll(route, newRoute);
final GlobalStateObject gso = simSolver.get().getCurrentState(
SolveArgs.create()
.useParcels(onMap)
.useCurrentRoutes(ImmutableList.of(ImmutableList.copyOf(route))));
final Optional<Parcel> dest = gso.getVehicles().get(0).getDestination();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("destination {}", dest);
if (dest.isPresent()) {
LOGGER.trace("parcel state {}",
pdpModel.get().getParcelState(dest.get()));
}
}
simSolver.get().solve(gso);
}
}
@Override
public boolean hasNext() {
return !route.isEmpty();
}
@Override
public Optional<Parcel> current() {
return Optional.fromNullable(route.peek());
}
@Override
public Optional<ImmutableList<Parcel>> currentRoute() {
if (route.isEmpty()) {
return Optional.absent();
}
return Optional.of(ImmutableList.copyOf(route));
}
@Override
protected void nextImpl(long time) {
route.poll();
}
@Override
public void setSolverProvider(RtSimSolverBuilder builder) {
simSolver = Optional.of(builder.setVehicles(vehicle.asSet()).build(solver));
final RtSolverRoutePlanner rp = this;
simSolver.get().getEventAPI().addListener(new Listener() {
@Override
public void handleEvent(Event e) {
route = newLinkedList(simSolver.get().getCurrentSchedule().get(0));
LOGGER.trace("Computed new route for {}: {}.", vehicle.get(), route);
rp.dispatchChangeEvent();
}
}, EventType.NEW_SCHEDULE);
}
@Override
public List<SolverTimeMeasurement> getTimeMeasurements() {
if (solver instanceof Measurable) {
return ((Measurable) solver).getTimeMeasurements();
}
throw new IllegalStateException("Solver " + solver + " is not measurable.");
}
public static StochasticSupplier<RoutePlanner> supplier(
StochasticSupplier<? extends RealtimeSolver> solver) {
return new RtSup(solver);
}
public static StochasticSupplier<RoutePlanner> simulatedTimeSupplier(
final StochasticSupplier<? extends Solver> solver) {
return new StSup(solver);
}
static String toStringHelper(String methodName, Object obj) {
return Joiner.on("").join(RtSolverRoutePlanner.class.getSimpleName(),
".", methodName, "(", obj, ")");
}
static class StSolverRoutePlanner extends ForwardingRoutePlanner
implements SolverUser {
RtSolverRoutePlanner delegate;
SolverUser stAdapter;
StSolverRoutePlanner(RtSolverRoutePlanner deleg) {
delegate = deleg;
stAdapter = RtStAdapters.toSimTime(deleg);
}
@Override
protected RtSolverRoutePlanner delegate() {
return delegate;
}
@Override
public void setSolverProvider(SimSolverBuilder builder) {
stAdapter.setSolverProvider(builder);
}
@Override
public String toString() {
return "ST" + delegate().toString();
}
}
static final class StSup
implements StochasticSupplier<RoutePlanner>, Serializable {
private static final long serialVersionUID = 8587803855370922629L;
final StochasticSupplier<? extends Solver> solver;
StSup(StochasticSupplier<? extends Solver> s) {
solver = s;
}
@Override
public RoutePlanner get(long seed) {
return new StSolverRoutePlanner(new RtSolverRoutePlanner(
RtStAdapters.toRealtime(solver.get(seed))));
}
@Override
public String toString() {
return toStringHelper("simulatedTimeSupplier", solver);
}
}
static final class RtSup implements StochasticSupplier<RoutePlanner> {
final StochasticSupplier<? extends RealtimeSolver> solver;
RtSup(StochasticSupplier<? extends RealtimeSolver> s) {
solver = s;
}
@Override
public RoutePlanner get(long seed) {
return new RtSolverRoutePlanner(solver.get(seed));
}
@Override
public String toString() {
return toStringHelper("supplier", solver);
}
}
}