Accord.Tests.Math.CobylaTest.ConstructorTest10 C# (CSharp) Method

ConstructorTest10() private method

private ConstructorTest10 ( ) : void
return void
        public void ConstructorTest10()
        {
            /// This problem is taken from page 415 of Luenberger's book Applied
            /// Nonlinear Programming. It is to maximize the area of a hexagon of
            /// unit diameter.
            /// 
            var function = new NonlinearObjectiveFunction(9, x =>
                -0.5 * (x[0] * x[3] - x[1] * x[2] + x[2] * x[8]
                - x[4] * x[8] + x[4] * x[7] - x[5] * x[6]));

            NonlinearConstraint[] constraints = 
            {
                new NonlinearConstraint(9, x => 1.0 - x[2] * x[2] - x[3] * x[3]),
                new NonlinearConstraint(9, x =>  1.0 - x[8] * x[8]),
                new NonlinearConstraint(9, x =>  1.0 - x[4] * x[4] - x[5] * x[5]),
                new NonlinearConstraint(9, x =>  1.0 - x[0] * x[0] - Math.Pow(x[1] - x[8], 2.0)),
                new NonlinearConstraint(9, x =>  1.0 - Math.Pow(x[0] - x[4], 2.0) - Math.Pow(x[1] - x[5], 2.0)),
                new NonlinearConstraint(9, x =>  1.0 - Math.Pow(x[0] - x[6], 2.0) - Math.Pow(x[1] - x[7], 2.0)),
                new NonlinearConstraint(9, x =>  1.0 - Math.Pow(x[2] - x[4], 2.0) - Math.Pow(x[3] - x[5], 2.0)),
                new NonlinearConstraint(9, x =>  1.0 - Math.Pow(x[2] - x[6], 2.0) - Math.Pow(x[3] - x[7], 2.0)),
                new NonlinearConstraint(9, x =>  1.0 - x[6] * x[6] - Math.Pow(x[7] - x[8], 2.0)),
                new NonlinearConstraint(9, x =>  x[0] * x[3] - x[1] * x[2]),
                new NonlinearConstraint(9, x =>  x[2] * x[8]),
                new NonlinearConstraint(9, x =>  -x[4] * x[8]),
                new NonlinearConstraint(9, x =>  x[4] * x[7] - x[5] * x[6]),
                new NonlinearConstraint(9, x =>  x[8]),
            };

            Cobyla cobyla = new Cobyla(function, constraints);

            for (int i = 0; i < cobyla.Solution.Length; i++)
                cobyla.Solution[i] = 1;

            Assert.IsTrue(cobyla.Minimize());
            double minimum = cobyla.Value;
            double[] solution = cobyla.Solution;

            double[] expected = 
            {
                0.688341, 0.725387, -0.284033, 0.958814, 0.688341, 0.725387, -0.284033, 0.958814, 0.0
            };

            for (int i = 0; i < expected.Length; i++)
                Assert.AreEqual(expected[i], cobyla.Solution[i], 1e-2);
            Assert.AreEqual(-0.86602540378486847, minimum, 1e-10);

            double expectedMinimum = function.Function(cobyla.Solution);
            Assert.AreEqual(expectedMinimum, minimum);
        }