1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

reproduced bug #112

This commit is contained in:
oblonski 2014-07-30 10:46:04 +02:00
parent c32507329d
commit fee2a396d9
3 changed files with 33 additions and 17 deletions

View file

@ -52,7 +52,8 @@ public class SearchStrategy {
return accepted; return accepted;
} }
public String getStrategyName() { @SuppressWarnings("UnusedDeclaration")
public String getStrategyName() {
return strategyName; return strategyName;
} }
@ -90,10 +91,12 @@ public class SearchStrategy {
return Collections.unmodifiableCollection(searchStrategyModules); return Collections.unmodifiableCollection(searchStrategyModules);
} }
public SolutionSelector getSolutionSelector() { @SuppressWarnings("UnusedDeclaration")
public SolutionSelector getSolutionSelector() {
return solutionSelector; return solutionSelector;
} }
@SuppressWarnings("UnusedDeclaration")
public SolutionAcceptor getSolutionAcceptor() { public SolutionAcceptor getSolutionAcceptor() {
return solutionAcceptor; return solutionAcceptor;
} }
@ -106,33 +109,37 @@ public class SearchStrategy {
/** /**
* Runs the search-strategy and its according modules, and returns DiscoveredSolution. * Runs the search-strategy and its according modules, and returns DiscoveredSolution.
* *
* <p>This involves three basic steps: 1) Selecting a solution from solutions (input parameter) according to {@link SolutionSelector}, 2) running the modules * <p>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 SearchStrategyModule}) on the selectedSolution and 3) accepting the new solution according to {@link SolutionAcceptor}. * ({@link jsprit.core.algorithm.SearchStrategyModule}) on the selectedSolution and 3) accepting the new solution according to {@link jsprit.core.algorithm.acceptor.SolutionAcceptor}.
* <p> Note that after 1) the selected solution is copied, thus the original solution is not modified. * <p> Note that after 1) the selected solution is copied, thus the original solution is not modified.
* <p> Note also that 3) modifies the input parameter solutions by adding, removing, replacing the existing solutions or whatever is defined in the solutionAcceptor. * <p> 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 * @param solutions which will be modified
* @return discoveredSolutin * @return discoveredSolution
* @see SolutionSelector, SearchStrategyModule, SolutionAcceptor
*/ */
public DiscoveredSolution run(VehicleRoutingProblem vrp, Collection<VehicleRoutingProblemSolution> solutions){ @SuppressWarnings("UnusedParameters")
public DiscoveredSolution run(VehicleRoutingProblem vrp, Collection<VehicleRoutingProblemSolution> solutions){
VehicleRoutingProblemSolution solution = solutionSelector.selectSolution(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); VehicleRoutingProblemSolution lastSolution = VehicleRoutingProblemSolution.copyOf(solution);
for(SearchStrategyModule module : searchStrategyModules){ for(SearchStrategyModule module : searchStrategyModules){
VehicleRoutingProblemSolution newSolution = module.runAndGetSolution(lastSolution); lastSolution = module.runAndGetSolution(lastSolution);
lastSolution = newSolution;
} }
double costs = solutionCostCalculator.getCosts(lastSolution); double costs = solutionCostCalculator.getCosts(lastSolution);
lastSolution.setCost(costs); lastSolution.setCost(costs);
boolean solutionAccepted = solutionAcceptor.acceptSolution(solutions, lastSolution); boolean solutionAccepted = solutionAcceptor.acceptSolution(solutions, lastSolution);
DiscoveredSolution discoveredSolution = new DiscoveredSolution(lastSolution, solutionAccepted, getName()); return new DiscoveredSolution(lastSolution, solutionAccepted, getName());
return discoveredSolution;
} }
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 <construction>...</construction> xml-snippet to your algorithm's config file.";
}
public void addModule(SearchStrategyModule module){
public void addModule(SearchStrategyModule module){
if(module == null) throw new IllegalStateException("module to be added is null."); if(module == null) throw new IllegalStateException("module to be added is null.");
searchStrategyModules.add(module); searchStrategyModules.add(module);
logger.info("module added [module="+module+"][#modules="+searchStrategyModules.size()+"]"); logger.info("module added [module="+module+"][#modules="+searchStrategyModules.size()+"]");

View file

@ -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); InsertionData newIData = bestInsertionCostCalculator.getInsertionData(newRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost);
if(newIData.getInsertionCost() < bestInsertionCost){ if(newIData.getInsertionCost() < bestInsertionCost){
bestInsertion = new Insertion(newRoute,newIData); bestInsertion = new Insertion(newRoute,newIData);
bestInsertionCost = newIData.getInsertionCost();
vehicleRoutes.add(newRoute); vehicleRoutes.add(newRoute);
} }
} }

View file

@ -19,14 +19,24 @@ import static org.junit.Assert.assertTrue;
public class InitialRoutesTest { public class InitialRoutesTest {
@Test @Test
public void whenReading_thereShouldBeNoDuplicates(){ public void whenReading_jobMapShouldOnlyContainJob2(){
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance(); VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml"); new VrpXMLReader(vrpBuilder).read("src/test/resources/simpleProblem_iniRoutes.xml");
VehicleRoutingProblem vrp = vrpBuilder.build(); VehicleRoutingProblem vrp = vrpBuilder.build();
assertEquals(1,vrp.getJobs().size()); 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 @Test