1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

added indeces to main elements

This commit is contained in:
oblonski 2014-07-15 12:35:02 +02:00
parent 8a5c1cceed
commit d1a8367ba5
3 changed files with 47 additions and 34 deletions

View file

@ -60,8 +60,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
public <T> T getState(StateId id, Class<T> type){ public <T> T getState(StateId id, Class<T> type){
if(states.containsKey(id)){ if(states.containsKey(id)){
T s = type.cast(states.get(id)); return type.cast(states.get(id));
return s;
} }
return null; return null;
} }
@ -78,7 +77,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
private Map<VehicleRoute,States_> vehicleRouteStates_ = new HashMap<VehicleRoute, States_>(); private Map<VehicleRoute,States_> vehicleRouteStates_ = new HashMap<VehicleRoute, States_>();
private Map<TourActivity,States_> activityStates_ = new HashMap<TourActivity, States_>(); // private Map<TourActivity,States_> activityStates_ = new HashMap<TourActivity, States_>();
private States_ problemStates_ = new States_(); private States_ problemStates_ = new States_();
@ -116,17 +115,19 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
private Object[][] activity_states; private Object[][] activity_states;
private Object[][] route_states;
private VehicleRoutingProblem vrp; private VehicleRoutingProblem vrp;
public StateId createStateId(String name){ public StateId createStateId(String name){
if(createdStateIds.containsKey(name)) return createdStateIds.get(name); if(createdStateIds.containsKey(name)) return createdStateIds.get(name);
if(stateIndexCounter>=activity_states[0].length){ if(stateIndexCounter>=activity_states[0].length){
activity_states = new Object[vrp.getNuActivities()+1][stateIndexCounter+1]; activity_states = new Object[vrp.getNuActivities()+1][stateIndexCounter+1];
route_states = new Object[vrp.getNuActivities()+1][stateIndexCounter+1];
} }
StateId id = StateFactory.createId(name,stateIndexCounter); StateId id = StateFactory.createId(name,stateIndexCounter);
incStateIndexCounter(); incStateIndexCounter();
createdStateIds.put(name, id); createdStateIds.put(name, id);
return id; return id;
} }
@ -155,9 +156,11 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
} }
@Deprecated
public StateManager(VehicleRoutingTransportCosts routingCosts){ public StateManager(VehicleRoutingTransportCosts routingCosts){
this.routingCosts = routingCosts; this.routingCosts = routingCosts;
activity_states = new Object[initialNuActivities+1][initialNuStates]; activity_states = new Object[initialNuActivities+1][initialNuStates];
route_states = new Object[initialNuActivities+1][initialNuStates];
addDefaultStates(); addDefaultStates();
} }
@ -165,6 +168,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
this.routingCosts = vehicleRoutingProblem.getTransportCosts(); this.routingCosts = vehicleRoutingProblem.getTransportCosts();
this.vrp = vehicleRoutingProblem; this.vrp = vehicleRoutingProblem;
activity_states = new Object[vrp.getNuActivities()+1][initialNuStates]; activity_states = new Object[vrp.getNuActivities()+1][initialNuStates];
route_states = new Object[vrp.getNuActivities()+1][initialNuStates];
addDefaultStates(); addDefaultStates();
} }
@ -195,9 +199,9 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
* can add the default simply by coding <br> * can add the default simply by coding <br>
* <code>addDefaultRouteState(StateFactory.createStateId("max_weight"), Integer.class, 0)</code> * <code>addDefaultRouteState(StateFactory.createStateId("max_weight"), Integer.class, 0)</code>
* *
* @param stateId * @param stateId for which a default state is added
* @param type * @param type of state
* @param defaultState * @param defaultState default state value
*/ */
public <T> void addDefaultRouteState(StateId stateId, Class<T> type, T defaultState){ public <T> void addDefaultRouteState(StateId stateId, Class<T> type, T defaultState){
if(StateFactory.isReservedId(stateId)) StateFactory.throwReservedIdException(stateId.toString()); if(StateFactory.isReservedId(stateId)) StateFactory.throwReservedIdException(stateId.toString());
@ -207,9 +211,9 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
/** /**
* Generic method to add default activity state. * Generic method to add default activity state.
* *
* @param stateId * @param stateId for which a default state is added
* @param type * @param type of state
* @param defaultState * @param defaultState default state value
*/ */
public <T> void addDefaultActivityState(StateId stateId, Class<T> type, T defaultState){ public <T> void addDefaultActivityState(StateId stateId, Class<T> type, T defaultState){
if(StateFactory.isReservedId(stateId)) StateFactory.throwReservedIdException(stateId.toString()); if(StateFactory.isReservedId(stateId)) StateFactory.throwReservedIdException(stateId.toString());
@ -222,8 +226,9 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
*/ */
public void clear(){ public void clear(){
// activity_states = new Object[101][10]; // activity_states = new Object[101][10];
route_states = new Object[vrp.getNuActivities()+1][initialNuStates];
vehicleRouteStates_.clear(); vehicleRouteStates_.clear();
activityStates_.clear(); // activityStates_.clear();
problemStates_.clear(); problemStates_.clear();
} }
@ -267,13 +272,10 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
*/ */
@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(!vehicleRouteStates_.containsKey(route)){ if(route.isEmpty()) return getDefaultTypedRouteState(stateId,type);
return getDefaultTypedRouteState(stateId, type); T state = type.cast(route_states[route.getActivities().get(0).getIndex()][stateId.getIndex()]);
} if(state==null) return getDefaultTypedRouteState(stateId,type);
States_ states = vehicleRouteStates_.get(route); return state;
T state = states.getState(stateId, type);
if(state == null) return getDefaultTypedRouteState(stateId, type);
return state;
} }
private <T> T getDefaultTypedRouteState(StateId stateId, Class<T> type) { private <T> T getDefaultTypedRouteState(StateId stateId, Class<T> type) {
@ -306,14 +308,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
activity_states[act.getIndex()][stateId.getIndex()]=state; activity_states[act.getIndex()][stateId.getIndex()]=state;
} }
<T> void putTypedInternalRouteState(VehicleRoute route, StateId stateId, Class<T> type, T state){
if(!vehicleRouteStates_.containsKey(route)){
vehicleRouteStates_.put(route, new States_());
}
States_ routeStates = vehicleRouteStates_.get(route);
routeStates.putState(stateId, type, state);
}
/** /**
* Generic method to memorize state 'state' of type 'type' of route and stateId. * Generic method to memorize state 'state' of type 'type' of route and stateId.
* *
@ -333,6 +327,11 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
putTypedInternalRouteState(route, stateId, type, state); putTypedInternalRouteState(route, stateId, type, state);
} }
<T> void putTypedInternalRouteState(VehicleRoute route, StateId stateId, Class<T> type, T state){
if(route.isEmpty()) return;
route_states[route.getActivities().get(0).getIndex()][stateId.getIndex()] = state;
}
/** /**
* Adds state updater. * Adds state updater.
* *

View file

@ -47,6 +47,21 @@ import jsprit.core.util.CalculationUtils;
@Override @Override
public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) { public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
double latestVehicleArrival = iFacts.getNewVehicle().getLatestArrival(); double latestVehicleArrival = iFacts.getNewVehicle().getLatestArrival();
Double latestArrTimeAtNextAct;
String nextActLocation;
if(nextAct instanceof End) {
latestArrTimeAtNextAct = latestVehicleArrival;
nextActLocation = iFacts.getNewVehicle().getEndLocationId();
if(!iFacts.getNewVehicle().isReturnToDepot()){
nextActLocation = newAct.getLocationId();
}
}
else{
latestArrTimeAtNextAct = states.getActivityState(nextAct, StateFactory.LATEST_OPERATION_START_TIME, Double.class);
if(latestArrTimeAtNextAct==null) latestArrTimeAtNextAct=nextAct.getTheoreticalLatestOperationStartTime();
nextActLocation = nextAct.getLocationId();
}
/* /*
* if latest arrival of vehicle (at its end) is smaller than earliest operation start times of activities, * if latest arrival of vehicle (at its end) is smaller than earliest operation start times of activities,
* then vehicle can never conduct activities. * then vehicle can never conduct activities.
@ -87,12 +102,12 @@ import jsprit.core.util.CalculationUtils;
return ConstraintsStatus.NOT_FULFILLED; return ConstraintsStatus.NOT_FULFILLED;
} }
// log.info("check insertion of " + newAct + " between " + prevAct + " and " + nextAct + ". prevActDepTime=" + prevActDepTime); // log.info("check insertion of " + newAct + " between " + prevAct + " and " + nextAct + ". prevActDepTime=" + prevActDepTime);
double latestArrTimeAtNextAct = states.getActivityState(nextAct, StateFactory.LATEST_OPERATION_START_TIME, Double.class); // double latestArrTimeAtNextAct = states.getActivityState(nextAct, StateFactory.LATEST_OPERATION_START_TIME, Double.class);
double arrTimeAtNewAct = prevActDepTime + routingCosts.getTransportTime(prevAct.getLocationId(), newAct.getLocationId(), prevActDepTime, iFacts.getNewDriver(), iFacts.getNewVehicle()); double arrTimeAtNewAct = prevActDepTime + routingCosts.getTransportTime(prevAct.getLocationId(), newAct.getLocationId(), prevActDepTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double latestArrTimeAtNewAct = Math.min(newAct.getTheoreticalLatestOperationStartTime(),latestArrTimeAtNextAct - double latestArrTimeAtNewAct = Math.min(newAct.getTheoreticalLatestOperationStartTime(),latestArrTimeAtNextAct -
routingCosts.getBackwardTransportTime(nextAct.getLocationId(), newAct.getLocationId(), latestArrTimeAtNextAct, iFacts.getNewDriver(), routingCosts.getBackwardTransportTime(nextActLocation, newAct.getLocationId(), latestArrTimeAtNextAct, iFacts.getNewDriver(),
iFacts.getNewVehicle()) - newAct.getOperationTime()); iFacts.getNewVehicle()) - newAct.getOperationTime());
/* /*
* |--- prevAct ---| * |--- prevAct ---|

View file

@ -16,10 +16,6 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm; package jsprit.core.algorithm;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import jsprit.core.algorithm.acceptor.GreedyAcceptance; import jsprit.core.algorithm.acceptor.GreedyAcceptance;
import jsprit.core.algorithm.module.RuinAndRecreateModule; import jsprit.core.algorithm.module.RuinAndRecreateModule;
import jsprit.core.algorithm.recreate.BestInsertionBuilder; import jsprit.core.algorithm.recreate.BestInsertionBuilder;
@ -40,11 +36,14 @@ import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.state.StateFactory; import jsprit.core.problem.solution.route.state.StateFactory;
import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory; import jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
import jsprit.core.problem.vehicle.VehicleFleetManager; import jsprit.core.problem.vehicle.VehicleFleetManager;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Collection;
import static org.junit.Assert.assertTrue;
public class BuildPDVRPWithShipmentsAlgoFromScratch_IT { public class BuildPDVRPWithShipmentsAlgoFromScratch_IT {
@ -62,7 +61,7 @@ public class BuildPDVRPWithShipmentsAlgoFromScratch_IT {
vrp = builder.build(); vrp = builder.build();
final StateManager stateManager = new StateManager(vrp.getTransportCosts()); final StateManager stateManager = new StateManager(vrp);
stateManager.updateLoadStates(); stateManager.updateLoadStates();
stateManager.updateTimeWindowStates(); stateManager.updateTimeWindowStates();
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager)); stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));