Skip to content

Commit 0f2484f

Browse files
committed
improve best insertion conc
1 parent 105af16 commit 0f2484f

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/recreate/BestInsertionConcurrent.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ public InsertionData getInsertionData() {
7676

7777
private final ExecutorService executorService;
7878

79+
/**
80+
* @deprecated use {@link #BestInsertionConcurrent(JobInsertionCostsCalculator, ExecutorService, VehicleRoutingProblem)} instead.
81+
* The nuOfBatches parameter is unused.
82+
*/
83+
@Deprecated
7984
public BestInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, ExecutorService executorService, int nuOfBatches, VehicleRoutingProblem vehicleRoutingProblem) {
85+
this(jobInsertionCalculator, executorService, vehicleRoutingProblem);
86+
}
87+
88+
public BestInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, ExecutorService executorService, VehicleRoutingProblem vehicleRoutingProblem) {
8089
super(vehicleRoutingProblem);
8190
bestInsertionCostCalculator = jobInsertionCalculator;
8291
this.executorService = executorService;
@@ -94,18 +103,19 @@ public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> vehicleRout
94103
List<Job> unassignedJobList = new ArrayList<>(unassignedJobs);
95104
Collections.shuffle(unassignedJobList, random);
96105
unassignedJobList.sort(new AccordingToPriorities());
97-
List<String> failedConstraintNames = new ArrayList<>();
106+
List<Callable<Insertion>> tasks = new ArrayList<>();
98107
for (final Job unassignedJob : unassignedJobList) {
108+
List<String> failedConstraintNames = new ArrayList<>();
99109
Insertion bestInsertion = null;
100110
double bestInsertionCost = Double.MAX_VALUE;
101-
List<Callable<Insertion>> tasks = new ArrayList<>(vehicleRoutes.size());
111+
tasks.clear();
102112
for (VehicleRoute route : vehicleRoutes) {
103113
tasks.add(() -> getBestInsertion(route, unassignedJob));
104114
}
105115
try {
106116
List<Future<Insertion>> futureResponses = executorService.invokeAll(tasks);
107-
for (int i = 0; i < vehicleRoutes.size(); i++) {
108-
Insertion insertion = futureResponses.get(i).get();
117+
for (Future<Insertion> futureResponse : futureResponses) {
118+
Insertion insertion = futureResponse.get();
109119
if (insertion.insertionData instanceof NoInsertionFound) {
110120
failedConstraintNames.addAll(insertion.getInsertionData().getFailedConstraintNames());
111121
continue;
@@ -126,12 +136,15 @@ public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> vehicleRout
126136
if (newIData.getInsertionCost() < bestInsertionCost) {
127137
bestInsertion = new Insertion(newRoute, newIData);
128138
vehicleRoutes.add(newRoute);
139+
} else if (newIData instanceof NoInsertionFound) {
140+
failedConstraintNames.addAll(newIData.getFailedConstraintNames());
129141
}
130142
if (bestInsertion == null) {
131143
badJobs.add(unassignedJob);
132144
markUnassigned(unassignedJob, failedConstraintNames);
145+
} else {
146+
insertJob(unassignedJob, bestInsertion.getInsertionData(), bestInsertion.getRoute());
133147
}
134-
else insertJob(unassignedJob, bestInsertion.getInsertionData(), bestInsertion.getRoute());
135148
}
136149
return badJobs;
137150
}

0 commit comments

Comments
 (0)