mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
issue #62 - end location setting in open routes
This commit is contained in:
parent
ad93dfeb47
commit
9d78874c22
2 changed files with 72 additions and 9 deletions
|
|
@ -66,7 +66,9 @@ import jsprit.core.algorithm.selector.SelectBest;
|
||||||
import jsprit.core.algorithm.selector.SelectRandomly;
|
import jsprit.core.algorithm.selector.SelectRandomly;
|
||||||
import jsprit.core.algorithm.selector.SolutionSelector;
|
import jsprit.core.algorithm.selector.SolutionSelector;
|
||||||
import jsprit.core.algorithm.state.StateManager;
|
import jsprit.core.algorithm.state.StateManager;
|
||||||
|
import jsprit.core.algorithm.state.StateUpdater;
|
||||||
import jsprit.core.algorithm.state.UpdateActivityTimes;
|
import jsprit.core.algorithm.state.UpdateActivityTimes;
|
||||||
|
import jsprit.core.algorithm.state.UpdateEndLocationIfRouteIsOpen;
|
||||||
import jsprit.core.algorithm.state.UpdateVariableCosts;
|
import jsprit.core.algorithm.state.UpdateVariableCosts;
|
||||||
import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
|
import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination;
|
||||||
import jsprit.core.algorithm.termination.PrematureAlgorithmTermination;
|
import jsprit.core.algorithm.termination.PrematureAlgorithmTermination;
|
||||||
|
|
@ -78,6 +80,9 @@ import jsprit.core.problem.constraint.ConstraintManager;
|
||||||
import jsprit.core.problem.solution.SolutionCostCalculator;
|
import jsprit.core.problem.solution.SolutionCostCalculator;
|
||||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||||
import jsprit.core.problem.solution.route.VehicleRoute;
|
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||||
|
import jsprit.core.problem.solution.route.activity.End;
|
||||||
|
import jsprit.core.problem.solution.route.activity.ReverseActivityVisitor;
|
||||||
|
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||||
import jsprit.core.problem.solution.route.state.StateFactory;
|
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||||
import jsprit.core.problem.vehicle.FiniteFleetManagerFactory;
|
import jsprit.core.problem.vehicle.FiniteFleetManagerFactory;
|
||||||
import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
|
import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
|
||||||
|
|
@ -439,7 +444,7 @@ public class VehicleRoutingAlgorithms {
|
||||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||||
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
||||||
xmlReader.read(configFileName);
|
xmlReader.read(configFileName);
|
||||||
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),nThreads, stateManager);
|
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(), nThreads, stateManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(VehicleRoutingProblem vrp, int nThreads, String configFileName) {
|
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(VehicleRoutingProblem vrp, int nThreads, String configFileName) {
|
||||||
|
|
@ -449,7 +454,40 @@ public class VehicleRoutingAlgorithms {
|
||||||
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),nThreads, null);
|
return createAlgo(vrp,algorithmConfig.getXMLConfiguration(),nThreads, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class OpenRouteStateVerifier implements StateUpdater, ReverseActivityVisitor{
|
||||||
|
|
||||||
|
private End end;
|
||||||
|
|
||||||
|
private boolean firstAct = true;
|
||||||
|
|
||||||
|
private Vehicle vehicle;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void begin(VehicleRoute route) {
|
||||||
|
end = route.getEnd();
|
||||||
|
vehicle = route.getVehicle();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(TourActivity activity) {
|
||||||
|
if(firstAct){
|
||||||
|
firstAct=false;
|
||||||
|
if(!vehicle.isReturnToDepot()){
|
||||||
|
assert activity.getLocationId() == end.getLocationId() : "route end and last activity are not equal even route is open. this should not be.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void finish() {
|
||||||
|
if(firstAct){
|
||||||
|
assert vehicle.getLocationId() == end.getLocationId() : "route end and last activity are not equal even route is open. this should not be.";
|
||||||
|
}
|
||||||
|
firstAct = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private static VehicleRoutingAlgorithm createAlgo(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, StateManager stateMan){
|
private static VehicleRoutingAlgorithm createAlgo(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, StateManager stateMan){
|
||||||
|
|
||||||
|
|
@ -509,6 +547,8 @@ public class VehicleRoutingAlgorithms {
|
||||||
}
|
}
|
||||||
stateManager.updateLoadStates();
|
stateManager.updateLoadStates();
|
||||||
stateManager.updateTimeWindowStates();
|
stateManager.updateTimeWindowStates();
|
||||||
|
stateManager.addStateUpdater(new UpdateEndLocationIfRouteIsOpen());
|
||||||
|
stateManager.addStateUpdater(new OpenRouteStateVerifier());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* define constraints
|
* define constraints
|
||||||
|
|
@ -553,15 +593,9 @@ public class VehicleRoutingAlgorithms {
|
||||||
/*
|
/*
|
||||||
* define stateUpdates
|
* define stateUpdates
|
||||||
*/
|
*/
|
||||||
// UpdateLoads loadUpdater = new UpdateLoads(stateManager);
|
|
||||||
// stateManager.addListener(loadUpdater);
|
|
||||||
// stateManager.addActivityVisitor(loadUpdater);
|
|
||||||
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts()));
|
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts()));
|
||||||
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
|
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
|
||||||
|
|
||||||
// stateManager.addActivityVisitor(new UpdateOccuredDeliveries(stateManager));
|
|
||||||
// stateManager.addActivityVisitor(new TimeWindowUpdater(stateManager, vrp.getTransportCosts()));
|
|
||||||
// stateManager.addActivityVisitor(new UpdateFuturePickups(stateManager));
|
|
||||||
|
|
||||||
metaAlgorithm.getSearchStrategyManager().addSearchStrategyModuleListener(stateManager);
|
metaAlgorithm.getSearchStrategyManager().addSearchStrategyModuleListener(stateManager);
|
||||||
metaAlgorithm.getAlgorithmListeners().addListener(stateManager);
|
metaAlgorithm.getAlgorithmListeners().addListener(stateManager);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
package jsprit.core.algorithm.state;
|
||||||
|
|
||||||
|
import jsprit.core.problem.solution.route.RouteVisitor;
|
||||||
|
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||||
|
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||||
|
|
||||||
|
public class UpdateEndLocationIfRouteIsOpen implements StateUpdater, RouteVisitor{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(VehicleRoute route) {
|
||||||
|
if(route.getVehicle() != null){
|
||||||
|
if(!route.getVehicle().isReturnToDepot()){
|
||||||
|
setRouteEndToLastActivity(route);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setRouteEndToLastActivity(VehicleRoute route) {
|
||||||
|
if(!route.getActivities().isEmpty()){
|
||||||
|
TourActivity lastAct = route.getActivities().get(route.getActivities().size()-1);
|
||||||
|
route.getEnd().setLocationId(lastAct.getLocationId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue