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
|
|
@ -37,8 +37,8 @@ import java.util.Collection;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class VehicleRoutingAlgorithm {
|
public class VehicleRoutingAlgorithm {
|
||||||
|
|
||||||
private static class Counter {
|
private static class Counter {
|
||||||
private final String name;
|
private final String name;
|
||||||
private long counter = 0;
|
private long counter = 0;
|
||||||
private long nextCounter = 1;
|
private long nextCounter = 1;
|
||||||
|
|
@ -69,7 +69,7 @@ public class VehicleRoutingAlgorithm {
|
||||||
|
|
||||||
private VehicleRoutingProblem problem;
|
private VehicleRoutingProblem problem;
|
||||||
|
|
||||||
private int nOfIterations = 100;
|
private int maxIterations = 100;
|
||||||
|
|
||||||
private Counter counter = new Counter("iterations ");
|
private Counter counter = new Counter("iterations ");
|
||||||
|
|
||||||
|
|
@ -168,12 +168,12 @@ public class VehicleRoutingAlgorithm {
|
||||||
logger.info("algorithm starts");
|
logger.info("algorithm starts");
|
||||||
double now = System.currentTimeMillis();
|
double now = System.currentTimeMillis();
|
||||||
verify();
|
verify();
|
||||||
int nuOfIterationsThisAlgoIsRunning = nOfIterations;
|
int nuOfIterationsThisAlgoIsRunning = maxIterations;
|
||||||
counter.reset();
|
counter.reset();
|
||||||
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
|
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
|
||||||
algorithmStarts(problem,solutions);
|
algorithmStarts(problem,solutions);
|
||||||
logger.info("iterations start");
|
logger.info("iterations start");
|
||||||
for(int i=0;i<nOfIterations;i++){
|
for(int i=0;i< maxIterations;i++){
|
||||||
iterationStarts(i+1,problem,solutions);
|
iterationStarts(i+1,problem,solutions);
|
||||||
counter.incCounter();
|
counter.incCounter();
|
||||||
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
|
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
|
||||||
|
|
@ -203,9 +203,11 @@ public class VehicleRoutingAlgorithm {
|
||||||
* Returns the number of iterations.
|
* Returns the number of iterations.
|
||||||
*
|
*
|
||||||
* @return iterations
|
* @return iterations
|
||||||
|
* @deprecated use .getMaxIterations() instead
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public int getNuOfIterations(){
|
public int getNuOfIterations(){
|
||||||
return nOfIterations;
|
return maxIterations;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -245,8 +247,34 @@ public class VehicleRoutingAlgorithm {
|
||||||
algoListeners.algorithmStarts(problem, this, solutions);
|
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) {
|
public void setNuOfIterations(int nOfIterations) {
|
||||||
this.nOfIterations = nOfIterations;
|
this.maxIterations = nOfIterations;
|
||||||
logger.info("set nuOfIterations to " + nOfIterations);
|
logger.info("set nuOfIterations to " + nOfIterations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,21 +18,20 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package jsprit.core.algorithm;
|
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.SearchStrategy.DiscoveredSolution;
|
||||||
import jsprit.core.algorithm.listener.IterationStartsListener;
|
import jsprit.core.algorithm.listener.IterationStartsListener;
|
||||||
import jsprit.core.algorithm.termination.PrematureAlgorithmTermination;
|
import jsprit.core.algorithm.termination.PrematureAlgorithmTermination;
|
||||||
import jsprit.core.problem.VehicleRoutingProblem;
|
import jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||||
|
|
||||||
import org.junit.Test;
|
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 {
|
public class VehicleRoutingAlgorithmTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -42,6 +41,14 @@ public class VehicleRoutingAlgorithmTest {
|
||||||
algorithm.setNuOfIterations(50);
|
algorithm.setNuOfIterations(50);
|
||||||
assertEquals(50,algorithm.getNuOfIterations());
|
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 static class CountIterations implements IterationStartsListener {
|
||||||
|
|
||||||
|
|
@ -57,7 +64,21 @@ 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
|
@Test
|
||||||
public void whenSettingIterations_iterAreExecutedCorrectly(){
|
public void whenSettingIterations_iterAreExecutedCorrectly(){
|
||||||
SearchStrategyManager stratManager = mock(SearchStrategyManager.class);
|
SearchStrategyManager stratManager = mock(SearchStrategyManager.class);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue