diff --git a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java index 845a7e2a..0e9c6f6d 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/VehicleRoutingProblem.java @@ -288,25 +288,43 @@ public class VehicleRoutingProblem { } - @SuppressWarnings("deprecation") + public Builder addInitialVehicleRoute(VehicleRoute route){ - addVehicle(route.getVehicle()); - for(Job job : route.getTourActivities().getJobs()){ - jobsInInitialRoutes.add(job.getId()); - if(job instanceof Service) { - tentative_coordinates.put(((Service)job).getLocationId(), ((Service)job).getCoord()); - } - if(job instanceof Shipment){ - Shipment shipment = (Shipment)job; - tentative_coordinates.put(shipment.getPickupLocation(), shipment.getPickupCoord()); - tentative_coordinates.put(shipment.getDeliveryLocation(), shipment.getDeliveryCoord()); - } - } + addVehicle((AbstractVehicle)route.getVehicle()); + for(TourActivity act : route.getActivities()){ + AbstractActivity abstractAct = (AbstractActivity) act; + abstractAct.setIndex(activityIndexCounter); + incActivityIndexCounter(); + if(act instanceof TourActivity.JobActivity) { + Job job = ((TourActivity.JobActivity) act).getJob(); + jobsInInitialRoutes.add(job.getId()); + registerLocation(job); + registerJobAndActivity(abstractAct, job); + } + } initialRoutes.add(route); return this; } - - public Builder addInitialVehicleRoutes(Collection routes){ + + private void registerLocation(Job job) { + if (job instanceof Service) tentative_coordinates.put(((Service) job).getLocationId(), ((Service) job).getCoord()); + if (job instanceof Shipment) { + Shipment shipment = (Shipment) job; + tentative_coordinates.put(shipment.getPickupLocation(), shipment.getPickupCoord()); + tentative_coordinates.put(shipment.getDeliveryLocation(), shipment.getDeliveryCoord()); + } + } + + private void registerJobAndActivity(AbstractActivity abstractAct, Job job) { + if(activityMap.containsKey(job)) activityMap.get(job).add(abstractAct); + else{ + List actList = new ArrayList(); + actList.add(abstractAct); + activityMap.put(job,actList); + } + } + + public Builder addInitialVehicleRoutes(Collection routes){ for(VehicleRoute r : routes){ addInitialVehicleRoute(r); } diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java new file mode 100644 index 00000000..6d4bdabe --- /dev/null +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java @@ -0,0 +1,101 @@ +package jsprit.core.algorithm; + + +import jsprit.core.algorithm.box.SchrimpfFactory; +import jsprit.core.problem.VehicleRoutingProblem; +import jsprit.core.problem.io.VrpXMLReader; +import jsprit.core.problem.solution.VehicleRoutingProblemSolution; +import jsprit.core.problem.solution.route.VehicleRoute; +import jsprit.core.problem.solution.route.activity.TourActivity; +import jsprit.core.reporting.SolutionPrinter; +import jsprit.core.util.Solutions; +import org.junit.Test; + +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class InitialRoutesTest { + + @Test + public void whenReading_thereShouldBeNoDuplicates(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + assertEquals(1,vrp.getJobs().size()); + + } + + @Test + public void whenSolving_nuJobsShouldBe2(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); + Collection solutions = vra.searchSolutions(); + VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions); + + SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE); + + assertEquals(2,solution.getRoutes().iterator().next().getTourActivities().getJobs().size()); + } + + @Test + public void whenSolving_nuActsShouldBe2(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); + Collection solutions = vra.searchSolutions(); + VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions); + + SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE); + + assertEquals(2, solution.getRoutes().iterator().next().getActivities().size()); + } + + @Test + public void whenSolving_deliverService1_shouldBeInRoute(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); + Collection solutions = vra.searchSolutions(); + VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions); + + assertTrue(hasActivityIn(solution.getRoutes().iterator().next(),"1")); + } + + private boolean hasActivityIn(VehicleRoute route, String jobId){ + boolean isInRoute = false; + for(TourActivity act : route.getActivities()){ + if(act instanceof TourActivity.JobActivity){ + if(((TourActivity.JobActivity) act).getJob().getId().equals(jobId)) isInRoute = true; + } + } + return isInRoute; + } + + @Test + public void whenSolving_deliverService2_shouldBeInRoute(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); + Collection solutions = vra.searchSolutions(); + VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions); + + assertTrue(hasActivityIn(solution.getRoutes().iterator().next(),"2")); + } +} diff --git a/jsprit-core/src/test/resources/simpleProblem_iniRoutes.xml b/jsprit-core/src/test/resources/simpleProblem_iniRoutes.xml new file mode 100644 index 00000000..1db2a906 --- /dev/null +++ b/jsprit-core/src/test/resources/simpleProblem_iniRoutes.xml @@ -0,0 +1,87 @@ + + + + FINITE + HOMOGENEOUS + + + + veh1 + type1 + + [x=0.0][y=0.0] + + + + [x=0.0][y=0.0] + + + + 0.0 + 46800.0 + + true + + + 2 + type1 + + [x=0.0][y=0.0] + + + + [x=0.0][y=0.0] + + + + 0.0 + 64800.0 + + true + + + + + type1 + + 0 + + + 0.0 + 1.0 + + + + + + + loc_s2 + + + 0 + + 0.0 + + + loc_s3 + + + 0 + + 0.0 + + + + + noDriver + veh1 + 0. + + 1 + + + + + +