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:
parent
8a5c1cceed
commit
d1a8367ba5
3 changed files with 47 additions and 34 deletions
|
|
@ -60,8 +60,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
|
||||
public <T> T getState(StateId id, Class<T> type){
|
||||
if(states.containsKey(id)){
|
||||
T s = type.cast(states.get(id));
|
||||
return s;
|
||||
return type.cast(states.get(id));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -78,7 +77,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
|
||||
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_();
|
||||
|
||||
|
|
@ -116,17 +115,19 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
|
||||
private Object[][] activity_states;
|
||||
|
||||
private Object[][] route_states;
|
||||
|
||||
private VehicleRoutingProblem vrp;
|
||||
|
||||
public StateId createStateId(String name){
|
||||
if(createdStateIds.containsKey(name)) return createdStateIds.get(name);
|
||||
if(stateIndexCounter>=activity_states[0].length){
|
||||
activity_states = new Object[vrp.getNuActivities()+1][stateIndexCounter+1];
|
||||
route_states = new Object[vrp.getNuActivities()+1][stateIndexCounter+1];
|
||||
}
|
||||
StateId id = StateFactory.createId(name,stateIndexCounter);
|
||||
incStateIndexCounter();
|
||||
createdStateIds.put(name, id);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
@ -155,9 +156,11 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public StateManager(VehicleRoutingTransportCosts routingCosts){
|
||||
this.routingCosts = routingCosts;
|
||||
activity_states = new Object[initialNuActivities+1][initialNuStates];
|
||||
route_states = new Object[initialNuActivities+1][initialNuStates];
|
||||
addDefaultStates();
|
||||
}
|
||||
|
||||
|
|
@ -165,6 +168,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
this.routingCosts = vehicleRoutingProblem.getTransportCosts();
|
||||
this.vrp = vehicleRoutingProblem;
|
||||
activity_states = new Object[vrp.getNuActivities()+1][initialNuStates];
|
||||
route_states = new Object[vrp.getNuActivities()+1][initialNuStates];
|
||||
addDefaultStates();
|
||||
}
|
||||
|
||||
|
|
@ -195,9 +199,9 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
* can add the default simply by coding <br>
|
||||
* <code>addDefaultRouteState(StateFactory.createStateId("max_weight"), Integer.class, 0)</code>
|
||||
*
|
||||
* @param stateId
|
||||
* @param type
|
||||
* @param defaultState
|
||||
* @param stateId for which a default state is added
|
||||
* @param type of state
|
||||
* @param defaultState default state value
|
||||
*/
|
||||
public <T> void addDefaultRouteState(StateId stateId, Class<T> type, T defaultState){
|
||||
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.
|
||||
*
|
||||
* @param stateId
|
||||
* @param type
|
||||
* @param defaultState
|
||||
* @param stateId for which a default state is added
|
||||
* @param type of state
|
||||
* @param defaultState default state value
|
||||
*/
|
||||
public <T> void addDefaultActivityState(StateId stateId, Class<T> type, T defaultState){
|
||||
if(StateFactory.isReservedId(stateId)) StateFactory.throwReservedIdException(stateId.toString());
|
||||
|
|
@ -222,8 +226,9 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
*/
|
||||
public void clear(){
|
||||
// activity_states = new Object[101][10];
|
||||
route_states = new Object[vrp.getNuActivities()+1][initialNuStates];
|
||||
vehicleRouteStates_.clear();
|
||||
activityStates_.clear();
|
||||
// activityStates_.clear();
|
||||
problemStates_.clear();
|
||||
}
|
||||
|
||||
|
|
@ -267,12 +272,9 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
*/
|
||||
@Override
|
||||
public <T> T getRouteState(VehicleRoute route, StateId stateId, Class<T> type) {
|
||||
if(!vehicleRouteStates_.containsKey(route)){
|
||||
return getDefaultTypedRouteState(stateId, type);
|
||||
}
|
||||
States_ states = vehicleRouteStates_.get(route);
|
||||
T state = states.getState(stateId, type);
|
||||
if(state == null) return getDefaultTypedRouteState(stateId, type);
|
||||
if(route.isEmpty()) return getDefaultTypedRouteState(stateId,type);
|
||||
T state = type.cast(route_states[route.getActivities().get(0).getIndex()][stateId.getIndex()]);
|
||||
if(state==null) return getDefaultTypedRouteState(stateId,type);
|
||||
return state;
|
||||
}
|
||||
|
||||
|
|
@ -306,14 +308,6 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
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.
|
||||
*
|
||||
|
|
@ -333,6 +327,11 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -47,6 +47,21 @@ import jsprit.core.util.CalculationUtils;
|
|||
@Override
|
||||
public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
|
||||
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,
|
||||
* then vehicle can never conduct activities.
|
||||
|
|
@ -87,12 +102,12 @@ import jsprit.core.util.CalculationUtils;
|
|||
return ConstraintsStatus.NOT_FULFILLED;
|
||||
}
|
||||
// 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 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());
|
||||
/*
|
||||
* |--- prevAct ---|
|
||||
|
|
|
|||
|
|
@ -16,10 +16,6 @@
|
|||
******************************************************************************/
|
||||
package jsprit.core.algorithm;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import jsprit.core.algorithm.acceptor.GreedyAcceptance;
|
||||
import jsprit.core.algorithm.module.RuinAndRecreateModule;
|
||||
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.vehicle.InfiniteFleetManagerFactory;
|
||||
import jsprit.core.problem.vehicle.VehicleFleetManager;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class BuildPDVRPWithShipmentsAlgoFromScratch_IT {
|
||||
|
||||
|
|
@ -62,7 +61,7 @@ public class BuildPDVRPWithShipmentsAlgoFromScratch_IT {
|
|||
|
||||
vrp = builder.build();
|
||||
|
||||
final StateManager stateManager = new StateManager(vrp.getTransportCosts());
|
||||
final StateManager stateManager = new StateManager(vrp);
|
||||
stateManager.updateLoadStates();
|
||||
stateManager.updateTimeWindowStates();
|
||||
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue