From e8a9363f8361e13fb63daa52887ed781d2d26a94 Mon Sep 17 00:00:00 2001 From: oblonski Date: Thu, 12 Mar 2015 09:59:12 +0100 Subject: [PATCH] IT tests with Jsprit algorithm --- .../core/algorithm/CVRPwithDeliveries_IT.java | 14 +- .../core/algorithm/CVRPwithPickups_IT.java | 13 + ...etManagerIdentifiesDistinctVehicle_IT.java | 23 ++ .../MeetTimeWindowConstraint_IT.java | 268 ++++++++++++++++++ .../java/jsprit/core/algorithm/PDTW_IT.java | 54 ++++ .../algorithm/PickupsAndDeliveries_IT.java | 13 + .../core/algorithm/RefuseCollection_IT.java | 147 ++++------ 7 files changed, 432 insertions(+), 100 deletions(-) diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithDeliveries_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithDeliveries_IT.java index 7172b04c..0e67036d 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithDeliveries_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithDeliveries_IT.java @@ -16,6 +16,7 @@ ******************************************************************************/ package jsprit.core.algorithm; +import jsprit.core.algorithm.box.Jsprit; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.io.VrpXMLReader; @@ -36,8 +37,19 @@ public class CVRPwithDeliveries_IT { VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, "src/test/resources/algorithmConfig.xml"); Collection solutions = vra.searchSolutions(); - assertEquals(530.0, Solutions.bestOf(solutions).getCost(),50.0); + assertEquals(530.0, Solutions.bestOf(solutions).getCost(),50.0); assertEquals(5, Solutions.bestOf(solutions).getRoutes().size()); } + @Test + public void whenSolvingVRPNC1withDeliveriesWithJsprit_solutionsMustNoBeWorseThan5PercentOfBestKnownSolution(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-deliveries.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + Collection solutions = vra.searchSolutions(); + assertEquals(530.0, Solutions.bestOf(solutions).getCost(), 50.0); + assertEquals(5, Solutions.bestOf(solutions).getRoutes().size()); + } + } diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithPickups_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithPickups_IT.java index c5ef22f9..a37ea13a 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithPickups_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/CVRPwithPickups_IT.java @@ -16,6 +16,7 @@ ******************************************************************************/ package jsprit.core.algorithm; +import jsprit.core.algorithm.box.Jsprit; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.io.VrpXMLReader; @@ -40,4 +41,16 @@ public class CVRPwithPickups_IT { assertEquals(5, Solutions.bestOf(solutions).getRoutes().size()); } + @Test + public void whenSolvingVRPNC1WithPickupsWithJsprit_solutionsMustNoBeWorseThan5PercentOfBestKnownSolution(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/vrpnc1-jsprit-with-pickups.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(1000); + Collection solutions = vra.searchSolutions(); + assertEquals(530.0, Solutions.bestOf(solutions).getCost(),50.0); + assertEquals(5, Solutions.bestOf(solutions).getRoutes().size()); + } + } diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/FiniteVehicleFleetManagerIdentifiesDistinctVehicle_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/FiniteVehicleFleetManagerIdentifiesDistinctVehicle_IT.java index 36d81135..c20250d0 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/FiniteVehicleFleetManagerIdentifiesDistinctVehicle_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/FiniteVehicleFleetManagerIdentifiesDistinctVehicle_IT.java @@ -18,6 +18,7 @@ ******************************************************************************/ package jsprit.core.algorithm; +import jsprit.core.algorithm.box.Jsprit; import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.recreate.NoSolutionFoundException; import jsprit.core.problem.VehicleRoutingProblem; @@ -55,4 +56,26 @@ public class FiniteVehicleFleetManagerIdentifiesDistinctVehicle_IT { assertTrue(testFailed.isEmpty()); } + @Test + public void whenEmployingVehicleWhereOnlyOneDistinctVehicleCanServeAParticularJobWith_jspritAlgorithmShouldFoundDistinctSolution(){ + final List testFailed = new ArrayList(); + for(int i=0;i<10;i++){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/biggerProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(10); + try{ + @SuppressWarnings("unused") + Collection solutions = vra.searchSolutions(); + } + catch(NoSolutionFoundException e){ + testFailed.add(true); + } + } + System.out.println("failed: " + testFailed.size()); + assertTrue(testFailed.isEmpty()); + } + } diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/MeetTimeWindowConstraint_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/MeetTimeWindowConstraint_IT.java index bb60053f..aa3ec6b4 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/MeetTimeWindowConstraint_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/MeetTimeWindowConstraint_IT.java @@ -18,6 +18,7 @@ ******************************************************************************/ package jsprit.core.algorithm; +import jsprit.core.algorithm.box.Jsprit; import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.algorithm.recreate.listener.JobInsertedListener; @@ -293,6 +294,273 @@ public class MeetTimeWindowConstraint_IT { assertTrue(containsJob(vrp.getJobs().get("2"),getRoute("19",Solutions.bestOf(solutions)))); } + + + + + + + + + + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_nRoutesShouldBeCorrect(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(100); + Collection solutions = vra.searchSolutions(); + + assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_certainJobsCanNeverBeAssignedToCertainVehicles(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(100); + final List testFailed = new ArrayList(); + vra.addListener(new JobInsertedListener() { + + @Override + public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) { + if(job2insert.getId().equals("1")){ + if(inRoute.getVehicle().getId().equals("19")){ + testFailed.add(true); + } + } + if(job2insert.getId().equals("2")){ + if(inRoute.getVehicle().getId().equals("21")){ + testFailed.add(true); + } + } + } + + }); + @SuppressWarnings("unused") + Collection solutions = vra.searchSolutions(); + + assertTrue(testFailed.isEmpty()); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_certainVehiclesCanNeverBeAssignedToCertainRoutes(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(100); + final List testFailed = new ArrayList(); + vra.addListener(new VehicleSwitchedListener() { + + @Override + public void vehicleSwitched(VehicleRoute vehicleRoute, Vehicle oldVehicle, Vehicle newVehicle) { + if(oldVehicle==null) return; + if(oldVehicle.getId().equals("21") && newVehicle.getId().equals("19")){ + for(Job j : vehicleRoute.getTourActivities().getJobs()){ + if(j.getId().equals("1")){ + testFailed.add(true); + } + } + } + if(oldVehicle.getId().equals("19") && newVehicle.getId().equals("21")){ + for(Job j : vehicleRoute.getTourActivities().getJobs()){ + if(j.getId().equals("2")){ + testFailed.add(true); + } + } + } + } + + }); + + + @SuppressWarnings("unused") + Collection solutions = vra.searchSolutions(); + System.out.println("failed " + testFailed.size()); + assertTrue(testFailed.isEmpty()); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_job2CanNeverBeInVehicle21(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(100); + Collection solutions = vra.searchSolutions(); + + assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_job1ShouldBeAssignedCorrectly(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(100); + Collection solutions = vra.searchSolutions(); + +// assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + assertTrue(containsJob(vrp.getJobs().get("1"),getRoute("21",Solutions.bestOf(solutions)))); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_job2ShouldBeAssignedCorrectly(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(100); + Collection solutions = vra.searchSolutions(); + +// assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + assertTrue(containsJob(vrp.getJobs().get("2"),getRoute("19",Solutions.bestOf(solutions)))); + } + + + + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_jsprit_and_vehicleSwitchIsNotAllowed_nRoutesShouldBeCorrect(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH, "false").buildAlgorithm(); + vra.setMaxIterations(100); + Collection solutions = vra.searchSolutions(); + + assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_certainJobsCanNeverBeAssignedToCertainVehicles(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm(); + vra.setMaxIterations(100); + final List testFailed = new ArrayList(); + vra.addListener(new JobInsertedListener() { + + @Override + public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) { + if(job2insert.getId().equals("1")){ + if(inRoute.getVehicle().getId().equals("19")){ + testFailed.add(true); + } + } + if(job2insert.getId().equals("2")){ + if(inRoute.getVehicle().getId().equals("21")){ + testFailed.add(true); + } + } + } + + }); + @SuppressWarnings("unused") + Collection solutions = vra.searchSolutions(); + + assertTrue(testFailed.isEmpty()); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_certainVehiclesCanNeverBeAssignedToCertainRoutes(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm(); + vra.setMaxIterations(100); + final List testFailed = new ArrayList(); + vra.addListener(new VehicleSwitchedListener() { + + @Override + public void vehicleSwitched(VehicleRoute vehicleRoute, Vehicle oldVehicle, Vehicle newVehicle) { + if(oldVehicle==null) return; + if(oldVehicle.getId().equals("21") && newVehicle.getId().equals("19")){ + for(Job j : vehicleRoute.getTourActivities().getJobs()){ + if(j.getId().equals("1")){ + testFailed.add(true); + } + } + } + if(oldVehicle.getId().equals("19") && newVehicle.getId().equals("21")){ + for(Job j : vehicleRoute.getTourActivities().getJobs()){ + if(j.getId().equals("2")){ + testFailed.add(true); + } + } + } + } + + }); + + + @SuppressWarnings("unused") + Collection solutions = vra.searchSolutions(); + System.out.println("failed " + testFailed.size()); + assertTrue(testFailed.isEmpty()); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_job2CanNeverBeInVehicle21(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm(); + vra.setMaxIterations(100); + Collection solutions = vra.searchSolutions(); + + assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_job1ShouldBeAssignedCorrectly(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm(); + vra.setMaxIterations(100); + Collection solutions = vra.searchSolutions(); + + assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + assertTrue(containsJob(vrp.getJobs().get("1"),getRoute("21",Solutions.bestOf(solutions)))); + } + + @Test + public void whenEmployingVehicleWithDifferentWorkingShifts_and_vehicleSwitchIsNotAllowed_jsprit_job2ShouldBeAssignedCorrectly(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.VEHICLE_SWITCH,"false").buildAlgorithm(); + vra.setMaxIterations(100); + Collection solutions = vra.searchSolutions(); + + assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + assertTrue(containsJob(vrp.getJobs().get("2"),getRoute("19",Solutions.bestOf(solutions)))); + } + + + + + private boolean containsJob(Job job, VehicleRoute route) { if(route == null) return false; for(Job j : route.getTourActivities().getJobs()){ diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/PDTW_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/PDTW_IT.java index 06163bff..e83b657a 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/PDTW_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/PDTW_IT.java @@ -1,6 +1,7 @@ package jsprit.core.algorithm; +import jsprit.core.algorithm.box.Jsprit; import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.problem.AbstractJob; import jsprit.core.problem.Location; @@ -87,6 +88,59 @@ public class PDTW_IT { } } + @Test + public void whenDealingWithShipments_usingJsprit_timeWindowsShouldNOTbeBroken() { + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + for(int i =0 ; i < nVehicles ; i++){ + vrpBuilder.addVehicle(createVehicle()); + } + for(int i =0 ; i < nJobs;i++){ + vrpBuilder.addJob(createShipment()); + } + vrpBuilder.setFleetSize(FleetSize.FINITE); + VehicleRoutingProblem problem = vrpBuilder.build(); + VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem); + algorithm.setMaxIterations(0); + Collection solutions = algorithm.searchSolutions(); + VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions); + + for(VehicleRoute route : bestSolution.getRoutes()){ + Vehicle v = route.getVehicle(); + for(TourActivity ta : route.getActivities()){ + if(ta.getArrTime() > v.getLatestArrival() * 1.00001){ + assertFalse(true); + } + } + } + } + + + @Test + public void whenDealingWithServices_usingJsprit_timeWindowsShouldNOTbeBroken() { + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + for(int i =0 ; i < nVehicles ; i++){ + vrpBuilder.addVehicle(createVehicle()); + } + for(int i =0 ; i < nJobs;i++){ + vrpBuilder.addJob(createService()); + } + vrpBuilder.setFleetSize(FleetSize.FINITE); + VehicleRoutingProblem problem = vrpBuilder.build(); + VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem); + algorithm.setMaxIterations(100); + Collection solutions = algorithm.searchSolutions(); + VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions); + + for(VehicleRoute route : bestSolution.getRoutes()){ + Vehicle v = route.getVehicle(); + for(TourActivity ta : route.getActivities()){ + if(ta.getArrTime() * 1.000001 > v.getLatestArrival()){ + assertFalse(true); + } + } + } + } + private AbstractJob createService() { Service.Builder b = Service.Builder.newInstance(Integer.toString(nextShipmentId++)); b.addSizeDimension(0, 1); diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/PickupsAndDeliveries_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/PickupsAndDeliveries_IT.java index 3184003d..71832e06 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/PickupsAndDeliveries_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/PickupsAndDeliveries_IT.java @@ -18,6 +18,7 @@ ******************************************************************************/ package jsprit.core.algorithm; +import jsprit.core.algorithm.box.Jsprit; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.io.VrpXMLReader; @@ -42,4 +43,16 @@ public class PickupsAndDeliveries_IT { assertEquals(19,Solutions.bestOf(solutions).getRoutes().size(),1); } + @Test + public void whenSolvingLR101InstanceOfLiLim_withJsprit_solutionsMustNoBeWorseThan5PercentOfBestKnownSolution(){ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/lilim_lr101.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setMaxIterations(1000); + Collection solutions = vra.searchSolutions(); + assertEquals(1650.8,Solutions.bestOf(solutions).getCost(),80.); + assertEquals(19,Solutions.bestOf(solutions).getRoutes().size(),1); + } + } diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/RefuseCollection_IT.java b/jsprit-core/src/test/java/jsprit/core/algorithm/RefuseCollection_IT.java index 3e10ca4a..5a9bf6f4 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/RefuseCollection_IT.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/RefuseCollection_IT.java @@ -16,18 +16,16 @@ ******************************************************************************/ package jsprit.core.algorithm; +import jsprit.core.algorithm.box.Jsprit; import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.termination.IterationWithoutImprovementTermination; import jsprit.core.problem.Location; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem.FleetSize; -import jsprit.core.problem.cost.VehicleRoutingTransportCosts; -import jsprit.core.problem.driver.Driver; import jsprit.core.problem.job.Delivery; import jsprit.core.problem.job.Pickup; import jsprit.core.problem.job.Service; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; -import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleTypeImpl; import jsprit.core.reporting.SolutionPrinter; @@ -39,106 +37,13 @@ import org.junit.Test; import java.io.*; import java.util.Collection; -import java.util.Map; import static org.junit.Assert.assertEquals; public class RefuseCollection_IT { - - static class RelationKey { - - static RelationKey newKey(String from, String to){ - int fromInt = Integer.parseInt(from); - int toInt = Integer.parseInt(to); - if(fromInt < toInt){ - return new RelationKey(from, to); - } - else { - return new RelationKey(to, from); - } - } - - final String from; - final String to; - - public RelationKey(String from, String to) { - super(); - this.from = from; - this.to = to; - } - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((from == null) ? 0 : from.hashCode()); - result = prime * result + ((to == null) ? 0 : to.hashCode()); - return result; - } - - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - RelationKey other = (RelationKey) obj; - if (from == null) { - if (other.from != null) - return false; - } else if (!from.equals(other.from)) - return false; - if (to == null) { - if (other.to != null) - return false; - } else if (!to.equals(other.to)) - return false; - return true; - } - } - - static class RoutingCosts implements VehicleRoutingTransportCosts { - - private Map distances; - - public RoutingCosts(Map distances) { - super(); - this.distances = distances; - } - - @Override - public double getTransportTime(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) { - return getTransportCost(from, to, departureTime, driver, vehicle); - } - - @Override - public double getBackwardTransportTime(Location from, Location to, double arrivalTime, Driver driver, Vehicle vehicle) { - return getTransportCost(from, to, arrivalTime, driver, vehicle); - } - - @Override - public double getTransportCost(Location from, Location to,double departureTime, Driver driver, Vehicle vehicle) { - if(from.equals(to)) return 0.0; - RelationKey key = RelationKey.newKey(from.getId(), to.getId()); - return distances.get(key); - } - - @Override - public double getBackwardTransportCost(Location from, Location to,double arrivalTime, Driver driver, Vehicle vehicle) { - return getTransportCost(from, to, arrivalTime, driver, vehicle); - } - - } @Test @@ -152,7 +57,7 @@ public class RefuseCollection_IT { VehicleTypeImpl bigType = typeBuilder.build(); VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle"); - vehicleBuilder.setStartLocationId("1"); + vehicleBuilder.setStartLocation(Location.newInstance("1")); vehicleBuilder.setType(bigType); VehicleImpl bigVehicle = vehicleBuilder.build(); @@ -185,6 +90,50 @@ public class RefuseCollection_IT { assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); } + @Test + public void whenReadingServices_usingJsprit_itShouldCalculateCorrectly(){ + + /* + * create vehicle-type and vehicle + */ + VehicleTypeImpl.Builder typeBuilder = VehicleTypeImpl.Builder.newInstance("vehicle-type").addCapacityDimension(0, 23); + typeBuilder.setCostPerDistance(1.0); + VehicleTypeImpl bigType = typeBuilder.build(); + + VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle"); + vehicleBuilder.setStartLocation(Location.newInstance("1")); + vehicleBuilder.setType(bigType); + VehicleImpl bigVehicle = vehicleBuilder.build(); + + /* + * start building the problem + */ + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + vrpBuilder.setFleetSize(FleetSize.INFINITE); + vrpBuilder.addVehicle(bigVehicle); + + /* + * create cost-matrix + */ + VehicleRoutingTransportCostsMatrix.Builder matrixBuilder = VehicleRoutingTransportCostsMatrix.Builder.newInstance(true); + /* + * read demand quantities + */ + readDemandQuantitiesAsServices(vrpBuilder); + readDistances(matrixBuilder); + + vrpBuilder.setRoutingCost(matrixBuilder.build()); + VehicleRoutingProblem vrp = vrpBuilder.build(); + VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp); + vra.setPrematureAlgorithmTermination(new IterationWithoutImprovementTermination(100)); + Collection solutions = vra.searchSolutions(); + + SolutionPrinter.print(vrp, Solutions.bestOf(solutions), Print.VERBOSE); + + assertEquals(397.0,Solutions.bestOf(solutions).getCost(),40.); + assertEquals(2,Solutions.bestOf(solutions).getRoutes().size()); + } + @Test public void whenReadingPickups_itShouldCalculateCorrectly(){ @@ -196,7 +145,7 @@ public class RefuseCollection_IT { VehicleTypeImpl bigType = typeBuilder.build(); VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle"); - vehicleBuilder.setStartLocationId("1"); + vehicleBuilder.setStartLocation(Location.newInstance("1")); vehicleBuilder.setType(bigType); VehicleImpl bigVehicle = vehicleBuilder.build(); @@ -240,7 +189,7 @@ public class RefuseCollection_IT { VehicleTypeImpl bigType = typeBuilder.build(); VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle"); - vehicleBuilder.setStartLocationId("1"); + vehicleBuilder.setStartLocation(Location.newInstance("1")); vehicleBuilder.setType(bigType); VehicleImpl bigVehicle = vehicleBuilder.build();