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

added soft constraints to shipment calc

This commit is contained in:
oblonski 2014-02-10 17:44:36 +01:00
parent 941e4ae29b
commit 330fa937a0
8 changed files with 257 additions and 96 deletions

View file

@ -252,7 +252,7 @@ class CalculatorBuilder {
actInsertionCalc = activityInsertionCostCalculator;
}
ShipmentInsertionCalculator shipmentInsertion = new ShipmentInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager, constraintManager);
ShipmentInsertionCalculator shipmentInsertion = new ShipmentInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager);
ServiceInsertionCalculator serviceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();

View file

@ -19,7 +19,10 @@ package jsprit.core.algorithm.recreate;
import java.util.List;
import jsprit.core.algorithm.recreate.ActivityInsertionCostsCalculator.ActivityInsertionCosts;
import jsprit.core.problem.constraint.ConstraintManager;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint;
import jsprit.core.problem.constraint.SoftActivityConstraint;
import jsprit.core.problem.constraint.SoftRouteConstraint;
import jsprit.core.problem.constraint.HardActivityStateLevelConstraint.ConstraintsStatus;
import jsprit.core.problem.constraint.HardRouteStateLevelConstraint;
import jsprit.core.problem.cost.VehicleRoutingTransportCosts;
@ -36,7 +39,6 @@ import jsprit.core.problem.solution.route.activity.TourShipmentActivityFactory;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl.NoVehicle;
import jsprit.core.util.CalculationUtils;
import jsprit.core.util.Neighborhood;
import org.apache.log4j.Logger;
@ -51,23 +53,28 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
private HardActivityStateLevelConstraint hardActivityLevelConstraint;
private SoftRouteConstraint softRouteConstraint;
private SoftActivityConstraint softActivityConstraint;
private ActivityInsertionCostsCalculator activityInsertionCostsCalculator;
private VehicleRoutingTransportCosts transportCosts;
private TourShipmentActivityFactory activityFactory;
public void setNeighborhood(Neighborhood neighborhood) {
logger.info("initialise neighborhood " + neighborhood);
}
private AdditionalAccessEgressCalculator additionalAccessEgressCalculator;
public ShipmentInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ActivityInsertionCostsCalculator activityInsertionCostsCalculator, HardRouteStateLevelConstraint hardRouteLevelConstraint, HardActivityStateLevelConstraint hardActivityLevelConstraint) {
public ShipmentInsertionCalculator(VehicleRoutingTransportCosts routingCosts, ActivityInsertionCostsCalculator activityInsertionCostsCalculator, ConstraintManager constraintManager) {
super();
this.activityInsertionCostsCalculator = activityInsertionCostsCalculator;
this.hardRouteLevelConstraint = hardRouteLevelConstraint;
this.hardActivityLevelConstraint = hardActivityLevelConstraint;
this.hardRouteLevelConstraint = constraintManager;
this.hardActivityLevelConstraint = constraintManager;
this.softActivityConstraint = constraintManager;
this.softRouteConstraint = constraintManager;
this.transportCosts = routingCosts;
activityFactory = new DefaultShipmentActivityFactory();
additionalAccessEgressCalculator = new AdditionalAccessEgressCalculator(routingCosts);
logger.info("initialise " + this);
}
@ -93,6 +100,10 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
}
double bestCost = bestKnownCosts;
double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);
additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);
Shipment shipment = (Shipment)jobToInsert;
TourActivity pickupShipment = activityFactory.createPickup(shipment);
TourActivity deliverShipment = activityFactory.createDelivery(shipment);
@ -122,6 +133,7 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
pickupShipmentLoopBroken = true;
break;
}
double additionalPickupICosts = softActivityConstraint.getCosts(insertionContext, prevAct, pickupShipment, activities.get(i), prevActEndTime);
ActivityInsertionCosts pickupAIC = calculate(insertionContext,prevAct,pickupShipment,activities.get(i),prevActEndTime);
TourActivity prevAct_deliveryLoop = pickupShipment;
double shipmentPickupArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), pickupShipment.getLocationId(), prevActEndTime, newDriver, newVehicle);
@ -132,8 +144,10 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
for(int j=i;j<activities.size();j++){
ConstraintsStatus deliverShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct_deliveryLoop, deliverShipment, activities.get(j), prevActEndTime_deliveryLoop);
if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
double additionalDeliveryICosts = softActivityConstraint.getCosts(insertionContext, prevAct_deliveryLoop, deliverShipment, activities.get(j), prevActEndTime_deliveryLoop);
ActivityInsertionCosts deliveryAIC = calculate(insertionContext,prevAct_deliveryLoop,deliverShipment,activities.get(j),prevActEndTime_deliveryLoop);
double totalActivityInsertionCosts = pickupAIC.getAdditionalCosts() + deliveryAIC.getAdditionalCosts();
double totalActivityInsertionCosts = pickupAIC.getAdditionalCosts() + deliveryAIC.getAdditionalCosts()
+ additionalICostsAtRouteLevel + additionalPickupICosts + additionalDeliveryICosts;
if(totalActivityInsertionCosts < bestCost){
bestCost = totalActivityInsertionCosts;
pickupInsertionIndex = i;
@ -152,8 +166,10 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
if(!deliverShipmentLoopBroken){ //check insertion between lastAct and endOfTour
ConstraintsStatus deliverShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);
if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
double additionalDeliveryICosts = softActivityConstraint.getCosts(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);
ActivityInsertionCosts deliveryAIC = calculate(insertionContext,prevAct_deliveryLoop,deliverShipment,end,prevActEndTime_deliveryLoop);
double totalActivityInsertionCosts = pickupAIC.getAdditionalCosts() + deliveryAIC.getAdditionalCosts();
double totalActivityInsertionCosts = pickupAIC.getAdditionalCosts() + deliveryAIC.getAdditionalCosts()
+ additionalICostsAtRouteLevel + additionalPickupICosts + additionalDeliveryICosts;
if(totalActivityInsertionCosts < bestCost){
bestCost = totalActivityInsertionCosts;
pickupInsertionIndex = i;
@ -169,8 +185,8 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
if(!pickupShipmentLoopBroken){ //check insertion of pickupShipment and deliverShipment at just before tour ended
ConstraintsStatus pickupShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct, pickupShipment, end, prevActEndTime);
if(pickupShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
double additionalPickupICosts = softActivityConstraint.getCosts(insertionContext, prevAct, pickupShipment, end, prevActEndTime);
ActivityInsertionCosts pickupAIC = calculate(insertionContext,prevAct,pickupShipment,end,prevActEndTime);
TourActivity prevAct_deliveryLoop = pickupShipment;
double shipmentPickupArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), pickupShipment.getLocationId(), prevActEndTime, newDriver, newVehicle);
double shipmentPickupEndTime = CalculationUtils.getActivityEndTime(shipmentPickupArrTime, pickupShipment);
@ -178,8 +194,10 @@ final class ShipmentInsertionCalculator implements JobInsertionCostsCalculator{
ConstraintsStatus deliverShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);
if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
double additionalDeliveryICosts = softActivityConstraint.getCosts(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);
ActivityInsertionCosts deliveryAIC = calculate(insertionContext,prevAct_deliveryLoop,deliverShipment,end,prevActEndTime_deliveryLoop);
double totalActivityInsertionCosts = pickupAIC.getAdditionalCosts() + deliveryAIC.getAdditionalCosts();
double totalActivityInsertionCosts = pickupAIC.getAdditionalCosts() + deliveryAIC.getAdditionalCosts()
+ additionalICostsAtRouteLevel + additionalPickupICosts + additionalDeliveryICosts;
if(totalActivityInsertionCosts < bestCost){
bestCost = totalActivityInsertionCosts;
pickupInsertionIndex = activities.size();

View file

@ -28,6 +28,7 @@ import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
@ -97,7 +98,9 @@ public class ServiceInsertionAndLoadConstraintsTest {
}
private void createInsertionCalculator(HardRouteStateLevelConstraint hardRouteLevelConstraint) {
insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, hardActivityLevelConstraint);
ConstraintManager constraintManager = new ConstraintManager(mock(VehicleRoutingProblem.class), mock(RouteAndActivityStateGetter.class));
constraintManager.addConstraint(hardRouteLevelConstraint);
insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, constraintManager);
}
@Test
@ -137,7 +140,7 @@ public class ServiceInsertionAndLoadConstraintsTest {
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, activityInsertionCostsCalculator, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, constraintManager);
switcher.put(Pickup.class, serviceInsertionCalc);
switcher.put(Delivery.class, serviceInsertionCalc);
switcher.put(Shipment.class, insertionCalculator);

