From 55e1baa205a663f9f8f6a5aa7b3e19715e028e55 Mon Sep 17 00:00:00 2001 From: oblonski Date: Sun, 20 Sep 2015 16:17:56 +0200 Subject: [PATCH] fix #186 --- .../io/VehicleRoutingAlgorithms.java | 2 + .../problem/constraint/SwitchNotFeasible.java | 2 +- .../MeetTimeWindowConstraint_IT.java | 36 ++++++++- .../test/resources/infiniteWriterV2Test.xml | 81 +++++++------------ 4 files changed, 65 insertions(+), 56 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java index 328ceac4..d96b75c5 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/io/VehicleRoutingAlgorithms.java @@ -42,6 +42,7 @@ import jsprit.core.algorithm.termination.VariationCoefficientTermination; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem.FleetSize; import jsprit.core.problem.constraint.ConstraintManager; +import jsprit.core.problem.constraint.SwitchNotFeasible; import jsprit.core.problem.solution.SolutionCostCalculator; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; import jsprit.core.problem.solution.route.VehicleRoute; @@ -478,6 +479,7 @@ public class VehicleRoutingAlgorithms { constraintManager.addTimeWindowConstraint(); constraintManager.addLoadConstraint(); constraintManager.addSkillsConstraint(); + constraintManager.addConstraint(new SwitchNotFeasible(stateManager)); return readAndCreateAlgorithm(vrp, config, nuOfThreads, null, stateManager, constraintManager, true); } diff --git a/jsprit-core/src/main/java/jsprit/core/problem/constraint/SwitchNotFeasible.java b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SwitchNotFeasible.java index 6be3645d..d923b84c 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/constraint/SwitchNotFeasible.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/constraint/SwitchNotFeasible.java @@ -18,7 +18,7 @@ public class SwitchNotFeasible implements HardRouteConstraint{ @Override public boolean fulfilled(JobInsertionContext insertionContext) { Boolean notFeasible = stateManager.getRouteState(insertionContext.getRoute(),insertionContext.getNewVehicle(), InternalStates.SWITCH_NOT_FEASIBLE,Boolean.class); - if(notFeasible == null) return true; + if(notFeasible == null || insertionContext.getRoute().getVehicle().getVehicleTypeIdentifier().equals(insertionContext.getNewVehicle().getVehicleTypeIdentifier())) return true; else return !notFeasible; } 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 c26301c2..0c0e5e23 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.GreedySchrimpfFactory; import jsprit.core.algorithm.box.Jsprit; import jsprit.core.algorithm.box.SchrimpfFactory; import jsprit.core.algorithm.io.VehicleRoutingAlgorithms; @@ -550,7 +551,7 @@ public class MeetTimeWindowConstraint_IT { } @Test - public void driverTimesShouldBeMet() throws IOException { + public void whenUsingJsprit_driverTimesShouldBeMet() throws IOException { VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); new VrpXMLReader(vrpBuilder).read("src/test/resources/twbug.xml"); final FastVehicleRoutingTransportCostsMatrix matrix = createMatrix(); @@ -565,6 +566,39 @@ public class MeetTimeWindowConstraint_IT { } } + @Test + public void whenUsingSchrimpf_driverTimesShouldBeMet() throws IOException { + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/twbug.xml"); + final FastVehicleRoutingTransportCostsMatrix matrix = createMatrix(); + vrpBuilder.setRoutingCost(matrix); + VehicleRoutingProblem vrp = vrpBuilder.build(); + VehicleRoutingAlgorithm algorithm = new SchrimpfFactory().createAlgorithm(vrp); + algorithm.setMaxIterations(1000); + VehicleRoutingProblemSolution solution = Solutions.bestOf(algorithm.searchSolutions()); + for(VehicleRoute r : solution.getRoutes()){ + assertTrue(r.getVehicle().getEarliestDeparture() <= r.getDepartureTime()); + assertTrue(r.getVehicle().getLatestArrival() >= r.getEnd().getArrTime()); + } + } + + @Test + public void whenUsingGreedySchrimpf_driverTimesShouldBeMet() throws IOException { + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/twbug.xml"); + final FastVehicleRoutingTransportCostsMatrix matrix = createMatrix(); + vrpBuilder.setRoutingCost(matrix); + VehicleRoutingProblem vrp = vrpBuilder.build(); + VehicleRoutingAlgorithm algorithm = new GreedySchrimpfFactory().createAlgorithm(vrp); + algorithm.setMaxIterations(1000); + VehicleRoutingProblemSolution solution = Solutions.bestOf(algorithm.searchSolutions()); + for(VehicleRoute r : solution.getRoutes()){ + assertTrue(r.getVehicle().getEarliestDeparture() <= r.getDepartureTime()); + assertTrue(r.getVehicle().getLatestArrival() >= r.getEnd().getArrTime()); + } + } + + private FastVehicleRoutingTransportCostsMatrix createMatrix() throws IOException { BufferedReader reader = new BufferedReader(new FileReader(new File("src/test/resources/matrix.txt"))); String line; diff --git a/jsprit-core/src/test/resources/infiniteWriterV2Test.xml b/jsprit-core/src/test/resources/infiniteWriterV2Test.xml index 2d9058ab..07c5406a 100644 --- a/jsprit-core/src/test/resources/infiniteWriterV2Test.xml +++ b/jsprit-core/src/test/resources/infiniteWriterV2Test.xml @@ -2,9 +2,24 @@ - INFINITE + FINITE + + v2 + vehType2 + + loc + + + loc + + + 0.0 + 1.7976931348623157E308 + + true + v1 vehType @@ -33,58 +48,16 @@ + + vehType2 + + 200 + + + 0.0 + 1.0 + + + - - - - loc - - - 1 - - 2.0 - - - 0.0 - 1.7976931348623157E308 - - - - - - loc2 - - - 1 - - 4.0 - - - 0.0 - 1.7976931348623157E308 - - - - - - - 10.0 - - - noDriver - v1 - 0.0 - - 1 - 0.0 - 0.0 - - 0.0 - - - - - - -