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

ConstructorTest3() private method

private ConstructorTest3 ( ) : void
return void
        public void ConstructorTest3()
        {
            // Easy three dimensional minimization in ellipsoid.
            var function = new NonlinearObjectiveFunction(3, x => x[0] * x[1] * x[2]);

            NonlinearConstraint[] constraints = 
            {
                new NonlinearConstraint(3, x =>  1.0 - x[0] * x[0] - 2.0 * x[1] * x[1] - 3.0 * x[2] * x[2])
            };

            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 sqrthalf = Math.Sqrt(0.5);

            double[] expected = 
            {
                1.0 / Math.Sqrt(3.0), 1.0 / Math.Sqrt(6.0), -1.0 / 3.0
            };


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

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