|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
namespace SwarmOps.Optimizers
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class GD : Optimizer
|
|
|
{
|
|
|
#region Constructors.
|
|
|
|
|
|
|
|
|
|
|
|
public GD()
|
|
|
: base()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public GD(Problem problem)
|
|
|
: base(problem)
|
|
|
{
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Get control parameters.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double GetStepsize(double[] parameters)
|
|
|
{
|
|
|
return parameters[0];
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Base-class overrides, Problem.
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
{
|
|
|
get { return "GD"; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override int Dimensionality
|
|
|
{
|
|
|
get { return 1; }
|
|
|
}
|
|
|
|
|
|
string[] _parameterName = { "Stepsize" };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override string[] ParameterName
|
|
|
{
|
|
|
get { return _parameterName ; }
|
|
|
}
|
|
|
|
|
|
static readonly double[] _defaultParameters = { 0.05 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override double[] DefaultParameters
|
|
|
{
|
|
|
get { return _defaultParameters; }
|
|
|
}
|
|
|
|
|
|
static readonly double[] _lowerBound = { 0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override double[] LowerBound
|
|
|
{
|
|
|
get { return _lowerBound; }
|
|
|
}
|
|
|
|
|
|
static readonly double[] _upperBound = { 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();
|
|
|
|
|
|
|
|
|
double stepsize = GetStepsize(parameters);
|
|
|
|
|
|
|
|
|
double[] lowerBound = Problem.LowerBound;
|
|
|
double[] upperBound = Problem.UpperBound;
|
|
|
double[] lowerInit = Problem.LowerInit;
|
|
|
double[] upperInit = Problem.UpperInit;
|
|
|
int n = Problem.Dimensionality;
|
|
|
|
|
|
|
|
|
double[] x = new double[n];
|
|
|
double[] v = new double[n];
|
|
|
double[] g = new double[n];
|
|
|
|
|
|
|
|
|
Tools.InitializeUniform(ref x, lowerInit, upperInit);
|
|
|
|
|
|
|
|
|
bool feasible = Problem.EnforceConstraints(ref x);
|
|
|
|
|
|
|
|
|
|
|
|
double fitness = Problem.Fitness(x, feasible);
|
|
|
|
|
|
|
|
|
x.CopyTo(g, 0);
|
|
|
|
|
|
|
|
|
Trace(0, fitness, feasible);
|
|
|
|
|
|
int i;
|
|
|
for (i = 1; Problem.Continue(i, fitness, feasible); i++)
|
|
|
{
|
|
|
|
|
|
int gradientIterations = Problem.Gradient(x, ref v);
|
|
|
|
|
|
|
|
|
double gradientNorm = Tools.Norm(v);
|
|
|
|
|
|
|
|
|
double normalizedStepsize = stepsize / gradientNorm;
|
|
|
|
|
|
|
|
|
for (int j = 0; j < n; j++)
|
|
|
{
|
|
|
x[j] -= normalizedStepsize * v[j];
|
|
|
}
|
|
|
|
|
|
|
|
|
bool newFeasible = Problem.EnforceConstraints(ref x);
|
|
|
|
|
|
|
|
|
if (Tools.BetterFeasible(feasible, newFeasible))
|
|
|
{
|
|
|
|
|
|
double newFitness = Problem.Fitness(x, fitness, feasible, newFeasible);
|
|
|
|
|
|
|
|
|
if (Tools.BetterFeasibleFitness(feasible, newFeasible, fitness, newFitness))
|
|
|
{
|
|
|
|
|
|
x.CopyTo(g, 0);
|
|
|
|
|
|
|
|
|
fitness = newFitness;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
Trace(i, fitness, feasible);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Problem.EndOptimizationRun();
|
|
|
|
|
|
|
|
|
return new Result(g, fitness, feasible, i);
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
} |