Skip to content
Alexander Kryuchkov edited this page Nov 23, 2017 · 6 revisions

Wiki

How to use?

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

Rastrigin function.

We want to find a minimum of the function with constraints:

Optimization problem.

We know that function has global minimum is equal 0 in point (0,0). We are trying to find an 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]) determines bounds for coordinates.

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 the overloaded constructors. Main requirement is to implement corresponding interface.

Now, we define the parameters of methods.

// Distance between points needs 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 of the 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.Minimize(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 a solution, which better or worse. For more confidence, you need to run several times method and choose best solution.

Full code

The full code of example available in the solution. The project has name Example.

Clone this wiki locally