Add new Generalized Schaffer function as F23Problem

Preserve best solution found in ABC
This commit is contained in:
Fabian Becker 2014-11-24 10:38:48 +01:00
parent 801c3513e7
commit 2e9343bf74
2 changed files with 118 additions and 1 deletions

View File

@ -253,7 +253,9 @@ public class ArtificialBeeColony extends AbstractOptimizer implements Serializab
/** /**
* ToDo: This should somehow preserve the best found individual. * ToDo: This should somehow preserve the best found individual.
*/ */
return new SolutionSet(this.population); Population solutions = (Population)this.population.clone();
solutions.add(bestIndividual);
return new SolutionSet(solutions);
} }
@Override @Override

View File

@ -0,0 +1,115 @@
package eva2.problems;
import eva2.optimization.strategies.InterfaceOptimizer;
import eva2.tools.math.Mathematics;
import eva2.util.annotation.Description;
/**
* Schaffer Problem
*/
@Description(value ="Generalized Schaffer")
public class F23Problem extends AbstractProblemDoubleOffset implements InterfaceHasInitRange, java.io.Serializable {
private double initialRangeRatio = 1.; // reduce to initialize in a smaller subrange of the original range (in the corner box)
public F23Problem() {
super();
setDefaultRange(100);
}
public F23Problem(F23Problem b) {
super();
super.cloneObjects(b);
}
public F23Problem(int dim) {
super(dim);
}
public F23Problem(int dim, double defRange) {
this(dim);
setDefaultRange(defRange);
}
/**
* This method returns a deep clone of the problem.
*
* @return the clone
*/
@Override
public Object clone() {
return new F23Problem(this);
}
/**
* This method allows you to evaluate a simple bit string to determine the fitness
*
* @param x The n-dimensional input vector
* @return The m-dimensional output vector.
*/
@Override
public double[] evaluate(double[] x) {
x = rotateMaybe(x);
double[] result = new double[1];
double sum = 0.0;
result[0] = yOffset;
// add an offset in solution space
for (double value : x) {
sum += Math.pow(value, 2);
}
result[0] += 0.5 + ((Math.pow(Math.sin(Math.sqrt(sum)), 2)) - 0.5)/Math.pow(1.0 + 0.001 * sum, 2);
return result;
}
/**
* This method returns a string describing the optimization problem.
*
* @param opt The Optimizer that is used or had been used.
* @return The description.
*/
@Override
public String getStringRepresentationForProblem(InterfaceOptimizer opt) {
StringBuilder sb = new StringBuilder(200);
sb.append("F23 Generalized Schaffer:\n");
sb.append("Here the individual codes a vector of real number x and F23(x) is to be minimized.\nParameters:\n");
sb.append("Dimension : ");
sb.append(this.problemDimension);
sb.append("\nNoise level : ");
sb.append(this.getNoise());
return sb.toString();
}
/**
* These are for GUI
*/
/**
* This method allows the CommonJavaObjectEditorPanel to read the
* name to the current object.
*
* @return The name.
*/
@Override
public String getName() {
return "Generalized Schaffer";
}
/**
* If initialRangeRatio<1, produce a reduced initial range in the negative corner of the range.
*/
@Override
public Object getInitializationRange() {
if (initialRangeRatio < 1.) {
double[][] gR = makeRange();
double[][] initR = makeRange();
Mathematics.scaleRange(initialRangeRatio, initR);
for (int i = 0; i < getProblemDimension(); i++) {
double d = gR[i][0] - initR[i][0];
initR[i][0] += d; // shift back by original offsets
initR[i][1] += d;
}
return initR;
} else {
return makeRange();
}
}
}