Skip to content

Commit

Permalink
refine internal concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
oblonski committed Oct 19, 2022
1 parent 985b693 commit e5a8b92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private Properties createDefaultProperties() {

defaults.put(Strategy.WORST_BEST.toString(), "0.");
defaults.put(Strategy.WORST_REGRET.toString(), "1.");

defaults.put(Strategy.CLUSTER_BEST.toString(), "0.");
defaults.put(Strategy.CLUSTER_REGRET.toString(), "1.");

Expand Down Expand Up @@ -400,8 +401,9 @@ private void ini(VehicleRoutingProblem vrp) {

private VehicleRoutingAlgorithm create(final VehicleRoutingProblem vrp) {
ini(vrp);
boolean isInfinite = vrp.getFleetSize().equals(VehicleRoutingProblem.FleetSize.INFINITE);
if (vehicleFleetManager == null) {
if (vrp.getFleetSize().equals(VehicleRoutingProblem.FleetSize.INFINITE)) {
if (isInfinite) {
vehicleFleetManager = new InfiniteFleetManagerFactory(vrp.getVehicles()).createFleetManager();
} else {
FiniteFleetManagerFactory finiteFleetManagerFactory = new FiniteFleetManagerFactory(vrp.getVehicles());
Expand Down Expand Up @@ -579,7 +581,7 @@ private VehicleRoutingAlgorithm create(final VehicleRoutingProblem vrp) {
regret.setRandom(random);

AbstractInsertionStrategy best;
if (vrp.getJobs().size() < 250 || es == null) {
if ((vrp.getVehicles().size() == 1 && !isInfinite) || vrp.getJobs().size() < 100 || es == null) {
BestInsertion bestInsertion = (BestInsertion) new InsertionStrategyBuilder(vrp, vehicleFleetManager, stateManager, constraintManager)
.setInsertionStrategy(InsertionStrategyBuilder.Strategy.BEST)
.considerFixedCosts(Double.valueOf(properties.getProperty(Parameter.FIXED_COST_PARAM.toString())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@

public final class BestInsertionConcurrent extends AbstractInsertionStrategy {

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

}

static class Insertion {

private final VehicleRoute route;
Expand Down Expand Up @@ -79,13 +74,10 @@ public InsertionData getInsertionData() {

private final JobInsertionCostsCalculator bestInsertionCostCalculator;

private final int nuOfBatches;

private final ExecutorService executorService;

public BestInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, ExecutorService executorService, int nuOfBatches, VehicleRoutingProblem vehicleRoutingProblem) {
super(vehicleRoutingProblem);
this.nuOfBatches = nuOfBatches;
bestInsertionCostCalculator = jobInsertionCalculator;
this.executorService = executorService;
logger.debug("initialise {}", this);
Expand All @@ -102,18 +94,17 @@ public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> vehicleRout
List<Job> unassignedJobList = new ArrayList<>(unassignedJobs);
Collections.shuffle(unassignedJobList, random);
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) {
tasks.add(() -> getBestInsertion(batch, unassignedJob));
List<Callable<Insertion>> tasks = new ArrayList<>(vehicleRoutes.size());
for (VehicleRoute route : vehicleRoutes) {
tasks.add(() -> getBestInsertion(route, unassignedJob));
}
try {
List<Future<Insertion>> futureResponses = executorService.invokeAll(tasks);
for (int i = 0; i < batches.size(); i++) {
for (int i = 0; i < vehicleRoutes.size(); i++) {
Insertion insertion = futureResponses.get(i).get();
if (insertion.insertionData instanceof NoInsertionFound) {
failedConstraintNames.addAll(insertion.getInsertionData().getFailedConstraintNames());
Expand All @@ -129,12 +120,12 @@ public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> vehicleRout
} catch (ExecutionException e) {
throw new RuntimeException(e);
}

VehicleRoute newRoute = VehicleRoute.emptyRoute();
InsertionData newIData = bestInsertionCostCalculator.getInsertionData(newRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost);
if (newIData.getInsertionCost() < bestInsertionCost) {
bestInsertion = new Insertion(newRoute, newIData);
vehicleRoutes.add(newRoute);
batches.get(random.nextInt(batches.size())).routes.add(newRoute);
}
if (bestInsertion == null) {
badJobs.add(unassignedJob);
Expand All @@ -146,50 +137,15 @@ public Collection<Job> insertUnassignedJobs(Collection<VehicleRoute> vehicleRout
}


private Insertion getBestInsertion(Batch batch, Job unassignedJob) {
Insertion bestInsertion = null;
private Insertion getBestInsertion(VehicleRoute vehicleRoute, Job unassignedJob) {
InsertionData empty = new InsertionData.NoInsertionFound();
double bestInsertionCost = Double.MAX_VALUE;
for (VehicleRoute vehicleRoute : batch.routes) {
InsertionData iData = bestInsertionCostCalculator.getInsertionData(vehicleRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost);
if (iData instanceof NoInsertionFound) {
empty.getFailedConstraintNames().addAll(iData.getFailedConstraintNames());
continue;
}
if (iData.getInsertionCost() < bestInsertionCost) {
bestInsertion = new Insertion(vehicleRoute, iData);
bestInsertionCost = iData.getInsertionCost();
}
}
if (bestInsertion == null) return new Insertion(null, empty);
return bestInsertion;
}

private List<Batch> distributeRoutes(Collection<VehicleRoute> vehicleRoutes, int nuOfBatches) {
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
* else add one empty route anyway
*/
if (vehicleRoutes.size() < nuOfBatches) {
int nOfNewRoutes = nuOfBatches - vehicleRoutes.size();
for (int i = 0; i < nOfNewRoutes; i++) {
vehicleRoutes.add(VehicleRoute.emptyRoute());
}
InsertionData iData = bestInsertionCostCalculator.getInsertionData(vehicleRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, Double.MAX_VALUE);
if (iData instanceof NoInsertionFound) {
empty.getFailedConstraintNames().addAll(iData.getFailedConstraintNames());
return new Insertion(null, empty);
} else {
vehicleRoutes.add(VehicleRoute.emptyRoute());
}
/*
* distribute routes to batches equally
*/
int count = 0;
for (VehicleRoute route : vehicleRoutes) {
if (count == nuOfBatches) count = 0;
batches.get(count).routes.add(route);
count++;
return new Insertion(vehicleRoute, iData);
}
return batches;
}


Expand Down

0 comments on commit e5a8b92

Please sign in to comment.