1
0
Fork 0
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:
oblonski 2014-07-22 20:45:50 +02:00
parent c13d6d8901
commit 218e7d9817
12 changed files with 73 additions and 33 deletions

View file

@ -64,17 +64,19 @@ class RouteLevelActivityInsertionCostsEstimator implements ActivityInsertionCost
* calculates the path costs with new vehicle, c(forwardPath,newVehicle). * calculates the path costs with new vehicle, c(forwardPath,newVehicle).
*/ */
double forwardPathCost_newVehicle = auxilliaryPathCostCalculator.costOfPath(path, depTimeAtPrevAct, iFacts.getNewDriver(), iFacts.getNewVehicle()); 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 forwardPathCost_newVehicle - (actCostsOld(iFacts.getRoute(), path.get(path.size()-1)) - actCostsOld(iFacts.getRoute(), prevAct));
return additionalCosts;
} }
private double actCostsOld(VehicleRoute vehicleRoute, TourActivity act) { private double actCostsOld(VehicleRoute vehicleRoute, TourActivity act) {
if(act instanceof End){ Double cost_at_act;
return stateManager.getRouteState(vehicleRoute,StateFactory.COSTS,Double.class); if(act instanceof End){
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) { private List<TourActivity> getForwardLookingPath(VehicleRoute route, int actIndex) {

View file

@ -261,7 +261,9 @@ final class ServiceInsertionOnRouteLevelCalculator implements JobInsertionCostsC
/** /**
* compute cost-diff of tour with and without new activity --> insertion_costs * 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 * 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) { private double sumOf_prevCosts_oldVehicle(VehicleRoute vehicleRoute, TourActivity act) {
if(act instanceof End){ Double prevCost;
return stateManager.getRouteState(vehicleRoute,StateFactory.COSTS,Double.class); if(act instanceof End){
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;
} }
/** /**

View file

@ -285,7 +285,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
*/ */
@Override @Override
public <T> T getActivityState(TourActivity act, StateId stateId, Class<T> type) { 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; T state;
try{ try{
state = type.cast(activity_states[act.getIndex()][stateId.getIndex()]); state = type.cast(activity_states[act.getIndex()][stateId.getIndex()]);
@ -293,7 +293,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
catch (ClassCastException e){ catch (ClassCastException e){
throw getClassCastException(e,stateId,type.toString(),activity_states[act.getIndex()][stateId.getIndex()].getClass().toString()); 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; 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 * @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) { 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; T state;
try { try {
state = type.cast(vehicle_dependent_activity_states[act.getIndex()][vehicle.getVehicleTypeIdentifier().getIndex()][stateId.getIndex()]); 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()]; Object state_class = vehicle_dependent_activity_states[act.getIndex()][vehicle.getVehicleTypeIdentifier().getIndex()][stateId.getIndex()];
throw getClassCastException(e,stateId,type.toString(),state_class.getClass().toString()); 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; return state;
} }
@ -363,10 +361,11 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
* @param type the class of the associated state value * @param type the class of the associated state value
* @param <T> the type of the class * @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. * @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 @Override
public <T> T getRouteState(VehicleRoute route, StateId stateId, Class<T> type) { 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; T state;
try{ try{
state = type.cast(route_states[route.getActivities().get(0).getIndex()][stateId.getIndex()]); 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){ catch (ClassCastException e){
throw getClassCastException(e,stateId,type.toString(),route_states[route.getActivities().get(0).getIndex()][stateId.getIndex()].getClass().toString()); 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; 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 * @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) { 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; T state;
try{ try{
state = type.cast(vehicle_dependent_route_states[route.getActivities().get(0).getIndex()][vehicle.getVehicleTypeIdentifier().getIndex()][stateId.getIndex()]); 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; return state;
} }
@Deprecated
private <T> T getDefaultTypedRouteState(StateId stateId, Class<T> type) { private <T> T getDefaultTypedRouteState(StateId stateId, Class<T> type) {
if(defaultRouteStates_.containsKey(stateId)){ if(defaultRouteStates_.containsKey(stateId)){
return type.cast(defaultRouteStates_.get(stateId)); return type.cast(defaultRouteStates_.get(stateId));

View file

@ -51,18 +51,22 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
/* /*
* default has one dimension with a value of zero * 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; private VehicleRoute route;
public UpdateLoads(StateManager stateManager) { public UpdateLoads(StateManager stateManager) {
super(); super();
this.stateManager = stateManager; this.stateManager = stateManager;
defaultValue = Capacity.Builder.newInstance().build();
} }
@Override @Override
public void begin(VehicleRoute route) { public void begin(VehicleRoute route) {
currentLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class); currentLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
if(currentLoad == null) currentLoad = defaultValue;
this.route = route; this.route = route;
} }
@ -103,10 +107,12 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) { public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
if(job2insert instanceof Delivery){ if(job2insert instanceof Delivery){
Capacity loadAtDepot = stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING, Capacity.class); 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())); stateManager.putTypedInternalRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING, Capacity.addup(loadAtDepot, job2insert.getSize()));
} }
else if(job2insert instanceof Pickup || job2insert instanceof Service){ else if(job2insert instanceof Pickup || job2insert instanceof Service){
Capacity loadAtEnd = stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_END, Capacity.class); 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())); stateManager.putTypedInternalRouteState(inRoute, StateFactory.LOAD_AT_END, Capacity.addup(loadAtEnd, job2insert.getSize()));
} }
} }

View file

@ -39,14 +39,18 @@ class UpdateMaxCapacityUtilisationAtActivitiesByLookingBackwardInRoute implement
private Capacity maxLoad; private Capacity maxLoad;
private Capacity defaultValue;
public UpdateMaxCapacityUtilisationAtActivitiesByLookingBackwardInRoute(StateManager stateManager) { public UpdateMaxCapacityUtilisationAtActivitiesByLookingBackwardInRoute(StateManager stateManager) {
this.stateManager = stateManager; this.stateManager = stateManager;
defaultValue = Capacity.Builder.newInstance().build();
} }
@Override @Override
public void begin(VehicleRoute route) { public void begin(VehicleRoute route) {
this.route = route; this.route = route;
maxLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class); maxLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
if(maxLoad == null) maxLoad = defaultValue;
} }
@Override @Override

View file

@ -49,17 +49,21 @@ class UpdateMaxCapacityUtilisationAtActivitiesByLookingForwardInRoute implements
private VehicleRoute route; private VehicleRoute route;
private Capacity maxLoad = Capacity.Builder.newInstance().build(); private Capacity maxLoad;
private Capacity defaultValue;
public UpdateMaxCapacityUtilisationAtActivitiesByLookingForwardInRoute(StateManager stateManager) { public UpdateMaxCapacityUtilisationAtActivitiesByLookingForwardInRoute(StateManager stateManager) {
super(); super();
this.stateManager = stateManager; this.stateManager = stateManager;
defaultValue = Capacity.Builder.newInstance().build();
} }
@Override @Override
public void begin(VehicleRoute route) { public void begin(VehicleRoute route) {
this.route = route; this.route = route;
maxLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_END, Capacity.class); maxLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_END, Capacity.class);
if(maxLoad == null) maxLoad = defaultValue;
} }
@Override @Override

View file

@ -43,16 +43,20 @@ class UpdateMaxCapacityUtilisationAtRoute implements ActivityVisitor, StateUpdat
private VehicleRoute route; private VehicleRoute route;
private Capacity maxLoad = Capacity.Builder.newInstance().build(); private Capacity maxLoad;
private Capacity defaultValue;
public UpdateMaxCapacityUtilisationAtRoute(StateManager stateManager) { public UpdateMaxCapacityUtilisationAtRoute(StateManager stateManager) {
super(); super();
this.stateManager = stateManager; this.stateManager = stateManager;
defaultValue = Capacity.Builder.newInstance().build();
} }
@Override @Override
public void begin(VehicleRoute route) { public void begin(VehicleRoute route) {
currentLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class); currentLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
if(currentLoad == null) currentLoad = defaultValue;
maxLoad = currentLoad; maxLoad = currentLoad;
this.route = route; this.route = route;
} }

View file

@ -41,6 +41,8 @@ public class PickupAndDeliverShipmentLoadActivityLevelConstraint implements Hard
private RouteAndActivityStateGetter stateManager; private RouteAndActivityStateGetter stateManager;
private Capacity defaultValue;
/** /**
* Constructs the constraint ensuring capacity constraint at each activity. * Constructs the constraint ensuring capacity constraint at each activity.
* *
@ -53,6 +55,7 @@ public class PickupAndDeliverShipmentLoadActivityLevelConstraint implements Hard
public PickupAndDeliverShipmentLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) { public PickupAndDeliverShipmentLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) {
super(); super();
this.stateManager = stateManager; this.stateManager = stateManager;
defaultValue = Capacity.Builder.newInstance().build();
} }
/** /**
@ -67,9 +70,11 @@ public class PickupAndDeliverShipmentLoadActivityLevelConstraint implements Hard
Capacity loadAtPrevAct; Capacity loadAtPrevAct;
if(prevAct instanceof Start){ if(prevAct instanceof Start){
loadAtPrevAct = stateManager.getRouteState(iFacts.getRoute(), StateFactory.LOAD_AT_BEGINNING, Capacity.class); loadAtPrevAct = stateManager.getRouteState(iFacts.getRoute(), StateFactory.LOAD_AT_BEGINNING, Capacity.class);
if(loadAtPrevAct == null) loadAtPrevAct = defaultValue;
} }
else{ else{
loadAtPrevAct = stateManager.getActivityState(prevAct, StateFactory.LOAD, Capacity.class); loadAtPrevAct = stateManager.getActivityState(prevAct, StateFactory.LOAD, Capacity.class);
if(loadAtPrevAct == null) loadAtPrevAct = defaultValue;
} }
if(newAct instanceof PickupShipment){ if(newAct instanceof PickupShipment){
if(!Capacity.addup(loadAtPrevAct, newAct.getSize()).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())){ if(!Capacity.addup(loadAtPrevAct, newAct.getSize()).isLessOrEqual(iFacts.getNewVehicle().getType().getCapacityDimensions())){

View file

@ -20,11 +20,7 @@ package jsprit.core.problem.constraint;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.misc.JobInsertionContext; import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.activity.DeliverService; import jsprit.core.problem.solution.route.activity.*;
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.state.RouteAndActivityStateGetter; import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import jsprit.core.problem.solution.route.state.StateFactory; import jsprit.core.problem.solution.route.state.StateFactory;
@ -42,9 +38,12 @@ class ServiceLoadActivityLevelConstraint implements HardActivityStateLevelConstr
private RouteAndActivityStateGetter stateManager; private RouteAndActivityStateGetter stateManager;
private Capacity defaultValue;
public ServiceLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) { public ServiceLoadActivityLevelConstraint(RouteAndActivityStateGetter stateManager) {
super(); super();
this.stateManager = stateManager; this.stateManager = stateManager;
defaultValue = Capacity.Builder.newInstance().build();
} }
@Override @Override
@ -53,11 +52,15 @@ class ServiceLoadActivityLevelConstraint implements HardActivityStateLevelConstr
Capacity prevMaxLoad; Capacity prevMaxLoad;
if(prevAct instanceof Start){ if(prevAct instanceof Start){
futureMaxLoad = stateManager.getRouteState(iFacts.getRoute(), StateFactory.MAXLOAD, Capacity.class); 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); prevMaxLoad = stateManager.getRouteState(iFacts.getRoute(), StateFactory.LOAD_AT_BEGINNING, Capacity.class);
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
} }
else{ else{
futureMaxLoad = stateManager.getActivityState(prevAct, StateFactory.FUTURE_MAXLOAD, Capacity.class); futureMaxLoad = stateManager.getActivityState(prevAct, StateFactory.FUTURE_MAXLOAD, Capacity.class);
if(futureMaxLoad == null) futureMaxLoad = defaultValue;
prevMaxLoad = stateManager.getActivityState(prevAct, StateFactory.PAST_MAXLOAD, Capacity.class); prevMaxLoad = stateManager.getActivityState(prevAct, StateFactory.PAST_MAXLOAD, Capacity.class);
if(prevMaxLoad == null) prevMaxLoad = defaultValue;
} }
if(newAct instanceof PickupService || newAct instanceof ServiceActivity){ if(newAct instanceof PickupService || newAct instanceof ServiceActivity){

View file

@ -39,27 +39,33 @@ class ServiceLoadRouteLevelConstraint implements HardRouteStateLevelConstraint {
private RouteAndActivityStateGetter stateManager; private RouteAndActivityStateGetter stateManager;
private Capacity defaultValue;
public ServiceLoadRouteLevelConstraint(RouteAndActivityStateGetter stateManager) { public ServiceLoadRouteLevelConstraint(RouteAndActivityStateGetter stateManager) {
super(); super();
this.stateManager = stateManager; this.stateManager = stateManager;
this.defaultValue = Capacity.Builder.newInstance().build();
} }
@Override @Override
public boolean fulfilled(JobInsertionContext insertionContext) { public boolean fulfilled(JobInsertionContext insertionContext) {
Capacity maxLoadAtRoute = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.MAXLOAD, Capacity.class); Capacity maxLoadAtRoute = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.MAXLOAD, Capacity.class);
Capacity capacityDimensions = insertionContext.getNewVehicle().getType().getCapacityDimensions(); if(maxLoadAtRoute == null) maxLoadAtRoute = defaultValue;
Capacity capacityDimensions = insertionContext.getNewVehicle().getType().getCapacityDimensions();
if(!maxLoadAtRoute.isLessOrEqual(capacityDimensions)){ if(!maxLoadAtRoute.isLessOrEqual(capacityDimensions)){
return false; return false;
} }
if(insertionContext.getJob() instanceof Delivery){ if(insertionContext.getJob() instanceof Delivery){
Capacity loadAtDepot = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.LOAD_AT_BEGINNING, Capacity.class); Capacity loadAtDepot = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.LOAD_AT_BEGINNING, Capacity.class);
if(!Capacity.addup(loadAtDepot, insertionContext.getJob().getSize()).isLessOrEqual(capacityDimensions)){ if(loadAtDepot == null) loadAtDepot = defaultValue;
if(!Capacity.addup(loadAtDepot, insertionContext.getJob().getSize()).isLessOrEqual(capacityDimensions)){
return false; return false;
} }
} }
else if(insertionContext.getJob() instanceof Pickup || insertionContext.getJob() instanceof Service){ else if(insertionContext.getJob() instanceof Pickup || insertionContext.getJob() instanceof Service){
Capacity loadAtEnd = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.LOAD_AT_END, Capacity.class); Capacity loadAtEnd = stateManager.getRouteState(insertionContext.getRoute(), StateFactory.LOAD_AT_END, Capacity.class);
if(!Capacity.addup(loadAtEnd, insertionContext.getJob().getSize()).isLessOrEqual(capacityDimensions)){ if(loadAtEnd == null) loadAtEnd = defaultValue;
if(!Capacity.addup(loadAtEnd, insertionContext.getJob().getSize()).isLessOrEqual(capacityDimensions)){
return false; return false;
} }
} }

View file

@ -80,7 +80,9 @@ public class BuildPDVRPAlgoFromScratch_IT {
public double getCosts(VehicleRoutingProblemSolution solution) { public double getCosts(VehicleRoutingProblemSolution solution) {
double costs = 0.0; double costs = 0.0;
for(VehicleRoute route : solution.getRoutes()){ 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; return costs;
} }

View file

@ -125,14 +125,14 @@ public class StateManagerTest {
} }
@Test @Test
public void whenProblemStateIsSetAndStateManagerClearedAfterwards_itReturnsDefault(){ public void whenProblemStateIsSetAndStateManagerClearedAfterwards_itReturnsNull(){
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class)); StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
StateId id = StateFactory.createId("problemState"); StateId id = StateFactory.createId("problemState");
stateManager.addDefaultProblemState(id, Boolean.class, false); stateManager.addDefaultProblemState(id, Boolean.class, false);
stateManager.putProblemState(id, Boolean.class, true); stateManager.putProblemState(id, Boolean.class, true);
stateManager.clear(); stateManager.clear();
boolean problemState = stateManager.getProblemState(id, Boolean.class); Boolean problemState = stateManager.getProblemState(id, Boolean.class);
assertFalse(problemState); assertNull(problemState);
} }
@Test @Test