Exemplo n.º 1
0
        public void Run()
        {
            var function = new F3();

            // var function = new DelegateFunction(x => Math.Pow(x[0] - 3, 2));

            for (int i = 10; i <= 15; i++)
            {
                // Coordinate descent method
                var opt1 = new CoordinateDescentMinimizer();
                opt1.IsOutputPerIterationEnabled = false;
                opt1.Minimize(function, new double[] { i });

                // Golden section search
                var opt2 = new GoldenSectionMinimizer();
                opt2.IsOutputPerIterationEnabled = false;
                opt2.Minimize(function, i);

                // Nelder-Mead search
                var opt3 = new NelderMeadMinimizer();
                opt3.IsOutputPerIterationEnabled = false;
                opt3.Minimize(function, new double[] { i });

                // Hooke-Jeeves pattern search
                var opt4 = new HookeJeevesMinimizer();
                opt4.IsOutputPerIterationEnabled = false;
                opt4.Minimize(function, new double[] { i });
            }
        }
Exemplo n.º 2
0
        public void Run()
        {
            var result    = new List <Tuple <string, string, string, string> >();
            var functions = new List <Tuple <Function, double[]> >()
            {
                new Tuple <Function, double[]>(new F1(), new double[] { -1.9, 2.0 }),
                new Tuple <Function, double[]>(new F2(), new double[] { 0.1, 0.3 }),
                new Tuple <Function, double[]>(new F3(), new double[] { 0, 0, 0, 0, 0 }),
                new Tuple <Function, double[]>(new F4(), new double[] { 5.1, 1.1 })
            };

            for (int i = 0; i < functions.Count; i++)
            {
                var item = functions[i];
                var opt1 = new NelderMeadMinimizer();
                opt1.IsOutputEnabled = false;
                var min1      = opt1.Minimize(item.Item1, item.Item2.Copy());
                var opt1Evals = item.Item1.Evaluations;

                var opt2 = new HookeJeevesMinimizer();
                opt2.IsOutputEnabled = false;
                var min2      = opt2.Minimize(item.Item1, item.Item2.Copy());
                var opt2Evals = item.Item1.Evaluations;

                var opt3 = new CoordinateDescentMinimizer();
                opt3.IsOutputEnabled = false;
                var min3      = opt3.Minimize(item.Item1, item.Item2.Copy());
                var opt3Evals = item.Item1.Evaluations;

                int outputPrecision = 6;

                result.Add(new Tuple <string, string, string, string>((i + 1).ToString(),
                                                                      "min: " + min1.Format(outputPrecision),
                                                                      "min: " + min2.Format(outputPrecision),
                                                                      "min: " + min3.Format(outputPrecision)));
                result.Add(new Tuple <string, string, string, string>(string.Empty,
                                                                      "f(min): " + item.Item1.Value(min1).ToString("F" + outputPrecision),
                                                                      "f(min): " + item.Item1.Value(min2).ToString("F" + outputPrecision),
                                                                      "f(min): " + item.Item1.Value(min3).ToString("F" + outputPrecision)));
                result.Add(new Tuple <string, string, string, string>(string.Empty,
                                                                      "evals: " + opt1Evals.ToString(),
                                                                      "evals: " + opt2Evals.ToString(),
                                                                      "evals: " + opt3Evals.ToString()));
            }

            Console.WriteLine(result.ToStringTable(
                                  new[] { "Function", "Nelder and Mead", "Hooke-Jeeves", "Coordinate Descent" },
                                  a => a.Item1, a => a.Item2, a => a.Item3, a => a.Item4));
            Console.WriteLine();

            // Hooke-Jeeves will stuck at function F4 because of the nature of exploration which explores a better point only
            // in one axis, not all at once, and because at point [3.1 3.1] only [2.975 2.975] is fitter, the algorithm
            // can't determine that there is a better point in the environment
        }
Exemplo n.º 3
0
        public void Run()
        {
            var function = new F4();

            // Nelder-Mead search
            var opt1 = new NelderMeadMinimizer();

            opt1.IsOutputPerIterationEnabled = false;
            opt1.Minimize(function, new double[] { 5, 5 });

            // Hooke-Jeeves pattern search
            var opt2 = new HookeJeevesMinimizer();

            opt2.IsOutputPerIterationEnabled = false;
            opt2.Minimize(function, new double[] { 5, 5 });
        }