mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
tested and redesigned StateManager and UpdateLoads
This commit is contained in:
parent
97819de9f2
commit
f6bf46ca9a
6 changed files with 338 additions and 102 deletions
|
|
@ -30,6 +30,7 @@ import jsprit.core.algorithm.recreate.listener.InsertionStartsListener;
|
|||
import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
|
||||
import jsprit.core.algorithm.ruin.listener.RuinListener;
|
||||
import jsprit.core.algorithm.ruin.listener.RuinListeners;
|
||||
import jsprit.core.problem.Capacity;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
|
||||
import jsprit.core.problem.job.Job;
|
||||
|
|
@ -113,20 +114,25 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
}
|
||||
|
||||
private void addDefaultStates() {
|
||||
defaultActivityStates_.put(StateFactory.LOAD, StateFactory.createState(0));
|
||||
defaultActivityStates_.put(StateFactory.LOAD, Capacity.Builder.newInstance().build());
|
||||
|
||||
|
||||
defaultActivityStates_.put(StateFactory.COSTS, StateFactory.createState(0));
|
||||
defaultActivityStates_.put(StateFactory.DURATION, StateFactory.createState(0));
|
||||
defaultActivityStates_.put(StateFactory.FUTURE_MAXLOAD, StateFactory.createState(0));
|
||||
defaultActivityStates_.put(StateFactory.PAST_MAXLOAD, StateFactory.createState(0));
|
||||
|
||||
defaultRouteStates_.put(StateFactory.LOAD, StateFactory.createState(0));
|
||||
defaultRouteStates_.put(StateFactory.LOAD, Capacity.Builder.newInstance().build());
|
||||
|
||||
defaultRouteStates_.put(StateFactory.COSTS, StateFactory.createState(0));
|
||||
defaultRouteStates_.put(StateFactory.DURATION, StateFactory.createState(0));
|
||||
defaultRouteStates_.put(StateFactory.FUTURE_MAXLOAD, StateFactory.createState(0));
|
||||
defaultRouteStates_.put(StateFactory.PAST_MAXLOAD, StateFactory.createState(0));
|
||||
defaultRouteStates_.put(StateFactory.LOAD_AT_END, StateFactory.createState(0));
|
||||
|
||||
defaultRouteStates_.put(StateFactory.MAXLOAD, StateFactory.createState(0));
|
||||
defaultRouteStates_.put(StateFactory.LOAD_AT_BEGINNING, StateFactory.createState(0));
|
||||
|
||||
defaultRouteStates_.put(StateFactory.LOAD_AT_END, Capacity.Builder.newInstance().build());
|
||||
defaultRouteStates_.put(StateFactory.LOAD_AT_BEGINNING, Capacity.Builder.newInstance().build());
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -423,7 +429,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
|
|||
addActivityVisitor(updateLoads);
|
||||
addListener(updateLoads);
|
||||
addActivityVisitor(new UpdatePrevMaxLoad(this));
|
||||
addActivityVisitor(new UpdateMaxLoad(this));
|
||||
addActivityVisitor(new UpdateMaxLoadForwardLooking(this));
|
||||
addActivityVisitor(new UpdateMaxLoad_(this));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import java.util.Collection;
|
|||
|
||||
import jsprit.core.algorithm.recreate.listener.InsertionStartsListener;
|
||||
import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
|
||||
import jsprit.core.problem.Capacity;
|
||||
import jsprit.core.problem.job.Delivery;
|
||||
import jsprit.core.problem.job.Job;
|
||||
import jsprit.core.problem.job.Pickup;
|
||||
|
|
@ -15,34 +16,27 @@ import jsprit.core.problem.solution.route.state.StateFactory;
|
|||
|
||||
|
||||
/**
|
||||
* Updates load at activity level.
|
||||
* Updates load at start and end of route as well as at each activity. And update is triggered when either
|
||||
* activityVisitor has been started, the insertion process has been started or a job has been inserted.
|
||||
*
|
||||
* <p>Note that this assumes that StateTypes.LOAD_AT_DEPOT is already updated, i.e. it starts by setting loadAtDepot to StateTypes.LOAD_AT_DEPOT.
|
||||
* If StateTypes.LOAD_AT_DEPOT is not set, it starts with 0 load at depot.
|
||||
*
|
||||
* <p>Thus it DEPENDS on StateTypes.LOAD_AT_DEPOT
|
||||
* <p>Note that this only works properly if you register this class as ActivityVisitor AND InsertionStartsListener AND JobInsertedListener.
|
||||
* The reason behind is that activity states are dependent on route-level states and vice versa. If this is properly registered,
|
||||
* this dependency is solved automatically.
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
*/
|
||||
class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListener, JobInsertedListener {
|
||||
|
||||
private StateManager stateManager;
|
||||
private int currentLoad = 0;
|
||||
private VehicleRoute route;
|
||||
/**
|
||||
* Updates load at activity level.
|
||||
*
|
||||
* <p>Note that this assumes that StateTypes.LOAD_AT_DEPOT is already updated, i.e. it starts by setting loadAtDepot to StateTypes.LOAD_AT_DEPOT.
|
||||
* If StateTypes.LOAD_AT_DEPOT is not set, it starts with 0 load at depot.
|
||||
*
|
||||
* <p>Thus it DEPENDS on StateTypes.LOAD_AT_DEPOT
|
||||
*
|
||||
* <p>The loads can be retrieved by <br>
|
||||
* <code>stateManager.getActivityState(activity,StateTypes.LOAD);</code>
|
||||
*
|
||||
* @author stefan
|
||||
*
|
||||
|
||||
/*
|
||||
* default has one dimension with a value of zero
|
||||
*/
|
||||
private Capacity currentLoad = Capacity.Builder.newInstance().build();
|
||||
|
||||
private VehicleRoute route;
|
||||
|
||||
public UpdateLoads(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
|
|
@ -50,36 +44,36 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
|
|||
|
||||
@Override
|
||||
public void begin(VehicleRoute route) {
|
||||
currentLoad = (int) stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING).toDouble();
|
||||
currentLoad = stateManager.getRouteState(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
this.route = route;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TourActivity act) {
|
||||
currentLoad += act.getCapacityDemand();
|
||||
stateManager.putInternalActivityState(act, StateFactory.LOAD, StateFactory.createState(currentLoad));
|
||||
assert currentLoad <= route.getVehicle().getCapacity() : "currentLoad at activity must not be > vehicleCapacity";
|
||||
assert currentLoad >= 0 : "currentLoad at act must not be < 0";
|
||||
currentLoad = Capacity.addup(currentLoad, act.getCapacity());
|
||||
stateManager.putInternalActivityState_(act, StateFactory.LOAD, Capacity.class, currentLoad);
|
||||
assert currentLoad.isLessOrEqual(route.getVehicle().getType().getCapacityDimensions()) : "currentLoad at activity must not be > vehicleCapacity";
|
||||
assert currentLoad.isGreaterOrEqual(Capacity.Builder.newInstance().build()) : "currentLoad at act must not be < 0 in one of the applied dimensions";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
currentLoad = 0;
|
||||
currentLoad = Capacity.Builder.newInstance().build();
|
||||
}
|
||||
|
||||
void insertionStarts(VehicleRoute route) {
|
||||
int loadAtDepot = 0;
|
||||
int loadAtEnd = 0;
|
||||
Capacity loadAtDepot = Capacity.Builder.newInstance().build();
|
||||
Capacity loadAtEnd = Capacity.Builder.newInstance().build();
|
||||
for(Job j : route.getTourActivities().getJobs()){
|
||||
if(j instanceof Delivery){
|
||||
loadAtDepot += j.getCapacityDemand();
|
||||
loadAtDepot = Capacity.addup(loadAtDepot, j.getSize());
|
||||
}
|
||||
else if(j instanceof Pickup || j instanceof Service){
|
||||
loadAtEnd += j.getCapacityDemand();
|
||||
loadAtEnd = Capacity.addup(loadAtEnd, j.getSize());
|
||||
}
|
||||
}
|
||||
stateManager.putInternalRouteState(route, StateFactory.LOAD_AT_BEGINNING, StateFactory.createState(loadAtDepot));
|
||||
stateManager.putInternalRouteState(route, StateFactory.LOAD_AT_END, StateFactory.createState(loadAtEnd));
|
||||
stateManager.putInternalRouteState_(route, StateFactory.LOAD_AT_BEGINNING, Capacity.class, loadAtDepot);
|
||||
stateManager.putInternalRouteState_(route, StateFactory.LOAD_AT_END, Capacity.class, loadAtEnd);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -90,12 +84,12 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
|
|||
@Override
|
||||
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
|
||||
if(job2insert instanceof Delivery){
|
||||
int loadAtDepot = (int) stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING).toDouble();
|
||||
stateManager.putInternalRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING, StateFactory.createState(loadAtDepot + job2insert.getCapacityDemand()));
|
||||
Capacity loadAtDepot = stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING, Capacity.class);
|
||||
stateManager.putInternalRouteState_(inRoute, StateFactory.LOAD_AT_BEGINNING, Capacity.class, Capacity.addup(loadAtDepot, job2insert.getSize()));
|
||||
}
|
||||
else if(job2insert instanceof Pickup || job2insert instanceof Service){
|
||||
int loadAtEnd = (int) stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_END).toDouble();
|
||||
stateManager.putInternalRouteState(inRoute, StateFactory.LOAD_AT_END, StateFactory.createState(loadAtEnd + job2insert.getCapacityDemand()));
|
||||
Capacity loadAtEnd = stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_END, Capacity.class);
|
||||
stateManager.putInternalRouteState_(inRoute, StateFactory.LOAD_AT_END, Capacity.class, Capacity.addup(loadAtEnd, job2insert.getSize()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,12 +5,24 @@ import jsprit.core.problem.solution.route.activity.ReverseActivityVisitor;
|
|||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||
|
||||
|
||||
class UpdateMaxLoad implements ReverseActivityVisitor, StateUpdater {
|
||||
/**
|
||||
* A {@link ReverseActivityVisitor} that looks forward in the vehicle route and determines
|
||||
* the maximum load for subsequent activities.
|
||||
*
|
||||
* <p>
|
||||
*
|
||||
* @author schroeder
|
||||
*
|
||||
*/
|
||||
class UpdateMaxLoadForwardLooking implements ReverseActivityVisitor, StateUpdater {
|
||||
|
||||
private StateManager stateManager;
|
||||
|
||||
private VehicleRoute route;
|
||||
|
||||
private double maxLoad;
|
||||
public UpdateMaxLoad(StateManager stateManager) {
|
||||
|
||||
public UpdateMaxLoadForwardLooking(StateManager stateManager) {
|
||||
super();
|
||||
this.stateManager = stateManager;
|
||||
}
|
||||
|
|
@ -202,6 +202,22 @@ public class Capacity {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this capacity is greater or equal than the capacity toCompare
|
||||
*
|
||||
* @param toCompare
|
||||
* @return
|
||||
* @throws NullPointerException if one of the args is null
|
||||
|
||||
*/
|
||||
public boolean isGreaterOrEqual(Capacity toCompare) {
|
||||
if(toCompare == null) throw new NullPointerException();
|
||||
for(int i=0;i<Math.max(this.getNuOfDimensions(), toCompare.getNuOfDimensions());i++){
|
||||
if(this.get(i) < toCompare.get(i)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sBuilder = new StringBuilder();
|
||||
|
|
@ -211,5 +227,23 @@ public class Capacity {
|
|||
}
|
||||
return sBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum, i.e. the maximum of each capacity dimension.
|
||||
*
|
||||
* @param cap1
|
||||
* @param cap2
|
||||
* @return
|
||||
*/
|
||||
public static Capacity max(Capacity cap1, Capacity cap2) {
|
||||
if(cap1 == null || cap2 == null) throw new IllegalArgumentException("arg must not be null");
|
||||
Capacity.Builder toReturnBuilder = Capacity.Builder.newInstance();
|
||||
for(int i=0;i<Math.max(cap1.getNuOfDimensions(), cap2.getNuOfDimensions());i++){
|
||||
toReturnBuilder.addDimension(i, Math.max(cap1.get(i), cap2.get(i)));
|
||||
}
|
||||
return toReturnBuilder.build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue