mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
document core.algorithm.termination.VariationCoefficientTermination
This commit is contained in:
parent
ad39bea373
commit
ec4a899938
1 changed files with 31 additions and 36 deletions
|
|
@ -33,22 +33,25 @@ import java.util.Collection;
|
|||
|
||||
|
||||
/**
|
||||
* Breaks algorithm prematurely based on variationCoefficient.
|
||||
* Terminates algorithm prematurely based on variationCoefficient (http://en.wikipedia.org/wiki/Coefficient_of_variation).
|
||||
*
|
||||
* <p>Note that this must be registered in algorithm<br>
|
||||
* <code>algorithm.getAlgorithmListeners().addListener(this);</code>
|
||||
* <p>Note, that this must be registered as AlgorithmListener <br>
|
||||
* It will be activated by:<br>
|
||||
*
|
||||
* <code>algorithm.setPrematureAlgorithmTermination(this);</code><br>
|
||||
* <code>algorithm.addListener(this);</code>
|
||||
*
|
||||
*
|
||||
* @author stefan
|
||||
* @author stefan schroeder
|
||||
*
|
||||
*/
|
||||
public class VariationCoefficientTermination implements PrematureAlgorithmTermination, IterationStartsListener, AlgorithmStartsListener, IterationEndsListener{
|
||||
|
||||
private static Logger logger = LogManager.getLogger(VariationCoefficientTermination.class);
|
||||
|
||||
private int nuOfIterations;
|
||||
private final int noIterations;
|
||||
|
||||
private double variationCoefficientThreshold;
|
||||
private final double variationCoefficientThreshold;
|
||||
|
||||
private int currentIteration;
|
||||
|
||||
|
|
@ -57,26 +60,25 @@ public class VariationCoefficientTermination implements PrematureAlgorithmTermin
|
|||
private VehicleRoutingProblemSolution lastAccepted = null;
|
||||
|
||||
/**
|
||||
* Breaks algorithm prematurely based on variationCoefficient.
|
||||
*
|
||||
* <p>Note that this must be registered in algorithm<br>
|
||||
* <code>algorithm.getAlgorithmListeners().addListener(this);</code>
|
||||
*
|
||||
*
|
||||
* @author stefan
|
||||
* Constructs termination.
|
||||
*
|
||||
* @param noIterations size of the sample, i.e. number previous solutions values to take into account. If for example
|
||||
* noIterations = 10 then every 10th iteration the variationCoefficient will be calculated with the
|
||||
* last 10 solution values.
|
||||
* @param variationCoefficientThreshold the threshold used to terminate the algorithm. If the calculated variationCoefficient
|
||||
* is smaller than the specified threshold, the algorithm terminates.
|
||||
*/
|
||||
public VariationCoefficientTermination(int nuOfIterations, double variationCoefficientThreshold) {
|
||||
public VariationCoefficientTermination(int noIterations, double variationCoefficientThreshold) {
|
||||
super();
|
||||
this.nuOfIterations = nuOfIterations;
|
||||
this.noIterations = noIterations;
|
||||
this.variationCoefficientThreshold = variationCoefficientThreshold;
|
||||
solutionValues = new double[nuOfIterations];
|
||||
solutionValues = new double[noIterations];
|
||||
logger.info("initialise " + this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[name=VariationCoefficientBreaker][variationCoefficientThreshold="+variationCoefficientThreshold+"][iterations="+nuOfIterations+"]";
|
||||
return "[name=VariationCoefficientBreaker][variationCoefficientThreshold="+variationCoefficientThreshold+"][iterations="+ noIterations +"]";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -91,15 +93,10 @@ public class VariationCoefficientTermination implements PrematureAlgorithmTermin
|
|||
}
|
||||
else solutionValues[currentIteration] = Integer.MAX_VALUE;
|
||||
}
|
||||
if(lastAccepted !=null) {
|
||||
// logger.info(lastAccepted.getCost());
|
||||
// logger.info("inArr,"+(currentIteration)+","+solutionValues[currentIteration]);
|
||||
}
|
||||
if(currentIteration == (nuOfIterations-1)){
|
||||
if(currentIteration == (noIterations - 1)){
|
||||
double mean = StatUtils.mean(solutionValues);
|
||||
double stdDev = new StandardDeviation(true).evaluate(solutionValues, mean);
|
||||
double variationCoefficient = stdDev / mean;
|
||||
// logger.info("[mean="+mean+"][stdDev="+stdDev+"][variationCoefficient="+variationCoefficient+"]");
|
||||
if(variationCoefficient < variationCoefficientThreshold){
|
||||
return true;
|
||||
}
|
||||
|
|
@ -118,7 +115,7 @@ public class VariationCoefficientTermination implements PrematureAlgorithmTermin
|
|||
|
||||
@Override
|
||||
public void informIterationEnds(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
if(currentIteration == (nuOfIterations-1)){
|
||||
if(currentIteration == (noIterations - 1)){
|
||||
reset();
|
||||
}
|
||||
else{
|
||||
|
|
@ -131,6 +128,4 @@ public class VariationCoefficientTermination implements PrematureAlgorithmTermin
|
|||
if(lastAccepted == null) lastAccepted = Solutions.bestOf(solutions);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue