public void FindMinimum_RosenbrockTestFunction_AnalyticResult(
            [Values(2, 4, 5, 7)]
            int d)
        {
            var optimizer          = new NelderMeadOptimizer();
            var optimizerAlgorithm = optimizer.Create(d);

            optimizerAlgorithm.Function = optimizer.Function.Create(d, x =>
            {
                double y = 0.0;
                for (int k = 0; k < d - 1; k++)
                {
                    y += 100 * Math.Pow(x[k + 1] - x[k] * x[k], 2) + Math.Pow(x[k] - 1.0, 2);
                }
                return(y);
            });

            /* take an initial guess which is not extremly fare away from the argMin: */
            var actualArgMin = new double[d];

            for (int k = 0; k < d; k++)
            {
                actualArgMin[k] = Math.Min(k, 2.75) * Math.Pow(-1, k);
            }
            double actualMinimum;
            var    state = optimizerAlgorithm.FindMinimum(actualArgMin, out actualMinimum);

            var expectedMinimum = 0.0;

            Assert.That(actualMinimum, Is.EqualTo(expectedMinimum).Within(1E-2), "Minimum");

            var expectedArgMin = Enumerable.Repeat(1.0, d).ToArray();

            Assert.That(actualArgMin, Is.EqualTo(expectedArgMin).AsCollection.Within(1E-1));
        }
        public void FindMinimumGoldsteinPriceFunction_AnalyticResult(MultiDimOptimizerConstraintProvider constraintTransformation)
        {
            var optimizer          = new NelderMeadOptimizer(NelderMeadOptimizer.StandardAbortCondition, constraintTransformation);
            var constraint         = optimizer.Constraint.Create(MultiDimRegion.Interval.Create(2, new[] { -2.0, -2.0 }, new[] { 2.0, 2.0 }));
            var optimizerAlgorithm = optimizer.Create(constraint);

            optimizerAlgorithm.Function = optimizer.Function.Create(2, z =>
            {
                var x = z[0];
                var y = z[1];
                return((1.0 + Math.Pow(x + y + 1.0, 2) * (19.0 - 14.0 * x + 3 * x * x - 14.0 * y + 6.0 * x * y + 3.0 * y * y)) * (30.0 + Math.Pow(2.0 * x - 3.0 * y, 2) * (18.0 - 32.0 * x + 12.0 * x * x + 48.0 * y - 36 * x * y + 27 * y * y)));
            });

            /* take an initial guess which is not extremly fare away from the argMin: */
            var actualArgMin = new double[2];

            actualArgMin[0] = 0.25;
            actualArgMin[1] = -0.7;
            double actualMinimum;
            var    state = optimizerAlgorithm.FindMinimum(actualArgMin, out actualMinimum);

            var expectedMinimum = 3.0;

            Assert.That(actualMinimum, Is.EqualTo(expectedMinimum).Within(1E-2), "Minimum");

            var expectedArgMin = new[] { 0.0, -1.0 };

            Assert.That(actualArgMin, Is.EqualTo(expectedArgMin).AsCollection.Within(1E-1));
        }
Exemple #3
0
            /// <summary>Initializes a new instance of the <see cref="Algorithm"/> class.
            /// </summary>
            /// <param name="optimizer">The <see cref="NelderMeadOptimizer"/> object that serves as factory of the current object.</param>
            /// <param name="dimension">The dimension of the feasible region.</param>
            internal Algorithm(NelderMeadOptimizer optimizer, int dimension)
            {
                m_Optimizer = optimizer;
                Dimension   = dimension;

                m_pTryTemp      = new double[dimension];
                m_Simplex       = new double[dimension + 1][];
                m_SimplexValues = new double[dimension + 1];
                for (int j = 0; j <= dimension; j++)
                {
                    m_Simplex[j] = new double[dimension];
                }
            }