mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
removed defaultValues from core.algorithm.state.StateManager.
Null checking is moved to client classes
This commit is contained in:
parent
c13d6d8901
commit
218e7d9817
12 changed files with 73 additions and 33 deletions
|
|
@ -64,17 +64,19 @@ class RouteLevelActivityInsertionCostsEstimator implements ActivityInsertionCost
|
|||
* calculates the path costs with new vehicle, c(forwardPath,newVehicle).
|
||||
*/
|
||||
double forwardPathCost_newVehicle = auxilliaryPathCostCalculator.costOfPath(path, depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||
double additionalCosts = forwardPathCost_newVehicle - (actCostsOld(iFacts.getRoute(), path.get(path.size()-1)) - actCostsOld(iFacts.getRoute(), prevAct));
|
||||
|
||||
return additionalCosts;
|
||||
|
||||
return forwardPathCost_newVehicle - (actCostsOld(iFacts.getRoute(), path.get(path.size()-1)) - actCostsOld(iFacts.getRoute(), prevAct));
|
||||
}
|
||||
|
||||
private double actCostsOld(VehicleRoute vehicleRoute, TourActivity act) {
|
||||
Double cost_at_act;
|
||||
if(act instanceof End){
|
||||
return stateManager.getRouteState(vehicleRoute,StateFactory.COSTS,Double.class);
|
||||
cost_at_act = stateManager.getRouteState(vehicleRoute, StateFactory.COSTS, Double.class);
|
||||
}
|
||||
return stateManager.getActivityState(act,StateFactory.COSTS,Double.class);
|
||||
else{
|
||||
cost_at_act = stateManager.getActivityState(act, StateFactory.COSTS, Double.class);
|
||||
}
|
||||
if(cost_at_act == null) cost_at_act = 0.;
|
||||
return cost_at_act;
|
||||
}
|
||||
|
||||
private List<TourActivity> getForwardLookingPath(VehicleRoute route, int actIndex) {
|
||||
|
|
|
|||
|
|
@ -261,7 +261,9 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
|
|||
/**
|
||||
* compute cost-diff of tour with and without new activity --> insertion_costs
|
||||
*/
|
||||
double insertion_costs = auxilliaryPathCostCalculator.costOfPath(wholeTour, start.getEndTime(), newDriver, newVehicle) - stateManager.getRouteState(currentRoute,StateFactory.COSTS,Double.class);
|
||||
Double currentRouteCosts = stateManager.getRouteState(currentRoute, StateFactory.COSTS, Double.class);
|
||||
if(currentRouteCosts == null) currentRouteCosts = 0.;
|
||||
double insertion_costs = auxilliaryPathCostCalculator.costOfPath(wholeTour, start.getEndTime(), newDriver, newVehicle) - currentRouteCosts;
|
||||
|
||||
/**
|
||||
* if better than best known, make it the best known
|
||||
|
|
@ -307,10 +309,13 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
|
|||
}
|
||||
|
||||
private double sumOf_prevCosts_oldVehicle(VehicleRoute vehicleRoute, TourActivity act) {
|
||||
Double prevCost;
|
||||
if(act instanceof End){
|
||||
return stateManager.getRouteState(vehicleRoute,StateFactory.COSTS,Double.class);
|
||||
prevCost = stateManager.getRouteState(vehicleRoute,StateFactory.COSTS,Double.class);
|
||||
}
|
||||
return stateManager.getActivityState(act,StateFactory.COSTS,Double.class);
|
||||
else prevCost = stateManager.getActivityState(act,StateFactory.COSTS,Double.class);
|
||||
if(prevCost == null) prevCost = 0.;
|
||||
return prevCost;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -285,7 +285,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
*/
|
||||
@Override
|
||||
public <T> T getActivityState(TourActivity act, StateId stateId, Class<T> type) {
|
||||
// if(act.getIndex()<0) return getDefaultTypedActivityState(act, stateId, type);
|
||||
if(act.getIndex()<0) return null;
|
||||
T state;
|
||||
try{
|
||||
state = type.cast(activity_states[act.getIndex()][stateId.getIndex()]);
|
||||
|
|
@ -293,7 +293,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
catch (ClassCastException e){
|
||||
throw getClassCastException(e,stateId,type.toString(),activity_states[act.getIndex()][stateId.getIndex()].getClass().toString());
|
||||
}
|
||||
// if(state == null) throw new NullPointerException("state " + stateId.toString() + " of activity " + act + " is missing.");
|
||||
return state;
|
||||
}
|
||||
|
||||
|
|
@ -323,7 +322,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
* @throws java.lang.ClassCastException if type class is not equal to the associated type class of the requested state value
|
||||
*/
|
||||
public <T> T getActivityState(TourActivity act, Vehicle vehicle, StateId stateId, Class<T> type) {
|
||||
// if(act.getIndex()<0) return getDefaultTypedActivityState(act, stateId, type);
|
||||
if(act.getIndex()<0) return null;
|
||||
T state;
|
||||
try {
|
||||
state = type.cast(vehicle_dependent_activity_states[act.getIndex()][vehicle.getVehicleTypeIdentifier().getIndex()][stateId.getIndex()]);
|
||||
|
|
@ -332,7 +331,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
Object state_class = vehicle_dependent_activity_states[act.getIndex()][vehicle.getVehicleTypeIdentifier().getIndex()][stateId.getIndex()];
|
||||
throw getClassCastException(e,stateId,type.toString(),state_class.getClass().toString());
|
||||
}
|
||||
// if(state == null) throw new NullPointerException("state " + stateId.toString() + " of activity " + act + " for vehicle " + vehicle.getId() + " is missing.");
|
||||
return state;
|
||||
}
|
||||
|
||||
|
|
@ -363,10 +361,11 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
* @param type the class of the associated state value
|
||||
* @param <T> the type of the class
|
||||
* @return the route state that is associated to the route and stateId, or null if no state is associated.
|
||||
* @throws java.lang.ClassCastException if type class is not equal to the associated type class of the requested state value
|
||||
*/
|
||||
@Override
|
||||
public <T> T getRouteState(VehicleRoute route, StateId stateId, Class<T> type) {
|
||||
// if(route.isEmpty()) return getDefaultTypedRouteState(stateId,type);
|
||||
if(route.isEmpty()) return null;
|
||||
T state;
|
||||
try{
|
||||
state = type.cast(route_states[route.getActivities().get(0).getIndex()][stateId.getIndex()]);
|
||||
|
|
@ -374,7 +373,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
catch (ClassCastException e){
|
||||
throw getClassCastException(e,stateId,type.toString(),route_states[route.getActivities().get(0).getIndex()][stateId.getIndex()].getClass().toString());
|
||||
}
|
||||
if(state==null) throw new NullPointerException("state " + stateId.toString() + " of route " + route + " is missing.");
|
||||
return state;
|
||||
}
|
||||
|
||||
|
|
@ -402,7 +400,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
* @throws java.lang.ClassCastException if specified type is not equal to the memorized type
|
||||
*/
|
||||
public <T> T getRouteState(VehicleRoute route, Vehicle vehicle, StateId stateId, Class<T> type) {
|
||||
if(route.isEmpty()) return getDefaultTypedRouteState(stateId,type);
|
||||
if(route.isEmpty()) return null;
|
||||
T state;
|
||||
try{
|
||||
state = type.cast(vehicle_dependent_route_states[route.getActivities().get(0).getIndex()][vehicle.getVehicleTypeIdentifier().getIndex()][stateId.getIndex()]);
|
||||
|
|
@ -413,6 +411,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
return state;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private <T> T getDefaultTypedRouteState(StateId stateId, Class<T> type) {
|
||||
if(defaultRouteStates_.containsKey(stateId)){
|
||||
return type.cast(defaultRouteStates_.get(stateId));
|
||||
|
|
|
|||
|
|
@ -51,18 +51,22 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
|
|||
/*
|
||||
* default has one dimension with a value of zero
|
||||
*/
|
||||
private Capacity currentLoad = Capacity.Builder.newInstance().build();
|
||||
private Capacity currentLoad;
|
||||
|
||||
private Capacity defaultValue;
|
||||
|
||||
private VehicleRoute route;
|
||||
|
||||
public UpdateLoads(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
defaultValue = Capacity.Builder.newInstance().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
currentLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
if(currentLoad == null) currentLoad = defaultValue;
|
||||
this.route = route;
|
||||
}
|
||||
|
||||
|
|
@ -103,10 +107,12 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
|
|||
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
|
||||
if(job2insert instanceof Delivery){
|
||||
Capacity loadAtDepot = stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
if(loadAtDepot == null) loadAtDepot = defaultValue;
|
||||
stateManager.putTypedInternalRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING, Capacity.addup(loadAtDepot, job2insert.getSize()));
|
||||
}
|
||||
else if(job2insert instanceof Pickup || job2insert instanceof Service){
|
||||
Capacity loadAtEnd = stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_END, Capacity.class);
|
||||
if(loadAtEnd == null) loadAtEnd = defaultValue;
|
||||
stateManager.putTypedInternalRouteState(inRoute, StateFactory.LOAD_AT_END, Capacity.addup(loadAtEnd, job2insert.getSize()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,14 +39,18 @@ class UpdateMaxCapacityUtilisationAtActivitiesByLookingBackwardInRoute implement
|
|||
|
||||
private Capacity maxLoad;
|
||||
|
||||
private Capacity defaultValue;
|
||||
|
||||
public UpdateMaxCapacityUtilisationAtActivitiesByLookingBackwardInRoute(StateManager stateManager) {
|
||||
this.stateManager = stateManager;
|
||||
defaultValue = Capacity.Builder.newInstance().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
this.route = route;
|
||||
maxLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
if(maxLoad == null) maxLoad = defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -49,17 +49,21 @@ class UpdateMaxCapacityUtilisationAtActivitiesByLookingForwardInRoute implements
|
|||
|
||||
private VehicleRoute route;
|
||||
|
||||
private Capacity maxLoad = Capacity.Builder.newInstance().build();
|
||||
private Capacity maxLoad;
|
||||
|
||||
private Capacity defaultValue;
|
||||
|
||||
public UpdateMaxCapacityUtilisationAtActivitiesByLookingForwardInRoute(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
defaultValue = Capacity.Builder.newInstance().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
this.route = route;
|
||||
maxLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_END, Capacity.class);
|
||||
if(maxLoad == null) maxLoad = defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -43,16 +43,20 @@ class UpdateMaxCapacityUtilisationAtRoute implements ActivityVisitor, StateUpdat
|
|||
|
||||
private VehicleRoute route;
|
||||
|
||||
private Capacity maxLoad = Capacity.Builder.newInstance().build();
|
||||
private Capacity maxLoad;
|
||||
|
||||
private Capacity defaultValue;
|
||||
|
||||
public UpdateMaxCapacityUtilisationAtRoute(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
defaultValue = Capacity.Builder.newInstance().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
currentLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
if(currentLoad == null) currentLoad = defaultValue;
|
||||
maxLoad = currentLoad;
|
||||
this.route = route;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,8 @@ public class PickupAndDeliverShipmentLoadActivityLevelConstraint implements Hard
|
|||
|
||||
private RouteAndActivityStateGetter stateManager;
|
||||
|
||||
private Capacity defaultValue;
|
||||
|
||||
/**
|
||||
* Constructs the constraint ensuring capacity constraint at each activity.
|
||||
*
|
||||
|
|
@ -53,6 +55,7 @@ public class PickupAndDeliverShipmentLoadActivityLevelConstraint implements Hard
|
|||
public PickupAndDeliverShipmentLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
defaultValue = Capacity.Builder.newInstance().build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -67,9 +70,11 @@ public class PickupAndDeliverShipmentLoadActivityLevelConstraint implements Hard
|
|||
Capacity loadAtPrevAct;
|
||||
if(prevAct instanceof Start){
|
||||
loadAtPrevAct = stateManager.getRouteState(iFacts.getRoute(), StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
if(loadAtPrevAct == null) loadAtPrevAct = defaultValue;
|
||||
}
|
||||
else{
|
||||
loadAtPrevAct = stateManager.getActivityState(prevAct, StateFactory.LOAD, Capacity.class);
|
||||
if(loadAtPrevAct == null) loadAtPrevAct = defaultValue;
|
||||
}
|
||||
if(newAct instanceof PickupShipment){
|
||||
if(!Capacity.addup(loadAtPrevAct, newAct.getSize()).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())){
|
||||
|
|
|
|||
|
|
@ -20,11 +20,7 @@ package jsprit.core.problem.constraint;
|
|||
|
||||
import jsprit.core.problem.Capacity;
|
||||
import jsprit.core.problem.misc.JobInsertionContext;
|
||||
import jsprit.core.problem.solution.route.activity.DeliverService;
|
||||
import jsprit.core.problem.solution.route.activity.PickupService;
|
||||
import jsprit.core.problem.solution.route.activity.ServiceActivity;
|
||||
import jsprit.core.problem.solution.route.activity.Start;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
import jsprit.core.problem.solution.route.activity.*;
|
||||
import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||
|
||||
|
|
@ -42,9 +38,12 @@ class ServiceLoadActivityLevelConstraint implements HardActivityStateLevelConstr
|
|||
|
||||
private RouteAndActivityStateGetter stateManager;
|
||||
|
||||
private Capacity defaultValue;
|
||||
|
||||
public ServiceLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
defaultValue = Capacity.Builder.newInstance().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -53,11 +52,15 @@ class ServiceLoadActivityLevelConstraint implements HardActivityStateLevelConstr
|
|||
Capacity prevMaxLoad;
|
||||
if(prevAct instanceof Start){
|
||||
futureMaxLoad = stateManager.getRouteState(iFacts.getRoute(), StateFactory.MAXLOAD, Capacity.class);
|
||||
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
|
||||
prevMaxLoad = stateManager.getRouteState(iFacts.getRoute(), StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
|
||||
}
|
||||
else{
|
||||
futureMaxLoad = stateManager.getActivityState(prevAct, StateFactory.FUTURE_MAXLOAD, Capacity.class);
|
||||
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
|
||||
prevMaxLoad = stateManager.getActivityState(prevAct, StateFactory.PAST_MAXLOAD, Capacity.class);
|
||||
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
|
||||
|
||||
}
|
||||
if(newAct instanceof PickupService || newAct instanceof ServiceActivity){
|
||||
|
|
|
|||
|
|
@ -39,26 +39,32 @@ class ServiceLoadRouteLevelConstraint implements HardRouteStateLevelConstraint {
|
|||
|
||||
private RouteAndActivityStateGetter stateManager;
|
||||
|
||||
private Capacity defaultValue;
|
||||
|
||||
public ServiceLoadRouteLevelConstraint(RouteAndActivityStateGetter stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
this.defaultValue = Capacity.Builder.newInstance().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fulfilled(JobInsertionContext insertionContext) {
|
||||
Capacity maxLoadAtRoute = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.MAXLOAD, Capacity.class);
|
||||
if(maxLoadAtRoute == null) maxLoadAtRoute = defaultValue;
|
||||
Capacity capacityDimensions = insertionContext.getNewVehicle().getType().getCapacityDimensions();
|
||||
if(!maxLoadAtRoute.isLessOrEqual(capacityDimensions)){
|
||||
return false;
|
||||
}
|
||||
if(insertionContext.getJob() instanceof Delivery){
|
||||
Capacity loadAtDepot = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
if(loadAtDepot == null) loadAtDepot = defaultValue;
|
||||
if(!Capacity.addup(loadAtDepot, insertionContext.getJob().getSize()).isLessOrEqual(capacityDimensions)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if(insertionContext.getJob() instanceof Pickup || insertionContext.getJob() instanceof Service){
|
||||
Capacity loadAtEnd = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.LOAD_AT_END, Capacity.class);
|
||||
if(loadAtEnd == null) loadAtEnd = defaultValue;
|
||||
if(!Capacity.addup(loadAtEnd, insertionContext.getJob().getSize()).isLessOrEqual(capacityDimensions)){
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,7 +80,9 @@ public class BuildPDVRPAlgoFromScratch_IT {
|
|||
public double getCosts(VehicleRoutingProblemSolution solution) {
|
||||
double costs = 0.0;
|
||||
for(VehicleRoute route : solution.getRoutes()){
|
||||
costs += stateManager.getRouteState(route, StateFactory.COSTS, Double.class);
|
||||
Double cost_of_route = stateManager.getRouteState(route, StateFactory.COSTS, Double.class);
|
||||
if(cost_of_route == null) cost_of_route = 0.;
|
||||
costs += cost_of_route;
|
||||
}
|
||||
return costs;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -125,14 +125,14 @@ public class StateManagerTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenProblemStateIsSetAndStateManagerClearedAfterwards_itReturnsDefault(){
|
||||
public void whenProblemStateIsSetAndStateManagerClearedAfterwards_itReturnsNull(){
|
||||
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
|
||||
StateId id = StateFactory.createId("problemState");
|
||||
stateManager.addDefaultProblemState(id, Boolean.class, false);
|
||||
stateManager.putProblemState(id, Boolean.class, true);
|
||||
stateManager.clear();
|
||||
boolean problemState = stateManager.getProblemState(id, Boolean.class);
|
||||
assertFalse(problemState);
|
||||
Boolean problemState = stateManager.getProblemState(id, Boolean.class);
|
||||
assertNull(problemState);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue