mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add multiple termination criteria
This commit is contained in:
parent
c75d3d5cc2
commit
4d8b4a6273
5 changed files with 100 additions and 22 deletions
|
|
@ -143,4 +143,43 @@ public class VehicleRoutingAlgorithmTest {
|
|||
assertEquals(50,counter.getCountIterations());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingPrematureTwoTerminationCriteria_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.setMaxIterations(1000);
|
||||
PrematureAlgorithmTermination termination = new PrematureAlgorithmTermination() {
|
||||
|
||||
private int nuOfIterations = 1;
|
||||
|
||||
@Override
|
||||
public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) {
|
||||
if(nuOfIterations == 50) return true;
|
||||
nuOfIterations++;
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
PrematureAlgorithmTermination termination2 = new PrematureAlgorithmTermination() {
|
||||
|
||||
private int nuOfIterations = 1;
|
||||
|
||||
@Override
|
||||
public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) {
|
||||
if(nuOfIterations == 25) return true;
|
||||
nuOfIterations++;
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
CountIterations counter = new CountIterations();
|
||||
algorithm.addListener(counter);
|
||||
algorithm.addTerminationCriterion(termination);
|
||||
algorithm.addTerminationCriterion(termination2);
|
||||
algorithm.searchSolutions();
|
||||
assertEquals(25,counter.getCountIterations());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ public class TestAlgorithmReader {
|
|||
IterationCounter iCounter = new IterationCounter();
|
||||
vra.addListener(iCounter);
|
||||
vra.searchSolutions();
|
||||
Assert.assertEquals(100,iCounter.iterations);
|
||||
Assert.assertEquals(25,iCounter.iterations);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,9 @@
|
|||
<termination basedOn="iterations">
|
||||
<iterations>100</iterations>
|
||||
</termination>
|
||||
<termination basedOn="iterations">
|
||||
<iterations>25</iterations>
|
||||
</termination>
|
||||
</terminationCriteria>
|
||||
|
||||
<construction>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue