|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
namespace SwarmOps.Optimizers.Parallel
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PSO : Optimizer
|
|
|
{
|
|
|
#region Constructors.
|
|
|
|
|
|
|
|
|
|
|
|
public PSO()
|
|
|
: this(1)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PSO(Problem problem)
|
|
|
: this(1, problem)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PSO(int numAgentsMultiple)
|
|
|
: base()
|
|
|
{
|
|
|
NumAgentsMultiple = numAgentsMultiple;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PSO(int numAgentsMultiple, Problem problem)
|
|
|
: base(problem)
|
|
|
{
|
|
|
NumAgentsMultiple = numAgentsMultiple;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Sets of control parameters.
|
|
|
|
|
|
|
|
|
|
|
|
public struct Parameters
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly double[] HandTuned = { 50.0, 0.729, 1.49445, 1.49445 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly double[] AllBenchmarks5Dim10000Iter = { 72.0, -0.4031, -0.5631, 3.4277 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static readonly double[] AllBenchmarks30Dim60000Iter = { 64.0, -0.2063, -2.7449, 2.3198 };
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Get 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 GetOmega(double[] parameters)
|
|
|
{
|
|
|
return parameters[1];
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double GetPhiP(double[] parameters)
|
|
|
{
|
|
|
return parameters[2];
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double GetPhiG(double[] parameters)
|
|
|
{
|
|
|
return parameters[3];
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Base-class overrides, Problem.
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
{
|
|
|
get { return "PSO-Par" + NumAgentsMultiple; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override int Dimensionality
|
|
|
{
|
|
|
get { return 4; }
|
|
|
}
|
|
|
|
|
|
string[] _parameterName = { "S", "omega", "phi_p", "phi_g" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override string[] ParameterName
|
|
|
{
|
|
|
get { return _parameterName; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override double[] DefaultParameters
|
|
|
{
|
|
|
get { return Parameters.AllBenchmarks30Dim60000Iter; }
|
|
|
}
|
|
|
|
|
|
static readonly double[] _lowerBound = { 1.0, -2.0, -4.0, -4.0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override double[] LowerBound
|
|
|
{
|
|
|
get { return _lowerBound; }
|
|
|
}
|
|
|
|
|
|
static readonly double[] _upperBound = { 200.0, 2.0, 4.0, 4.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 omega = GetOmega(parameters);
|
|
|
double phiP = GetPhiP(parameters);
|
|
|
double phiG = GetPhiG(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[][] agents = Tools.NewMatrix(numAgents, n);
|
|
|
double[][] velocities = Tools.NewMatrix(numAgents, n);
|
|
|
double[][] bestPosition = Tools.NewMatrix(numAgents, n);
|
|
|
double[] bestFitness = new double[numAgents];
|
|
|
bool[] bestFeasible = new bool[numAgents];
|
|
|
|
|
|
|
|
|
double[] velocityLowerBound = new double[n];
|
|
|
double[] velocityUpperBound = new double[n];
|
|
|
|
|
|
|
|
|
int i, j, k;
|
|
|
|
|
|
|
|
|
double[] g = null;
|
|
|
double gFitness = Problem.MaxFitness;
|
|
|
bool gFeasible = false;
|
|
|
|
|
|
|
|
|
for (k = 0; k < n; k++)
|
|
|
{
|
|
|
double range = System.Math.Abs(upperBound[k] - lowerBound[k]);
|
|
|
|
|
|
velocityLowerBound[k] = -range;
|
|
|
velocityUpperBound[k] = range;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < numAgents; j++)
|
|
|
{
|
|
|
|
|
|
double[] x = agents[j];
|
|
|
double[] v = velocities[j];
|
|
|
|
|
|
|
|
|
Tools.InitializeUniform(ref v, velocityLowerBound, velocityUpperBound);
|
|
|
|
|
|
|
|
|
Tools.InitializeUniform(ref x, lowerInit, upperInit);
|
|
|
|
|
|
|
|
|
bestFeasible[j] = Problem.EnforceConstraints(ref x);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x.CopyTo(bestPosition[j], 0);
|
|
|
}
|
|
|
|
|
|
|
|
|
System.Threading.Tasks.Parallel.For(0, numAgents, Globals.ParallelOptions, (jPar) =>
|
|
|
{
|
|
|
bestFitness[jPar] = Problem.Fitness(bestPosition[jPar], bestFeasible[jPar]);
|
|
|
});
|
|
|
|
|
|
|
|
|
for (j = 0; j < numAgents; j++)
|
|
|
{
|
|
|
if (Tools.BetterFeasibleFitness(gFeasible, bestFeasible[j], gFitness, bestFitness[j]))
|
|
|
{
|
|
|
|
|
|
|
|
|
g = bestPosition[j];
|
|
|
gFitness = bestFitness[j];
|
|
|
gFeasible = bestFeasible[j];
|
|
|
}
|
|
|
|
|
|
|
|
|
Trace(j, gFitness, gFeasible);
|
|
|
}
|
|
|
|
|
|
|
|
|
for (i = numAgents; Problem.Continue(i, gFitness, gFeasible); )
|
|
|
{
|
|
|
|
|
|
for (j = 0; j < numAgents; j++)
|
|
|
{
|
|
|
|
|
|
double[] x = agents[j];
|
|
|
double[] v = velocities[j];
|
|
|
double[] p = bestPosition[j];
|
|
|
|
|
|
|
|
|
double rP = Globals.Random.Uniform();
|
|
|
double rG = Globals.Random.Uniform();
|
|
|
|
|
|
|
|
|
for (k = 0; k < n; k++)
|
|
|
{
|
|
|
v[k] = omega * v[k] + phiP * rP * (p[k] - x[k]) + phiG * rG * (g[k] - x[k]);
|
|
|
}
|
|
|
|
|
|
|
|
|
Tools.Denormalize(ref v);
|
|
|
|
|
|
|
|
|
Tools.Bound(ref v, velocityLowerBound, velocityUpperBound);
|
|
|
|
|
|
|
|
|
for (k = 0; k < n; k++)
|
|
|
{
|
|
|
x[k] = x[k] + v[k];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
System.Threading.Tasks.Parallel.For(0, numAgents, Globals.ParallelOptions, (jPar) =>
|
|
|
{
|
|
|
|
|
|
bool newFeasible = Problem.EnforceConstraints(ref agents[jPar]);
|
|
|
|
|
|
|
|
|
if (Tools.BetterFeasible(bestFeasible[jPar], newFeasible))
|
|
|
{
|
|
|
double newFitness = Problem.Fitness(agents[jPar], bestFitness[jPar], bestFeasible[jPar], newFeasible);
|
|
|
|
|
|
|
|
|
if (Tools.BetterFeasibleFitness(bestFeasible[jPar], newFeasible, bestFitness[jPar], newFitness))
|
|
|
{
|
|
|
|
|
|
|
|
|
agents[jPar].CopyTo(bestPosition[jPar], 0);
|
|
|
bestFitness[jPar] = newFitness;
|
|
|
bestFeasible[jPar] = newFeasible;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
for (j = 0; j < numAgents; j++, i++)
|
|
|
{
|
|
|
if (Tools.BetterFeasibleFitness(gFeasible, bestFeasible[j], gFitness, bestFitness[j]))
|
|
|
{
|
|
|
|
|
|
|
|
|
g = bestPosition[j];
|
|
|
gFitness = bestFitness[j];
|
|
|
gFeasible = bestFeasible[j];
|
|
|
}
|
|
|
|
|
|
|
|
|
Trace(i, gFitness, gFeasible);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
Problem.EndOptimizationRun();
|
|
|
|
|
|
|
|
|
return new Result(g, gFitness, gFeasible, i);
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
} |