From fee2a396d9c6f97c0cddbe0580a8512fa1fe0f72 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Wed, 30 Jul 2014 10:46:04 +0200 Subject: [PATCH] reproduced bug #112 --- .../jsprit/core/algorithm/SearchStrategy.java | 37 +++++++++++-------- .../algorithm/recreate/BestInsertion.java | 1 - .../core/algorithm/InitialRoutesTest.java | 12 +++++- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/SearchStrategy.java b/jsprit-core/src/main/java/jsprit/core/algorithm/SearchStrategy.java index f97afd0a..dc0bdb6f 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/SearchStrategy.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/SearchStrategy.java @@ -52,7 +52,8 @@ public class SearchStrategy { return accepted; } - public String getStrategyName() { + @SuppressWarnings("UnusedDeclaration") + public String getStrategyName() { return strategyName; } @@ -90,10 +91,12 @@ public class SearchStrategy { return Collections.unmodifiableCollection(searchStrategyModules); } - public SolutionSelector getSolutionSelector() { + @SuppressWarnings("UnusedDeclaration") + public SolutionSelector getSolutionSelector() { return solutionSelector; } + @SuppressWarnings("UnusedDeclaration") public SolutionAcceptor getSolutionAcceptor() { return solutionAcceptor; } @@ -106,33 +109,37 @@ public class SearchStrategy { /** * Runs the search-strategy and its according modules, and returns DiscoveredSolution. * - *

This involves three basic steps: 1) Selecting a solution from solutions (input parameter) according to {@link SolutionSelector}, 2) running the modules - * ({@link SearchStrategyModule}) on the selectedSolution and 3) accepting the new solution according to {@link SolutionAcceptor}. + *

This involves three basic steps: 1) Selecting a solution from solutions (input parameter) according to {@link jsprit.core.algorithm.selector.SolutionSelector}, 2) running the modules + * ({@link jsprit.core.algorithm.SearchStrategyModule}) on the selectedSolution and 3) accepting the new solution according to {@link jsprit.core.algorithm.acceptor.SolutionAcceptor}. *

Note that after 1) the selected solution is copied, thus the original solution is not modified. *

Note also that 3) modifies the input parameter solutions by adding, removing, replacing the existing solutions or whatever is defined in the solutionAcceptor. * - * @param vrp + * @param vrp the underlying vehicle routing problem * @param solutions which will be modified - * @return discoveredSolutin - * @see SolutionSelector, SearchStrategyModule, SolutionAcceptor + * @return discoveredSolution */ - public DiscoveredSolution run(VehicleRoutingProblem vrp, Collection solutions){ + @SuppressWarnings("UnusedParameters") + public DiscoveredSolution run(VehicleRoutingProblem vrp, Collection solutions){ VehicleRoutingProblemSolution solution = solutionSelector.selectSolution(solutions); - if(solution == null) throw new IllegalStateException("solution is null. check solutionSelector to return an appropiate solution."); + if(solution == null) throw new IllegalStateException(getErrMsg()); VehicleRoutingProblemSolution lastSolution = VehicleRoutingProblemSolution.copyOf(solution); for(SearchStrategyModule module : searchStrategyModules){ - VehicleRoutingProblemSolution newSolution = module.runAndGetSolution(lastSolution); - lastSolution = newSolution; + lastSolution = module.runAndGetSolution(lastSolution); } double costs = solutionCostCalculator.getCosts(lastSolution); lastSolution.setCost(costs); boolean solutionAccepted = solutionAcceptor.acceptSolution(solutions, lastSolution); - DiscoveredSolution discoveredSolution = new DiscoveredSolution(lastSolution, solutionAccepted, getName()); - return discoveredSolution; + return new DiscoveredSolution(lastSolution, solutionAccepted, getName()); } - - public void addModule(SearchStrategyModule module){ + private String getErrMsg() { + return "solution is null. check solutionSelector to return an appropriate solution. " + + "\nfigure out whether you start with an initial solution. either you set it manually by algorithm.addInitialSolution(...)" + + " or let the algorithm create an initial solution for you. then add the ... xml-snippet to your algorithm's config file."; + } + + + public void addModule(SearchStrategyModule module){ if(module == null) throw new IllegalStateException("module to be added is null."); searchStrategyModules.add(module); logger.info("module added [module="+module+"][#modules="+searchStrategyModules.size()+"]"); diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/BestInsertion.java b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/BestInsertion.java index e3edee8b..15f9fd26 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/BestInsertion.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/recreate/BestInsertion.java @@ -121,7 +121,6 @@ final class BestInsertion implements InsertionStrategy{ InsertionData newIData = bestInsertionCostCalculator.getInsertionData(newRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost); if(newIData.getInsertionCost() < bestInsertionCost){ bestInsertion = new Insertion(newRoute,newIData); - bestInsertionCost = newIData.getInsertionCost(); vehicleRoutes.add(newRoute); } } 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 6d4bdabe..3db00a6f 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/InitialRoutesTest.java @@ -19,14 +19,24 @@ import static org.junit.Assert.assertTrue; public class InitialRoutesTest { @Test - public void whenReading_thereShouldBeNoDuplicates(){ + public void whenReading_jobMapShouldOnlyContainJob2(){ 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()); + assertTrue(vrp.getJobs().containsKey("2")); + } + @Test + public void whenReading_thereShouldBeOnlyOneActAssociatedToJob2(){ + + VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); + new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); + VehicleRoutingProblem vrp = vrpBuilder.build(); + + assertEquals(1,vrp.getActivities(vrp.getJobs().get("2")).size()); } @Test