View file

@ -24,6 +24,7 @@ import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.misc.JobInsertionContext;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
@ -93,7 +94,9 @@ public class ShipmentInsertionCalculatorTest {
}
private void createInsertionCalculator(HardRouteStateLevelConstraint hardRouteLevelConstraint) {
insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, hardActivityLevelConstraint);
ConstraintManager constraintManager = new ConstraintManager(mock(VehicleRoutingProblem.class), mock(RouteAndActivityStateGetter.class));
constraintManager.addConstraint(hardRouteLevelConstraint);
insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, constraintManager);
}
@Test
@ -199,7 +202,7 @@ public class ShipmentInsertionCalculatorTest {
constraintManager.addConstraint(new ShipmentPickupsFirstConstraint(),Priority.CRITICAL);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator,
hardRouteLevelConstraint, constraintManager);
constraintManager);
InsertionData iData = insertionCalculator.getInsertionData(route, shipment3, vehicle, 0.0, DriverImpl.noDriver(), Double.MAX_VALUE);
@ -241,7 +244,7 @@ public class ShipmentInsertionCalculatorTest {
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
ServiceInsertionCalculator serviceInsertionCalc = new ServiceInsertionCalculator(routingCosts, activityInsertionCostsCalculator, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, hardRouteLevelConstraint, constraintManager);
ShipmentInsertionCalculator insertionCalculator = new ShipmentInsertionCalculator(routingCosts, activityInsertionCostsCalculator, constraintManager);
switcher.put(Pickup.class, serviceInsertionCalc);
switcher.put(Shipment.class, insertionCalculator);

View file

@ -45,8 +45,12 @@ import jsprit.core.problem.solution.route.activity.TourActivities;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.Coordinate;
import jsprit.core.util.EuclideanDistanceCalculator;
import jsprit.core.util.Locations;
import jsprit.core.util.ManhattanDistanceCalculator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
@ -85,80 +89,46 @@ public class TestCalculatesServiceInsertion {
costs = mock(VehicleRoutingTransportCosts.class);
vehicle = mock(Vehicle.class);
when(vehicle.getCapacity()).thenReturn(1000);
when(vehicle.getLocationId()).thenReturn("depot");
when(vehicle.getLocationId()).thenReturn("0,0");
when(vehicle.getEarliestDeparture()).thenReturn(0.0);
when(vehicle.getLatestArrival()).thenReturn(100.0);
when(vehicle.isReturnToDepot()).thenReturn(true);
VehicleType t1 = VehicleTypeImpl.Builder.newInstance("t1", 1000).setCostPerDistance(1.0).build();
vehicle = VehicleImpl.Builder.newInstance("vehicle").setLatestArrival(100.0).setLocationId("0,0").setType(t1).build();
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);
VehicleType t2 = VehicleTypeImpl.Builder.newInstance("t2", 1000).setCostPerDistance(2.0).build();
newVehicle = VehicleImpl.Builder.newInstance("newVehicle").setLatestArrival(100.0).setLocationId("0,0").setType(t2).build();
driver = DriverImpl.noDriver();
when(costs.getTransportCost("depot", "1", 0.0, driver, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("depot", "2", 0.0, driver, vehicle)).thenReturn(20.0);
when(costs.getTransportCost("depot", "3", 0.0, driver, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("1", "2", 0.0, driver, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("1", "3", 0.0, driver, vehicle)).thenReturn(20.0);
when(costs.getTransportCost("2", "3", 0.0, driver, vehicle)).thenReturn(10.0);
final Locations locations = new Locations(){
@Override
public Coordinate getCoord(String id) {
//assume: locationId="x,y"
String[] splitted = id.split(",");
return Coordinate.newInstance(Double.parseDouble(splitted[0]),
Double.parseDouble(splitted[1]));
}
};
costs = new AbstractForwardVehicleRoutingTransportCosts() {
@Override
public double getTransportTime(String fromId, String toId,double departureTime, Driver driver, Vehicle vehicle) {
return ManhattanDistanceCalculator.calculateDistance(locations.getCoord(fromId), locations.getCoord(toId));
}
@Override
public double getTransportCost(String fromId, String toId, double departureTime, Driver driver, Vehicle vehicle) {
return vehicle.getType().getVehicleCostParams().perDistanceUnit*ManhattanDistanceCalculator.calculateDistance(locations.getCoord(fromId), locations.getCoord(toId));
}
};
when(costs.getTransportCost("1", "depot", 0.0, driver, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("2", "depot", 0.0, driver, vehicle)).thenReturn(20.0);
when(costs.getTransportCost("3", "depot", 0.0, driver, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("2", "1", 0.0, driver, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("3", "1", 0.0, driver, vehicle)).thenReturn(20.0);
when(costs.getTransportCost("3", "2", 0.0, driver, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("depot", "1", 0.0, driver, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("depot", "2", 0.0, driver, newVehicle)).thenReturn(40.0);
when(costs.getTransportCost("depot", "3", 0.0, driver, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("1", "2", 0.0, driver, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("1", "3", 0.0, driver, newVehicle)).thenReturn(40.0);
when(costs.getTransportCost("2", "3", 0.0, driver, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("1", "depot", 0.0, driver, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("2", "depot", 0.0, driver, newVehicle)).thenReturn(40.0);
when(costs.getTransportCost("3", "depot", 0.0, driver, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("2", "1", 0.0, driver, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("3", "1", 0.0, driver, newVehicle)).thenReturn(40.0);
when(costs.getTransportCost("3", "2", 0.0, driver, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("depot", "1", 0.0, null, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("depot", "2", 0.0, null, vehicle)).thenReturn(20.0);
when(costs.getTransportCost("depot", "3", 0.0, null, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("1", "2", 0.0, null, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("1", "3", 0.0, null, vehicle)).thenReturn(20.0);
when(costs.getTransportCost("2", "3", 0.0, null, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("1", "depot", 0.0, null, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("2", "depot", 0.0, null, vehicle)).thenReturn(20.0);
when(costs.getTransportCost("3", "depot", 0.0, null, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("2", "1", 0.0, null, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("3", "1", 0.0, null, vehicle)).thenReturn(20.0);
when(costs.getTransportCost("3", "2", 0.0, null, vehicle)).thenReturn(10.0);
when(costs.getTransportCost("depot", "1", 0.0, null, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("depot", "2", 0.0, null, newVehicle)).thenReturn(40.0);
when(costs.getTransportCost("depot", "3", 0.0, null, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("1", "2", 0.0, null, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("1", "3", 0.0, null, newVehicle)).thenReturn(40.0);
when(costs.getTransportCost("2", "3", 0.0, null, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("1", "depot", 0.0, null, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("2", "depot", 0.0, null, newVehicle)).thenReturn(40.0);
when(costs.getTransportCost("3", "depot", 0.0, null, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("2", "1", 0.0, null, newVehicle)).thenReturn(20.0);
when(costs.getTransportCost("3", "1", 0.0, null, newVehicle)).thenReturn(40.0);
when(costs.getTransportCost("3", "2", 0.0, null, newVehicle)).thenReturn(20.0);
first = Service.Builder.newInstance("1", 0).setLocationId("1").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
second = Service.Builder.newInstance("3", 0).setLocationId("3").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
third = Service.Builder.newInstance("2", 0).setLocationId("2").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
first = Service.Builder.newInstance("1", 0).setLocationId("0,10").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
second = Service.Builder.newInstance("3", 0).setLocationId("10,0").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
third = Service.Builder.newInstance("2", 0).setLocationId("10,10").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
Collection<Job> jobs = new ArrayList<Job>();
jobs.add(first);
jobs.add(second);
@ -279,12 +249,11 @@ public class TestCalculatesServiceInsertion {
@Test
public void whenInsertingJobAndCurrRouteAndVehicleHaveTheSameLocation_accessEggressCalcShouldReturnZero(){
VehicleRoute route = VehicleRoute.Builder.newInstance(newVehicle, DriverImpl.noDriver())
.addService(Service.Builder.newInstance("1", 0).setLocationId("1").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build())
.addService(first)
.build();
AdditionalAccessEgressCalculator accessEgressCalc = new AdditionalAccessEgressCalculator(costs);
Job job = Service.Builder.newInstance("1", 0).setLocationId("1").setTimeWindow(TimeWindow.newInstance(0.0, 100.0)).build();
JobInsertionContext iContex = new JobInsertionContext(route, job, newVehicle, mock(Driver.class), 0.0);
JobInsertionContext iContex = new JobInsertionContext(route, first, newVehicle, mock(Driver.class), 0.0);
assertEquals(0.0, accessEgressCalc.getCosts(iContex),0.01);
}