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-14 20:56:11 +02:00
parent aa1b7214ae
commit 4f2689529f
33 changed files with 512 additions and 243 deletions

View file

@ -16,23 +16,19 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.recreate; package jsprit.core.algorithm.recreate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound; import jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound;
import jsprit.core.algorithm.recreate.listener.InsertionListener; import jsprit.core.algorithm.recreate.listener.InsertionListener;
import jsprit.core.algorithm.recreate.listener.InsertionListeners; import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.util.RandomNumberGeneration; import jsprit.core.util.RandomNumberGeneration;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.*;
@ -88,10 +84,10 @@ final class BestInsertion implements InsertionStrategy{
this.random = random; this.random = random;
} }
public BestInsertion(JobInsertionCostsCalculator jobInsertionCalculator) { public BestInsertion(JobInsertionCostsCalculator jobInsertionCalculator, VehicleRoutingProblem vehicleRoutingProblem) {
super(); super();
this.insertionsListeners = new InsertionListeners(); this.insertionsListeners = new InsertionListeners();
inserter = new Inserter(insertionsListeners); inserter = new Inserter(insertionsListeners, vehicleRoutingProblem);
bestInsertionCostCalculator = jobInsertionCalculator; bestInsertionCostCalculator = jobInsertionCalculator;
logger.info("initialise " + this); logger.info("initialise " + this);
} }

View file

