@@ -392,7 +392,7 @@ private void runOptimiser(final ComponentExecutionApi api, ODLDatastore<? extend
392
392
// store the best every solution found and it isn't always the one returned...
393
393
class BestEver {
394
394
VehicleRoutingProblemSolution solution ;
395
- double cost = Double .POSITIVE_INFINITY ;
395
+ // double cost = Double.POSITIVE_INFINITY;
396
396
}
397
397
final BestEver bestEver = new BestEver ();
398
398
@@ -410,11 +410,24 @@ class LastUpdate {
410
410
411
411
@ Override
412
412
public void informIterationEnds (int i , VehicleRoutingProblem problem , Collection <VehicleRoutingProblemSolution > solutions ) {
413
- VehicleRoutingProblemSolution bestSolution = Solutions .bestOf (solutions );
414
- if (bestSolution != null ) {
415
- if (bestSolution .getCost () < bestEver .cost ) {
416
- bestEver .solution = VehicleRoutingProblemSolution .copyOf (bestSolution );
417
- bestEver .cost = bestSolution .getCost ();
413
+ VehicleRoutingProblemSolution newSln = Solutions .bestOf (solutions );
414
+ if (newSln != null ) {
415
+ // accept if we don't have a solution
416
+ boolean accept = bestEver .solution ==null ;
417
+
418
+ // accept if the new solution has less unassigned jobs
419
+ if (!accept && (newSln .getUnassignedJobs ().size () < bestEver .solution .getUnassignedJobs ().size ())){
420
+ accept = true ;
421
+ }
422
+
423
+ // accept if we have the same number of unassigned jobs but the cost is less
424
+ if (!accept && (newSln .getUnassignedJobs ().size () == bestEver .solution .getUnassignedJobs ().size ())
425
+ && (newSln .getCost () < bestEver .solution .getCost ())){
426
+ accept = true ;
427
+ }
428
+
429
+ if (accept ) {
430
+ bestEver .solution = VehicleRoutingProblemSolution .copyOf (newSln );
418
431
}
419
432
}
420
433
@@ -426,11 +439,9 @@ public void informIterationEnds(int i, VehicleRoutingProblem problem, Collection
426
439
427
440
builder .append ("Runtime " + ((time - startTime ) / 1000 ) + "s" );
428
441
builder .append (", Step " + i );
429
- if (bestEver .cost != Double .POSITIVE_INFINITY ) {
430
- builder .append (", " + DecimalFormat .getInstance ().format (bestEver .cost ) + " cost" );
431
- }
432
-
442
+
433
443
if (bestEver .solution != null ) {
444
+ builder .append (", " + DecimalFormat .getInstance ().format (bestEver .solution .getCost ()) + " cost" );
434
445
builder .append (", " + bestEver .solution .getRoutes ().size () + " routes" );
435
446
builder .append (", " + bestEver .solution .getUnassignedJobs ().size () + " unassigned jobs" );
436
447
}
0 commit comments