|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
namespace SwarmOps.Problems
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public class Ackley : Benchmark
|
|
|
{
|
|
|
#region Constructors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Ackley(int dimensionality, int maxIterations)
|
|
|
: base(dimensionality, -30, 30, 15, 30, maxIterations)
|
|
|
{
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Base-class overrides.
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
{
|
|
|
get { return "Ackley"; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override double MinFitness
|
|
|
{
|
|
|
get { return 0; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fitness < 0)
|
|
|
{
|
|
|
fitness = 0;
|
|
|
}
|
|
|
|
|
|
return fitness;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override bool HasGradient
|
|
|
{
|
|
|
get { return true; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
}
|
|
|
|