mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add and test core.algorithm.VehicleRoutingAlgorithm.setMaxIterations(...) and .getMaxIterations()
deprecate .setNuOfIterations(...) and .getNuOfIterations()
This commit is contained in:
parent
ae83eadad7
commit
632a889c4b
2 changed files with 65 additions and 16 deletions
|
|
@ -69,7 +69,7 @@ public class VehicleRoutingAlgorithm {
|
|||
|
||||
private VehicleRoutingProblem problem;
|
||||
|
||||
private int nOfIterations = 100;
|
||||
private int maxIterations = 100;
|
||||
|
||||
private Counter counter = new Counter("iterations ");
|
||||
|
||||
|
|
@ -168,12 +168,12 @@ public class VehicleRoutingAlgorithm {
|
|||
logger.info("algorithm starts");
|
||||
double now = System.currentTimeMillis();
|
||||
verify();
|
||||
int nuOfIterationsThisAlgoIsRunning = nOfIterations;
|
||||
int nuOfIterationsThisAlgoIsRunning = maxIterations;
|
||||
counter.reset();
|
||||
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
|
||||
algorithmStarts(problem,solutions);
|
||||
logger.info("iterations start");
|
||||
for(int i=0;i<nOfIterations;i++){
|
||||
for(int i=0;i< maxIterations;i++){
|
||||
iterationStarts(i+1,problem,solutions);
|
||||
counter.incCounter();
|
||||
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
|
||||
|
|
@ -203,9 +203,11 @@ public class VehicleRoutingAlgorithm {
|
|||
* Returns the number of iterations.
|
||||
*
|
||||
* @return iterations
|
||||
* @deprecated use .getMaxIterations() instead
|
||||
*/
|
||||
@Deprecated
|
||||
public int getNuOfIterations(){
|
||||
return nOfIterations;
|
||||
return maxIterations;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -245,8 +247,34 @@ public class VehicleRoutingAlgorithm {
|
|||
algoListeners.algorithmStarts(problem, this, solutions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets max number of iterations.
|
||||
*
|
||||
* @param maxIterations max number of iteration the algorithm runs
|
||||
*/
|
||||
public void setMaxIterations(int maxIterations) {
|
||||
this.maxIterations = maxIterations;
|
||||
logger.info("set maxIterations to " + this.maxIterations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets max number of iterations.
|
||||
*
|
||||
* @return max number of iterations
|
||||
*/
|
||||
public int getMaxIterations() {
|
||||
return maxIterations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets max number of iterations.
|
||||
*
|
||||
* @param nOfIterations max number of iteration the algorithm runs
|
||||
* @deprecated use .setMaxIterations(int maxIterations) instead
|
||||
*/
|
||||
@Deprecated
|
||||
public void setNuOfIterations(int nOfIterations) {
|
||||
this.nOfIterations = nOfIterations;
|
||||
this.maxIterations = nOfIterations;
|
||||
logger.info("set nuOfIterations to " + nOfIterations);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,21 +18,20 @@
|
|||
******************************************************************************/
|
||||
package jsprit.core.algorithm;
|
||||
|
||||
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;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class VehicleRoutingAlgorithmTest {
|
||||
|
||||
@Test
|
||||
|
|
@ -43,6 +42,14 @@ public class VehicleRoutingAlgorithmTest {
|
|||
assertEquals(50,algorithm.getNuOfIterations());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSettingIterationsWithMaxIterations_itIsSetCorrectly(){
|
||||
VehicleRoutingAlgorithm algorithm = new VehicleRoutingAlgorithm(mock(VehicleRoutingProblem.class),
|
||||
mock(SearchStrategyManager.class));
|
||||
algorithm.setMaxIterations(50);
|
||||
assertEquals(50,algorithm.getMaxIterations());
|
||||
}
|
||||
|
||||
private static class CountIterations implements IterationStartsListener {
|
||||
|
||||
private int countIterations = 0;
|
||||
|
|
@ -58,6 +65,20 @@ public class VehicleRoutingAlgorithmTest {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSettingIterationsWithMaxIterations_iterAreExecutedCorrectly(){
|
||||
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.setMaxIterations(1000);
|
||||
CountIterations counter = new CountIterations();
|
||||
algorithm.addListener(counter);
|
||||
algorithm.searchSolutions();
|
||||
assertEquals(1000,counter.getCountIterations());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSettingIterations_iterAreExecutedCorrectly(){
|
||||
SearchStrategyManager stratManager = mock(SearchStrategyManager.class);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue