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,6 +52,7 @@ public class SearchStrategy {
return accepted;
}
@SuppressWarnings("UnusedDeclaration")
public String getStrategyName() {
return strategyName;
}
@ -90,10 +91,12 @@ public class SearchStrategy {
return Collections.unmodifiableCollection(searchStrategyModules);
}
@SuppressWarnings("UnusedDeclaration")
public SolutionSelector getSolutionSelector() {
return solutionSelector;
}
@SuppressWarnings("UnusedDeclaration")
public SolutionAcceptor getSolutionAcceptor() {
return solutionAcceptor;
}
@ -106,29 +109,33 @@ public class SearchStrategy {
/**
* 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
* ({@link SearchStrategyModule}) on the selectedSolution and 3) accepting the new solution according to {@link SolutionAcceptor}.
* <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 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 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
*/
@SuppressWarnings("UnusedParameters")
public DiscoveredSolution run(VehicleRoutingProblem vrp, Collection<VehicleRoutingProblemSolution> 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());
}
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.";
}

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);
if(newIData.getInsertionCost() < bestInsertionCost){
bestInsertion = new Insertion(newRoute,newIData);
bestInsertionCost = newIData.getInsertionCost();
vehicleRoutes.add(newRoute);
}
}

View file

@ -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