Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2 migration continues #372

Open
wants to merge 8 commits into
base: job-activity-refactoring
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@ public MissedBreak() {
@Override
protected double calculateRouteLevelCost(VehicleRoutingProblem problem, VehicleRoute route) {
for (TourActivity act : route.getActivities()) {
if (act instanceof BreakActivity) {
if (act instanceof BreakActivity)
return 0d;
}
}
if (route.getVehicle().getBreak() != null) {
if (route.getEnd().getArrTime() > route.getVehicle().getBreak().getActivity().getSingleTimeWindow()
.getEnd()) {
if (route.getEnd().getArrTime() > route.getVehicle().getBreak().getActivity().getBreakTimeWindow().getEnd())
return 4 * (getMaxCosts() * 2 + route.getVehicle().getBreak().getActivity().getOperationTime()
* route.getVehicle().getType().getVehicleCostParams().perServiceTimeUnit);

}
* route.getVehicle().getType().getVehicleCostParams().perServiceTimeUnit);
}
return 0d;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,10 @@ public String toString() {
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownCosts) {
Break breakToInsert = (Break) jobToInsert;
if (newVehicle.getBreak() == null || newVehicle.getBreak() != breakToInsert) {
if (newVehicle.getBreak() == null || newVehicle.getBreak() != breakToInsert)
return InsertionData.createEmptyInsertionData();
}
if (currentRoute.isEmpty()) {
if (currentRoute.isEmpty())
return InsertionData.createEmptyInsertionData();
}

JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
int insertionIndex = InsertionData.NO_INDEX;
Expand All @@ -118,9 +116,8 @@ public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job
/*
check hard constraints at route level
*/
if (!hardRouteLevelConstraint.fulfilled(insertionContext)) {
if (!hardRouteLevelConstraint.fulfilled(insertionContext))
return InsertionData.createEmptyInsertionData();
}

/*
check soft constraints at route level
Expand Down Expand Up @@ -156,7 +153,7 @@ public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job
List<Location> locations = Arrays.asList(prevAct.getLocation(), nextAct.getLocation());
for (Location location : locations) {
breakAct2Insert.setLocation(location);
TimeWindow timeWindow = breakToInsert.getActivity().getTimeWindow();
TimeWindow timeWindow = breakToInsert.getActivity().getBreakTimeWindow();
breakAct2Insert.setTheoreticalEarliestOperationStartTime(timeWindow.getStart());
breakAct2Insert.setTheoreticalLatestOperationStartTime(timeWindow.getEnd());
ConstraintsStatus status = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, breakAct2Insert, nextAct, prevActStartTime);
Expand All @@ -182,9 +179,8 @@ public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job
break;
}
}
if (insertionIndex == InsertionData.NO_INDEX) {
if (insertionIndex == InsertionData.NO_INDEX)
return InsertionData.createEmptyInsertionData();
}
InsertionData insertionData = new InsertionData(bestCost, InsertionData.NO_INDEX, insertionIndex, newVehicle, newDriver);
breakAct2Insert.setLocation(bestLocation);
insertionData.getEvents().add(new InsertBreak(currentRoute, newVehicle, breakAct2Insert, insertionIndex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class BreakScheduling implements InsertionStartsListener, JobInsertedList

private final EventListeners eventListeners;

private Set<VehicleRoute> modifiedRoutes = new HashSet<VehicleRoute>();
private Set<VehicleRoute> modifiedRoutes = new HashSet<>();

public BreakScheduling(VehicleRoutingProblem vrp, StateManager stateManager, ConstraintManager constraintManager) {
this.stateManager = stateManager;
Expand All @@ -69,7 +69,7 @@ public void informJobInserted(Job job2insert, VehicleRoute inRoute, double addit
stateManager.removed(aBreak, inRoute);
stateManager.reCalculateStates(inRoute);
}
if (inRoute.getEnd().getArrTime() > aBreak.getActivity().getTimeWindow().getEnd()) {
if (inRoute.getEnd().getArrTime() > aBreak.getActivity().getBreakTimeWindow().getEnd()) {
InsertionData iData = breakInsertionCalculator.getInsertionData(inRoute, aBreak, inRoute.getVehicle(), inRoute.getDepartureTime(), inRoute.getDriver(), Double.MAX_VALUE);
if (!(iData instanceof InsertionData.NoInsertionFound)) {
logger.trace("insert: [jobId={}]{}", aBreak.getId(), iData);
Expand All @@ -96,7 +96,7 @@ public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassigned
logger.trace("ruin: {}", aBreak.getId());
}
}
List<Break> breaks = new ArrayList<Break>();
List<Break> breaks = new ArrayList<>();
for (Job j : unassignedJobs) {
if (j instanceof Break) {
breaks.add((Break) j);
Expand All @@ -119,7 +119,7 @@ public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collec
for (VehicleRoute route : vehicleRoutes) {
Break aBreak = route.getVehicle().getBreak();
if (aBreak != null && !route.getTourActivities().servesJob(aBreak)) {
if (route.getEnd().getArrTime() > aBreak.getActivity().getTimeWindow().getEnd()) {
if (route.getEnd().getArrTime() > aBreak.getActivity().getBreakTimeWindow().getEnd()) {
InsertionData iData = breakInsertionCalculator.getInsertionData(route, aBreak, route.getVehicle(), route.getDepartureTime(), route.getDriver(), Double.MAX_VALUE);
if (!(iData instanceof InsertionData.NoInsertionFound)) {
logger.trace("insert: [jobId={}]{}", aBreak.getId(), iData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,37 @@

package com.graphhopper.jsprit.core.algorithm.recreate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.graphhopper.jsprit.core.problem.job.Job;
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
import com.graphhopper.jsprit.core.problem.vehicle.Vehicle;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleFleetManager;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleImpl;

import java.util.*;

/**
* Created by schroeder on 15/10/15.
*/
class InsertionDataUpdater {

static boolean update(boolean addAllAvailable, Set<String> initialVehicleIds, VehicleFleetManager fleetManager, JobInsertionCostsCalculator insertionCostsCalculator, TreeSet<VersionedInsertionData> insertionDataSet, int updateRound, Job unassignedJob, Collection<VehicleRoute> routes) {
for (VehicleRoute route : routes) {
Collection<Vehicle> relevantVehicles = new ArrayList<Vehicle>();
Collection<Vehicle> relevantVehicles = new ArrayList<>();
if (!(route.getVehicle() instanceof VehicleImpl.NoVehicle)) {
relevantVehicles.add(route.getVehicle());
if (addAllAvailable && !initialVehicleIds.contains(route.getVehicle().getId())) {
relevantVehicles.addAll(fleetManager.getAvailableVehicles(route.getVehicle()));
}
} else relevantVehicles.addAll(fleetManager.getAvailableVehicles());
} else {
relevantVehicles.addAll(fleetManager.getAvailableVehicles());
}
for (Vehicle v : relevantVehicles) {
double depTime = v.getEarliestDeparture();
InsertionData iData = insertionCostsCalculator.getInsertionData(route, unassignedJob, v, depTime, route.getDriver(), Double.MAX_VALUE);
Expand Down Expand Up @@ -67,13 +76,16 @@ static Comparator<VersionedInsertionData> getComparator() {
};
}

static ScoredJob getBest(boolean switchAllowed, Set<String> initialVehicleIds, VehicleFleetManager fleetManager, JobInsertionCostsCalculator insertionCostsCalculator, ScoringFunction scoringFunction, TreeSet<VersionedInsertionData>[] priorityQueues, Map<VehicleRoute, Integer> updates, List<Job> unassignedJobList, List<Job> badJobs) {
static ScoredJob getBest(boolean switchAllowed, Set<String> initialVehicleIds, VehicleFleetManager fleetManager,
JobInsertionCostsCalculator insertionCostsCalculator, ScoringFunction scoringFunction,
Map<String, TreeSet<VersionedInsertionData>> priorityQueues, Map<VehicleRoute, Integer> updates,
List<Job> unassignedJobList, List<Job> badJobs) {
ScoredJob bestScoredJob = null;
for (Job j : unassignedJobList) {
VehicleRoute bestRoute = null;
InsertionData best = null;
InsertionData secondBest = null;
TreeSet<VersionedInsertionData> priorityQueue = priorityQueues[j.getIndex()];
TreeSet<VersionedInsertionData> priorityQueue = priorityQueues.get(j.getId());
Iterator<VersionedInsertionData> iterator = priorityQueue.iterator();
while (iterator.hasNext()) {
VersionedInsertionData versionedIData = iterator.next();
Expand All @@ -82,11 +94,17 @@ static ScoredJob getBest(boolean switchAllowed, Set<String> initialVehicleIds, V
continue;
}
}
if (versionedIData.getiData() instanceof InsertionData.NoInsertionFound) continue;
if (versionedIData.getiData() instanceof InsertionData.NoInsertionFound) {
continue;
}
if (!(versionedIData.getRoute().getVehicle() instanceof VehicleImpl.NoVehicle)) {
if (versionedIData.getiData().getSelectedVehicle() != versionedIData.getRoute().getVehicle()) {
if (!switchAllowed) continue;
if (initialVehicleIds.contains(versionedIData.getRoute().getVehicle().getId())) continue;
if (!switchAllowed) {
continue;
}
if (initialVehicleIds.contains(versionedIData.getRoute().getVehicle().getId())) {
continue;
}
}
}
if (versionedIData.getiData().getSelectedVehicle() != versionedIData.getRoute().getVehicle()) {
Expand All @@ -98,10 +116,14 @@ static ScoredJob getBest(boolean switchAllowed, Set<String> initialVehicleIds, V
for (Event e : oldData.getEvents()) {
if (e instanceof SwitchVehicle) {
newData.getEvents().add(new SwitchVehicle(versionedIData.getRoute(), available, oldData.getVehicleDepartureTime()));
} else newData.getEvents().add(e);
} else {
newData.getEvents().add(e);
}
}
versionedIData = new VersionedInsertionData(newData, versionedIData.getVersion(), versionedIData.getRoute());
} else continue;
} else {
continue;
}
}
}
int currentDataVersion = updates.get(versionedIData.getRoute());
Expand Down Expand Up @@ -137,7 +159,9 @@ static ScoredJob getBest(boolean switchAllowed, Set<String> initialVehicleIds, V
ScoredJob scoredJob;
if (bestRoute == emptyRoute) {
scoredJob = new ScoredJob(j, score, best, bestRoute, true);
} else scoredJob = new ScoredJob(j, score, best, bestRoute, false);
} else {
scoredJob = new ScoredJob(j, score, best, bestRoute, false);
}

if (bestScoredJob == null) {
bestScoredJob = scoredJob;
Expand Down
Loading