17
17
*/
18
18
package com .graphhopper .jsprit .core .algorithm ;
19
19
20
+ import java .util .ArrayList ;
21
+ import java .util .Collection ;
22
+ import java .util .HashSet ;
23
+ import java .util .Set ;
24
+
25
+ import org .slf4j .Logger ;
26
+ import org .slf4j .LoggerFactory ;
27
+
20
28
import com .graphhopper .jsprit .core .algorithm .SearchStrategy .DiscoveredSolution ;
21
29
import com .graphhopper .jsprit .core .algorithm .listener .SearchStrategyListener ;
22
30
import com .graphhopper .jsprit .core .algorithm .listener .SearchStrategyModuleListener ;
30
38
import com .graphhopper .jsprit .core .problem .solution .route .VehicleRoute ;
31
39
import com .graphhopper .jsprit .core .problem .solution .route .activity .TourActivity ;
32
40
import com .graphhopper .jsprit .core .util .Solutions ;
33
- import org .slf4j .Logger ;
34
- import org .slf4j .LoggerFactory ;
35
-
36
- import java .util .ArrayList ;
37
- import java .util .Collection ;
38
41
39
42
40
43
/**
@@ -57,9 +60,8 @@ void addTermination(PrematureAlgorithmTermination termination) {
57
60
@ Override
58
61
public boolean isPrematureBreak (DiscoveredSolution discoveredSolution ) {
59
62
for (PrematureAlgorithmTermination termination : terminationCriteria ) {
60
- if (termination .isPrematureBreak (discoveredSolution )) {
63
+ if (termination .isPrematureBreak (discoveredSolution ))
61
64
return true ;
62
- }
63
65
}
64
66
return false ;
65
67
}
@@ -145,25 +147,41 @@ public void addInitialSolution(VehicleRoutingProblemSolution solution) {
145
147
}
146
148
147
149
private void verify (VehicleRoutingProblemSolution solution ) {
148
- int nuJobs = 0 ;
150
+ Set <Job > allJobs = new HashSet <Job >(problem .getJobs ().values ());
151
+ allJobs .removeAll (solution .getUnassignedJobs ());
149
152
for (VehicleRoute route : solution .getRoutes ()) {
150
- nuJobs += route .getTourActivities ().getJobs (). size ( );
153
+ allJobs . removeAll ( route .getTourActivities ().getJobs ());
151
154
if (route .getVehicle ().getIndex () == 0 )
152
155
throw new IllegalStateException ("vehicle used in initial solution has no index. probably a vehicle is used that has not been added to the " +
153
- " the VehicleRoutingProblem. only use vehicles that have already been added to the problem." );
156
+ " the VehicleRoutingProblem. only use vehicles that have already been added to the problem." );
154
157
for (TourActivity act : route .getActivities ()) {
155
- if (act .getIndex () == 0 ) {
158
+ if (act .getIndex () == 0 )
156
159
throw new IllegalStateException ("act in initial solution has no index. activities are created and associated to their job in VehicleRoutingProblem\n ." +
157
- " thus if you build vehicle-routes use the jobActivityFactory from vehicle routing problem like that \n " +
158
- " VehicleRoute.Builder.newInstance(knownVehicle).setJobActivityFactory(vrp.getJobActivityFactory).addService(..)....build() \n " +
159
- " then the activities that are created to build the route are identical to the ones used in VehicleRoutingProblem" );
160
- }
160
+ " thus if you build vehicle-routes use the jobActivityFactory from vehicle routing problem like that \n " +
161
+ " VehicleRoute.Builder.newInstance(knownVehicle).setJobActivityFactory(vrp.getJobActivityFactory).addService(..)....build() \n " +
162
+ " then the activities that are created to build the route are identical to the ones used in VehicleRoutingProblem" );
161
163
}
162
164
}
163
- // if (nuJobs != problem.getJobs().values().size()) {
164
- // logger.warn("number of jobs in initial solution ({}) is not equal nuJobs in vehicle routing problem ({})" +
165
- // "\n this might yield unintended effects, e.g. initial solution cannot be improved anymore.", nuJobs, problem.getJobs().values().size());
166
- // }
165
+
166
+ solution .getUnassignedJobs ().addAll (allJobs );
167
+
168
+ // Doesn't work
169
+ // for (Vehicle v : problem.getVehicles()) {
170
+ // boolean found = false;
171
+ // for (VehicleRoute vr : solution.getRoutes())
172
+ // if (vr.getVehicle().getId().equals(v.getId())) {
173
+ // found = true;
174
+ // break;
175
+ // }
176
+ // if (!found) {
177
+ // solution.getRoutes().add(VehicleRoute.Builder.newInstance(v).build());
178
+ // }
179
+ // }
180
+
181
+ // if (nuJobs != problem.getJobs().values().size()) {
182
+ // logger.warn("number of jobs in initial solution ({}) is not equal nuJobs in vehicle routing problem ({})" +
183
+ // "\n this might yield unintended effects, e.g. initial solution cannot be improved anymore.", nuJobs, problem.getJobs().values().size());
184
+ // }
167
185
}
168
186
169
187
/**
@@ -214,15 +232,19 @@ public Collection<VehicleRoutingProblemSolution> searchSolutions() {
214
232
Collection <VehicleRoutingProblemSolution > solutions = new ArrayList <VehicleRoutingProblemSolution >(initialSolutions );
215
233
algorithmStarts (problem , solutions );
216
234
bestEver = Solutions .bestOf (solutions );
217
- if (logger .isTraceEnabled ()) log (solutions );
235
+ if (logger .isTraceEnabled ()) {
236
+ log (solutions );
237
+ }
218
238
logger .info ("iterations start" );
219
239
for (int i = 0 ; i < maxIterations ; i ++) {
220
240
iterationStarts (i + 1 , problem , solutions );
221
241
logger .debug ("start iteration: {}" , i );
222
242
counter .incCounter ();
223
243
SearchStrategy strategy = searchStrategyManager .getRandomStrategy ();
224
244
DiscoveredSolution discoveredSolution = strategy .run (problem , solutions );
225
- if (logger .isTraceEnabled ()) log (discoveredSolution );
245
+ if (logger .isTraceEnabled ()) {
246
+ log (discoveredSolution );
247
+ }
226
248
memorizeIfBestEver (discoveredSolution );
227
249
selectedStrategy (discoveredSolution , problem , solutions );
228
250
if (terminationManager .isPrematureBreak (discoveredSolution )) {
@@ -240,11 +262,15 @@ public Collection<VehicleRoutingProblemSolution> searchSolutions() {
240
262
}
241
263
242
264
private void addBestEver (Collection <VehicleRoutingProblemSolution > solutions ) {
243
- if (bestEver != null ) solutions .add (bestEver );
265
+ if (bestEver != null ) {
266
+ solutions .add (bestEver );
267
+ }
244
268
}
245
269
246
270
private void log (Collection <VehicleRoutingProblemSolution > solutions ) {
247
- for (VehicleRoutingProblemSolution sol : solutions ) log (sol );
271
+ for (VehicleRoutingProblemSolution sol : solutions ) {
272
+ log (sol );
273
+ }
248
274
}
249
275
250
276
private void log (VehicleRoutingProblemSolution solution ) {
@@ -277,9 +303,11 @@ private void log(DiscoveredSolution discoveredSolution) {
277
303
278
304
private void memorizeIfBestEver (DiscoveredSolution discoveredSolution ) {
279
305
if (discoveredSolution == null ) return ;
280
- if (bestEver == null ) bestEver = discoveredSolution .getSolution ();
281
- else if (discoveredSolution .getSolution ().getCost () < bestEver .getCost ())
306
+ if (bestEver == null ) {
307
+ bestEver = discoveredSolution .getSolution ();
308
+ } else if (discoveredSolution .getSolution ().getCost () < bestEver .getCost ()) {
282
309
bestEver = discoveredSolution .getSolution ();
310
+ }
283
311
}
284
312
285
313
@@ -297,10 +325,12 @@ public VehicleRoutingAlgorithmListeners getAlgorithmListeners() {
297
325
298
326
public void addListener (VehicleRoutingAlgorithmListener l ) {
299
327
algoListeners .addListener (l );
300
- if (l instanceof SearchStrategyListener )
328
+ if (l instanceof SearchStrategyListener ) {
301
329
searchStrategyManager .addSearchStrategyListener ((SearchStrategyListener ) l );
302
- if (l instanceof SearchStrategyModuleListener )
330
+ }
331
+ if (l instanceof SearchStrategyModuleListener ) {
303
332
searchStrategyManager .addSearchStrategyModuleListener ((SearchStrategyModuleListener ) l );
333
+ }
304
334
}
305
335
306
336
private void iterationEnds (int i , VehicleRoutingProblem problem , Collection <VehicleRoutingProblemSolution > solutions ) {
0 commit comments