/// <summary>
        /// Find vector x that minimizes the function f(x) using the Nelder-Mead Simplex algorithm.
        /// For more options and diagnostics consider to use <see cref="NelderMeadSimplex"/> directly.
        /// </summary>
        public static (double P0, double P1, double P2, double P3) OfFunction(Func <double, double, double, double, double> function, double initialGuess0, double initialGuess1, double initialGuess2, double initialGuess3, double tolerance = 1e-8, int maxIterations = 1000)
        {
            var objective = ObjectiveFunction.Value(v => function(v[0], v[1], v[2], v[3]));
            var result    = NelderMeadSimplex.Minimum(objective, CreateVector.Dense(new[] { initialGuess0, initialGuess1, initialGuess2, initialGuess3 }), tolerance, maxIterations);

            return(result.MinimizingPoint[0], result.MinimizingPoint[1], result.MinimizingPoint[2], result.MinimizingPoint[3]);
        }
Beispiel #2
0
        public static MinimizationResult OfFunction(Func <Vector <double>, double> function, Vector <double> initialGuess, double tolerance = 1e-8, int maxIterations = 1000)
        {
            var objective = ObjectiveFunction.Value(v => function(v));
            var result    = NelderMeadSimplex.Minimum(objective, initialGuess, tolerance, maxIterations);

            return(result);
        }
        /// <summary>
        /// Find vector x that minimizes the function f(x) using the Nelder-Mead Simplex algorithm.
        /// For more options and diagnostics consider to use <see cref="NelderMeadSimplex"/> directly.
        /// </summary>
        public static double OfScalarFunction(Func <double, double> function, double initialGuess, double tolerance = 1e-8, int maxIterations = 1000)
        {
            var objective = ObjectiveFunction.Value(v => function(v[0]));
            var result    = NelderMeadSimplex.Minimum(objective, CreateVector.Dense(new[] { initialGuess }), tolerance, maxIterations);

            return(result.MinimizingPoint[0]);
        }
Beispiel #4
0
        public void Mgh_Tests(TestFunctions.TestCase test_case)
        {
            var obj = new MghObjectiveFunction(test_case.Function, true, true);

            var result = NelderMeadSimplex.Minimum(obj, test_case.InitialGuess, 1e-8, 1000);

            if (test_case.MinimizingPoint != null)
            {
                Assert.That((result.MinimizingPoint - test_case.MinimizingPoint).L2Norm(), Is.LessThan(1e-3));
            }

            var val1    = result.FunctionInfoAtMinimum.Value;
            var val2    = test_case.MinimalValue;
            var abs_min = Math.Min(Math.Abs(val1), Math.Abs(val2));
            var abs_err = Math.Abs(val1 - val2);
            var rel_err = abs_err / abs_min;
            var success = (abs_min <= 1 && abs_err < 1e-3) || (abs_min > 1 && rel_err < 1e-3);

            Assert.That(success, "Minimal function value is not as expected.");
        }