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 09:24:38 +02:00
parent 4f2689529f
commit f9bad784ff
25 changed files with 334 additions and 203 deletions

View file

@ -18,6 +18,7 @@ package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound;
import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
@ -104,7 +105,7 @@ class Inserter {
@Override
public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
if(job instanceof Shipment){
List<TourActivity> acts = vehicleRoutingProblem.copyAndGetActivities(job);
List<AbstractActivity> acts = vehicleRoutingProblem.copyAndGetActivities(job);
TourActivity pickupShipment = acts.get(0);
TourActivity deliverShipment = acts.get(1);
route.setVehicleAndDepartureTime(iData.getSelectedVehicle(),iData.getVehicleDepartureTime());

View file

@ -16,18 +16,8 @@
******************************************************************************/
package jsprit.core.algorithm.state;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import jsprit.core.algorithm.listener.IterationStartsListener;
import jsprit.core.algorithm.recreate.listener.InsertionEndsListener;
import jsprit.core.algorithm.recreate.listener.InsertionListener;
import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.algorithm.recreate.listener.InsertionStartsListener;
import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
import jsprit.core.algorithm.recreate.listener.*;
import jsprit.core.algorithm.ruin.listener.RuinListener;
import jsprit.core.algorithm.ruin.listener.RuinListeners;
import jsprit.core.problem.Capacity;
@ -46,6 +36,8 @@ import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import jsprit.core.problem.solution.route.state.StateFactory;
import jsprit.core.problem.solution.route.state.StateFactory.StateId;
import java.util.*;
/**
* Manages states.
*
@ -56,8 +48,9 @@ import jsprit.core.problem.solution.route.state.StateFactory.StateId;
*
*/
public class StateManager implements RouteAndActivityStateGetter, IterationStartsListener, RuinListener, InsertionStartsListener, JobInsertedListener, InsertionEndsListener {
static class States_ {
static class States_ {
private Map<StateId,Object> states = new HashMap<StateId,Object>();
@ -112,8 +105,36 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
private boolean updateLoad = false;
private boolean updateTWs = false;
private void addDefaultStates() {
private int stateIndexCounter = 10;
private Map<String,StateId> createdStateIds = new HashMap<String, StateId>();
private int initialNuStates = 20;
private int initialNuActivities = 100;
private Object[][] activity_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];
}
StateId id = StateFactory.createId(name,stateIndexCounter);
incStateIndexCounter();
createdStateIds.put(name, id);
return id;
}
private void incStateIndexCounter() {
stateIndexCounter++;
}
private void addDefaultStates() {
defaultActivityStates_.put(StateFactory.LOAD, Capacity.Builder.newInstance().build());
defaultActivityStates_.put(StateFactory.COSTS, 0.);
defaultActivityStates_.put(StateFactory.DURATION, 0.);
@ -136,8 +157,16 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
public StateManager(VehicleRoutingTransportCosts routingCosts){
this.routingCosts = routingCosts;
activity_states = new Object[initialNuActivities+1][initialNuStates];
addDefaultStates();
}
public StateManager(VehicleRoutingProblem vehicleRoutingProblem){
this.routingCosts = vehicleRoutingProblem.getTransportCosts();
this.vrp = vehicleRoutingProblem;
activity_states = new Object[vrp.getNuActivities()+1][initialNuStates];
addDefaultStates();
}
public <T> void addDefaultProblemState(StateId stateId, Class<T> type, T defaultState){
defaultProblemStates_.putState(stateId, type, defaultState);
@ -192,6 +221,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
*
*/
public void clear(){
// activity_states = new Object[101][10];
vehicleRouteStates_.clear();
activityStates_.clear();
problemStates_.clear();
@ -203,21 +233,18 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
*/
@Override
public <T> T getActivityState(TourActivity act, StateId stateId, Class<T> type) {
if(!activityStates_.containsKey(act)){
return getDefaultTypedActivityState(act, stateId, type);
}
States_ states = activityStates_.get(act);
T state = states.getState(stateId, type);
if(state == null) return getDefaultTypedActivityState(act, stateId, type);
return state;
if(act.getIndex()<0) return getDefaultTypedActivityState(act, stateId, type);
T state = type.cast(activity_states[act.getIndex()][stateId.getIndex()]);
if(state == null) return getDefaultTypedActivityState(act, stateId, type);
return state;
}
/**
*
* @param act
* @param stateId
* @param type
* @return
* @param act activity for which the state is requested
* @param stateId stateId of requested state
* @param type class of state value
* @return state value
*/
private <T> T getDefaultTypedActivityState(TourActivity act, StateId stateId,Class<T> type) {
if(defaultActivityStates_.containsKey(stateId)){
@ -265,10 +292,10 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
* <p>you can retrieve the load at myActivity by <br>
* <code>Capacity load = stateManager.getActivityState(myActivity, StateFactory.createStateId("act-load"), Capacity.class);</code>
*
* @param act
* @param stateId
* @param type
* @param state
* @param act for which a new state should be memorized
* @param stateId stateId of state
* @param type class of state-value
* @param state state-value
*/
public <T> void putTypedActivityState(TourActivity act, StateId stateId, Class<T> type, T state){
if(StateFactory.isReservedId(stateId)) StateFactory.throwReservedIdException(stateId.toString());
@ -276,11 +303,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
}
<T> void putInternalTypedActivityState(TourActivity act, StateId stateId, Class<T> type, T state){
if(!activityStates_.containsKey(act)){
activityStates_.put(act, new States_());
}
States_ actStates = activityStates_.get(act);
actStates.putState(stateId, type, state);
activity_states[act.getIndex()][stateId.getIndex()]=state;
}
<T> void putTypedInternalRouteState(VehicleRoute route, StateId stateId, Class<T> type, T state){
@ -300,7 +323,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
* <p>you can retrieve the duration of myRoute then by <br>
* <code>double totalRouteDuration = stateManager.getRouteState(myRoute, StateFactory.createStateId("route-duration"), Double.class);</code>
*
* @param act
* @param route
* @param stateId
* @param type
* @param state
@ -353,7 +376,7 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
* <p>This reverseVisitor visits all activities in a route subsequently (starting from the end of the route) in two cases. First, if insertionStart (after ruinStrategies have removed activities from routes)
* and, second, if a job has been inserted and thus if a route has changed.
*
* @param reverseActivityVistor
* @param activityVistor activityVisitor to add
*/
void addActivityVisitor(ReverseActivityVisitor activityVistor){
revRouteActivityVisitor.addActivityVisitor(activityVistor);
@ -431,9 +454,9 @@ public class StateManager implements RouteAndActivityStateGetter, IterationStart
UpdateLoads updateLoads = new UpdateLoads(this);
addActivityVisitor(updateLoads);
addListener(updateLoads);
addActivityVisitor(new UpdateMaxCapacityUtilisationAtActivitiesByLookingBackwardInRoute(this));
addActivityVisitor(new UpdateMaxCapacityUtilisationAtActivitiesByLookingForwardInRoute(this));
addActivityVisitor(new UpdateMaxCapacityUtilisationAtRoute(this));
// addActivityVisitor(new UpdateMaxCapacityUtilisationAtActivitiesByLookingBackwardInRoute(this));
// addActivityVisitor(new UpdateMaxCapacityUtilisationAtActivitiesByLookingForwardInRoute(this));
// addActivityVisitor(new UpdateMaxCapacityUtilisationAtRoute(this));
}
}

View file

@ -18,8 +18,6 @@
******************************************************************************/
package jsprit.core.algorithm.state;
import java.util.Collection;
import jsprit.core.algorithm.recreate.listener.InsertionStartsListener;
import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
import jsprit.core.problem.Capacity;
@ -32,6 +30,8 @@ import jsprit.core.problem.solution.route.activity.ActivityVisitor;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.state.StateFactory;
import java.util.Collection;
/**
* Updates load at start and end of route as well as at each activity. And update is triggered when either
@ -76,7 +76,7 @@ class UpdateLoads implements ActivityVisitor, StateUpdater, InsertionStartsListe
@Override
public void finish() {
currentLoad = Capacity.Builder.newInstance().build();
currentLoad = Capacity.Builder.newInstance().build();
}
void insertionStarts(VehicleRoute route) {

View file

@ -5,7 +5,7 @@ import jsprit.core.problem.solution.route.activity.TourActivity;
/**
* Created by schroeder on 14.07.14.
*/
public abstract class AbstractTourActivity implements TourActivity {
public abstract class AbstractActivity implements TourActivity {
private int index;

View file

@ -0,0 +1,14 @@
package jsprit.core.problem;
import jsprit.core.problem.job.Job;
import java.util.List;
/**
* Created by schroeder on 14.07.14.
*/
public interface JobActivityFactory {
public List<AbstractActivity> createActivity(Job job);
}

View file

@ -107,7 +107,24 @@ public class VehicleRoutingProblem {
private Collection<jsprit.core.problem.constraint.Constraint> constraints = new ArrayList<jsprit.core.problem.constraint.Constraint>();
private boolean addPenaltyVehicles = false;
private JobActivityFactory jobActivityFactory = new JobActivityFactory() {
@Override
public List<AbstractActivity> createActivity(Job job) {
List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
if(job instanceof Service){
acts.add(serviceActivityFactory.createActivity((Service) job));
}
else if(job instanceof Shipment){
acts.add(shipmentActivityFactory.createPickup((Shipment) job));
acts.add(shipmentActivityFactory.createDelivery((Shipment) job));
}
return acts;
}
};
private boolean addPenaltyVehicles = false;
private double penaltyFactor = 1.0;
@ -119,12 +136,14 @@ public class VehicleRoutingProblem {
private int activityIndexCounter = 0;
private Map<Job,List<TourActivity>> activityMap = new HashMap<Job, List<TourActivity>>();
private int nuActivities = 0;
private Map<Job,List<AbstractActivity>> activityMap = new HashMap<Job, List<AbstractActivity>>();
// private ArrayList<List<TourActivity>> activityList;
private DefaultShipmentActivityFactory shipmentActivityFactory = new DefaultShipmentActivityFactory();
private final DefaultShipmentActivityFactory shipmentActivityFactory = new DefaultShipmentActivityFactory();
private DefaultTourActivityFactory serviceActivityFactory = new DefaultTourActivityFactory();
private final DefaultTourActivityFactory serviceActivityFactory = new DefaultTourActivityFactory();
/**
* Create a location (i.e. coordinate) and returns the key of the location which is Coordinate.toString().
@ -143,7 +162,11 @@ public class VehicleRoutingProblem {
}
return id;
}
public Builder setJobActivityFactory(JobActivityFactory factory){
this.jobActivityFactory = factory;
return this;
}
private void incJobIndexCounter(){
jobIndexCounter++;
@ -254,29 +277,21 @@ public class VehicleRoutingProblem {
}
private void addJobToFinalJobMapAndCreateActivities(Job job){
List<TourActivity> acts = new ArrayList<TourActivity>();
List<TourActivity> acts;
if(job instanceof Service) {
Service service = (Service) job;
addService(service);
AbstractTourActivity activity = serviceActivityFactory.createActivity(service);
activity.setIndex(activityIndexCounter);
incActivityIndexCounter();
acts.add(activity);
activityMap.put(service, acts);
}
else if(job instanceof Shipment){
Shipment shipment = (Shipment)job;
addShipment(shipment);
AbstractTourActivity pickup = shipmentActivityFactory.createPickup(shipment);
pickup.setIndex(activityIndexCounter);
incActivityIndexCounter();
AbstractTourActivity delivery = shipmentActivityFactory.createDelivery(shipment);
delivery.setIndex(activityIndexCounter);
incActivityIndexCounter();
acts.add(pickup);
acts.add(delivery);
activityMap.put(shipment, acts);
}
List<AbstractActivity> jobActs = jobActivityFactory.createActivity(job);
for(AbstractActivity act : jobActs){
act.setIndex(activityIndexCounter);
incActivityIndexCounter();
}
activityMap.put(job, jobActs);
}
public Builder addInitialVehicleRoute(VehicleRoute route){
@ -621,8 +636,9 @@ public class VehicleRoutingProblem {
private final Locations locations;
private Map<Job,List<TourActivity>> activityMap;
// private List<List<TourActivity>> activityList;
private Map<Job,List<AbstractActivity>> activityMap;
private int nuActivities;
private VehicleRoutingProblem(Builder builder) {
this.jobs = builder.jobs;
@ -635,6 +651,7 @@ public class VehicleRoutingProblem {
this.constraints = builder.constraints;
this.locations = builder.getLocations();
this.activityMap = builder.activityMap;
this.nuActivities = builder.activityIndexCounter;
logger.info("initialise " + this);
}
@ -719,14 +736,16 @@ public class VehicleRoutingProblem {
return locations;
}
public List<TourActivity> getActivities(Job job){
public List<AbstractActivity> getActivities(Job job){
return Collections.unmodifiableList(activityMap.get(job));
}
public List<TourActivity> copyAndGetActivities(Job job){
List<TourActivity> acts = new ArrayList<TourActivity>();
for(TourActivity act : activityMap.get(job)){
acts.add(act.duplicate());
public int getNuActivities(){ return nuActivities; }
public List<AbstractActivity> copyAndGetActivities(Job job){
List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
for(AbstractActivity act : activityMap.get(job)){
acts.add((AbstractActivity)act.duplicate());
}
return acts;
}

View file

@ -87,8 +87,13 @@ import jsprit.core.util.CalculationUtils;
return ConstraintsStatus.NOT_FULFILLED;
}
// log.info("check insertion of " + newAct + " between " + prevAct + " and " + nextAct + ". prevActDepTime=" + prevActDepTime);
double arrTimeAtNewAct = prevActDepTime + routingCosts.getTransportTime(prevAct.getLocationId(), newAct.getLocationId(), prevActDepTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double latestArrTimeAtNewAct = states.getActivityState(newAct, 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(),
iFacts.getNewVehicle()) - newAct.getOperationTime());
/*
* |--- prevAct ---|
* |--- vehicle's arrival @newAct
@ -106,7 +111,7 @@ import jsprit.core.util.CalculationUtils;
// log.info(newAct + " arrTime=" + arrTimeAtNewAct);
double endTimeAtNewAct = CalculationUtils.getActivityEndTime(arrTimeAtNewAct, newAct);
double arrTimeAtNextAct = endTimeAtNewAct + routingCosts.getTransportTime(newAct.getLocationId(), nextAct.getLocationId(), endTimeAtNewAct, iFacts.getNewDriver(), iFacts.getNewVehicle());
double latestArrTimeAtNextAct = states.getActivityState(nextAct, StateFactory.LATEST_OPERATION_START_TIME, Double.class);
/*
* |--- newAct ---|
* |--- vehicle's arrival @nextAct

View file

@ -16,27 +16,20 @@
******************************************************************************/
package jsprit.core.problem.solution.route;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.JobActivityFactory;
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.driver.DriverImpl;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.activity.DefaultShipmentActivityFactory;
import jsprit.core.problem.solution.route.activity.DefaultTourActivityFactory;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.Start;
import jsprit.core.problem.solution.route.activity.TourActivities;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.activity.TourActivityFactory;
import jsprit.core.problem.solution.route.activity.TourShipmentActivityFactory;
import jsprit.core.problem.solution.route.activity.*;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleImpl.NoVehicle;
import java.util.*;
/**
* Contains the tour, i.e. a number of activities, a vehicle servicing the tour and a driver.
*
@ -49,7 +42,7 @@ public class VehicleRoute {
/**
* Returns a deep copy of this vehicleRoute.
*
* @param route
* @param route route to copy
* @return copied route
* @throws IllegalArgumentException if route is null
*/
@ -63,7 +56,7 @@ public class VehicleRoute {
*
* <p>An empty route has an empty list of tour-activities, no driver (DriverImpl.noDriver()) and no vehicle (VehicleImpl.createNoVehicle()).
*
* @return
* @return empty route
*/
public static VehicleRoute emptyRoute() {
return Builder.newInstance(VehicleImpl.createNoVehicle(), DriverImpl.noDriver()).build();
@ -76,8 +69,10 @@ public class VehicleRoute {
*
*/
public static class Builder {
/**
private Map<Shipment, TourActivity> openActivities = new HashMap<Shipment, TourActivity>();
/**
* Returns new instance of this builder.
*
* <p><b>Construction-settings of vehicleRoute:</b>
@ -87,8 +82,8 @@ public class VehicleRoute {
* <p>latestStart == Double.MAX_VALUE
* <p>earliestEnd == 0.0
*
* @param vehicle
* @param driver
* @param vehicle employed vehicle
* @param driver employed driver
* @return this builder
*/
public static Builder newInstance(Vehicle vehicle, Driver driver){
@ -106,8 +101,7 @@ public class VehicleRoute {
* <p>latestStart == Double.MAX_VALUE
* <p>earliestEnd == 0.0
*
* @param vehicle
* @param driver
* @param vehicle employed vehicle
* @return this builder
*/
public static Builder newInstance(Vehicle vehicle){
@ -130,16 +124,34 @@ public class VehicleRoute {
private TourShipmentActivityFactory shipmentActivityFactory = new DefaultShipmentActivityFactory();
private Set<Shipment> openShipments = new HashSet<Shipment>();
private JobActivityFactory jobActivityFactory = new JobActivityFactory() {
@Override
public List<AbstractActivity> createActivity(Job job) {
List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
if(job instanceof Service){
acts.add(serviceActivityFactory.createActivity((Service) job));
}
else if(job instanceof Shipment){
acts.add(shipmentActivityFactory.createPickup((Shipment) job));
acts.add(shipmentActivityFactory.createDelivery((Shipment) job));
}
return acts;
}
};
/**
* Sets the serviceActivityFactory to create serviceActivities.
*
* <p>By default {@link DefaultTourActivityFactory} is used.
*
* @param serviceActivityFactory
* @param serviceActivityFactory the factory to create serviceActivities
*/
public void setServiceActivityFactory(TourActivityFactory serviceActivityFactory) {
public Builder setServiceActivityFactory(TourActivityFactory serviceActivityFactory) {
this.serviceActivityFactory = serviceActivityFactory;
return this;
}
/**
@ -147,23 +159,18 @@ public class VehicleRoute {
*
* <p>By default {@link DefaultShipmentActivityFactory} is used.
*
* @param shipmentActivityFactory
* @param shipmentActivityFactory the factory to create shipmentActivities
*/
public void setShipmentActivityFactory(TourShipmentActivityFactory shipmentActivityFactory) {
public Builder setShipmentActivityFactory(TourShipmentActivityFactory shipmentActivityFactory) {
this.shipmentActivityFactory = shipmentActivityFactory;
return this;
}
/**
* Constructs the route-builder.
*
* <p>startLocation == vehicle.getStartLocationId()
* <p>endLocation == vehicle.getEndLocationId()
* <p>departureTime == vehicle.getEarliestDepartureTime()
* <p>latestStart == Double.MAX_VALUE
* <p>earliestEnd == 0.0
* @param vehicle
* @param driver
*/
public Builder setJobActivityFactory(JobActivityFactory jobActivityFactory){
this.jobActivityFactory = jobActivityFactory;
return this;
}
private Builder(Vehicle vehicle, Driver driver) {
super();
this.vehicle = vehicle;
@ -178,7 +185,7 @@ public class VehicleRoute {
*
* <p><b>Note</b> that departureTime cannot be lower than earliestDepartureTime of vehicle.
*
* @param departureTime
* @param departureTime departure time of vehicle being employed for this route
* @return builder
* @throws IllegalArgumentException if departureTime < vehicle.getEarliestDeparture()
*/
@ -191,10 +198,11 @@ public class VehicleRoute {
/**
* Sets the end-time of the route, i.e. which is the time the vehicle has to be at its end-location at latest.
*
* @param endTime
* @param endTime endTime of route
* @return this builder
* @throws IllegalArgumentException if endTime > vehicle.getLatestArrival()
*/
@Deprecated
public Builder setRouteEndArrivalTime(double endTime){
if(endTime > vehicle.getLatestArrival()) throw new IllegalArgumentException("endTime > vehicle.getLatestArrival(). this must not be.");
end.setArrTime(endTime);
@ -208,11 +216,12 @@ public class VehicleRoute {
*
* <p>The resulting activity occurs in the activity-sequence in the order adding/inserting.
*
* @param service
* @param service to be added
* @return this builder
* @throws IllegalArgumentException if service is null
*/
public Builder addService(Service service){
@SuppressWarnings("deprecation")
public Builder addService(Service service){
if(service == null) throw new IllegalArgumentException("service must not be null");
addService(service,0.0,0.0);
return this;
@ -225,13 +234,15 @@ public class VehicleRoute {
*
* <p>Basically this activity is then scheduled with an activity arrival and activity endTime.
*
* @param service
* @param arrTime
* @param endTime
* @param service to be added
* @param arrTime at service activity
* @param endTime of service activity
* @return builder
*/
@Deprecated
public Builder addService(Service service, double arrTime, double endTime){
TourActivity act = serviceActivityFactory.createActivity(service);
List<AbstractActivity> acts = jobActivityFactory.createActivity(service);
TourActivity act = acts.get(0);
act.setArrTime(arrTime);
act.setEndTime(endTime);
tourActivities.addActivity(act);
@ -241,54 +252,60 @@ public class VehicleRoute {
/**
* Adds a the pickup of the specified shipment.
*
* @param shipment
* @param shipment to be picked up and added to this route
* @throws IllegalStateException if method has already been called with the specified shipment.
* @return
* @return the builder
*/
public Builder addPickup(Shipment shipment){
addPickup(shipment,0.0,0.0);
@SuppressWarnings("deprecation")
public Builder addPickup(Shipment shipment){
addPickup(shipment,0.,0.);
return this;
}
/**
* Adds a the pickup of the specified shipment at specified arrival and end-time.
*
* @param shipment
* @param shipment to be picked up and added to this route
* @throws IllegalStateException if method has already been called with the specified shipment.
* @return
* @return builder
*/
@Deprecated
public Builder addPickup(Shipment shipment, double arrTime, double endTime){
if(openShipments.contains(shipment)) throw new IllegalStateException("shipment has already been added. cannot add it twice.");
TourActivity act = shipmentActivityFactory.createPickup(shipment);
List<AbstractActivity> acts = jobActivityFactory.createActivity(shipment);
TourActivity act = acts.get(0);
act.setArrTime(arrTime);
act.setEndTime(endTime);
tourActivities.addActivity(act);
openShipments.add(shipment);
openActivities.put(shipment,acts.get(1));
return this;
}
/**
* Adds a the delivery of the specified shipment.
*
* @param shipment
* @param shipment to be delivered and add to this vehicleRoute
* @throws IllegalStateException if specified shipment has not been picked up yet (i.e. method addPickup(shipment) has not been called yet).
* @return
* @return builder
*/
public Builder addDelivery(Shipment shipment){
addDelivery(shipment,0.0,0.0);
@SuppressWarnings("deprecation")
public Builder addDelivery(Shipment shipment){
addDelivery(shipment,0.,0.);
return this;
}
/**
* Adds a the delivery of the specified shipment at a specified arrival and endTime.
*
* @param shipment
* @param shipment to be delivered and added to this route
* @throws IllegalStateException if specified shipment has not been picked up yet (i.e. method addPickup(shipment) has not been called yet).
* @return
* @return the builder
*/
@Deprecated
public Builder addDelivery(Shipment shipment, double arrTime, double endTime){
if(openShipments.contains(shipment)){
TourActivity act = shipmentActivityFactory.createDelivery(shipment);
TourActivity act = openActivities.get(shipment);
act.setArrTime(arrTime);
act.setEndTime(endTime);
tourActivities.addActivity(act);
@ -313,8 +330,7 @@ public class VehicleRoute {
end.setLocationId(tourActivities.getActivities().get(tourActivities.getActivities().size()-1).getLocationId());
}
}
VehicleRoute route = new VehicleRoute(this);
return route;
return new VehicleRoute(this);
}
}
@ -332,7 +348,7 @@ public class VehicleRoute {
/**
* Copy constructor copying a route.
*
* @param route
* @param route to copy
*/
private VehicleRoute(VehicleRoute route){
this.start = Start.copyOf(route.getStart());
@ -345,7 +361,7 @@ public class VehicleRoute {
/**
* Constructs route.
*
* @param builder
* @param builder used to build route
*/
private VehicleRoute(Builder builder){
this.tourActivities = builder.tourActivities;
@ -358,7 +374,7 @@ public class VehicleRoute {
/**
* Returns an unmodifiable list of activities on this route (without start/end).
*
* @return
* @return list of tourActivities
*/
public List<TourActivity> getActivities(){
return Collections.unmodifiableList(tourActivities.getActivities());
@ -403,8 +419,8 @@ public class VehicleRoute {
* <p>startActivity.endTime (<code>startActivity.getEndTime()</code>) is set to max{<code>vehicle.getEarliestDeparture()</code>, <code>vehicleDepTime</code>}.
* thus, <code>vehicle.getEarliestDeparture()</code> is a physical constraint that has to be met.
*
* @param vehicle
* @param vehicleDepTime
* @param vehicle to be employed
* @param vehicleDepTime of employed vehicle
*/
public void setVehicleAndDepartureTime(Vehicle vehicle, double vehicleDepTime){
this.vehicle = vehicle;

View file

@ -18,18 +18,18 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.job.Shipment;
public class DefaultShipmentActivityFactory implements TourShipmentActivityFactory{
@Override
public AbstractTourActivity createPickup(Shipment shipment) {
public AbstractActivity createPickup(Shipment shipment) {
return new PickupShipment(shipment);
}
@Override
public AbstractTourActivity createDelivery(Shipment shipment) {
public AbstractActivity createDelivery(Shipment shipment) {
return new DeliverShipment(shipment);
}

View file

@ -16,7 +16,7 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.job.Delivery;
import jsprit.core.problem.job.Pickup;
import jsprit.core.problem.job.Service;
@ -24,8 +24,8 @@ import jsprit.core.problem.job.Service;
public class DefaultTourActivityFactory implements TourActivityFactory{
@Override
public AbstractTourActivity createActivity(Service service) {
AbstractTourActivity act;
public AbstractActivity createActivity(Service service) {
AbstractActivity act;
if(service instanceof Pickup){
act = new PickupService((Pickup) service);
}

View file

@ -18,11 +18,11 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Delivery;
public final class DeliverService extends AbstractTourActivity implements DeliveryActivity{
public final class DeliverService extends AbstractActivity implements DeliveryActivity{
private Delivery delivery;

View file

@ -18,12 +18,12 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Shipment;
public final class DeliverShipment extends AbstractTourActivity implements DeliveryActivity{
public final class DeliverShipment extends AbstractActivity implements DeliveryActivity{
private Shipment shipment;

View file

@ -16,11 +16,11 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.util.Coordinate;
public final class End extends AbstractTourActivity implements TourActivity {
public final class End extends AbstractActivity implements TourActivity {
@Deprecated
public static int creation = 0;

View file

@ -18,12 +18,12 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Pickup;
import jsprit.core.problem.job.Service;
public final class PickupService extends AbstractTourActivity implements PickupActivity{
public final class PickupService extends AbstractActivity implements PickupActivity{
private Service pickup;

View file

@ -18,12 +18,12 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Shipment;
public final class PickupShipment extends AbstractTourActivity implements PickupActivity{
public final class PickupShipment extends AbstractActivity implements PickupActivity{
private Shipment shipment;

View file

@ -16,12 +16,12 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity;
public class ServiceActivity extends AbstractTourActivity implements JobActivity{
public class ServiceActivity extends AbstractActivity implements JobActivity{
public static int counter = 0;

View file

@ -16,10 +16,10 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
public final class Start extends AbstractTourActivity implements TourActivity {
public final class Start extends AbstractActivity implements TourActivity {
public final static String ACTIVITY_NAME = "start";

View file

@ -16,11 +16,11 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.job.Service;
public interface TourActivityFactory {
public AbstractTourActivity createActivity(Service service);
public AbstractActivity createActivity(Service service);
}

View file

@ -18,13 +18,13 @@
******************************************************************************/
package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.job.Shipment;
public interface TourShipmentActivityFactory {
public AbstractTourActivity createPickup(Shipment shipment);
public AbstractActivity createPickup(Shipment shipment);
public AbstractTourActivity createDelivery(Shipment shipment);
public AbstractActivity createDelivery(Shipment shipment);
}

View file

@ -20,6 +20,8 @@
******************************************************************************/
package jsprit.core.problem.solution.route.state;
import jsprit.core.problem.HasIndex;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -29,7 +31,8 @@ import java.util.Map;
public class StateFactory {
public interface StateId {
public interface StateId extends HasIndex{
}
@ -78,25 +81,25 @@ public class StateFactory {
}
public final static StateId MAXLOAD = new StateIdImpl("maxload");
public final static StateId MAXLOAD = new StateIdImpl("maxload", 0);
public final static StateId LOAD = new StateIdImpl("load");
public final static StateId LOAD = new StateIdImpl("load", 1);
public final static StateId COSTS = new StateIdImpl("costs");
public final static StateId COSTS = new StateIdImpl("costs", 2);
public final static StateId LOAD_AT_BEGINNING = new StateIdImpl("loadAtBeginning");
public final static StateId LOAD_AT_BEGINNING = new StateIdImpl("loadAtBeginning", 3);
public final static StateId LOAD_AT_END = new StateIdImpl("loadAtEnd");
public final static StateId LOAD_AT_END = new StateIdImpl("loadAtEnd", 4);
public final static StateId DURATION = new StateIdImpl("duration");
public final static StateId DURATION = new StateIdImpl("duration", 5);
public final static StateId LATEST_OPERATION_START_TIME = new StateIdImpl("latestOST");
public final static StateId LATEST_OPERATION_START_TIME = new StateIdImpl("latestOST", 6);
public final static StateId EARLIEST_OPERATION_START_TIME = new StateIdImpl("earliestOST");
public final static StateId EARLIEST_OPERATION_START_TIME = new StateIdImpl("earliestOST", 7);
public final static StateId FUTURE_MAXLOAD = new StateIdImpl("futureMaxload");
public final static StateId FUTURE_MAXLOAD = new StateIdImpl("futureMaxload", 8);
public final static StateId PAST_MAXLOAD = new StateIdImpl("pastMaxload");
public final static StateId PAST_MAXLOAD = new StateIdImpl("pastMaxload", 9);
final static List<String> reservedIds = Arrays.asList("maxload","load","costs","loadAtBeginning","loadAtEnd","duration","latestOST","earliestOST"
,"futureMaxload","pastMaxload");
@ -108,8 +111,14 @@ public class StateFactory {
public static StateId createId(String name){
if(reservedIds.contains(name)){ throwReservedIdException(name); }
return new StateIdImpl(name);
return new StateIdImpl(name, -1);
}
public static StateId createId(String name, int index){
if(reservedIds.contains(name)) throwReservedIdException(name);
if(index < 10) throwReservedIdException(name);
return new StateIdImpl(name, index);
}
public static State createState(double value){
return new StateImpl(value);
@ -131,7 +140,11 @@ public class StateFactory {
static class StateIdImpl implements StateId {
private int index;
public int getIndex(){ return index; }
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@ -165,9 +178,10 @@ public class StateFactory {
private String name;
public StateIdImpl(String name) {
public StateIdImpl(String name, int index) {
super();
this.name = name;
this.index = index;
}
public String toString(){

View file

@ -20,6 +20,7 @@ package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint;
@ -119,7 +120,7 @@ public class ServiceInsertionAndLoadConstraintsTest {
route.setVehicleAndDepartureTime(vehicle, 0.0);
Inserter inserter = new Inserter(new InsertionListeners(), vehicleRoutingProblem);
List<TourActivity> acts = new ArrayList<TourActivity>();
List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
acts.add(new DeliverService(delivery));
when(vehicleRoutingProblem.copyAndGetActivities(delivery)).thenReturn(acts);
inserter.insertJob(delivery, new InsertionData(0,0,0,vehicle,null), route);

View file

@ -20,6 +20,7 @@ package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.*;
import jsprit.core.problem.constraint.ConstraintManager.Priority;
@ -131,8 +132,8 @@ public class ShipmentInsertionCalculatorTest {
assertEquals(2,iData.getDeliveryInsertionIndex());
}
private List<TourActivity> getTourActivities(Shipment shipment) {
List<TourActivity> acts = new ArrayList<TourActivity>();
private List<AbstractActivity> getTourActivities(Shipment shipment) {
List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
PickupShipment pick = new PickupShipment(shipment);
DeliverShipment del = new DeliverShipment(shipment);
acts.add(pick);

View file

@ -20,6 +20,7 @@ package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.algorithm.state.UpdateEndLocationIfRouteIsOpen;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.driver.Driver;
@ -29,7 +30,6 @@ import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.DeliverShipment;
import jsprit.core.problem.solution.route.activity.PickupService;
import jsprit.core.problem.solution.route.activity.PickupShipment;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
@ -65,7 +65,7 @@ public class TestInserter {
when(iData.getSelectedVehicle()).thenReturn(vehicle);
VehicleRoutingProblem vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
List<TourActivity> acts = new ArrayList<TourActivity>();
List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
PickupService act = new PickupService(serviceToInsert);
acts.add(act);
when(vehicleRoutingProblem.copyAndGetActivities(serviceToInsert)).thenReturn(acts);
@ -105,8 +105,8 @@ public class TestInserter {
assertEquals(route.getEnd().getLocationId(),serviceToInsert.getLocationId());
}
private List<TourActivity> getTourActivities(Service serviceToInsert) {
List<TourActivity> acts = new ArrayList<TourActivity>();
private List<AbstractActivity> getTourActivities(Service serviceToInsert) {
List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
acts.add(new PickupService(serviceToInsert));
return acts;
}
@ -126,11 +126,7 @@ public class TestInserter {
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build();
//start - pick(shipment) - del(shipment) - end
Shipment shipmentToInsert = Shipment.Builder.newInstance("s").setDeliveryLocation("delLoc").setPickupLocation("pickLoc").build();
// Shipment shipmentToInsert = mock(Shipment.class);
// when(shipmentToInsert.getSize()).thenReturn(capacity);
// when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc");
// when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc");
// when(shipmentToInsert.getSize()).thenReturn(Capacity.Builder.newInstance().build());
InsertionData iData = mock(InsertionData.class);
when(iData.getPickupInsertionIndex()).thenReturn(2);
when(iData.getDeliveryInsertionIndex()).thenReturn(2);
@ -147,8 +143,8 @@ public class TestInserter {
assertEquals(route.getEnd().getLocationId(),vehicle.getEndLocationId());
}
private List<TourActivity> getTourActivities(Shipment shipmentToInsert) {
List<TourActivity> acts = new ArrayList<TourActivity>();
private List<AbstractActivity> getTourActivities(Shipment shipmentToInsert) {
List<AbstractActivity> acts = new ArrayList<AbstractActivity>();
acts.add(new PickupShipment(shipmentToInsert));
acts.add(new DeliverShipment(shipmentToInsert));
return acts;

View file

@ -1,6 +1,9 @@
package jsprit.core.algorithm.state;
import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.JobActivityFactory;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.job.*;
import jsprit.core.problem.solution.route.VehicleRoute;
@ -12,13 +15,14 @@ import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Created by schroeder on 13.07.14.
* Unit tests to test correct calc of load states
*/
public class LoadStateTest {
@ -37,10 +41,11 @@ public class LoadStateTest {
when(type.getCapacityDimensions()).thenReturn(Capacity.Builder.newInstance().addDimension(0,20).build());
when(vehicle.getType()).thenReturn(type);
Service s1 = mock(Service.class);
when(s1.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0,10).build());
Service s2 = mock(Service.class);
when(s2.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0,5).build());
VehicleRoutingProblem.Builder serviceProblemBuilder = VehicleRoutingProblem.Builder.newInstance();
Service s1 = Service.Builder.newInstance("s").addSizeDimension(0,10).setLocationId("loc").build();
Service s2 = Service.Builder.newInstance("s2").addSizeDimension(0,5).setLocationId("loc").build();
serviceProblemBuilder.addJob(s1).addJob(s2);
final VehicleRoutingProblem serviceProblem = serviceProblemBuilder.build();
Pickup pickup = mock(Pickup.class);
when(pickup.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 10).build());
@ -52,16 +57,27 @@ public class LoadStateTest {
Shipment shipment2 = mock(Shipment.class);
when(shipment2.getSize()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 5).build());
serviceRoute = VehicleRoute.Builder.newInstance(vehicle).addService(s1).addService(s2).build();
VehicleRoute.Builder serviceRouteBuilder = VehicleRoute.Builder.newInstance(vehicle);
serviceRouteBuilder.setJobActivityFactory(new JobActivityFactory() {
@Override
public List<AbstractActivity> createActivity(Job job) {
return serviceProblem.copyAndGetActivities(job);
}
});
serviceRoute = serviceRouteBuilder.addService(s1).addService(s2).build();
pickup_delivery_route = VehicleRoute.Builder.newInstance(vehicle).addService(pickup).addService(delivery).build();
shipment_route = VehicleRoute.Builder.newInstance(vehicle).addPickup(shipment1).addPickup(shipment2).addDelivery(shipment2).addDelivery(shipment1).build();
stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
stateManager.updateLoadStates();
stateManager.informInsertionStarts(Arrays.asList(serviceRoute,pickup_delivery_route,shipment_route), Collections.<Job>emptyList());
stateManager.informInsertionStarts(Arrays.asList(serviceRoute), Collections.<Job>emptyList());
// <stateManager.informInsertionStarts(Arrays.asList(serviceRoute,pickup_delivery_route,shipment_route), Collections.<Job>emptyList());
}
@Test
public void loadAtEndShouldBe15(){
Capacity routeState = stateManager.getRouteState(serviceRoute, StateFactory.LOAD_AT_END, Capacity.class);

View file

@ -18,8 +18,6 @@
******************************************************************************/
package jsprit.core.algorithm.state;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import jsprit.core.problem.Capacity;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.solution.route.VehicleRoute;
@ -27,9 +25,11 @@ import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.state.StateFactory;
import jsprit.core.problem.solution.route.state.StateFactory.State;
import jsprit.core.problem.solution.route.state.StateFactory.StateId;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
public class StateManagerTest {
@Test
@ -145,4 +145,29 @@ public class StateManagerTest {
boolean problemState = stateManager.getProblemState(id, Boolean.class);
assertFalse(problemState);
}
@Test
public void whenCreatingNewState_itShouldHaveAnIndex(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateId stateId = stateManager.createStateId("foo-state");
assertEquals(10,stateId.getIndex());
}
@Test
public void whenCreatingNewStates_theyShouldHaveAnIndex(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateId fooState = stateManager.createStateId("foo-state");
StateId foofooState = stateManager.createStateId("foo-foo-state");
assertEquals(10,fooState.getIndex());
assertEquals(11,foofooState.getIndex());
}
@Test
public void whenCreatingTwoStatesWithTheSameName_theyShouldHaveTheSameIndex(){
StateManager stateManager = new StateManager(mock(VehicleRoutingTransportCosts.class));
StateId fooState = stateManager.createStateId("foo-state");
StateId foofooState = stateManager.createStateId("foo-state");
assertEquals(10,fooState.getIndex());
assertEquals(10,foofooState.getIndex());
}
}