@ -18,10 +18,6 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.recreate; package jsprit.core.algorithm.recreate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners.PrioritizedVRAListener; import jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListeners.PrioritizedVRAListener;
import jsprit.core.algorithm.recreate.listener.InsertionListener; import jsprit.core.algorithm.recreate.listener.InsertionListener;
import jsprit.core.algorithm.state.StateManager; import jsprit.core.algorithm.state.StateManager;
@ -29,6 +25,10 @@ import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager; import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.vehicle.VehicleFleetManager; import jsprit.core.problem.vehicle.VehicleFleetManager;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
public class BestInsertionBuilder { public class BestInsertionBuilder {
@ -150,12 +150,10 @@ public class BestInsertionBuilder {
JobInsertionCostsCalculator jobInsertions = calcBuilder.build(); JobInsertionCostsCalculator jobInsertions = calcBuilder.build();
InsertionStrategy bestInsertion; InsertionStrategy bestInsertion;
if(executor == null){ if(executor == null){
bestInsertion = new BestInsertion(jobInsertions); bestInsertion = new BestInsertion(jobInsertions,vrp);
} }
else{ else{
bestInsertion = new BestInsertionConcurrent(jobInsertions,executor,nuOfThreads,vrp);
bestInsertion = new BestInsertionConcurrent(jobInsertions,executor,nuOfThreads);
} }
for(InsertionListener l : iListeners) bestInsertion.addListener(l); for(InsertionListener l : iListeners) bestInsertion.addListener(l);
@ -164,8 +162,8 @@ public class BestInsertionBuilder {
/** /**
* @deprecated this is experimental and can disappear. * @deprecated this is experimental and can disappear.
* @param parseDouble * @param timeSlice the time slice
* @param parseInt * @param nNeighbors number of neighbors
*/ */
@Deprecated @Deprecated
public void experimentalTimeScheduler(double timeSlice, int nNeighbors) { public void experimentalTimeScheduler(double timeSlice, int nNeighbors) {

View file

@ -16,28 +16,20 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.recreate; package jsprit.core.algorithm.recreate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound; import jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound;
import jsprit.core.algorithm.recreate.listener.InsertionListener; import jsprit.core.algorithm.recreate.listener.InsertionListener;
import jsprit.core.algorithm.recreate.listener.InsertionListeners; import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.util.RandomNumberGeneration; import jsprit.core.util.RandomNumberGeneration;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.*;
import java.util.concurrent.*;
@ -104,12 +96,12 @@ final class BestInsertionConcurrent implements InsertionStrategy{
this.random = random; this.random = random;
} }
public BestInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, ExecutorService executorService, int nuOfBatches) { public BestInsertionConcurrent(JobInsertionCostsCalculator jobInsertionCalculator, ExecutorService executorService, int nuOfBatches, VehicleRoutingProblem vehicleRoutingProblem) {
super(); super();
this.insertionsListeners = new InsertionListeners(); this.insertionsListeners = new InsertionListeners();
this.executor = executorService; this.executor = executorService;
this.nuOfBatches = nuOfBatches; this.nuOfBatches = nuOfBatches;
inserter = new Inserter(insertionsListeners); inserter = new Inserter(insertionsListeners, vehicleRoutingProblem);
bestInsertionCostCalculator = jobInsertionCalculator; bestInsertionCostCalculator = jobInsertionCalculator;
completionService = new ExecutorCompletionService<Insertion>(executor); completionService = new ExecutorCompletionService<Insertion>(executor);
logger.info("initialise " + this); logger.info("initialise " + this);

View file

@ -18,22 +18,23 @@ package jsprit.core.algorithm.recreate;
import jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound; import jsprit.core.algorithm.recreate.InsertionData.NoInsertionFound;
import jsprit.core.algorithm.recreate.listener.InsertionListeners; import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service; import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment; import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.DefaultShipmentActivityFactory; import jsprit.core.problem.solution.route.activity.*;
import jsprit.core.problem.solution.route.activity.DefaultTourActivityFactory;
import jsprit.core.problem.solution.route.activity.TourActivity; import java.util.List;
import jsprit.core.problem.solution.route.activity.TourActivityFactory;
import jsprit.core.problem.solution.route.activity.TourShipmentActivityFactory;
class Inserter { class Inserter {
interface JobInsertionHandler { interface JobInsertionHandler {
void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route); void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route);
void setNextHandler(JobInsertionHandler handler); void setNextHandler(JobInsertionHandler handler);
} }
class JobExceptionHandler implements JobInsertionHandler{ class JobExceptionHandler implements JobInsertionHandler{
@ -57,6 +58,12 @@ class Inserter {
private JobInsertionHandler delegator = new JobExceptionHandler(); private JobInsertionHandler delegator = new JobExceptionHandler();
private VehicleRoutingProblem vehicleRoutingProblem;
public ServiceInsertionHandler(VehicleRoutingProblem vehicleRoutingProblem) {
this.vehicleRoutingProblem = vehicleRoutingProblem;
}
@Override @Override
public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) { public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
if(job instanceof Service){ if(job instanceof Service){
@ -66,7 +73,8 @@ class Inserter {
setEndLocation(route,(Service)job); setEndLocation(route,(Service)job);
} }
} }
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), this.activityFactory.createActivity((Service)job)); TourActivity activity = vehicleRoutingProblem.copyAndGetActivities(job).get(0);
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), activity);
} }
else delegator.handleJobInsertion(job, iData, route); else delegator.handleJobInsertion(job, iData, route);
} }
@ -83,15 +91,22 @@ class Inserter {
class ShipmentInsertionHandler implements JobInsertionHandler { class ShipmentInsertionHandler implements JobInsertionHandler {
private final VehicleRoutingProblem vehicleRoutingProblem;
private TourShipmentActivityFactory activityFactory = new DefaultShipmentActivityFactory(); private TourShipmentActivityFactory activityFactory = new DefaultShipmentActivityFactory();
private JobInsertionHandler delegator = new JobExceptionHandler(); private JobInsertionHandler delegator = new JobExceptionHandler();
public ShipmentInsertionHandler(VehicleRoutingProblem vehicleRoutingProblem) {
this.vehicleRoutingProblem = vehicleRoutingProblem;
}
@Override @Override
public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) { public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
if(job instanceof Shipment){ if(job instanceof Shipment){
TourActivity pickupShipment = this.activityFactory.createPickup((Shipment)job); List<TourActivity> acts = vehicleRoutingProblem.copyAndGetActivities(job);
TourActivity deliverShipment = this.activityFactory.createDelivery((Shipment)job); TourActivity pickupShipment = acts.get(0);
TourActivity deliverShipment = acts.get(1);
route.setVehicleAndDepartureTime(iData.getSelectedVehicle(),iData.getVehicleDepartureTime()); route.setVehicleAndDepartureTime(iData.getSelectedVehicle(),iData.getVehicleDepartureTime());
if(!iData.getSelectedVehicle().isReturnToDepot()){ if(!iData.getSelectedVehicle().isReturnToDepot()){
if(iData.getDeliveryInsertionIndex()>=route.getActivities().size()){ if(iData.getDeliveryInsertionIndex()>=route.getActivities().size()){
@ -118,11 +133,13 @@ class Inserter {
private JobInsertionHandler jobInsertionHandler; private JobInsertionHandler jobInsertionHandler;
public Inserter(InsertionListeners insertionListeners) { private VehicleRoutingProblem vehicleRoutingProblem;
public Inserter(InsertionListeners insertionListeners, VehicleRoutingProblem vehicleRoutingProblem) {
this.insertionListeners = insertionListeners; this.insertionListeners = insertionListeners;
new DefaultTourActivityFactory(); new DefaultTourActivityFactory();
jobInsertionHandler = new ServiceInsertionHandler(); jobInsertionHandler = new ServiceInsertionHandler(vehicleRoutingProblem);
jobInsertionHandler.setNextHandler(new ShipmentInsertionHandler()); jobInsertionHandler.setNextHandler(new ShipmentInsertionHandler(vehicleRoutingProblem));
} }
public void insertJob(Job job, InsertionData insertionData, VehicleRoute vehicleRoute){ public void insertJob(Job job, InsertionData insertionData, VehicleRoute vehicleRoute){
@ -130,7 +147,7 @@ class Inserter {
if(insertionData == null || (insertionData instanceof NoInsertionFound)) throw new IllegalStateException("insertionData null. cannot insert job."); if(insertionData == null || (insertionData instanceof NoInsertionFound)) throw new IllegalStateException("insertionData null. cannot insert job.");
if(job == null) throw new IllegalStateException("cannot insert null-job"); if(job == null) throw new IllegalStateException("cannot insert null-job");
if(!(vehicleRoute.getVehicle().getId().toString().equals(insertionData.getSelectedVehicle().getId().toString()))){ if(!(vehicleRoute.getVehicle().getId().equals(insertionData.getSelectedVehicle().getId()))){
insertionListeners.informVehicleSwitched(vehicleRoute, vehicleRoute.getVehicle(), insertionData.getSelectedVehicle()); insertionListeners.informVehicleSwitched(vehicleRoute, vehicleRoute.getVehicle(), insertionData.getSelectedVehicle());
vehicleRoute.setVehicleAndDepartureTime(insertionData.getSelectedVehicle(), insertionData.getVehicleDepartureTime()); vehicleRoute.setVehicleAndDepartureTime(insertionData.getSelectedVehicle(), insertionData.getVehicleDepartureTime());
} }

View file

@ -0,0 +1,16 @@
package jsprit.core.problem;
import jsprit.core.problem.job.Job;
/**
* Created by schroeder on 14.07.14.
*/
public abstract class AbstractJob implements Job {
private int index;
public int getIndex(){ return index; }
protected void setIndex(int index){ this.index = index; }
}

View file

@ -0,0 +1,15 @@
package jsprit.core.problem;
import jsprit.core.problem.solution.route.activity.TourActivity;
/**
* Created by schroeder on 14.07.14.
*/
public abstract class AbstractTourActivity implements TourActivity {
private int index;
public int getIndex(){ return index; }
protected void setIndex(int index){ this.index = index; }
}

View file

@ -0,0 +1,15 @@
package jsprit.core.problem;
import jsprit.core.problem.vehicle.Vehicle;
/**
* Created by schroeder on 14.07.14.
*/
public abstract class AbstractVehicle implements Vehicle {
private int index;
public int getIndex(){ return index; }
protected void setIndex(int index){ this.index = index; }
}

View file

@ -0,0 +1,10 @@
package jsprit.core.problem;
/**
* Created by schroeder on 14.07.14.
*/
public interface HasId {
public String getId();
}

View file

@ -0,0 +1,10 @@
package jsprit.core.problem;
/**
* Created by schroeder on 14.07.14.
*/
public interface HasIndex {
public int getIndex();
}

View file

@ -16,15 +16,6 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem; package jsprit.core.problem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts; import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts; import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
@ -32,19 +23,17 @@ import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service; import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment; import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.DefaultShipmentActivityFactory;
import jsprit.core.problem.solution.route.activity.DefaultTourActivityFactory;
import jsprit.core.problem.solution.route.activity.TourActivity; import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.PenaltyVehicleType; import jsprit.core.problem.vehicle.*;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.problem.vehicle.VehicleTypeKey;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
import jsprit.core.util.CrowFlyCosts; import jsprit.core.util.CrowFlyCosts;
import jsprit.core.util.Locations; import jsprit.core.util.Locations;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.util.*;
/** /**
* Contains and defines the vehicle routing problem. * Contains and defines the vehicle routing problem.
@ -71,6 +60,8 @@ public class VehicleRoutingProblem {
*/ */
public static class Builder { public static class Builder {
/** /**
* Returns a new instance of this builder. * Returns a new instance of this builder.
* *
@ -122,6 +113,19 @@ public class VehicleRoutingProblem {
private Double penaltyFixedCosts = null; private Double penaltyFixedCosts = null;
private int jobIndexCounter = 0;
private int vehicleIndexCounter = 0;
private int activityIndexCounter = 0;
private Map<Job,List<TourActivity>> activityMap = new HashMap<Job, List<TourActivity>>();
// private ArrayList<List<TourActivity>> activityList;
private DefaultShipmentActivityFactory shipmentActivityFactory = new DefaultShipmentActivityFactory();
private DefaultTourActivityFactory serviceActivityFactory = new DefaultTourActivityFactory();
/** /**
* Create a location (i.e. coordinate) and returns the key of the location which is Coordinate.toString(). * Create a location (i.e. coordinate) and returns the key of the location which is Coordinate.toString().
* *
@ -141,6 +145,14 @@ public class VehicleRoutingProblem {
} }
private void incJobIndexCounter(){
jobIndexCounter++;
}
private void incActivityIndexCounter(){
activityIndexCounter++;
}
/** /**
* Returns the unmodifiable map of collected locations (mapped by their location-id). * Returns the unmodifiable map of collected locations (mapped by their location-id).
* *
@ -203,10 +215,28 @@ public class VehicleRoutingProblem {
* @param job * @param job
* @return this builder * @return this builder
* @throws IllegalStateException if job is neither a shipment nor a service, or jobId has already been added. * @throws IllegalStateException if job is neither a shipment nor a service, or jobId has already been added.
* @deprecated use addJob(AbstractJob job) instead
*/ */
@Deprecated
public Builder addJob(Job job) { public Builder addJob(Job job) {
if(!(job instanceof AbstractJob)) throw new IllegalArgumentException("job must be of type AbstractJob");
return addJob((AbstractJob)job);
}
/**
* Adds a job which is either a service or a shipment.
*
* <p>Note that job.getId() must be unique, i.e. no job (either it is a shipment or a service) is allowed to have an already allocated id.
*
* @param job
* @return this builder
* @throws IllegalStateException if job is neither a shipment nor a service, or jobId has already been added.
*/
public Builder addJob(AbstractJob job) {
if(tentativeJobs.containsKey(job.getId())) throw new IllegalStateException("jobList already contains a job with id " + job.getId() + ". make sure you use unique ids for your jobs (i.e. service and shipments)"); if(tentativeJobs.containsKey(job.getId())) throw new IllegalStateException("jobList already contains a job with id " + job.getId() + ". make sure you use unique ids for your jobs (i.e. service and shipments)");
if(!(job instanceof Service || job instanceof Shipment)) throw new IllegalStateException("job must be either a service or a shipment"); if(!(job instanceof Service || job instanceof Shipment)) throw new IllegalStateException("job must be either a service or a shipment");
job.setIndex(jobIndexCounter);
incJobIndexCounter();
tentativeJobs.put(job.getId(), job); tentativeJobs.put(job.getId(), job);
addLocationToTentativeLocations(job); addLocationToTentativeLocations(job);
return this; return this;
@ -223,12 +253,29 @@ public class VehicleRoutingProblem {
} }
} }
private void addJobToFinalJobMap(Job job){ private void addJobToFinalJobMapAndCreateActivities(Job job){
List<TourActivity> acts = new ArrayList<TourActivity>();
if(job instanceof Service) { if(job instanceof Service) {
addService((Service) job); 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){ else if(job instanceof Shipment){
addShipment((Shipment)job); 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);
} }
} }
@ -272,8 +319,26 @@ public class VehicleRoutingProblem {
* *
* @param vehicle * @param vehicle
* @return this builder * @return this builder
* @deprecated use addVehicle(AbstractVehicle vehicle) instead
*/ */
@Deprecated
public Builder addVehicle(Vehicle vehicle) { public Builder addVehicle(Vehicle vehicle) {
if(!(vehicle instanceof AbstractVehicle)) throw new IllegalStateException("vehicle must be an AbstractVehicle");
return addVehicle((AbstractVehicle)vehicle);
}
/**
* Adds a vehicle.
*
*
* @param vehicle
* @return this builder
*/
public Builder addVehicle(AbstractVehicle vehicle) {
if(!uniqueVehicles.contains(vehicle)){
vehicle.setIndex(vehicleIndexCounter);
incVehicleIndexCounter();
}
uniqueVehicles.add(vehicle); uniqueVehicles.add(vehicle);
if(!vehicleTypes.contains(vehicle.getType())){ if(!vehicleTypes.contains(vehicle.getType())){
vehicleTypes.add(vehicle.getType()); vehicleTypes.add(vehicle.getType());
@ -288,6 +353,10 @@ public class VehicleRoutingProblem {
return this; return this;
} }
private void incVehicleIndexCounter() {
vehicleIndexCounter++;
}
/** /**
* Sets the activity-costs. * Sets the activity-costs.
* *
@ -323,14 +392,14 @@ public class VehicleRoutingProblem {
addPenaltyVehicles(); addPenaltyVehicles();
} }
} }
for(Job job : tentativeJobs.values()){ for(Job job : tentativeJobs.values())
if(!jobsInInitialRoutes.contains(job.getId())){ if (!jobsInInitialRoutes.contains(job.getId())) {
addJobToFinalJobMap(job); addJobToFinalJobMapAndCreateActivities(job);
}
} }
return new VehicleRoutingProblem(this); return new VehicleRoutingProblem(this);
} }
private void addPenaltyVehicles() { private void addPenaltyVehicles() {
Set<VehicleTypeKey> vehicleTypeKeys = new HashSet<VehicleTypeKey>(); Set<VehicleTypeKey> vehicleTypeKeys = new HashSet<VehicleTypeKey>();
List<Vehicle> uniqueVehicles = new ArrayList<Vehicle>(); List<Vehicle> uniqueVehicles = new ArrayList<Vehicle>();
@ -552,6 +621,9 @@ public class VehicleRoutingProblem {
private final Locations locations; private final Locations locations;
private Map<Job,List<TourActivity>> activityMap;
// private List<List<TourActivity>> activityList;
private VehicleRoutingProblem(Builder builder) { private VehicleRoutingProblem(Builder builder) {
this.jobs = builder.jobs; this.jobs = builder.jobs;
this.fleetSize = builder.fleetSize; this.fleetSize = builder.fleetSize;
@ -562,6 +634,7 @@ public class VehicleRoutingProblem {
this.activityCosts = builder.activityCosts; this.activityCosts = builder.activityCosts;
this.constraints = builder.constraints; this.constraints = builder.constraints;
this.locations = builder.getLocations(); this.locations = builder.getLocations();
this.activityMap = builder.activityMap;
logger.info("initialise " + this); logger.info("initialise " + this);
} }
@ -646,4 +719,16 @@ public class VehicleRoutingProblem {
return locations; return locations;
} }
public List<TourActivity> 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());
}
return acts;
}
} }

View file

@ -18,6 +18,8 @@ package jsprit.core.problem.job;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.HasId;
import jsprit.core.problem.HasIndex;
/** /**
* Basic interface for all jobs. * Basic interface for all jobs.
@ -25,7 +27,7 @@ import jsprit.core.problem.Capacity;
* @author schroeder * @author schroeder
* *
*/ */
public interface Job { public interface Job extends HasId, HasIndex {
/** /**
* Returns the unique identifier (id) of a job. * Returns the unique identifier (id) of a job.

View file

@ -16,6 +16,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.job; package jsprit.core.problem.job;
import jsprit.core.problem.AbstractJob;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.solution.route.activity.TimeWindow; import jsprit.core.problem.solution.route.activity.TimeWindow;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
@ -31,7 +32,7 @@ import jsprit.core.util.Coordinate;
* @author schroeder * @author schroeder
* *
*/ */
public class Service implements Job { public class Service extends AbstractJob {
/** /**
* Builder that builds a service. * Builder that builds a service.

View file

@ -18,6 +18,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.job; package jsprit.core.problem.job;
import jsprit.core.problem.AbstractJob;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.solution.route.activity.TimeWindow; import jsprit.core.problem.solution.route.activity.TimeWindow;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
@ -39,7 +40,7 @@ import jsprit.core.util.Coordinate;
* @author schroeder * @author schroeder
* *
*/ */
public class Shipment implements Job{ public class Shipment extends AbstractJob{
/** /**
* Builder that builds the shipment. * Builder that builds the shipment.

View file

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

View file

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

View file

@ -18,10 +18,11 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.solution.route.activity; package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Delivery; import jsprit.core.problem.job.Delivery;
public final class DeliverService implements DeliveryActivity{ public final class DeliverService extends AbstractTourActivity implements DeliveryActivity{
private Delivery delivery; private Delivery delivery;
@ -42,6 +43,7 @@ public final class DeliverService implements DeliveryActivity{
this.arrTime=deliveryActivity.getArrTime(); this.arrTime=deliveryActivity.getArrTime();
this.endTime=deliveryActivity.getEndTime(); this.endTime=deliveryActivity.getEndTime();
capacity = deliveryActivity.getSize(); capacity = deliveryActivity.getSize();
setIndex(deliveryActivity.getIndex());
} }
@Override @Override

View file

@ -18,11 +18,12 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.solution.route.activity; package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Shipment; import jsprit.core.problem.job.Shipment;
public final class DeliverShipment implements DeliveryActivity{ public final class DeliverShipment extends AbstractTourActivity implements DeliveryActivity{
private Shipment shipment; private Shipment shipment;
@ -38,11 +39,13 @@ public final class DeliverShipment implements DeliveryActivity{
this.capacity = Capacity.invert(shipment.getSize()); this.capacity = Capacity.invert(shipment.getSize());
} }
@Deprecated
public DeliverShipment(DeliverShipment deliveryShipmentActivity) { public DeliverShipment(DeliverShipment deliveryShipmentActivity) {
this.shipment = (Shipment) deliveryShipmentActivity.getJob(); this.shipment = (Shipment) deliveryShipmentActivity.getJob();
this.arrTime = deliveryShipmentActivity.getArrTime(); this.arrTime = deliveryShipmentActivity.getArrTime();
this.endTime = deliveryShipmentActivity.getEndTime(); this.endTime = deliveryShipmentActivity.getEndTime();
this.capacity = deliveryShipmentActivity.getSize(); this.capacity = deliveryShipmentActivity.getSize();
setIndex(deliveryShipmentActivity.getIndex());
} }
@Override @Override

View file

@ -16,11 +16,13 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.solution.route.activity; package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
public final class End implements TourActivity { public final class End extends AbstractTourActivity implements TourActivity {
@Deprecated
public static int creation = 0; public static int creation = 0;
public static End newInstance(String locationId, double earliestArrival, double latestArrival) { public static End newInstance(String locationId, double earliestArrival, double latestArrival) {
@ -69,6 +71,7 @@ public final class End implements TourActivity {
theoretical_earliestOperationStartTime = theoreticalStart; theoretical_earliestOperationStartTime = theoreticalStart;
theoretical_latestOperationStartTime = theoreticalEnd; theoretical_latestOperationStartTime = theoreticalEnd;
endTime = theoreticalEnd; endTime = theoreticalEnd;
setIndex(-2);
} }
public End(End end) { public End(End end) {
@ -77,6 +80,7 @@ public final class End implements TourActivity {
theoretical_latestOperationStartTime = end.getTheoreticalLatestOperationStartTime(); theoretical_latestOperationStartTime = end.getTheoreticalLatestOperationStartTime();
arrTime = end.getArrTime(); arrTime = end.getArrTime();
endTime = end.getEndTime(); endTime = end.getEndTime();
setIndex(-2);
} }
public double getTheoreticalEarliestOperationStartTime() { public double getTheoreticalEarliestOperationStartTime() {

View file

@ -18,11 +18,12 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.solution.route.activity; package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Pickup; import jsprit.core.problem.job.Pickup;
import jsprit.core.problem.job.Service; import jsprit.core.problem.job.Service;
public final class PickupService implements PickupActivity{ public final class PickupService extends AbstractTourActivity implements PickupActivity{
private Service pickup; private Service pickup;
@ -43,6 +44,7 @@ public final class PickupService implements PickupActivity{
this.pickup=pickupActivity.getJob(); this.pickup=pickupActivity.getJob();
this.arrTime=pickupActivity.getArrTime(); this.arrTime=pickupActivity.getArrTime();
this.depTime=pickupActivity.getEndTime(); this.depTime=pickupActivity.getEndTime();
setIndex(pickupActivity.getIndex());
} }
@Override @Override

View file

@ -18,11 +18,12 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.solution.route.activity; package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Shipment; import jsprit.core.problem.job.Shipment;
public final class PickupShipment implements PickupActivity{ public final class PickupShipment extends AbstractTourActivity implements PickupActivity{
private Shipment shipment; private Shipment shipment;
@ -35,10 +36,12 @@ public final class PickupShipment implements PickupActivity{
this.shipment = shipment; this.shipment = shipment;
} }
@Deprecated
public PickupShipment(PickupShipment pickupShipmentActivity) { public PickupShipment(PickupShipment pickupShipmentActivity) {
this.shipment = (Shipment) pickupShipmentActivity.getJob(); this.shipment = (Shipment) pickupShipmentActivity.getJob();
this.arrTime = pickupShipmentActivity.getArrTime(); this.arrTime = pickupShipmentActivity.getArrTime();
this.endTime = pickupShipmentActivity.getEndTime(); this.endTime = pickupShipmentActivity.getEndTime();
setIndex(pickupShipmentActivity.getIndex());
} }
@Override @Override

View file

@ -16,11 +16,12 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.solution.route.activity; package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.job.Service; import jsprit.core.problem.job.Service;
import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity; import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity;
public class ServiceActivity implements JobActivity{ public class ServiceActivity extends AbstractTourActivity implements JobActivity{
public static int counter = 0; public static int counter = 0;
@ -77,6 +78,7 @@ public class ServiceActivity implements JobActivity{
this.service = serviceActivity.getJob(); this.service = serviceActivity.getJob();
this.arrTime = serviceActivity.getArrTime(); this.arrTime = serviceActivity.getArrTime();
this.endTime = serviceActivity.getEndTime(); this.endTime = serviceActivity.getEndTime();
setIndex(serviceActivity.getIndex());
} }

View file

@ -16,12 +16,14 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.solution.route.activity; package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.AbstractTourActivity;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
public final class Start implements TourActivity { public final class Start extends AbstractTourActivity implements TourActivity {
public final static String ACTIVITY_NAME = "start"; public final static String ACTIVITY_NAME = "start";
@Deprecated
public static int creation; public static int creation;
private final static Capacity capacity = Capacity.Builder.newInstance().build(); private final static Capacity capacity = Capacity.Builder.newInstance().build();
@ -51,7 +53,7 @@ public final class Start implements TourActivity {
this.theoretical_earliestOperationStartTime = theoreticalStart; this.theoretical_earliestOperationStartTime = theoreticalStart;
this.theoretical_latestOperationStartTime = theoreticalEnd; this.theoretical_latestOperationStartTime = theoreticalEnd;
this.endTime = theoreticalStart; this.endTime = theoreticalStart;
setIndex(-1);
} }
private Start(Start start) { private Start(Start start) {
@ -59,6 +61,7 @@ public final class Start implements TourActivity {
theoretical_earliestOperationStartTime = start.getTheoreticalEarliestOperationStartTime(); theoretical_earliestOperationStartTime = start.getTheoreticalEarliestOperationStartTime();
theoretical_latestOperationStartTime = start.getTheoreticalLatestOperationStartTime(); theoretical_latestOperationStartTime = start.getTheoreticalLatestOperationStartTime();
endTime = start.getEndTime(); endTime = start.getEndTime();
setIndex(-1);
} }
public double getTheoreticalEarliestOperationStartTime() { public double getTheoreticalEarliestOperationStartTime() {

View file

@ -17,6 +17,7 @@
package jsprit.core.problem.solution.route.activity; package jsprit.core.problem.solution.route.activity;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.HasIndex;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
/** /**
@ -27,7 +28,7 @@ import jsprit.core.problem.job.Job;
* @author schroeder * @author schroeder
* *
*/ */
public interface TourActivity { public interface TourActivity extends HasIndex {
/** /**
* Basic interface of job-activies. * Basic interface of job-activies.

View file

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

View file

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

View file

@ -16,6 +16,8 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.vehicle; package jsprit.core.problem.vehicle;
import jsprit.core.problem.HasId;
import jsprit.core.problem.HasIndex;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
/** /**
@ -24,7 +26,7 @@ import jsprit.core.util.Coordinate;
* @author schroeder * @author schroeder
* *
*/ */
public interface Vehicle { public interface Vehicle extends HasId, HasIndex {
/** /**
* Returns the earliest departure of vehicle which should be the lower bound of this vehicle's departure times. * Returns the earliest departure of vehicle which should be the lower bound of this vehicle's departure times.

View file

@ -16,8 +16,8 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem.vehicle; package jsprit.core.problem.vehicle;
import jsprit.core.problem.AbstractVehicle;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -28,7 +28,7 @@ import org.apache.log4j.Logger;
* *
*/ */
public class VehicleImpl implements Vehicle { public class VehicleImpl extends AbstractVehicle{
/** /**
* Extension of {@link VehicleImpl} representing an unspecified vehicle with the id 'noVehicle' * Extension of {@link VehicleImpl} representing an unspecified vehicle with the id 'noVehicle'

View file

@ -18,11 +18,6 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.recreate; package jsprit.core.algorithm.recreate;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import jsprit.core.algorithm.recreate.listener.InsertionListeners; import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.algorithm.state.StateManager; import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
@ -38,6 +33,7 @@ import jsprit.core.problem.job.Pickup;
import jsprit.core.problem.job.Shipment; import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.misc.JobInsertionContext; import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.DeliverService;
import jsprit.core.problem.solution.route.activity.TourActivity; 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.vehicle.Vehicle; import jsprit.core.problem.vehicle.Vehicle;
@ -45,10 +41,17 @@ import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType; import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl; import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.CostFactory; import jsprit.core.util.CostFactory;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ServiceInsertionAndLoadConstraintsTest { public class ServiceInsertionAndLoadConstraintsTest {
@ -84,6 +87,8 @@ public class ServiceInsertionAndLoadConstraintsTest {
ShipmentInsertionCalculator insertionCalculator; ShipmentInsertionCalculator insertionCalculator;
VehicleRoutingProblem vehicleRoutingProblem;
Vehicle vehicle; Vehicle vehicle;
@Before @Before
@ -93,6 +98,7 @@ public class ServiceInsertionAndLoadConstraintsTest {
vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("0,0").setType(type).build(); vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("0,0").setType(type).build();
activityInsertionCostsCalculator = new LocalActivityInsertionCostsCalculator(routingCosts, activityCosts); activityInsertionCostsCalculator = new LocalActivityInsertionCostsCalculator(routingCosts, activityCosts);
createInsertionCalculator(hardRouteLevelConstraint); createInsertionCalculator(hardRouteLevelConstraint);
vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
} }
private void createInsertionCalculator(HardRouteStateLevelConstraint hardRouteLevelConstraint) { private void createInsertionCalculator(HardRouteStateLevelConstraint hardRouteLevelConstraint) {
@ -112,8 +118,10 @@ public class ServiceInsertionAndLoadConstraintsTest {
VehicleRoute route = VehicleRoute.emptyRoute(); VehicleRoute route = VehicleRoute.emptyRoute();
route.setVehicleAndDepartureTime(vehicle, 0.0); route.setVehicleAndDepartureTime(vehicle, 0.0);
Inserter inserter = new Inserter(new InsertionListeners()); Inserter inserter = new Inserter(new InsertionListeners(), vehicleRoutingProblem);
List<TourActivity> acts = new ArrayList<TourActivity>();
acts.add(new DeliverService(delivery));
when(vehicleRoutingProblem.copyAndGetActivities(delivery)).thenReturn(acts);
inserter.insertJob(delivery, new InsertionData(0,0,0,vehicle,null), route); inserter.insertJob(delivery, new InsertionData(0,0,0,vehicle,null), route);
VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class); VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);

View file

@ -18,21 +18,11 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.recreate; package jsprit.core.algorithm.recreate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import jsprit.core.algorithm.recreate.listener.InsertionListeners; import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.algorithm.state.StateManager; import jsprit.core.algorithm.state.StateManager;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager; import jsprit.core.problem.constraint.*;
import jsprit.core.problem.constraint.ConstraintManager.Priority; import jsprit.core.problem.constraint.ConstraintManager.Priority;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint;
import jsprit.core.problem.constraint.HardRouteStateLevelConstraint;
import jsprit.core.problem.constraint.PickupAndDeliverShipmentLoadActivityLevelConstraint;
import jsprit.core.problem.constraint.ShipmentPickupsFirstConstraint;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts; import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts; import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
@ -42,6 +32,8 @@ import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment; import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.misc.JobInsertionContext; import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.DeliverShipment;
import jsprit.core.problem.solution.route.activity.PickupShipment;
import jsprit.core.problem.solution.route.activity.TourActivity; 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.vehicle.Vehicle; import jsprit.core.problem.vehicle.Vehicle;
@ -49,15 +41,25 @@ import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType; import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl; import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.CostFactory; import jsprit.core.util.CostFactory;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ShipmentInsertionCalculatorTest { public class ShipmentInsertionCalculatorTest {
VehicleRoutingTransportCosts routingCosts; VehicleRoutingTransportCosts routingCosts;
VehicleRoutingProblem vehicleRoutingProblem;
VehicleRoutingActivityCosts activityCosts = new VehicleRoutingActivityCosts(){ VehicleRoutingActivityCosts activityCosts = new VehicleRoutingActivityCosts(){
@Override @Override
@ -97,6 +99,7 @@ public class ShipmentInsertionCalculatorTest {
vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("0,0").setType(type).build(); vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("0,0").setType(type).build();
activityInsertionCostsCalculator = new LocalActivityInsertionCostsCalculator(routingCosts, activityCosts); activityInsertionCostsCalculator = new LocalActivityInsertionCostsCalculator(routingCosts, activityCosts);
createInsertionCalculator(hardRouteLevelConstraint); createInsertionCalculator(hardRouteLevelConstraint);
vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
} }
private void createInsertionCalculator(HardRouteStateLevelConstraint hardRouteLevelConstraint) { private void createInsertionCalculator(HardRouteStateLevelConstraint hardRouteLevelConstraint) {
@ -119,7 +122,8 @@ public class ShipmentInsertionCalculatorTest {
Shipment shipment = Shipment.Builder.newInstance("s").addSizeDimension(0, 1).setPickupLocation("0,10").setDeliveryLocation("10,0").build(); Shipment shipment = Shipment.Builder.newInstance("s").addSizeDimension(0, 1).setPickupLocation("0,10").setDeliveryLocation("10,0").build();
Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build(); Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build();
VehicleRoute route = VehicleRoute.emptyRoute(); VehicleRoute route = VehicleRoute.emptyRoute();
new Inserter(new InsertionListeners()).insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route); when(vehicleRoutingProblem.copyAndGetActivities(shipment)).thenReturn(getTourActivities(shipment));
new Inserter(new InsertionListeners(), vehicleRoutingProblem).insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route);
InsertionData iData = insertionCalculator.getInsertionData(route, shipment2, vehicle, 0.0, null, Double.MAX_VALUE); InsertionData iData = insertionCalculator.getInsertionData(route, shipment2, vehicle, 0.0, null, Double.MAX_VALUE);
assertEquals(0.0,iData.getInsertionCost(),0.05); assertEquals(0.0,iData.getInsertionCost(),0.05);
@ -127,12 +131,22 @@ public class ShipmentInsertionCalculatorTest {
assertEquals(2,iData.getDeliveryInsertionIndex()); assertEquals(2,iData.getDeliveryInsertionIndex());
} }
private List<TourActivity> getTourActivities(Shipment shipment) {
List<TourActivity> acts = new ArrayList<TourActivity>();
PickupShipment pick = new PickupShipment(shipment);
DeliverShipment del = new DeliverShipment(shipment);
acts.add(pick);
acts.add(del);
return acts;
}
@Test @Test
public void whenInsertingShipmentInRouteWithNotEnoughCapacity_itShouldReturnNoInsertion(){ public void whenInsertingShipmentInRouteWithNotEnoughCapacity_itShouldReturnNoInsertion(){
Shipment shipment = Shipment.Builder.newInstance("s").addSizeDimension(0, 1).setPickupLocation("0,10").setDeliveryLocation("10,0").build(); Shipment shipment = Shipment.Builder.newInstance("s").addSizeDimension(0, 1).setPickupLocation("0,10").setDeliveryLocation("10,0").build();
Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build(); Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build();
VehicleRoute route = VehicleRoute.emptyRoute(); VehicleRoute route = VehicleRoute.emptyRoute();
new Inserter(new InsertionListeners()).insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route); when(vehicleRoutingProblem.copyAndGetActivities(shipment)).thenReturn(getTourActivities(shipment));
new Inserter(new InsertionListeners(), vehicleRoutingProblem).insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route);
createInsertionCalculator(new HardRouteStateLevelConstraint() { createInsertionCalculator(new HardRouteStateLevelConstraint() {
@Override @Override
@ -154,7 +168,9 @@ public class ShipmentInsertionCalculatorTest {
Shipment shipment3 = Shipment.Builder.newInstance("s3").addSizeDimension(0, 1).setPickupLocation("0,0").setDeliveryLocation("9,10").build(); Shipment shipment3 = Shipment.Builder.newInstance("s3").addSizeDimension(0, 1).setPickupLocation("0,0").setDeliveryLocation("9,10").build();
VehicleRoute route = VehicleRoute.emptyRoute(); VehicleRoute route = VehicleRoute.emptyRoute();
Inserter inserter = new Inserter(new InsertionListeners()); when(vehicleRoutingProblem.copyAndGetActivities(shipment)).thenReturn(getTourActivities(shipment));
when(vehicleRoutingProblem.copyAndGetActivities(shipment2)).thenReturn(getTourActivities(shipment2));
Inserter inserter = new Inserter(new InsertionListeners(),vehicleRoutingProblem );
inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route); inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route);
inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null),route); inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null),route);
@ -169,9 +185,10 @@ public class ShipmentInsertionCalculatorTest {
Shipment shipment = Shipment.Builder.newInstance("s").addSizeDimension(0, 1).setPickupLocation("0,10").setDeliveryLocation("10,0").build(); Shipment shipment = Shipment.Builder.newInstance("s").addSizeDimension(0, 1).setPickupLocation("0,10").setDeliveryLocation("10,0").build();
Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build(); Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build();
Shipment shipment3 = Shipment.Builder.newInstance("s3").addSizeDimension(0, 1).setPickupLocation("0,0").setDeliveryLocation("9,9").build(); Shipment shipment3 = Shipment.Builder.newInstance("s3").addSizeDimension(0, 1).setPickupLocation("0,0").setDeliveryLocation("9,9").build();
when(vehicleRoutingProblem.copyAndGetActivities(shipment)).thenReturn(getTourActivities(shipment));
when(vehicleRoutingProblem.copyAndGetActivities(shipment2)).thenReturn(getTourActivities(shipment2));
VehicleRoute route = VehicleRoute.emptyRoute(); VehicleRoute route = VehicleRoute.emptyRoute();
Inserter inserter = new Inserter(new InsertionListeners()); Inserter inserter = new Inserter(new InsertionListeners(), vehicleRoutingProblem);
inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route); inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route);
inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null),route); inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null),route);
@ -187,12 +204,13 @@ public class ShipmentInsertionCalculatorTest {
Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build(); Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build();
Shipment shipment3 = Shipment.Builder.newInstance("s3").addSizeDimension(0, 1).setPickupLocation("0,0").setDeliveryLocation("9,9").build(); Shipment shipment3 = Shipment.Builder.newInstance("s3").addSizeDimension(0, 1).setPickupLocation("0,0").setDeliveryLocation("9,9").build();
when(vehicleRoutingProblem.copyAndGetActivities(shipment)).thenReturn(getTourActivities(shipment));
when(vehicleRoutingProblem.copyAndGetActivities(shipment2)).thenReturn(getTourActivities(shipment2));
VehicleRoute route = VehicleRoute.emptyRoute(); VehicleRoute route = VehicleRoute.emptyRoute();
route.setVehicleAndDepartureTime(vehicle, 0.0); route.setVehicleAndDepartureTime(vehicle, 0.0);
Inserter inserter = new Inserter(new InsertionListeners()); Inserter inserter = new Inserter(new InsertionListeners(), vehicleRoutingProblem);
inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route); inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route);
inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null), route); inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null), route);
@ -222,12 +240,12 @@ public class ShipmentInsertionCalculatorTest {
Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build(); Shipment shipment2 = Shipment.Builder.newInstance("s2").addSizeDimension(0, 1).setPickupLocation("10,10").setDeliveryLocation("0,0").build();
VehicleRoute route = VehicleRoute.emptyRoute(); VehicleRoute route = VehicleRoute.emptyRoute();
route.setVehicleAndDepartureTime(vehicle, 0.0); route.setVehicleAndDepartureTime(vehicle, 0.0);
when(vehicleRoutingProblem.copyAndGetActivities(shipment)).thenReturn(getTourActivities(shipment));
Inserter inserter = new Inserter(new InsertionListeners()); when(vehicleRoutingProblem.copyAndGetActivities(shipment2)).thenReturn(getTourActivities(shipment2));
Inserter inserter = new Inserter(new InsertionListeners(), vehicleRoutingProblem);
inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route); inserter.insertJob(shipment, new InsertionData(0,0,0,vehicle,null), route);
inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null), route); inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null), route);
// inserter.insertJob(shipment2, new InsertionData(0,1,2,vehicle,null), route);
VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class); VehicleRoutingProblem vrp = mock(VehicleRoutingProblem.class);
@ -237,8 +255,6 @@ public class ShipmentInsertionCalculatorTest {
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager); ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
constraintManager.addLoadConstraint(); constraintManager.addLoadConstraint();
// constraintManager.addConstraint(new PickupAndDeliverShipmentLoadActivityLevelConstraint(stateManager),Priority.CRITICAL);
// constraintManager.addConstraint(new ShipmentPickupsFirstConstraint(),Priority.CRITICAL);
stateManager.informInsertionStarts(Arrays.asList(route), null); stateManager.informInsertionStarts(Arrays.asList(route), null);

View file

@ -16,17 +16,10 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.recreate; package jsprit.core.algorithm.recreate;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import jsprit.core.algorithm.ExampleActivityCostFunction; import jsprit.core.algorithm.ExampleActivityCostFunction;
import jsprit.core.algorithm.state.StateManager; import jsprit.core.algorithm.state.StateManager;
import jsprit.core.algorithm.state.UpdateVariableCosts; import jsprit.core.algorithm.state.UpdateVariableCosts;
import jsprit.core.problem.AbstractVehicle;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.constraint.ConstraintManager; import jsprit.core.problem.constraint.ConstraintManager;
@ -45,12 +38,19 @@ import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleType; import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
import jsprit.core.util.ManhattanDistanceCalculator; import jsprit.core.util.ManhattanDistanceCalculator;
import org.apache.log4j.Level; import org.apache.log4j.Level;
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.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -60,9 +60,9 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
VehicleRoutingTransportCosts costs; VehicleRoutingTransportCosts costs;
Vehicle vehicle; AbstractVehicle vehicle;
Vehicle newVehicle; AbstractVehicle newVehicle;
private Service first; private Service first;
@ -79,7 +79,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
Logger.getRootLogger().setLevel(Level.DEBUG); Logger.getRootLogger().setLevel(Level.DEBUG);
costs = mock(VehicleRoutingTransportCosts.class); costs = mock(VehicleRoutingTransportCosts.class);
vehicle = mock(Vehicle.class); vehicle = mock(AbstractVehicle.class);
VehicleType type = mock(VehicleType.class); VehicleType type = mock(VehicleType.class);
when(type.getCapacityDimensions()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 1000).build()); when(type.getCapacityDimensions()).thenReturn(Capacity.Builder.newInstance().addDimension(0, 1000).build());
@ -91,7 +91,7 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
when(vehicle.getLatestArrival()).thenReturn(100.0); when(vehicle.getLatestArrival()).thenReturn(100.0);
when(vehicle.isReturnToDepot()).thenReturn(true); when(vehicle.isReturnToDepot()).thenReturn(true);
newVehicle = mock(Vehicle.class); newVehicle = mock(AbstractVehicle.class);
when(newVehicle.getType()).thenReturn(type); when(newVehicle.getType()).thenReturn(type);
when(newVehicle.getStartLocationId()).thenReturn("0,0"); when(newVehicle.getStartLocationId()).thenReturn("0,0");
when(newVehicle.getEndLocationId()).thenReturn("0,0"); when(newVehicle.getEndLocationId()).thenReturn("0,0");

View file

@ -18,25 +18,34 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.algorithm.recreate; package jsprit.core.algorithm.recreate;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import jsprit.core.algorithm.recreate.listener.InsertionListeners; import jsprit.core.algorithm.recreate.listener.InsertionListeners;
import jsprit.core.algorithm.state.UpdateEndLocationIfRouteIsOpen; import jsprit.core.algorithm.state.UpdateEndLocationIfRouteIsOpen;
import jsprit.core.problem.Capacity; import jsprit.core.problem.Capacity;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Service; import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment; import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.VehicleRoute; 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.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType; import jsprit.core.problem.vehicle.VehicleType;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TestInserter { public class TestInserter {
@Test @Test
public void whenInsertingServiceAndRouteIsClosed_itInsertsCorrectly(){ public void whenInsertingServiceAndRouteIsClosed_itInsertsCorrectly(){
Service service = mock(Service.class); Service service = mock(Service.class);
@ -55,7 +64,12 @@ public class TestInserter {
when(iData.getDeliveryInsertionIndex()).thenReturn(1); when(iData.getDeliveryInsertionIndex()).thenReturn(1);
when(iData.getSelectedVehicle()).thenReturn(vehicle); when(iData.getSelectedVehicle()).thenReturn(vehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class)); VehicleRoutingProblem vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
List<TourActivity> acts = new ArrayList<TourActivity>();
PickupService act = new PickupService(serviceToInsert);
acts.add(act);
when(vehicleRoutingProblem.copyAndGetActivities(serviceToInsert)).thenReturn(acts);
Inserter inserter = new Inserter(mock(InsertionListeners.class), vehicleRoutingProblem);
inserter.insertJob(serviceToInsert, iData, route); inserter.insertJob(serviceToInsert, iData, route);
assertEquals(2,route.getTourActivities().getActivities().size()); assertEquals(2,route.getTourActivities().getActivities().size());
@ -80,7 +94,10 @@ public class TestInserter {
when(iData.getDeliveryInsertionIndex()).thenReturn(1); when(iData.getDeliveryInsertionIndex()).thenReturn(1);
when(iData.getSelectedVehicle()).thenReturn(vehicle); when(iData.getSelectedVehicle()).thenReturn(vehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class)); VehicleRoutingProblem vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
when(vehicleRoutingProblem.copyAndGetActivities(serviceToInsert)).thenReturn(getTourActivities(serviceToInsert));
Inserter inserter = new Inserter(mock(InsertionListeners.class),vehicleRoutingProblem);
inserter.insertJob(serviceToInsert, iData, route); inserter.insertJob(serviceToInsert, iData, route);
assertEquals(2,route.getTourActivities().getActivities().size()); assertEquals(2,route.getTourActivities().getActivities().size());
@ -88,6 +105,12 @@ public class TestInserter {
assertEquals(route.getEnd().getLocationId(),serviceToInsert.getLocationId()); assertEquals(route.getEnd().getLocationId(),serviceToInsert.getLocationId());
} }
private List<TourActivity> getTourActivities(Service serviceToInsert) {
List<TourActivity> acts = new ArrayList<TourActivity>();
acts.add(new PickupService(serviceToInsert));
return acts;
}
@Test @Test
public void whenInsertingShipmentAndRouteIsClosed_itInsertsCorrectly(){ public void whenInsertingShipmentAndRouteIsClosed_itInsertsCorrectly(){
@ -102,16 +125,20 @@ public class TestInserter {
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build(); VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build();
//start - pick(shipment) - del(shipment) - end //start - pick(shipment) - del(shipment) - end
Shipment shipmentToInsert = mock(Shipment.class); Shipment shipmentToInsert = Shipment.Builder.newInstance("s").setDeliveryLocation("delLoc").setPickupLocation("pickLoc").build();
when(shipmentToInsert.getSize()).thenReturn(capacity); // Shipment shipmentToInsert = mock(Shipment.class);
when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc"); // when(shipmentToInsert.getSize()).thenReturn(capacity);
when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc"); // when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc");
// when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc");
// when(shipmentToInsert.getSize()).thenReturn(Capacity.Builder.newInstance().build());
InsertionData iData = mock(InsertionData.class); InsertionData iData = mock(InsertionData.class);
when(iData.getPickupInsertionIndex()).thenReturn(2); when(iData.getPickupInsertionIndex()).thenReturn(2);
when(iData.getDeliveryInsertionIndex()).thenReturn(2); when(iData.getDeliveryInsertionIndex()).thenReturn(2);
when(iData.getSelectedVehicle()).thenReturn(vehicle); when(iData.getSelectedVehicle()).thenReturn(vehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class)); VehicleRoutingProblem vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
when(vehicleRoutingProblem.copyAndGetActivities(shipmentToInsert)).thenReturn(getTourActivities(shipmentToInsert));
Inserter inserter = new Inserter(mock(InsertionListeners.class), vehicleRoutingProblem);
inserter.insertJob(shipmentToInsert, iData, route); inserter.insertJob(shipmentToInsert, iData, route);
assertEquals(4,route.getTourActivities().getActivities().size()); assertEquals(4,route.getTourActivities().getActivities().size());
@ -120,6 +147,13 @@ public class TestInserter {
assertEquals(route.getEnd().getLocationId(),vehicle.getEndLocationId()); assertEquals(route.getEnd().getLocationId(),vehicle.getEndLocationId());
} }
private List<TourActivity> getTourActivities(Shipment shipmentToInsert) {
List<TourActivity> acts = new ArrayList<TourActivity>();
acts.add(new PickupShipment(shipmentToInsert));
acts.add(new DeliverShipment(shipmentToInsert));
return acts;
}
@Test @Test
public void whenInsertingShipmentAndRouteIsOpen_itInsertsCorrectlyAndSwitchesEndLocation(){ public void whenInsertingShipmentAndRouteIsOpen_itInsertsCorrectlyAndSwitchesEndLocation(){
Shipment shipment = mock(Shipment.class); Shipment shipment = mock(Shipment.class);
@ -131,16 +165,15 @@ public class TestInserter {
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build(); VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build();
//start - pick(shipment) - del(shipment) - end //start - pick(shipment) - del(shipment) - end
Shipment shipmentToInsert = mock(Shipment.class); Shipment shipmentToInsert = Shipment.Builder.newInstance("s").setDeliveryLocation("delLoc").setPickupLocation("pickLoc").build();
when(shipmentToInsert.getSize()).thenReturn(capacity);
when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc");
when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc");
InsertionData iData = mock(InsertionData.class); InsertionData iData = mock(InsertionData.class);
when(iData.getPickupInsertionIndex()).thenReturn(2); when(iData.getPickupInsertionIndex()).thenReturn(2);
when(iData.getDeliveryInsertionIndex()).thenReturn(2); when(iData.getDeliveryInsertionIndex()).thenReturn(2);
when(iData.getSelectedVehicle()).thenReturn(vehicle); when(iData.getSelectedVehicle()).thenReturn(vehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class)); VehicleRoutingProblem vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
when(vehicleRoutingProblem.copyAndGetActivities(shipmentToInsert)).thenReturn(getTourActivities(shipmentToInsert));
Inserter inserter = new Inserter(mock(InsertionListeners.class), vehicleRoutingProblem);
inserter.insertJob(shipmentToInsert, iData, route); inserter.insertJob(shipmentToInsert, iData, route);
assertEquals(4,route.getTourActivities().getActivities().size()); assertEquals(4,route.getTourActivities().getActivities().size());
@ -159,17 +192,16 @@ public class TestInserter {
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build(); VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build();
//start - pick(shipment) - del(shipment) - end //start - pick(shipment) - del(shipment) - end
Shipment shipmentToInsert = mock(Shipment.class); Shipment shipmentToInsert = Shipment.Builder.newInstance("s").setPickupLocation("pickLoc").setDeliveryLocation("delLoc").build();
when(shipmentToInsert.getSize()).thenReturn(capacity);
when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc");
when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc");
InsertionData iData = mock(InsertionData.class); InsertionData iData = mock(InsertionData.class);
when(iData.getPickupInsertionIndex()).thenReturn(2); when(iData.getPickupInsertionIndex()).thenReturn(2);
when(iData.getDeliveryInsertionIndex()).thenReturn(2); when(iData.getDeliveryInsertionIndex()).thenReturn(2);
when(iData.getSelectedVehicle()).thenReturn(newVehicle); when(iData.getSelectedVehicle()).thenReturn(newVehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class)); VehicleRoutingProblem vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
when(vehicleRoutingProblem.copyAndGetActivities(shipmentToInsert)).thenReturn(getTourActivities(shipmentToInsert));
Inserter inserter = new Inserter(mock(InsertionListeners.class), vehicleRoutingProblem);
inserter.insertJob(shipmentToInsert, iData, route); inserter.insertJob(shipmentToInsert, iData, route);
assertEquals(route.getEnd().getLocationId(),newVehicle.getEndLocationId()); assertEquals(route.getEnd().getLocationId(),newVehicle.getEndLocationId());
@ -185,17 +217,16 @@ public class TestInserter {
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build(); VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build();
//start - pick(shipment) - del(shipment) - end //start - pick(shipment) - del(shipment) - end
Shipment shipmentToInsert = mock(Shipment.class); Shipment shipmentToInsert = Shipment.Builder.newInstance("s").setPickupLocation("pickLoc").setDeliveryLocation("delLoc").build();
when(shipmentToInsert.getSize()).thenReturn(capacity);
when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc");
when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc");
InsertionData iData = mock(InsertionData.class); InsertionData iData = mock(InsertionData.class);
when(iData.getPickupInsertionIndex()).thenReturn(2); when(iData.getPickupInsertionIndex()).thenReturn(2);
when(iData.getDeliveryInsertionIndex()).thenReturn(2); when(iData.getDeliveryInsertionIndex()).thenReturn(2);
when(iData.getSelectedVehicle()).thenReturn(newVehicle); when(iData.getSelectedVehicle()).thenReturn(newVehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class)); VehicleRoutingProblem vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
when(vehicleRoutingProblem.copyAndGetActivities(shipmentToInsert)).thenReturn(getTourActivities(shipmentToInsert));
Inserter inserter = new Inserter(mock(InsertionListeners.class),vehicleRoutingProblem );
inserter.insertJob(shipmentToInsert, iData, route); inserter.insertJob(shipmentToInsert, iData, route);
assertEquals("delLoc",route.getEnd().getLocationId()); assertEquals("delLoc",route.getEnd().getLocationId());
@ -212,17 +243,16 @@ public class TestInserter {
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build(); VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build();
//start - pick(shipment) - del(shipment) - end //start - pick(shipment) - del(shipment) - end
Shipment shipmentToInsert = mock(Shipment.class); Shipment shipmentToInsert = Shipment.Builder.newInstance("s").setPickupLocation("pickLoc").setDeliveryLocation("delLoc").build();
when(shipmentToInsert.getSize()).thenReturn(capacity);
when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc");
when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc");
InsertionData iData = mock(InsertionData.class); InsertionData iData = mock(InsertionData.class);
when(iData.getPickupInsertionIndex()).thenReturn(0); when(iData.getPickupInsertionIndex()).thenReturn(0);
when(iData.getDeliveryInsertionIndex()).thenReturn(0); when(iData.getDeliveryInsertionIndex()).thenReturn(0);
when(iData.getSelectedVehicle()).thenReturn(newVehicle); when(iData.getSelectedVehicle()).thenReturn(newVehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class)); VehicleRoutingProblem vehicleRoutingProblem = mock(VehicleRoutingProblem.class);
when(vehicleRoutingProblem.copyAndGetActivities(shipmentToInsert)).thenReturn(getTourActivities(shipmentToInsert));
Inserter inserter = new Inserter(mock(InsertionListeners.class),vehicleRoutingProblem );
inserter.insertJob(shipmentToInsert, iData, route); inserter.insertJob(shipmentToInsert, iData, route);
UpdateEndLocationIfRouteIsOpen updateEnd = new UpdateEndLocationIfRouteIsOpen(); UpdateEndLocationIfRouteIsOpen updateEnd = new UpdateEndLocationIfRouteIsOpen();

View file

@ -16,37 +16,27 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.problem; package jsprit.core.problem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import jsprit.core.problem.VehicleRoutingProblem.FleetSize; import jsprit.core.problem.VehicleRoutingProblem.FleetSize;
import jsprit.core.problem.constraint.Constraint; import jsprit.core.problem.constraint.Constraint;
import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts; import jsprit.core.problem.cost.AbstractForwardVehicleRoutingTransportCosts;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts; import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.driver.DriverImpl; import jsprit.core.problem.driver.DriverImpl;
import jsprit.core.problem.job.Delivery; import jsprit.core.problem.job.*;
import jsprit.core.problem.job.Pickup;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity; import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.PenaltyVehicleType; import jsprit.core.problem.vehicle.*;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.Coordinate; import jsprit.core.util.Coordinate;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class VehicleRoutingProblemTest { public class VehicleRoutingProblemTest {
@ -186,8 +176,10 @@ public class VehicleRoutingProblemTest {
public void whenDelivieriesAreAdded_vrpShouldContainThem(){ public void whenDelivieriesAreAdded_vrpShouldContainThem(){
Delivery s1 = mock(Delivery.class); Delivery s1 = mock(Delivery.class);
when(s1.getId()).thenReturn("s1"); when(s1.getId()).thenReturn("s1");
when(s1.getSize()).thenReturn(Capacity.Builder.newInstance().build());
Delivery s2 = mock(Delivery.class); Delivery s2 = mock(Delivery.class);
when(s2.getId()).thenReturn("s2"); when(s2.getId()).thenReturn("s2");
when(s2.getSize()).thenReturn(Capacity.Builder.newInstance().build());
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.addJob(s1).addJob(s2); vrpBuilder.addJob(s1).addJob(s2);
@ -203,8 +195,10 @@ public class VehicleRoutingProblemTest {
public void whenDelivieriesAreAddedAllAtOnce_vrpShouldContainThem(){ public void whenDelivieriesAreAddedAllAtOnce_vrpShouldContainThem(){
Delivery s1 = mock(Delivery.class); Delivery s1 = mock(Delivery.class);
when(s1.getId()).thenReturn("s1"); when(s1.getId()).thenReturn("s1");
when(s1.getSize()).thenReturn(Capacity.Builder.newInstance().build());
Delivery s2 = mock(Delivery.class); Delivery s2 = mock(Delivery.class);
when(s2.getId()).thenReturn("s2"); when(s2.getId()).thenReturn("s2");
when(s2.getSize()).thenReturn(Capacity.Builder.newInstance().build());
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.addAllJobs(Arrays.asList(s1,s2)); vrpBuilder.addAllJobs(Arrays.asList(s1,s2));
@ -541,4 +535,33 @@ public class VehicleRoutingProblemTest {
VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
assertFalse(vrp.getJobs().containsKey("myService")); assertFalse(vrp.getJobs().containsKey("myService"));
} }
@Test
public void whenAddingTwoJobs_theyShouldHaveProperIndeces(){
Job service = Service.Builder.newInstance("myService").setLocationId("loc").build();
Job shipment = Shipment.Builder.newInstance("shipment").setPickupLocation("pick").setDeliveryLocation("del").build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.addJob(service);
vrpBuilder.addJob(shipment);
vrpBuilder.build();
assertEquals(0,service.getIndex());
assertEquals(1,shipment.getIndex());
}
@Test
public void whenAddingTwoVehicles_theyShouldHaveProperIndeces(){
Vehicle veh1 = VehicleImpl.Builder.newInstance("v1").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build();
Vehicle veh2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("start").setStartLocationCoordinate(Coordinate.newInstance(0, 1)).setEndLocationId("end").build();
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
vrpBuilder.addVehicle(veh1);
vrpBuilder.addVehicle(veh2);
vrpBuilder.build();
assertEquals(0,veh1.getIndex());
assertEquals(1,veh2.getIndex());
}
} }

View file

@ -16,12 +16,9 @@
******************************************************************************/ ******************************************************************************/
package jsprit.examples; package jsprit.examples;
import java.util.Collection;
import jsprit.analysis.toolbox.GraphStreamViewer; import jsprit.analysis.toolbox.GraphStreamViewer;
import jsprit.analysis.toolbox.GraphStreamViewer.Label; import jsprit.analysis.toolbox.GraphStreamViewer.Label;
import jsprit.analysis.toolbox.Plotter; import jsprit.analysis.toolbox.Plotter;
import jsprit.analysis.toolbox.SolutionPrinter; import jsprit.analysis.toolbox.SolutionPrinter;
import jsprit.analysis.toolbox.SolutionPrinter.Print; import jsprit.analysis.toolbox.SolutionPrinter.Print;
import jsprit.core.algorithm.VehicleRoutingAlgorithm; import jsprit.core.algorithm.VehicleRoutingAlgorithm;
@ -29,9 +26,13 @@ import jsprit.core.algorithm.io.VehicleRoutingAlgorithms;
import jsprit.core.algorithm.selector.SelectBest; import jsprit.core.algorithm.selector.SelectBest;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.instance.reader.SolomonReader; import jsprit.instance.reader.SolomonReader;
import jsprit.util.Examples; import jsprit.util.Examples;
import java.util.Collection;
public class SolomonExample { public class SolomonExample {
@ -81,6 +82,13 @@ public class SolomonExample {
*/ */
VehicleRoutingProblemSolution solution = new SelectBest().selectSolution(solutions); VehicleRoutingProblemSolution solution = new SelectBest().selectSolution(solutions);
for(VehicleRoute r : solution.getRoutes()){
System.out.println(r.getStart() + ";" + r.getStart().getIndex());
for(TourActivity act : r.getActivities()){
System.out.println(act + ";" + act.getIndex());
}
System.out.println(r.getEnd() + ";" + r.getEnd().getIndex());
}
/* /*
* print solution * print solution
*/ */