|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
namespace SwarmOps.Optimizers.Parallel
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class DE : Optimizer
|
|
|
{
|
|
|
#region Constructors.
|
|
|
|
|
|
|
|
|
|
|
|
public DE()
|
|
|
: this(1)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DE(Problem problem)
|
|
|
: this(1, problem)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DE(int numAgentsMultiple)
|
|
|
: base()
|
|
|
{
|
|
|
NumAgentsMultiple = numAgentsMultiple;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DE(int numAgentsMultiple, Problem problem)
|
|
|
: base(problem)
|
|
|
{
|
|
|
NumAgentsMultiple = numAgentsMultiple;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Sets of control parameters.
|
|
|
|
|
|
|
|
|
|
|
|
public struct Parameters
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly double[] AllBenchmarks5Dim10000Iter = { 32.0, 0.4845, 0.9833 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly double[] AllBenchmarks30Dim60000Iter = { 32.0, 0.3176, 0.5543 };
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Get individual control parameters.
|
|
|
|
|
|
|
|
|
|
|
|
public int NumAgentsMultiple
|
|
|
{
|
|
|
get;
|
|
|
protected set;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int GetNumAgents(double[] parameters)
|
|
|
{
|
|
|
int numAgents = (int)System.Math.Round(parameters[0], System.MidpointRounding.AwayFromZero);
|
|
|
|
|
|
|
|
|
numAgents--;
|
|
|
int mod = numAgents % NumAgentsMultiple;
|
|
|
numAgents += NumAgentsMultiple - mod;
|
|
|
|
|
|
return numAgents;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double GetCR(double[] parameters)
|
|
|
{
|
|
|
|
|
|
return parameters[1];
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double GetF(double[] parameters)
|
|
|
{
|
|
|
return parameters[2];
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Base-class overrides, Problem.
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
{
|
|
|
get { return "DE-Simple-Par" + NumAgentsMultiple; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override int Dimensionality
|
|
|
{
|
|
|
get { return 3; }
|
|
|
}
|
|
|
|
|
|
string[] _parameterName = { "NP", "CR", "F" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override string[] ParameterName
|
|
|
{
|
|
|
get { return _parameterName; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override double[] DefaultParameters
|
|
|
{
|
|
|
get { return Parameters.AllBenchmarks30Dim60000Iter; }
|
|
|
}
|
|
|
|
|
|
static readonly double[] _lowerBound = { 3, 0, 0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override double[] LowerBound
|
|
|
{
|
|
|
get { return _lowerBound; }
|
|
|
}
|
|
|
|
|
|
static readonly double[] _upperBound = { 200, 1, 2.0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override double[] UpperBound
|
|
|
{
|
|
|
get { return _upperBound; }
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Base-class overrides, Optimizer.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override Result Optimize(double[] parameters)
|
|
|
{
|
|
|
Debug.Assert(parameters != null && parameters.Length == Dimensionality);
|
|
|
|
|
|
|
|
|
Problem.BeginOptimizationRun();
|
|
|
|
|
|
|
|
|
int numAgents = GetNumAgents(parameters);
|
|
|
double CR = GetCR(parameters);
|
|
|
double F = GetF(parameters);
|
|
|
|
|
|
Debug.Assert(numAgents > 0);
|
|
|
|
|
|
|
|
|
double[] lowerBound = Problem.LowerBound;
|
|
|
double[] upperBound = Problem.UpperBound;
|
|
|
double[] lowerInit = Problem.LowerInit;
|
|
|
double[] upperInit = Problem.UpperInit;
|
|
|
int n = Problem.Dimensionality;
|
|
|
|
|
|
|
|
|
double[][] agentsX = Tools.NewMatrix(numAgents, n);
|
|
|
double[][] agentsY = Tools.NewMatrix(numAgents, n);
|
|
|
double[] fitnessX = new double[numAgents];
|
|
|
double[] fitnessY = new double[numAgents];
|
|
|
bool[] feasibleX = new bool[numAgents];
|
|
|
bool[] feasibleY = new bool[numAgents];
|
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
|
double[] g = null;
|
|
|
double gFitness = Problem.MaxFitness;
|
|
|
bool gFeasible = false;
|
|
|
|
|
|
|
|
|
for (j = 0; j < numAgents; j++)
|
|
|
{
|
|
|
|
|
|
Tools.InitializeUniform(ref agentsX[j], lowerInit, upperInit);
|
|
|
|
|
|
|
|
|
feasibleX[j] = Problem.EnforceConstraints(ref agentsX[j]);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
System.Threading.Tasks.Parallel.For(0, numAgents, Globals.ParallelOptions, (jPar) =>
|
|
|
{
|
|
|
fitnessX[jPar] = Problem.Fitness(agentsX[jPar], feasibleX[jPar]);
|
|
|
});
|
|
|
|
|
|
|
|
|
for (j = 0; j < numAgents; j++)
|
|
|
{
|
|
|
if (Tools.BetterFeasibleFitness(gFeasible, feasibleX[j], gFitness, fitnessX[j]))
|
|
|
{
|
|
|
g = agentsX[j];
|
|
|
gFitness = fitnessX[j];
|
|
|
gFeasible = feasibleX[j];
|
|
|
}
|
|
|
|
|
|
|
|
|
Trace(j, gFitness, gFeasible);
|
|
|
}
|
|
|
|
|
|
|
|
|
for (i = numAgents; Problem.Continue(i, gFitness, gFeasible); )
|
|
|
{
|
|
|
|
|
|
for (j=0; j<numAgents; j++)
|
|
|
{
|
|
|
|
|
|
double[] x = agentsX[j];
|
|
|
|
|
|
|
|
|
double[] y = agentsY[j];
|
|
|
|
|
|
|
|
|
int R = Globals.Random.Index(n);
|
|
|
|
|
|
|
|
|
|
|
|
int R1, R2;
|
|
|
Globals.Random.Index2(numAgents, out R1, out R2);
|
|
|
|
|
|
|
|
|
double[] a = agentsX[R1];
|
|
|
double[] b = agentsX[R2];
|
|
|
|
|
|
|
|
|
for (int k = 0; k < n; k++)
|
|
|
{
|
|
|
if (k == R || Globals.Random.Uniform() < CR)
|
|
|
{
|
|
|
y[k] = g[k] + F * (a[k] - b[k]);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
y[k] = x[k];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
System.Threading.Tasks.Parallel.For(0, numAgents, Globals.ParallelOptions, (jPar) =>
|
|
|
{
|
|
|
|
|
|
feasibleY[jPar] = Problem.EnforceConstraints(ref agentsY[jPar]);
|
|
|
|
|
|
|
|
|
if (Tools.BetterFeasible(feasibleX[jPar], feasibleY[jPar]))
|
|
|
{
|
|
|
fitnessY[jPar] = Problem.Fitness(agentsY[jPar], fitnessX[jPar], feasibleX[jPar], feasibleY[jPar]);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
for (j = 0; j < numAgents; j++, i++)
|
|
|
{
|
|
|
|
|
|
if (Tools.BetterFeasibleFitness(feasibleX[j], feasibleY[j], fitnessX[j], fitnessY[j]))
|
|
|
{
|
|
|
|
|
|
agentsY[j].CopyTo(agentsX[j], 0);
|
|
|
|
|
|
|
|
|
fitnessX[j] = fitnessY[j];
|
|
|
|
|
|
|
|
|
feasibleX[j] = feasibleY[j];
|
|
|
|
|
|
|
|
|
if (Tools.BetterFeasibleFitness(gFeasible, feasibleX[j], gFitness, fitnessX[j]))
|
|
|
{
|
|
|
g = agentsX[j];
|
|
|
gFitness = fitnessX[j];
|
|
|
gFeasible = feasibleX[j];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
Trace(i, gFitness, gFeasible);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
Problem.EndOptimizationRun();
|
|
|
|
|
|
|
|
|
return new Result(g, gFitness, gFeasible, i);
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
} |