mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add unit tests
This commit is contained in:
parent
48a0455a67
commit
a53048adb2
2 changed files with 65 additions and 14 deletions
|
|
@ -45,7 +45,7 @@ public class IterationWithoutImprovementTermination implements PrematureAlgorith
|
|||
|
||||
private List<Double> costs;
|
||||
|
||||
private List<Integer> unassignedCount;
|
||||
private List<Integer> unassignedJobsCount;
|
||||
|
||||
private double bestCost = Double.MAX_VALUE;
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ public class IterationWithoutImprovementTermination implements PrematureAlgorith
|
|||
this.noIterationWithoutImprovement = noIterationsWithoutImprovement;
|
||||
this.terminationByCostPercentage = terminationByCostPercentage;
|
||||
costs = new ArrayList<>();
|
||||
unassignedCount = new ArrayList<>();
|
||||
unassignedJobsCount = new ArrayList<>();
|
||||
log.debug("initialise " + this);
|
||||
}
|
||||
|
||||
|
|
@ -99,15 +99,15 @@ public class IterationWithoutImprovementTermination implements PrematureAlgorith
|
|||
bestCost = Math.min(currentCost, bestCost);
|
||||
costs.add(bestCost);
|
||||
|
||||
int currentUnassigned = sol.getUnassignedJobs().size();
|
||||
unassignedCount.add(currentUnassigned);
|
||||
int currentJobsUnassigned = sol.getUnassignedJobs().size();
|
||||
unassignedJobsCount.add(currentJobsUnassigned);
|
||||
|
||||
int i = costs.size() - 1;
|
||||
if (i < noIterationWithoutImprovement)
|
||||
return false;
|
||||
|
||||
boolean unassignedEqual = (currentUnassigned == unassignedCount.get(i - noIterationWithoutImprovement));
|
||||
boolean progressTooSlow = (100 * Math.abs(currentCost - costs.get(i - noIterationWithoutImprovement))) / currentCost < terminationByCostPercentage;
|
||||
return (unassignedEqual && progressTooSlow);
|
||||
boolean unassignedJobsEqual = (currentJobsUnassigned == unassignedJobsCount.get(i - noIterationWithoutImprovement));
|
||||
boolean progressTooSlow = 100 * ((costs.get(i - noIterationWithoutImprovement) - currentCost) / currentCost) <= terminationByCostPercentage;
|
||||
return (unassignedJobsEqual && progressTooSlow);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,9 +22,11 @@ package com.graphhopper.jsprit.core.algorithm.termination;
|
|||
import com.graphhopper.jsprit.core.algorithm.SearchStrategy;
|
||||
import com.graphhopper.jsprit.core.problem.job.Job;
|
||||
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
|
@ -81,22 +83,71 @@ public class IterationsWithoutImprovementTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void itShouldTerminateAfter100ByPercentage() {
|
||||
IterationWithoutImprovementTermination termination = new IterationWithoutImprovementTermination(100, 0.1);
|
||||
public void isPrematureBreakZeroPercentage() {
|
||||
int maxIterations = 200;
|
||||
IterationWithoutImprovementTermination termination = new IterationWithoutImprovementTermination(100, 0);
|
||||
SearchStrategy.DiscoveredSolution discoveredSolution = mock(SearchStrategy.DiscoveredSolution.class);
|
||||
|
||||
int terminatedAfter = maxIterations;
|
||||
for (int i = 0; i < maxIterations; i++) {
|
||||
when(discoveredSolution.isAccepted()).thenReturn(i< 50 ? true : false);
|
||||
if (termination.isPrematureBreak(discoveredSolution)) {
|
||||
terminatedAfter = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(150, terminatedAfter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isPrematureBreakWithPercentageShouldBreak() {
|
||||
int maxIterations = 200;
|
||||
IterationWithoutImprovementTermination termination = new IterationWithoutImprovementTermination(10, 1.0);
|
||||
SearchStrategy.DiscoveredSolution discoveredSolution = mock(SearchStrategy.DiscoveredSolution.class);
|
||||
VehicleRoutingProblemSolution solution = mock(VehicleRoutingProblemSolution.class);
|
||||
when(discoveredSolution.getSolution()).thenReturn(solution);
|
||||
when(solution.getCost()).thenReturn(100.0);
|
||||
when(solution.getUnassignedJobs()).thenReturn(new ArrayList<Job>());
|
||||
|
||||
int terminatedAfter = 0;
|
||||
for (int i = 0; i < 200; i++) {
|
||||
int terminatedAfter = maxIterations;
|
||||
for (int i = 0; i < maxIterations; i++) {
|
||||
|
||||
when(solution.getCost()).thenReturn(i < 100 ? 100.0 - (0.1*i) : 40-((i-100)*0.01));
|
||||
boolean terminate = termination.isPrematureBreak(discoveredSolution);
|
||||
if (terminate) {
|
||||
terminatedAfter = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(100, terminatedAfter);
|
||||
Assert.assertEquals(110, terminatedAfter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isPrematureBreakWithPercentageCostNotImprovedButUnassignedImproved() {
|
||||
int maxIterations = 200;
|
||||
IterationWithoutImprovementTermination termination = new IterationWithoutImprovementTermination(10, 1.0);
|
||||
SearchStrategy.DiscoveredSolution discoveredSolution = mock(SearchStrategy.DiscoveredSolution.class);
|
||||
VehicleRoutingProblemSolution solution = mock(VehicleRoutingProblemSolution.class);
|
||||
Job job = mock(Job.class);
|
||||
when(discoveredSolution.getSolution()).thenReturn(solution);
|
||||
List<Job> unassignedJobs = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
unassignedJobs.add(job);
|
||||
}
|
||||
|
||||
int terminatedAfter = maxIterations;
|
||||
for (int i = 0; i < maxIterations; i++) {
|
||||
when(solution.getCost()).thenReturn(100.0);
|
||||
if (i <= 50){
|
||||
unassignedJobs.remove(0);
|
||||
}
|
||||
when(solution.getUnassignedJobs()).thenReturn(unassignedJobs);
|
||||
boolean terminate = termination.isPrematureBreak(discoveredSolution);
|
||||
if (terminate) {
|
||||
terminatedAfter = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(60, terminatedAfter);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue