Skip to content
Alexander Kriuchkov edited this page Apr 4, 2017 · 6 revisions

Wiki

How use?

Consider a simple example. We have a target function - Rastrigin function:

Rastrigin function.

We want to find minimum of function with constraints:

Optimization problem.

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<BBBCParams> bbbc = new BBBCOptimizer();
IOptimizer<FWParams> fw = new FWOptimizer();
IOptimizer<GEMParams> gem = 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]);

BBBCParams param1 = new BBBCParams(20, 100, 0.4, 0.5);
FWParams param2 = new FWParams(20, 100, distance, 20);
GEMParams param3 = new GEMParams(1, 100, 50, 2 * Math.Sqrt(2), 100);

Remarks. For choosing parameters, you can test methods with different combinations of parameters and choose optimum.

At finally, we search solution optimization problem.

            Console.WriteLine("Exact solution: f(x) = 0, x = (0,0).");
            Console.WriteLine();

            Test(bbbc, param1, param, "BBBC");
            Test(fw, param2, param, "Fireworks");
            Test(gem, param3, param, "GEM");

            Console.WriteLine("Complete");
            Console.ReadKey();


        static void Test<T>(IOptimizer<T> Opt, T Parameters, GeneralParams GenParam, string Method)
        {
            Opt.InitializeParameters(Parameters);

            PointND bestSolution = null;

            double min = 0;

            for (int i = 0; i < 10; i++)
            {
                Opt.Optimize(GenParam);

                if (bestSolution == null)
                {
                    bestSolution = Opt.Solution;
                    min = bestSolution[2];
                }
                    
                else if(Opt.Solution[2] < min)
                {
                    bestSolution = Opt.Solution;
                    min = bestSolution[2];
                }
            }
            
            Console.WriteLine($"Method: {Method}.");
            Console.WriteLine($"Solution: f(x) = {bestSolution[2]}, x = ({bestSolution[0]}, {bestSolution[1]}).");
            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.

Full code

Here full code of example.

Clone this wiki locally