Skip to content

Commit

Permalink
remove completion service
Browse files Browse the repository at this point in the history
  • Loading branch information
oblonski committed Oct 18, 2022
1 parent 9e42bc8 commit 985b693
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.graphhopper.jsprit.core.algorithm.recreate;

import com.graphhopper.jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound;
import com.graphhopper.jsprit.core.algorithm.recreate.listener.InsertionListeners;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
import com.graphhopper.jsprit.core.problem.driver.Driver;
import com.graphhopper.jsprit.core.problem.job.Job;
Expand All @@ -31,7 +30,10 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;


/**
Expand All @@ -41,11 +43,11 @@
public final class BestInsertionConcurrent extends AbstractInsertionStrategy {

static class Batch {
List<VehicleRoute> routes = new ArrayList<VehicleRoute>();
List<VehicleRoute> routes = new ArrayList<>();

}

class Insertion {
static class Insertion {

private final VehicleRoute route;

Expand All @@ -67,28 +69,25 @@ public InsertionData getInsertionData() {

}

private static Logger logger = LoggerFactory.getLogger(BestInsertionConcurrent.class);
private final static Logger logger = LoggerFactory.getLogger(BestInsertionConcurrent.class);

private final static double NO_NEW_DEPARTURE_TIME_YET = -12345.12345;

private final static Vehicle NO_NEW_VEHICLE_YET = null;

private final static Driver NO_NEW_DRIVER_YET = null;

private InsertionListeners insertionsListeners;
private final JobInsertionCostsCalculator bestInsertionCostCalculator;

private JobInsertionCostsCalculator bestInsertionCostCalculator;
private final int nuOfBatches;

private int nuOfBatches;

private ExecutorCompletionService<Insertion> completionService;
private final ExecutorService executorService;

public BestInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, ExecutorService executorService, int nuOfBatches, VehicleRoutingProblem vehicleRoutingProblem) {
super(vehicleRoutingProblem);
this.insertionsListeners = new InsertionListeners();
this.nuOfBatches = nuOfBatches;
bestInsertionCostCalculator = jobInsertionCalculator;
completionService = new ExecutorCompletionService<Insertion>(executorService);
this.executorService = executorService;
logger.debug("initialise {}", this);
}

Expand All @@ -99,29 +98,23 @@ public String toString() {

@Override
public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> vehicleRoutes, Collection<Job> unassignedJobs) {
List<Job> badJobs = new ArrayList<Job>(unassignedJobs.size());
List<Job> unassignedJobList = new ArrayList<Job>(unassignedJobs);
List<Job> badJobs = new ArrayList<>(unassignedJobs.size());
List<Job> unassignedJobList = new ArrayList<>(unassignedJobs);
Collections.shuffle(unassignedJobList, random);
Collections.sort(unassignedJobList, new AccordingToPriorities());
unassignedJobList.sort(new AccordingToPriorities());
List<Batch> batches = distributeRoutes(vehicleRoutes, nuOfBatches);
List<Callable<Insertion>> tasks = new ArrayList<>(batches.size());
List<String> failedConstraintNames = new ArrayList<>();
for (final Job unassignedJob : unassignedJobList) {
Insertion bestInsertion = null;
double bestInsertionCost = Double.MAX_VALUE;
for (final Batch batch : batches) {
completionService.submit(new Callable<Insertion>() {

@Override
public Insertion call() throws Exception {
return getBestInsertion(batch, unassignedJob);
}

});
tasks.add(() -> getBestInsertion(batch, unassignedJob));
}
try {
List<Future<Insertion>> futureResponses = executorService.invokeAll(tasks);
for (int i = 0; i < batches.size(); i++) {
Future<Insertion> futureIData = completionService.take();
Insertion insertion = futureIData.get();
Insertion insertion = futureResponses.get(i).get();
if (insertion.insertionData instanceof NoInsertionFound) {
failedConstraintNames.addAll(insertion.getInsertionData().getFailedConstraintNames());
continue;
Expand Down Expand Up @@ -173,7 +166,7 @@ private Insertion getBestInsertion(Batch batch, Job unassignedJob) {
}

private List<Batch> distributeRoutes(Collection<VehicleRoute> vehicleRoutes, int nuOfBatches) {
List<Batch> batches = new ArrayList<Batch>();
List<Batch> batches = new ArrayList<>();
for (int i = 0; i < nuOfBatches; i++) batches.add(new Batch());
/*
* if route.size < nuOfBatches add as much routes as empty batches are available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

Expand All @@ -47,13 +47,13 @@
public class RegretInsertionConcurrent extends AbstractInsertionStrategy {


private static Logger logger = LoggerFactory.getLogger(RegretInsertionConcurrentFast.class);
private final static Logger logger = LoggerFactory.getLogger(RegretInsertionConcurrentFast.class);

private ScoringFunction scoringFunction;

private final JobInsertionCostsCalculator insertionCostsCalculator;

private final ExecutorCompletionService<ScoredJob> completionService;
private final ExecutorService executorService;

/**
* Sets the scoring function.
Expand All @@ -71,7 +71,7 @@ public RegretInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalcula
this.scoringFunction = new DefaultScorer(vehicleRoutingProblem);
this.insertionCostsCalculator = jobInsertionCalculator;
this.vrp = vehicleRoutingProblem;
completionService = new ExecutorCompletionService<>(executorService);
this.executorService = executorService;
logger.debug("initialise " + this);
}

Expand Down Expand Up @@ -136,15 +136,15 @@ public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> routes, Col

private ScoredJob nextJob(final Collection<VehicleRoute> routes, List<Job> unassignedJobList, List<ScoredJob> badJobList) {
ScoredJob bestScoredJob = null;

List<Callable<ScoredJob>> tasks = new ArrayList<>(unassignedJobList.size());
for (final Job unassignedJob : unassignedJobList) {
completionService.submit(() -> RegretInsertion.getScoredJob(routes, unassignedJob, insertionCostsCalculator, scoringFunction));
tasks.add(() -> RegretInsertion.getScoredJob(routes, unassignedJob, insertionCostsCalculator, scoringFunction));
}

try {
List<Future<ScoredJob>> futureResponses = executorService.invokeAll(tasks);
for (int i = 0; i < unassignedJobList.size(); i++) {
Future<ScoredJob> fsj = completionService.take();
ScoredJob sJob = fsj.get();
ScoredJob sJob = futureResponses.get(i).get();
if (sJob instanceof ScoredJob.BadJob) {
badJobList.add(sJob);
continue;
Expand Down

0 comments on commit 985b693

Please sign in to comment.