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 428f0276..e1793c71 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 @@ -94,9 +94,6 @@ import org.apache.commons.configuration.HierarchicalConfiguration; import org.apache.commons.configuration.XMLConfiguration; import org.apache.log4j.Logger; - - - public class VehicleRoutingAlgorithms { static class TypedMap { @@ -583,7 +580,6 @@ public class VehicleRoutingAlgorithms { if(config.containsKey("iterations")){ int iter = config.getInt("iterations"); metaAlgorithm.setNuOfIterations(iter); - log.info("set nuOfIterations to " + iter); } diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/VehicleRoutingAlgorithmTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/VehicleRoutingAlgorithmTest.java index c8b8b2cd..166b2a3b 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/VehicleRoutingAlgorithmTest.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/VehicleRoutingAlgorithmTest.java @@ -1,11 +1,15 @@ package jsprit.core.algorithm; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.util.Arrays; import java.util.Collection; +import jsprit.core.algorithm.SearchStrategy.DiscoveredSolution; import jsprit.core.algorithm.listener.IterationStartsListener; +import jsprit.core.algorithm.termination.PrematureAlgorithmTermination; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.solution.VehicleRoutingProblemSolution; @@ -38,13 +42,42 @@ public class VehicleRoutingAlgorithmTest { @Test public void whenSettingIterations_iterAreExecutedCorrectly(){ + SearchStrategyManager stratManager = mock(SearchStrategyManager.class); VehicleRoutingAlgorithm algorithm = new VehicleRoutingAlgorithm(mock(VehicleRoutingProblem.class), - mock(SearchStrategyManager.class)); - algorithm.setNuOfIterations(100); + stratManager); + when(stratManager.getRandomStrategy()).thenReturn(mock(SearchStrategy.class)); + when(stratManager.getProbabilities()).thenReturn(Arrays.asList(1.0)); + algorithm.setNuOfIterations(1000); CountIterations counter = new CountIterations(); algorithm.addListener(counter); algorithm.searchSolutions(); - assertEquals(100,counter.getCountIterations()); + assertEquals(1000,counter.getCountIterations()); + } + + @Test + public void whenSettingPrematureTermination_itIsExecutedCorrectly(){ + SearchStrategyManager stratManager = mock(SearchStrategyManager.class); + VehicleRoutingAlgorithm algorithm = new VehicleRoutingAlgorithm(mock(VehicleRoutingProblem.class), + stratManager); + when(stratManager.getRandomStrategy()).thenReturn(mock(SearchStrategy.class)); + when(stratManager.getProbabilities()).thenReturn(Arrays.asList(1.0)); + algorithm.setNuOfIterations(1000); + PrematureAlgorithmTermination termination = new PrematureAlgorithmTermination() { + + private int nuOfIterations = 1; + + @Override + public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) { + if(nuOfIterations == 50) return true; + nuOfIterations++; + return false; + } + }; + CountIterations counter = new CountIterations(); + algorithm.addListener(counter); + algorithm.setPrematureAlgorithmTermination(termination); + algorithm.searchSolutions(); + assertEquals(50,counter.getCountIterations()); } }