/// ------------------------------------------------------ /// SwarmOps - Numeric and heuristic optimization for C# /// Copyright (C) 2003-2011 Magnus Erik Hvass Pedersen. /// Please see the file license.txt for license details. /// SwarmOps on the internet: http://www.Hvass-Labs.org/ /// ------------------------------------------------------ using System.Diagnostics; namespace SwarmOps.Problems { /// /// Ackley benchmark problem. /// public class Ackley : Benchmark { #region Constructors. /// /// Construct the object. /// /// Dimensionality of the problem (e.g. 20) /// Max optimization iterations to perform. public Ackley(int dimensionality, int maxIterations) : base(dimensionality, -30, 30, 15, 30, maxIterations) { } #endregion #region Base-class overrides. /// /// Name of the optimization problem. /// public override string Name { get { return "Ackley"; } } /// /// Minimum possible fitness. /// public override double MinFitness { get { return 0; } } /// /// Compute and return fitness for the given parameters. /// /// Candidate solution. public override double Fitness(double[] x) { Debug.Assert(x != null && x.Length == Dimensionality); double fitness = System.Math.E + 20 - 20 * System.Math.Exp(-0.2 * SqrtSum(x)) - CosSum(x); // Rounding errors may cause negative fitnesses to occur even // though the mathematical global minimum has fitness zero. // Ensure this still works with meta-optimization which // requires non-negative fitnesses. if (fitness < 0) { fitness = 0; } return fitness; } /// /// Return whether the gradient has been implemented. /// public override bool HasGradient { get { return true; } } /// /// Compute the gradient of the fitness-function. /// /// Candidate solution. /// Array for holding the gradient. public override int Gradient(double[] x, ref double[] v) { Debug.Assert(x != null && x.Length == Dimensionality); Debug.Assert(v != null && v.Length == Dimensionality); double sqrtSum = SqrtSum(x); double cosSum = CosSum(x); double DimRec = 1.0 / Dimensionality; for (int i = 0; i < Dimensionality; i++) { double elm = x[i]; v[i] = 4 * DimRec * System.Math.Exp(-0.2 * sqrtSum) * elm / sqrtSum + cosSum * System.Math.Sin(System.Math.PI * 2 * elm) * System.Math.PI * 2 * DimRec; } return 0; } #endregion #region Protected methods. /// /// Helper-method used in both the Fitness- and Gradient-methods. /// protected double SqrtSum(double[] x) { double sum = 0; int n = x.Length; for (int i = 0; i < n; i++) { double elm = x[i]; sum += elm * elm; } return System.Math.Sqrt(sum / n); } /// /// Helper-method used in both the Fitness- and Gradient-methods. /// protected double CosSum(double[] x) { double sum = 0; int n = x.Length; for (int i = 0; i < n; i++) { double elm = x[i]; sum += System.Math.Cos(System.Math.PI * 2 * elm); } return System.Math.Exp(sum / n); } #endregion } }