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,73 +33,70 @@ 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>
|
* <p>Note, that this must be registered as AlgorithmListener <br>
|
||||||
* <code>algorithm.getAlgorithmListeners().addListener(this);</code>
|
* It will be activated by:<br>
|
||||||
|
*
|
||||||
|
* <code>algorithm.setPrematureAlgorithmTermination(this);</code><br>
|
||||||
|
* <code>algorithm.addListener(this);</code>
|
||||||
|
*
|
||||||
*
|
*
|
||||||
*
|
* @author stefan schroeder
|
||||||
* @author stefan
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class VariationCoefficientTermination implements PrematureAlgorithmTermination, IterationStartsListener, AlgorithmStartsListener, IterationEndsListener{
|
public class VariationCoefficientTermination implements PrematureAlgorithmTermination, IterationStartsListener, AlgorithmStartsListener, IterationEndsListener{
|
||||||
|
|
||||||
private static Logger logger = LogManager.getLogger(VariationCoefficientTermination.class);
|
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;
|
private int currentIteration;
|
||||||
|
|
||||||
private double[] solutionValues;
|
private double[] solutionValues;
|
||||||
|
|
||||||
private VehicleRoutingProblemSolution lastAccepted = null;
|
private VehicleRoutingProblemSolution lastAccepted = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Breaks algorithm prematurely based on variationCoefficient.
|
* Constructs termination.
|
||||||
*
|
*
|
||||||
* <p>Note that this must be registered in algorithm<br>
|
* @param noIterations size of the sample, i.e. number previous solutions values to take into account. If for example
|
||||||
* <code>algorithm.getAlgorithmListeners().addListener(this);</code>
|
* 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
|
||||||
* @author stefan
|
* is smaller than the specified threshold, the algorithm terminates.
|
||||||
*
|
*/
|
||||||
*/
|
public VariationCoefficientTermination(int noIterations, double variationCoefficientThreshold) {
|
||||||
public VariationCoefficientTermination(int nuOfIterations, double variationCoefficientThreshold) {
|
|
||||||
super();
|
super();
|
||||||
this.nuOfIterations = nuOfIterations;
|
this.noIterations = noIterations;
|
||||||
this.variationCoefficientThreshold = variationCoefficientThreshold;
|
this.variationCoefficientThreshold = variationCoefficientThreshold;
|
||||||
solutionValues = new double[nuOfIterations];
|
solutionValues = new double[noIterations];
|
||||||
logger.info("initialise " + this);
|
logger.info("initialise " + this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "[name=VariationCoefficientBreaker][variationCoefficientThreshold="+variationCoefficientThreshold+"][iterations="+nuOfIterations+"]";
|
return "[name=VariationCoefficientBreaker][variationCoefficientThreshold="+variationCoefficientThreshold+"][iterations="+ noIterations +"]";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) {
|
public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) {
|
||||||
if(discoveredSolution.isAccepted()){
|
if(discoveredSolution.isAccepted()){
|
||||||
lastAccepted = discoveredSolution.getSolution();
|
lastAccepted = discoveredSolution.getSolution();
|
||||||
solutionValues[currentIteration]=discoveredSolution.getSolution().getCost();
|
solutionValues[currentIteration] = discoveredSolution.getSolution().getCost();
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if(lastAccepted != null){
|
if(lastAccepted != null){
|
||||||
solutionValues[currentIteration]=lastAccepted.getCost();
|
solutionValues[currentIteration] = lastAccepted.getCost();
|
||||||
}
|
}
|
||||||
else solutionValues[currentIteration]=Integer.MAX_VALUE;
|
else solutionValues[currentIteration] = Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
if(lastAccepted !=null) {
|
if(currentIteration == (noIterations - 1)){
|
||||||
// logger.info(lastAccepted.getCost());
|
|
||||||
// logger.info("inArr,"+(currentIteration)+","+solutionValues[currentIteration]);
|
|
||||||
}
|
|
||||||
if(currentIteration == (nuOfIterations-1)){
|
|
||||||
double mean = StatUtils.mean(solutionValues);
|
double mean = StatUtils.mean(solutionValues);
|
||||||
double stdDev = new StandardDeviation(true).evaluate(solutionValues, mean);
|
double stdDev = new StandardDeviation(true).evaluate(solutionValues, mean);
|
||||||
double variationCoefficient = stdDev/mean;
|
double variationCoefficient = stdDev / mean;
|
||||||
// logger.info("[mean="+mean+"][stdDev="+stdDev+"][variationCoefficient="+variationCoefficient+"]");
|
|
||||||
if(variationCoefficient < variationCoefficientThreshold){
|
if(variationCoefficient < variationCoefficientThreshold){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -108,7 +105,7 @@ public class VariationCoefficientTermination implements PrematureAlgorithmTermin
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reset(){
|
private void reset(){
|
||||||
currentIteration=0;
|
currentIteration = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -118,7 +115,7 @@ public class VariationCoefficientTermination implements PrematureAlgorithmTermin
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void informIterationEnds(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
public void informIterationEnds(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||||
if(currentIteration == (nuOfIterations-1)){
|
if(currentIteration == (noIterations - 1)){
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|
@ -131,6 +128,4 @@ public class VariationCoefficientTermination implements PrematureAlgorithmTermin
|
||||||
if(lastAccepted == null) lastAccepted = Solutions.bestOf(solutions);
|
if(lastAccepted == null) lastAccepted = Solutions.bestOf(solutions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue