From 2e9343bf748832af554d83b1e59bbe1670dc4306 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Mon, 24 Nov 2014 10:38:48 +0100 Subject: [PATCH] Add new Generalized Schaffer function as F23Problem Preserve best solution found in ABC --- .../strategies/ArtificialBeeColony.java | 4 +- src/eva2/problems/F23Problem.java | 115 ++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/eva2/problems/F23Problem.java diff --git a/src/eva2/optimization/strategies/ArtificialBeeColony.java b/src/eva2/optimization/strategies/ArtificialBeeColony.java index 306a6e09..a97901ad 100644 --- a/src/eva2/optimization/strategies/ArtificialBeeColony.java +++ b/src/eva2/optimization/strategies/ArtificialBeeColony.java @@ -253,7 +253,9 @@ public class ArtificialBeeColony extends AbstractOptimizer implements Serializab /** * 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 diff --git a/src/eva2/problems/F23Problem.java b/src/eva2/problems/F23Problem.java new file mode 100644 index 00000000..b1f2d46f --- /dev/null +++ b/src/eva2/problems/F23Problem.java @@ -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(); + } + } +}