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

Merge branch 'master' into constraints

This commit is contained in:
oblonski 2013-12-02 21:00:07 +01:00
commit 3335f235a3
30 changed files with 4851 additions and 58 deletions

View file

@ -22,6 +22,7 @@ import java.util.List;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
@ -33,10 +34,10 @@ final class AuxilliaryCostCalculator {
private final VehicleRoutingActivityCosts activityCosts;
public AuxilliaryCostCalculator(final VehicleRoutingTransportCosts routingCosts, final VehicleRoutingActivityCosts costFunction) {
public AuxilliaryCostCalculator(final VehicleRoutingTransportCosts routingCosts, final VehicleRoutingActivityCosts actCosts) {
super();
this.routingCosts = routingCosts;
this.activityCosts = costFunction;
this.activityCosts = actCosts;
}
/**
@ -59,6 +60,11 @@ final class AuxilliaryCostCalculator {
double departureTimePrevAct = depTime;
while(actIter.hasNext()){
TourActivity act = actIter.next();
if(act instanceof End){
if(!vehicle.isReturnToDepot()){
return cost;
}
}
double transportCost = routingCosts.getTransportCost(prevAct.getLocationId(), act.getLocationId(), departureTimePrevAct, driver, vehicle);
double transportTime = routingCosts.getTransportTime(prevAct.getLocationId(), act.getLocationId(), departureTimePrevAct, driver, vehicle);
cost += transportCost;
@ -72,37 +78,5 @@ final class AuxilliaryCostCalculator {
return cost;
}
public double costOfPath(String startLocationId, final double startTime, final List<TourActivity> path, String endLocationId, final Driver driver, final Vehicle vehicle){
if(path.isEmpty()){
return 0.0;
}
double cost = 0.0;
// Iterator<TourActivity> actIter = path.iterator();
String prevActLocation = startLocationId;
// TourActivity prevAct = actIter.next();
double startCost = 0.0;
cost += startCost;
double departureTimePrevAct = startTime;
for(TourActivity act : path){
// TourActivity act = actIter.next();
double transportCost = routingCosts.getTransportCost(prevActLocation, act.getLocationId(), departureTimePrevAct, driver, vehicle);
double transportTime = routingCosts.getTransportTime(prevActLocation, act.getLocationId(), departureTimePrevAct, driver, vehicle);
cost += transportCost;
double actStartTime = departureTimePrevAct + transportTime;
double earliestOperationStartTime = Math.max(actStartTime, act.getTheoreticalEarliestOperationStartTime());
double actEndTime = earliestOperationStartTime + act.getOperationTime();
departureTimePrevAct = actEndTime;
cost += activityCosts.getActivityCost(act, actStartTime, driver, vehicle);
prevActLocation = act.getLocationId();
}
/*
*!!! ENDLOCATION
=> Start u. End können primitiv sein.
*/
return cost;
}
}

View file

