diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithm.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithm.java index 1155955b..18ea6e1d 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithm.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithm.java @@ -214,11 +214,14 @@ public class VehicleRoutingAlgorithm { * @see {@link SearchStrategyManager}, {@link com.graphhopper.jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListener}, {@link com.graphhopper.jsprit.core.algorithm.listener.AlgorithmStartsListener}, {@link com.graphhopper.jsprit.core.algorithm.listener.AlgorithmEndsListener}, {@link com.graphhopper.jsprit.core.algorithm.listener.IterationStartsListener}, {@link com.graphhopper.jsprit.core.algorithm.listener.IterationEndsListener} */ public Collection searchSolutions() { + return searchSolutions(new ArrayList()); + } + + public Collection searchSolutions(Collection solutions) { logger.info("algorithm starts: [maxIterations={}]", maxIterations); double now = System.currentTimeMillis(); int noIterationsThisAlgoIsRunning = maxIterations; counter.reset(); - Collection solutions = new ArrayList(initialSolutions); algorithmStarts(problem, solutions); bestEver = Solutions.bestOf(solutions); if (logger.isTraceEnabled()) { diff --git a/jsprit-core/src/test/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithmTest.java b/jsprit-core/src/test/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithmTest.java index 1deed4cc..ee0a2eec 100644 --- a/jsprit-core/src/test/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithmTest.java +++ b/jsprit-core/src/test/java/com/graphhopper/jsprit/core/algorithm/VehicleRoutingAlgorithmTest.java @@ -21,10 +21,14 @@ import com.graphhopper.jsprit.core.algorithm.listener.IterationStartsListener; import com.graphhopper.jsprit.core.algorithm.termination.PrematureAlgorithmTermination; import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem; import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution; +import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute; +import com.graphhopper.jsprit.core.util.Solutions; import org.junit.Test; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Random; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; @@ -91,6 +95,28 @@ public class VehicleRoutingAlgorithmTest { assertEquals(1000, counter.getCountIterations()); } + @Test + public void whenSettingIterations_iterAreExecutedCorrectlyWithSolutions() { + Collection solutions = new ArrayList<>(); + double bestSolutionCost = Double.MAX_VALUE; + Random random = new Random(); + for (int i = 0; i < random.nextInt(10) + 10; ++i) { + double cost = Math.abs(random.nextInt() + random.nextDouble()); + solutions.add(new VehicleRoutingProblemSolution(new ArrayList(), cost)); + bestSolutionCost = Math.min(bestSolutionCost, cost); + } + SearchStrategyManager stratManager = mock(SearchStrategyManager.class); + VehicleRoutingAlgorithm algorithm = new VehicleRoutingAlgorithm(mock(VehicleRoutingProblem.class), stratManager); + when(stratManager.getRandomStrategy()).thenReturn(mock(SearchStrategy.class)); + when(stratManager.getWeights()).thenReturn(Arrays.asList(1.0)); + algorithm.setMaxIterations(1000); + CountIterations counter = new CountIterations(); + algorithm.addListener(counter); + Collection vehicleRoutingProblemSolutions = algorithm.searchSolutions(solutions); + assertEquals(1000, counter.getCountIterations()); + assertEquals(Solutions.bestOf(vehicleRoutingProblemSolutions).getCost(), bestSolutionCost, 0.001); + } + @Test public void whenSettingPrematureTermination_itIsExecutedCorrectly() { SearchStrategyManager stratManager = mock(SearchStrategyManager.class);