diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/VehicleTypeDependentJobInsertionCalculator.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/VehicleTypeDependentJobInsertionCalculator.java index 529abd42..aa7c2ac8 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/VehicleTypeDependentJobInsertionCalculator.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/VehicleTypeDependentJobInsertionCalculator.java @@ -64,7 +64,8 @@ final class VehicleTypeDependentJobInsertionCalculator implements JobInsertionCo } private void getInitialVehicleIds() { - for(VehicleRoute initialRoute : vrp.getInitialVehicleRoutes()){ + Collection initialVehicleRoutes = vrp.getInitialVehicleRoutes(); + for(VehicleRoute initialRoute : initialVehicleRoutes){ initialVehicleIds.add(initialRoute.getVehicle().getId()); } } diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java index 3db00a6f..28ff79a7 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java @@ -2,8 +2,12 @@ package jsprit.core.algorithm; import jsprit.core.algorithm.box.SchrimpfFactory; +import jsprit.core.problem.AbstractActivity; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.io.VrpXMLReader; +import jsprit.core.problem.job.Job; +import jsprit.core.problem.job.Service; +import jsprit.core.problem.job.Shipment; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.activity.TourActivity; @@ -12,6 +16,7 @@ import jsprit.core.util.Solutions; import org.junit.Test; import java.util.Collection; +import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -25,10 +30,48 @@ public class InitialRoutesTest { new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); VehicleRoutingProblem vrp = vrpBuilder.build(); - assertEquals(1,vrp.getJobs().size()); + assertEquals(1,getNuServices(vrp)); assertTrue(vrp.getJobs().containsKey("2")); } + @Test + public void whenReadingProblem2_jobMapShouldContain_service2(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + assertEquals(1,getNuServices(vrp)); + assertTrue(vrp.getJobs().containsKey("2")); + } + + @Test + public void whenReading_jobMapShouldContain_shipment4(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + assertEquals(1,getNuShipments(vrp)); + assertTrue(vrp.getJobs().containsKey("4")); + } + + private int getNuShipments(VehicleRoutingProblem vrp) { + int nuShipments = 0; + for(Job job : vrp.getJobs().values()){ + if(job instanceof Shipment) nuShipments++; + } + return nuShipments; + } + + private int getNuServices(VehicleRoutingProblem vrp) { + int nuServices = 0; + for(Job job : vrp.getJobs().values()){ + if(job instanceof Service) nuServices++; + } + return nuServices; + } + @Test public void whenReading_thereShouldBeOnlyOneActAssociatedToJob2(){ @@ -36,11 +79,34 @@ public class InitialRoutesTest { new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); VehicleRoutingProblem vrp = vrpBuilder.build(); - assertEquals(1,vrp.getActivities(vrp.getJobs().get("2")).size()); + assertEquals(1, vrp.getActivities(vrp.getJobs().get("2")).size()); } @Test - public void whenSolving_nuJobsShouldBe2(){ + public void whenReading_thereShouldBeOnlyOneActAssociatedToJob2_v2(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + assertEquals(1, vrp.getActivities(vrp.getJobs().get("2")).size()); + } + + @Test + public void whenReading_thereShouldBeTwoActsAssociatedToShipment4(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + Job job = vrp.getJobs().get("4"); + List activities = vrp.getActivities(job); + + assertEquals(2, activities.size()); + } + + @Test + public void whenSolving_nuJobsInSolutionShouldBe2(){ VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); @@ -55,6 +121,26 @@ public class InitialRoutesTest { assertEquals(2,solution.getRoutes().iterator().next().getTourActivities().getJobs().size()); } + @Test + public void whenSolvingProblem2_nuJobsInSolutionShouldBe4(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_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); + + int jobsInSolution = 0; + for(VehicleRoute r : solution.getRoutes()){ + jobsInSolution += r.getTourActivities().jobSize(); + } + assertEquals(4,jobsInSolution); + } + @Test public void whenSolving_nuActsShouldBe2(){ @@ -71,6 +157,24 @@ public class InitialRoutesTest { assertEquals(2, solution.getRoutes().iterator().next().getActivities().size()); } + @Test + public void whenSolvingProblem2_nuActsShouldBe6(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); + Collection solutions = vra.searchSolutions(); + VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions); + + int nuActs = 0; + for(VehicleRoute r : solution.getRoutes()){ + nuActs += r.getActivities().size(); + } + assertEquals(6, nuActs); + } + @Test public void whenSolving_deliverService1_shouldBeInRoute(){ @@ -85,6 +189,35 @@ public class InitialRoutesTest { assertTrue(hasActivityIn(solution.getRoutes().iterator().next(),"1")); } + @Test + public void whenSolvingProblem2_deliverServices_and_allShipmentActs_shouldBeInRoute(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_inclShipments_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(),"1")); + assertTrue(hasActivityIn(solution.getRoutes(),"2")); + assertTrue(hasActivityIn(solution.getRoutes(),"3")); + assertTrue(hasActivityIn(solution.getRoutes(),"4")); + } + + private boolean hasActivityIn(Collection routes, String jobId) { + boolean isInRoute = false; + for(VehicleRoute route : routes) { + for (TourActivity act : route.getActivities()) { + if (act instanceof TourActivity.JobActivity) { + if (((TourActivity.JobActivity) act).getJob().getId().equals(jobId)) isInRoute = true; + } + } + } + return isInRoute; + } + private boolean hasActivityIn(VehicleRoute route, String jobId){ boolean isInRoute = false; for(TourActivity act : route.getActivities()){ @@ -106,6 +239,6 @@ public class InitialRoutesTest { Collection solutions = vra.searchSolutions(); VehicleRoutingProblemSolution solution = Solutions.bestOf(solutions); - assertTrue(hasActivityIn(solution.getRoutes().iterator().next(),"2")); + assertTrue(hasActivityIn(solution.getRoutes().iterator().next(), "2")); } } diff --git a/jsprit-core/src/test/resources/simpleProblem_inclShipments_iniRoutes.xml b/jsprit-core/src/test/resources/simpleProblem_inclShipments_iniRoutes.xml new file mode 100644 index 00000000..0b18d0b7 --- /dev/null +++ b/jsprit-core/src/test/resources/simpleProblem_inclShipments_iniRoutes.xml @@ -0,0 +1,131 @@ + + + + FINITE + HOMOGENEOUS + + + + veh1 + type1 + + [x=0.0][y=0.0] + + + + [x=0.0][y=0.0] + + + + 0.0 + 46800.0 + + true + + + veh2 + 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_s1 + + + 0 + + 0.0 + + + + + + + loc_pickup_shipment_3 + + + + loc_deliver_shipment_3 + + + + 0 + + + + + + loc_pickup_shipment_4 + + + + loc_deliver_shipment_4 + + + + 0 + + + + + + + + noDriver + veh1 + 0. + + 1 + + + + + + noDriver + veh2 + 0. + + 3 + + + 3 + + + + +