From dc8adca385406fd36c45f591f99b6bb1788ebbbe Mon Sep 17 00:00:00 2001 From: Kandel Irina Date: Mon, 12 Mar 2018 08:35:04 +0200 Subject: [PATCH] fixes --- .../MaxTimeInVehicleConstraint.java | 15 +++--- .../core/algorithm/MaxTimeInVehicleTest.java | 52 ++++++++++++++----- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/constraint/MaxTimeInVehicleConstraint.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/constraint/MaxTimeInVehicleConstraint.java index e1f689e3..bc463474 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/constraint/MaxTimeInVehicleConstraint.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/constraint/MaxTimeInVehicleConstraint.java @@ -26,10 +26,7 @@ import com.graphhopper.jsprit.core.problem.cost.VehicleRoutingActivityCosts; import com.graphhopper.jsprit.core.problem.job.Job; import com.graphhopper.jsprit.core.problem.job.Shipment; import com.graphhopper.jsprit.core.problem.misc.JobInsertionContext; -import com.graphhopper.jsprit.core.problem.solution.route.activity.DeliveryActivity; -import com.graphhopper.jsprit.core.problem.solution.route.activity.End; -import com.graphhopper.jsprit.core.problem.solution.route.activity.PickupActivity; -import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity; +import com.graphhopper.jsprit.core.problem.solution.route.activity.*; import java.util.Collections; import java.util.Map; @@ -74,7 +71,10 @@ public class MaxTimeInVehicleConstraint implements HardActivityConstraint { double newActStart = Math.max(newActArrival, newAct.getTheoreticalEarliestOperationStartTime()); double newActDeparture = newActStart + activityCosts.getActivityDuration(prevAct, newAct, newActArrival, iFacts.getNewDriver(), iFacts.getNewVehicle()); double nextActArrival = newActDeparture + transportTime.getTransportTime(newAct.getLocation(),nextAct.getLocation(),newActDeparture,iFacts.getNewDriver(),iFacts.getNewVehicle()); - double nextActStart = Math.max(nextActArrival,nextAct.getTheoreticalEarliestOperationStartTime()); + double nextActStart = Math.max(nextActArrival, nextAct.getTheoreticalEarliestOperationStartTime()); + if (nextAct instanceof DeliverService && nextActStart - iFacts.getNewVehicle().getEarliestDeparture() > ((TourActivity.JobActivity)nextAct).getJob().getMaxTimeInVehicle()) { + return ConstraintsStatus.NOT_FULFILLED; + } if(newAct instanceof DeliveryActivity){ double pickupEnd; if(iFacts.getAssociatedActivities().size() == 1){ @@ -107,10 +107,9 @@ public class MaxTimeInVehicleConstraint implements HardActivityConstraint { } double directArrTimeNextAct = prevActDepTime + transportTime.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevActDepTime, iFacts.getNewDriver(), iFacts.getNewVehicle()); double directNextActStart = Math.max(directArrTimeNextAct, nextAct.getTheoreticalEarliestOperationStartTime()); - double additionalTimeOfNewAct = (nextActStart - prevActDepTime) - (directNextActStart - prevActDepTime); + double additionalTimeOfNewAct = nextActStart - directNextActStart; if (additionalTimeOfNewAct > minSlack) { - if (newActIsPickup) return ConstraintsStatus.NOT_FULFILLED; - else return ConstraintsStatus.NOT_FULFILLED; + return ConstraintsStatus.NOT_FULFILLED; } if (newActIsDelivery) { Map openJobsAtNext; diff --git a/jsprit-core/src/test/java/com/graphhopper/jsprit/core/algorithm/MaxTimeInVehicleTest.java b/jsprit-core/src/test/java/com/graphhopper/jsprit/core/algorithm/MaxTimeInVehicleTest.java index f70df159..9e5134fc 100644 --- a/jsprit-core/src/test/java/com/graphhopper/jsprit/core/algorithm/MaxTimeInVehicleTest.java +++ b/jsprit-core/src/test/java/com/graphhopper/jsprit/core/algorithm/MaxTimeInVehicleTest.java @@ -50,6 +50,8 @@ import static org.junit.Assert.assertTrue; public class MaxTimeInVehicleTest { + Random RANDOM = new Random(); + @Test public void testShipment(){ Shipment s1 = Shipment.Builder.newInstance("s1").setPickupLocation(Location.newInstance(34.773586,32.079754)).setDeliveryLocation(Location.newInstance(34.781247,38.294571)) @@ -73,9 +75,6 @@ public class MaxTimeInVehicleTest { SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE); assertEquals(0,solution.getUnassignedJobs().size()); assertEquals(1,solution.getRoutes().size()); - - - } @Test @@ -307,8 +306,6 @@ public class MaxTimeInVehicleTest { assertEquals(0,solution.getUnassignedJobs().size()); } - Random RANDOM = new Random(); - @Test public void testRouteTwoDriversTwoRouts_NotAllAssigned() { int numJobs = Math.abs(RANDOM.nextInt(100)) + 10, @@ -334,7 +331,7 @@ public class MaxTimeInVehicleTest { .setLocation(Location.newInstance(RANDOM.nextDouble(), RANDOM.nextDouble())).setServiceTime(serviceTime).build()); } - VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(builder); + VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(builder, true, 4); assertFalse(solution.getRoutes().isEmpty()); final Iterator iterator = solution.getRoutes().iterator(); @@ -383,7 +380,7 @@ public class MaxTimeInVehicleTest { .setLocation(Location.newInstance(RANDOM.nextDouble(), RANDOM.nextDouble())).setServiceTime(serviceTime).build()); } - VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(builder); + VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(builder, true, 4); final Iterator iterator = solution.getRoutes().iterator(); while (iterator.hasNext()) { @@ -396,9 +393,38 @@ public class MaxTimeInVehicleTest { assertTrue(solution.getUnassignedJobs().isEmpty()); } - private VehicleRoutingProblemSolution getVehicleRoutingProblemSolution(VehicleRoutingProblem.Builder builder) { - VehicleRoutingProblem vrp = builder - .setRoutingCost(new VehicleRoutingTransportCosts() { + @Test + public void testLowMaxTimeCauseTwoRoutes(){ + Shipment s1 = Shipment.Builder.newInstance("s1").setPickupLocation(Location.newInstance(8,0)) + .setDeliveryLocation(Location.newInstance(10,0)) + .setDeliveryServiceTime(2) + .setMaxTimeInVehicle(10) + .build(); + + Delivery d2 = Delivery.Builder.newInstance("d2") + .setMaxTimeInVehicle(13) + .setLocation(Location.newInstance(10, 5)).setServiceTime(2).build(); + + VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1") + .setStartLocation(Location.newInstance(8,5)).setReturnToDepot(true).build(); + + VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2") + .setStartLocation(Location.newInstance(5,0)).setReturnToDepot(true).build(); + + + final VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(VehicleRoutingProblem.Builder.newInstance() + .addVehicle(v1) + .addVehicle(v2) + .addJob(s1) + .addJob(d2), false, 4); + assertEquals(2, solution.getRoutes().size()); + assertEquals(0, solution.getUnassignedJobs().size()); + + } + + private VehicleRoutingProblemSolution getVehicleRoutingProblemSolution(VehicleRoutingProblem.Builder builder, boolean routeCost, int numThreads) { + if (routeCost) { + builder.setRoutingCost(new VehicleRoutingTransportCosts() { @Override public double getBackwardTransportCost(Location location, Location location1, double v, Driver driver, Vehicle vehicle) { return 1; @@ -423,7 +449,9 @@ public class MaxTimeInVehicleTest { public double getDistance(Location location, Location location1, double v, Vehicle vehicle) { return 1; } - }) + }); + } + VehicleRoutingProblem vrp = builder .setFleetSize(VehicleRoutingProblem.FleetSize.FINITE) .build(); @@ -435,7 +463,7 @@ public class MaxTimeInVehicleTest { ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager); constraintManager.addConstraint(new MaxTimeInVehicleConstraint(vrp.getTransportCosts(), vrp.getActivityCosts(), id, stateManager, vrp, openJobsId), ConstraintManager.Priority.CRITICAL); - VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setStateAndConstraintManager(stateManager,constraintManager).buildAlgorithm(); + VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setStateAndConstraintManager(stateManager,constraintManager).setProperty(Jsprit.Parameter.THREADS, String.valueOf(numThreads)).buildAlgorithm(); vra.setMaxIterations(100); VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions()); SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE);