-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Consider a simple example. We have a target function - Rastrigin function:
We want to find minimum of function with constraints:
We know that function has global minimum equal 0 in point (0,0). We trying to find approximate solution.
At first, we define target function and constraints.
Func<double[], double> func = (x) => 20 + (x[0] * x[0] - 10 * Math.Cos(2 * Math.PI * x[0])) +
(x[1] * x[1] - 10 * Math.Cos(2 * Math.PI * x[1]));
double[] constr1 = { -5.12, -5.12 };
double[] constr2 = { 5.12, 5.12 };
Each pair (constr1[i], constr2[i])
determine bounds for coordinate.
At second, we define instances of methods.
IOptimizer[] opts = {
new BBBCOptimizer(),
new FireworksOptimizer(),
new GEMOptimizer()
};
Remarks. Methods use random number generator and if you have own implementation, then can use overloaded constructors. Main requirement is implement corresponding interface.
Now, we define parameters for methods.
// Distance between points need for Fireworks method.
// It is squared Euclidean distance.
Func<PointND, PointND, double> distance = (a, b) => (a[0] - b[0]) * (a[0] - b[0]) +
(a[1] - b[1]) * (a[1] - b[1]);
object[] parameters = {
new BBBCParams(20, 100, 0.4, 0.5),
new FireWorksParams(20, 50, distance, 20),
new GEMParams(1, 20, 50, 2 * Math.Sqrt(2), 100)
};
Remarks. Recommendation by choosing parameters is no. You can test methods with different combinations parameters and choose optimum.
At finally, we search solution optimization problem.
string[] names =
{
"BBBC",
"Fireworks",
"GEM"
};
// The definition for general parameters: target function, constraints.
GeneralParams param = new GeneralParams(func, constr1, constr2);
for (int i = 0; i < opts.Length; i++)
{
opts[i].InitializeParameters(parameters[i]);
opts[i].Optimize(param);
Console.WriteLine($"Method: {names[i]}.");
Console.WriteLine(opts[i].Solution);
Console.WriteLine();
}
Remarks. The convergence is not proven. Consequently, you can get more best or worst solution. For more confidence you need run several times method and choose best solution.