@ -60,12 +60,21 @@ class Inserter {
@Override
public void handleJobInsertion(Job job, InsertionData iData, VehicleRoute route) {
if(job instanceof Service){
if(!iData.getSelectedVehicle().isReturnToDepot()){
if(iData.getDeliveryInsertionIndex()>=route.getTourActivities().getActivities().size()){
setEndLocation(route,(Service)job);
}
}
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), this.activityFactory.createActivity((Service)job));
route.setDepartureTime(iData.getVehicleDepartureTime());
}
else delegator.handleJobInsertion(job, iData, route);
}
private void setEndLocation(VehicleRoute route, Service service) {
route.getEnd().setLocationId(service.getLocationId());
}
public void setNextHandler(JobInsertionHandler jobInsertionHandler){
this.delegator = jobInsertionHandler;
}
@ -83,6 +92,11 @@ class Inserter {
if(job instanceof Shipment){
TourActivity pickupShipment = this.activityFactory.createPickup((Shipment)job);
TourActivity deliverShipment = this.activityFactory.createDelivery((Shipment)job);
if(!iData.getSelectedVehicle().isReturnToDepot()){
if(iData.getDeliveryInsertionIndex()>=route.getTourActivities().getActivities().size()){
setEndLocation(route,(Shipment)job);
}
}
route.getTourActivities().addActivity(iData.getDeliveryInsertionIndex(), deliverShipment);
route.getTourActivities().addActivity(iData.getPickupInsertionIndex(), pickupShipment);
route.setDepartureTime(iData.getVehicleDepartureTime());
@ -90,6 +104,10 @@ class Inserter {
else delegator.handleJobInsertion(job, iData, route);
}
private void setEndLocation(VehicleRoute route, Shipment shipment) {
route.getEnd().setLocationId(shipment.getDeliveryLocation());
}
public void setNextHandler(JobInsertionHandler jobInsertionHandler){
this.delegator = jobInsertionHandler;
}

View file

@ -23,6 +23,7 @@ package jsprit.core.algorithm.recreate;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.util.CalculationUtils;
@ -61,6 +62,13 @@ class LocalActivityInsertionCostsCalculator implements ActivityInsertionCostsCal
double act_costs_newAct = activityCosts.getActivityCost(newAct, newAct_arrTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
//open routes
if(nextAct instanceof End){
if(!iFacts.getNewVehicle().isReturnToDepot()){
return new ActivityInsertionCosts(tp_costs_prevAct_newAct, tp_time_prevAct_newAct);
}
}
double tp_costs_newAct_nextAct = routingCosts.getTransportCost(newAct.getLocationId(), nextAct.getLocationId(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
double tp_time_newAct_nextAct = routingCosts.getTransportTime(newAct.getLocationId(), nextAct.getLocationId(), newAct_endTime, iFacts.getNewDriver(), iFacts.getNewVehicle());

View file

@ -230,14 +230,19 @@ public class VrpXMLReader{
if(vehicle == null) throw new IllegalStateException("vehicle is missing.");
String start = routeConfig.getString("start");
if(start == null) throw new IllegalStateException("route start-time is missing.");
double departureTime = Double.parseDouble(start);
String end = routeConfig.getString("end");
if(end == null) throw new IllegalStateException("route end-time is missing.");
Start startAct = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
startAct.setEndTime(Double.parseDouble(start));
// Start startAct = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
// startAct.setEndTime(Double.parseDouble(start));
End endAct = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
endAct.setArrTime(Double.parseDouble(end));
VehicleRoute.Builder routeBuilder = VehicleRoute.Builder.newInstance(vehicle, driver);
routeBuilder.setDepartureTime(departureTime);
routeBuilder.setRouteEndArrivalTime(Double.parseDouble(end));
List<HierarchicalConfiguration> actConfigs = routeConfig.configurationsAt("act");
for(HierarchicalConfiguration actConfig : actConfigs){
String type = actConfig.getString("[@type]");
@ -466,6 +471,10 @@ public class VrpXMLReader{
String end = vehicleConfig.getString("timeSchedule.end");
if(start != null) builder.setEarliestStart(Double.parseDouble(start));
if(end != null) builder.setLatestArrival(Double.parseDouble(end));
String returnToDepot = vehicleConfig.getString("returnToDepot");
if(returnToDepot != null){
builder.setReturnToDepot(vehicleConfig.getBoolean("returnToDepot"));
}
VehicleImpl vehicle = builder.build();
vrpBuilder.addVehicle(vehicle);
vehicleMap.put(vehicleId, vehicle);

View file

@ -234,6 +234,7 @@ public class VrpXMLWriter {
xmlConfig.setProperty(vehiclePathString + "("+counter+").timeSchedule.start", vehicle.getEarliestDeparture());
xmlConfig.setProperty(vehiclePathString + "("+counter+").timeSchedule.end", vehicle.getLatestArrival());
xmlConfig.setProperty(vehiclePathString + "("+counter+").returnToDepot", vehicle.isReturnToDepot());
counter++;
}

View file

@ -61,6 +61,8 @@ public class VehicleRoute {
private Start start;
private End end;
private TourActivities tourActivities = new TourActivities();
private TourActivityFactory serviceActivityFactory = new DefaultTourActivityFactory();
@ -79,6 +81,10 @@ public class VehicleRoute {
/**
* Constructs the route-builder.
*
* <p>Default startLocation is vehicle.getLocationId()<br>
* Default departureTime is vehicle.getEarliestDeparture()<br>
* Default endLocation is either vehicle.getLocationId() or (if !vehicle.isReturnToDepot()) last specified activityLocation
* @param vehicle
* @param driver
*/
@ -88,7 +94,7 @@ public class VehicleRoute {
this.driver = driver;
start = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
start.setEndTime(vehicle.getEarliestDeparture());
End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
end = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
}
/**
@ -102,6 +108,11 @@ public class VehicleRoute {
return this;
}
public Builder setRouteEndArrivalTime(double endTime){
end.setArrTime(endTime);
return this;
}
public Builder addService(Service service){
addService(service,0.0,0.0);
return this;
@ -185,7 +196,12 @@ public class VehicleRoute {
if(!openShipments.isEmpty()){
throw new IllegalStateException("there are still shipments that have not been delivered yet.");
}
VehicleRoute route = VehicleRoute.newInstance(tourActivities, driver, vehicle);
if(!vehicle.isReturnToDepot()){
if(!tourActivities.isEmpty()){
end.setLocationId(tourActivities.getActivities().get(tourActivities.getActivities().size()-1).getLocationId());
}
}
VehicleRoute route = new VehicleRoute(this);
return route;
}
@ -218,13 +234,14 @@ public class VehicleRoute {
setStartAndEnd(vehicle, vehicle.getEarliestDeparture());
}
// private VehicleRoute(Builder builder){
// this.tourActivities = builder.tour;
// this.vehicle = builder.vehicle;
// this.driver = builder.driver;
// this.start = builder.start;
// this.end = builder.end;
// }
private VehicleRoute(Builder builder){
this.tourActivities = builder.tourActivities;
this.vehicle = builder.vehicle;
this.driver = builder.driver;
this.start = builder.start;
this.end = builder.end;
}
private void verify(TourActivities tour, Driver driver, Vehicle vehicle) {
if(tour == null || driver == null || vehicle == null) throw new IllegalStateException("null is not allowed for tour, driver or vehicle. use emptyRoute. use Tour.emptyTour, DriverImpl.noDriver() and VehicleImpl.noVehicle() instead." +

View file

@ -35,11 +35,11 @@ public final class End implements TourActivity {
private Coordinate coordinate;
public Coordinate getCoordinate() {
Coordinate getCoordinate() {
return coordinate;
}
public void setCoordinate(Coordinate coordinate) {
void setCoordinate(Coordinate coordinate) {
this.coordinate = coordinate;
}

View file

@ -65,11 +65,11 @@ public final class Start implements TourActivity {
public Coordinate getCoordinate() {
Coordinate getCoordinate() {
return coordinate;
}
public void setCoordinate(Coordinate coordinate) {
void setCoordinate(Coordinate coordinate) {
this.coordinate = coordinate;
}

View file

@ -35,5 +35,7 @@ public interface Vehicle {
public abstract String getId();
public abstract int getCapacity();
public abstract boolean isReturnToDepot();
}

View file

@ -45,6 +45,14 @@ public class VehicleImpl implements Vehicle {
}
/**
* builds the vehicle.
*
* <p>by default, it returns to the depot.
*
* @author stefan
*
*/
public static class Builder {
static Logger log = Logger.getLogger(Builder.class);
private String id;
@ -54,6 +62,8 @@ public class VehicleImpl implements Vehicle {
private double earliestStart = 0.0;
private double latestArrival = Double.MAX_VALUE;
private boolean returnToDepot = true;
private VehicleType type = VehicleTypeImpl.Builder.newInstance("default", 0).build();
private Builder(String id) {
@ -66,6 +76,11 @@ public class VehicleImpl implements Vehicle {
return this;
}
public Builder setReturnToDepot(boolean returnToDepot){
this.returnToDepot = returnToDepot;
return this;
}
public Builder setLocationId(String id){
this.locationId = id;
return this;
@ -113,6 +128,8 @@ public class VehicleImpl implements Vehicle {
private final double earliestDeparture;
private final double latestArrival;
private boolean returnToDepot;
private VehicleImpl(Builder builder){
id = builder.id;
@ -121,6 +138,7 @@ public class VehicleImpl implements Vehicle {
locationId = builder.locationId;
earliestDeparture = builder.earliestStart;
latestArrival = builder.latestArrival;
returnToDepot = builder.returnToDepot;
}
@ -193,5 +211,16 @@ public class VehicleImpl implements Vehicle {
public int getCapacity() {
return type.getCapacity();
}
/**
* @return the returnToDepot
*/
public boolean isReturnToDepot() {
return returnToDepot;
}
}

View file

@ -53,7 +53,7 @@
</xs:element>
<xs:element name="typeId" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="timeSchedule" type="timeWindowType"/>
<xs:element name="returnToDepot" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:element>

View file

@ -0,0 +1,103 @@
package jsprit.core.algorithm.recreate;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
import org.junit.Before;
import org.junit.Test;
public class TestAuxilliaryCostCalculator {
private VehicleRoutingTransportCosts routingCosts;
private VehicleRoutingActivityCosts actCosts;
private Vehicle vehicle;
@Before
public void doBefore(){
vehicle = mock(Vehicle.class);
routingCosts = mock(VehicleRoutingTransportCosts.class);
actCosts = mock(VehicleRoutingActivityCosts.class);
when(routingCosts.getTransportCost("i", "j", 0.0, null, vehicle)).thenReturn(2.0);
when(routingCosts.getTransportTime("i", "j", 0.0, null, vehicle)).thenReturn(0.0);
when(routingCosts.getTransportCost("i", "k", 0.0, null, vehicle)).thenReturn(3.0);
when(routingCosts.getTransportTime("i", "k", 0.0, null, vehicle)).thenReturn(0.0);
when(routingCosts.getTransportCost("k", "j", 0.0, null, vehicle)).thenReturn(3.0);
when(routingCosts.getTransportTime("k", "j", 0.0, null, vehicle)).thenReturn(0.0);
}
@Test
public void whenRouteIsClosed_itCalculatesCostUpToEnd_v1(){
TourActivity prevAct = mock(TourActivity.class);
when(prevAct.getLocationId()).thenReturn("i");
TourActivity nextAct = mock(TourActivity.class);
when(nextAct.getLocationId()).thenReturn("j");
TourActivity newAct = mock(TourActivity.class);
when(newAct.getLocationId()).thenReturn("k");
when(vehicle.isReturnToDepot()).thenReturn(true);
AuxilliaryCostCalculator aCalc = new AuxilliaryCostCalculator(routingCosts, actCosts);
double costs = aCalc.costOfPath(Arrays.asList(prevAct,newAct,nextAct), 0.0, null, vehicle);
assertEquals(6.0,costs,0.01);
}
@Test
public void whenRouteIsClosed_itCalculatesCostUpToEnd_v2(){
TourActivity prevAct = mock(TourActivity.class);
when(prevAct.getLocationId()).thenReturn("i");
End nextAct = End.newInstance("j", 0.0, 0.0);
TourActivity newAct = mock(TourActivity.class);
when(newAct.getLocationId()).thenReturn("k");
when(vehicle.isReturnToDepot()).thenReturn(true);
AuxilliaryCostCalculator aCalc = new AuxilliaryCostCalculator(routingCosts, actCosts);
double costs = aCalc.costOfPath(Arrays.asList(prevAct,newAct,nextAct), 0.0, null, vehicle);
assertEquals(6.0,costs,0.01);
}
@Test
public void whenRouteIsOpen_itCalculatesCostUpToEnd_v1(){
TourActivity prevAct = mock(TourActivity.class);
when(prevAct.getLocationId()).thenReturn("i");
TourActivity nextAct = mock(TourActivity.class);
when(nextAct.getLocationId()).thenReturn("j");
TourActivity newAct = mock(TourActivity.class);
when(newAct.getLocationId()).thenReturn("k");
when(vehicle.isReturnToDepot()).thenReturn(false);
AuxilliaryCostCalculator aCalc = new AuxilliaryCostCalculator(routingCosts, actCosts);
double costs = aCalc.costOfPath(Arrays.asList(prevAct,newAct,nextAct), 0.0, null, vehicle);
assertEquals(6.0,costs,0.01);
}
@Test
public void whenRouteIsOpen_itCalculatesCostUpToEnd_v2(){
TourActivity prevAct = mock(TourActivity.class);
when(prevAct.getLocationId()).thenReturn("i");
End nextAct = End.newInstance("j", 0.0, 0.0);
TourActivity newAct = mock(TourActivity.class);
when(newAct.getLocationId()).thenReturn("k");
when(vehicle.isReturnToDepot()).thenReturn(false);
AuxilliaryCostCalculator aCalc = new AuxilliaryCostCalculator(routingCosts, actCosts);
double costs = aCalc.costOfPath(Arrays.asList(prevAct,newAct,nextAct), 0.0, null, vehicle);
assertEquals(3.0,costs,0.01);
}
}

View file

@ -16,6 +16,7 @@
******************************************************************************/
package jsprit.core.algorithm.recreate;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -80,12 +81,14 @@ public class TestCalculatesServiceInsertion {
when(vehicle.getLocationId()).thenReturn("depot");
when(vehicle.getEarliestDeparture()).thenReturn(0.0);
when(vehicle.getLatestArrival()).thenReturn(100.0);
when(vehicle.isReturnToDepot()).thenReturn(true);
newVehicle = mock(Vehicle.class);
when(newVehicle.getCapacity()).thenReturn(1000);
when(newVehicle.getLocationId()).thenReturn("depot");
when(newVehicle.getEarliestDeparture()).thenReturn(0.0);
when(newVehicle.getLatestArrival()).thenReturn(100.0);
when(newVehicle.isReturnToDepot()).thenReturn(true);
driver = DriverImpl.noDriver();

View file

@ -17,6 +17,7 @@
package jsprit.core.algorithm.recreate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -82,12 +83,14 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
when(vehicle.getLocationId()).thenReturn("0,0");
when(vehicle.getEarliestDeparture()).thenReturn(0.0);
when(vehicle.getLatestArrival()).thenReturn(100.0);
when(vehicle.isReturnToDepot()).thenReturn(true);
newVehicle = mock(Vehicle.class);
when(newVehicle.getCapacity()).thenReturn(1000);
when(newVehicle.getLocationId()).thenReturn("0,0");
when(newVehicle.getEarliestDeparture()).thenReturn(0.0);
when(newVehicle.getLatestArrival()).thenReturn(100.0);
when(newVehicle.isReturnToDepot()).thenReturn(true);
driver = DriverImpl.noDriver();
@ -239,4 +242,5 @@ public class TestCalculatesServiceInsertionOnRouteLevel {
assertEquals(2, iData.getDeliveryInsertionIndex());
}
}

View file

@ -0,0 +1,122 @@
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.problem.driver.Driver;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.vehicle.Vehicle;
import org.junit.Test;
public class TestInserter {
@Test
public void whenInsertingServiceAndRouteIsClosed_itInsertsCorrectly(){
Service service = mock(Service.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(true);
when(vehicle.getId()).thenReturn("vehId");
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addService(service).build();
//start - pick(shipment) - del(shipment) - end
Service serviceToInsert = mock(Service.class);
when(serviceToInsert.getLocationId()).thenReturn("delLoc");
InsertionData iData = mock(InsertionData.class);
when(iData.getDeliveryInsertionIndex()).thenReturn(1);
when(iData.getSelectedVehicle()).thenReturn(vehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class));
inserter.insertJob(serviceToInsert, iData, route);
assertEquals(2,route.getTourActivities().getActivities().size());
assertEquals(route.getTourActivities().getActivities().get(1).getLocationId(),serviceToInsert.getLocationId());
assertEquals(route.getEnd().getLocationId(),vehicle.getLocationId());
}
@Test
public void whenInsertingServiceAndRouteIsOpen_itInsertsCorrectlyAndSwitchesEndLocation(){
Service service = mock(Service.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getId()).thenReturn("vehId");
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addService(service).build();
Service serviceToInsert = mock(Service.class);
when(serviceToInsert.getLocationId()).thenReturn("delLoc");
InsertionData iData = mock(InsertionData.class);
when(iData.getDeliveryInsertionIndex()).thenReturn(1);
when(iData.getSelectedVehicle()).thenReturn(vehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class));
inserter.insertJob(serviceToInsert, iData, route);
assertEquals(2,route.getTourActivities().getActivities().size());
assertEquals(route.getTourActivities().getActivities().get(1).getLocationId(),serviceToInsert.getLocationId());
assertEquals(route.getEnd().getLocationId(),serviceToInsert.getLocationId());
}
@Test
public void whenInsertingShipmentAndRouteIsClosed_itInsertsCorrectly(){
Shipment shipment = mock(Shipment.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(true);
when(vehicle.getId()).thenReturn("vehId");
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build();
//start - pick(shipment) - del(shipment) - end
Shipment shipmentToInsert = mock(Shipment.class);
when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc");
when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc");
InsertionData iData = mock(InsertionData.class);
when(iData.getPickupInsertionIndex()).thenReturn(2);
when(iData.getDeliveryInsertionIndex()).thenReturn(2);
when(iData.getSelectedVehicle()).thenReturn(vehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class));
inserter.insertJob(shipmentToInsert, iData, route);
assertEquals(4,route.getTourActivities().getActivities().size());
assertEquals(route.getTourActivities().getActivities().get(2).getLocationId(),shipmentToInsert.getPickupLocation());
assertEquals(route.getTourActivities().getActivities().get(3).getLocationId(),shipmentToInsert.getDeliveryLocation());
assertEquals(route.getEnd().getLocationId(),vehicle.getLocationId());
}
@Test
public void whenInsertingShipmentAndRouteIsOpen_itInsertsCorrectlyAndSwitchesEndLocation(){
Shipment shipment = mock(Shipment.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.getLocationId()).thenReturn("vehLoc");
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getId()).thenReturn("vehId");
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class)).addPickup(shipment).addDelivery(shipment).build();
//start - pick(shipment) - del(shipment) - end
Shipment shipmentToInsert = mock(Shipment.class);
when(shipmentToInsert.getDeliveryLocation()).thenReturn("delLoc");
when(shipmentToInsert.getPickupLocation()).thenReturn("pickLoc");
InsertionData iData = mock(InsertionData.class);
when(iData.getPickupInsertionIndex()).thenReturn(2);
when(iData.getDeliveryInsertionIndex()).thenReturn(2);
when(iData.getSelectedVehicle()).thenReturn(vehicle);
Inserter inserter = new Inserter(mock(InsertionListeners.class));
inserter.insertJob(shipmentToInsert, iData, route);
assertEquals(4,route.getTourActivities().getActivities().size());
assertEquals(route.getTourActivities().getActivities().get(2).getLocationId(),shipmentToInsert.getPickupLocation());
assertEquals(route.getTourActivities().getActivities().get(3).getLocationId(),shipmentToInsert.getDeliveryLocation());
assertEquals(route.getEnd().getLocationId(),shipmentToInsert.getDeliveryLocation());
}
}

View file

@ -0,0 +1,113 @@
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.ActivityInsertionCostsCalculator.ActivityInsertionCosts;
import jsprit.core.problem.cost.VehicleRoutingActivityCosts;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.End;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
import org.junit.Before;
import org.junit.Test;
public class TestLocalActivityInsertionCostsCalculator {
VehicleRoutingTransportCosts tpCosts;
VehicleRoutingActivityCosts actCosts;
LocalActivityInsertionCostsCalculator calc;
Vehicle vehicle;
VehicleRoute route;
JobInsertionContext jic;
@Before
public void doBefore(){
vehicle = mock(Vehicle.class);
route = mock(VehicleRoute.class);
when(route.isEmpty()).thenReturn(false);
when(route.getVehicle()).thenReturn(vehicle);
jic = mock(JobInsertionContext.class);
when(jic.getRoute()).thenReturn(route);
when(jic.getNewVehicle()).thenReturn(vehicle);
tpCosts = mock(VehicleRoutingTransportCosts.class);
when(tpCosts.getTransportCost("i", "j", 0.0, null, vehicle)).thenReturn(2.0);
when(tpCosts.getTransportTime("i", "j", 0.0, null, vehicle)).thenReturn(0.0);
when(tpCosts.getTransportCost("i", "k", 0.0, null, vehicle)).thenReturn(3.0);
when(tpCosts.getTransportTime("i", "k", 0.0, null, vehicle)).thenReturn(0.0);
when(tpCosts.getTransportCost("k", "j", 0.0, null, vehicle)).thenReturn(3.0);
when(tpCosts.getTransportTime("k", "j", 0.0, null, vehicle)).thenReturn(0.0);
actCosts = mock(VehicleRoutingActivityCosts.class);
calc = new LocalActivityInsertionCostsCalculator(tpCosts, actCosts);
}
@Test
public void whenInsertingActBetweenTwoRouteActs_itCalcsMarginalTpCosts(){
TourActivity prevAct = mock(TourActivity.class);
when(prevAct.getLocationId()).thenReturn("i");
TourActivity nextAct = mock(TourActivity.class);
when(nextAct.getLocationId()).thenReturn("j");
TourActivity newAct = mock(TourActivity.class);
when(newAct.getLocationId()).thenReturn("k");
when(vehicle.isReturnToDepot()).thenReturn(true);
ActivityInsertionCosts costs = calc.getCosts(jic, prevAct, nextAct, newAct, 0.0);
assertEquals(4.0,costs.getAdditionalCosts(),0.01);
}
@Test
public void whenInsertingActBetweenLastActAndEnd_itCalcsMarginalTpCosts(){
TourActivity prevAct = mock(TourActivity.class);
when(prevAct.getLocationId()).thenReturn("i");
End nextAct = End.newInstance("j", 0.0, 0.0);
TourActivity newAct = mock(TourActivity.class);
when(newAct.getLocationId()).thenReturn("k");
when(vehicle.isReturnToDepot()).thenReturn(true);
ActivityInsertionCosts costs = calc.getCosts(jic, prevAct, nextAct, newAct, 0.0);
assertEquals(4.0,costs.getAdditionalCosts(),0.01);
}
@Test
public void whenInsertingActBetweenTwoRouteActsAndRouteIsOpen_itCalcsMarginalTpCosts(){
TourActivity prevAct = mock(TourActivity.class);
when(prevAct.getLocationId()).thenReturn("i");
TourActivity nextAct = mock(TourActivity.class);
when(nextAct.getLocationId()).thenReturn("j");
TourActivity newAct = mock(TourActivity.class);
when(newAct.getLocationId()).thenReturn("k");
when(vehicle.isReturnToDepot()).thenReturn(false);
ActivityInsertionCosts costs = calc.getCosts(jic, prevAct, nextAct, newAct, 0.0);
assertEquals(4.0,costs.getAdditionalCosts(),0.01);
}
@Test
public void whenInsertingActBetweenLastActAndEndAndRouteIsOpen_itCalculatesTpCostsFromPrevToNewAct(){
TourActivity prevAct = mock(TourActivity.class);
when(prevAct.getLocationId()).thenReturn("i");
End nextAct = End.newInstance("j", 0.0, 0.0);
TourActivity newAct = mock(TourActivity.class);
when(newAct.getLocationId()).thenReturn("k");
when(vehicle.isReturnToDepot()).thenReturn(false);
ActivityInsertionCosts costs = calc.getCosts(jic, prevAct, nextAct, newAct, 0.0);
assertEquals(3.0,costs.getAdditionalCosts(),0.01);
}
}

View file

@ -2,6 +2,7 @@ package jsprit.core.problem.solution.route;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.vehicle.Vehicle;
@ -58,4 +59,74 @@ public class VehicleRouteBuilderTest {
assertEquals(4,route.getTourActivities().getActivities().size());
}
@Test
public void whenBuildingClosedRoute_routeEndShouldHaveLocationOfVehicle(){
Shipment s = mock(Shipment.class);
Shipment s2 = mock(Shipment.class);
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(true);
when(vehicle.getLocationId()).thenReturn("vehLoc");
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
builder.addDelivery(s);
builder.addDelivery(s2);
VehicleRoute route = builder.build();
assertEquals(route.getEnd().getLocationId(), vehicle.getLocationId());
}
@Test
public void whenBuildingOpenRoute_routeEndShouldHaveLocationOfLastActivity(){
Shipment s = mock(Shipment.class);
Shipment s2 = mock(Shipment.class);
when(s2.getDeliveryLocation()).thenReturn("delLoc");
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getLocationId()).thenReturn("vehLoc");
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
builder.addDelivery(s);
builder.addDelivery(s2);
VehicleRoute route = builder.build();
assertEquals(route.getEnd().getLocationId(), s2.getDeliveryLocation());
}
@Test
public void whenSettingDepartureTime(){
Shipment s = mock(Shipment.class);
Shipment s2 = mock(Shipment.class);
when(s2.getDeliveryLocation()).thenReturn("delLoc");
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getLocationId()).thenReturn("vehLoc");
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
builder.addDelivery(s);
builder.addDelivery(s2);
builder.setDepartureTime(100);
VehicleRoute route = builder.build();
assertEquals(100.0,route.getDepartureTime(),0.01);
assertEquals(100.0,route.getStart().getEndTime(),0.01);
}
@Test
public void whenSettingEndTime(){
Shipment s = mock(Shipment.class);
Shipment s2 = mock(Shipment.class);
when(s2.getDeliveryLocation()).thenReturn("delLoc");
Vehicle vehicle = mock(Vehicle.class);
when(vehicle.isReturnToDepot()).thenReturn(false);
when(vehicle.getLocationId()).thenReturn("vehLoc");
VehicleRoute.Builder builder = VehicleRoute.Builder.newInstance(vehicle, mock(Driver.class));
builder.addPickup(s);
builder.addPickup(s2);
builder.addDelivery(s);
builder.addDelivery(s2);
builder.setRouteEndArrivalTime(100.0);
VehicleRoute route = builder.build();
assertEquals(100.0,route.getEnd().getArrTime(),0.01);
}
}

View file

@ -16,6 +16,7 @@
<start>0.0</start>
<end>1.7976931348623157E308</end>
</timeSchedule>
<returnToDepot>true</returnToDepot>
</vehicle>
<vehicle>
<id>v2</id>
@ -27,6 +28,7 @@
<start>0.0</start>
<end>1.7976931348623157E308</end>
</timeSchedule>
<returnToDepot>true</returnToDepot>
</vehicle>
</vehicles>
<vehicleTypes>