Accord.Tests.Math.GoldfarbIdnaniTest.GoldfarbIdnaniConstructorTest3 C# (CSharp) Method

GoldfarbIdnaniConstructorTest3() private method

private GoldfarbIdnaniConstructorTest3 ( ) : void
return void
        public void GoldfarbIdnaniConstructorTest3()
        {
            // http://www.wolframalpha.com/input/?i=min+2x%C2%B2+-+xy+%2B+4y%C2%B2+-+5x+-+6y+s.t.+x+-+y++%3D%3D+++5%2C+x++%3E%3D++10

            // Solve the following optimization problem:
            //
            //  min f(x) = 2x² - xy + 4y² - 5x - 6y
            // 
            //  s.t.   x - y  ==   5  (x minus y should be equal to 5)
            //             x  >=  10  (x should be greater than or equal to 10)
            //

            // In this example we will be using some symbolic processing. 
            // The following variables could be initialized to any value.
            double x = 0, y = 0;

            // Create our objective function using a lambda expression
            var f = new QuadraticObjectiveFunction(() => 2 * (x * x) - (x * y) + 4 * (y * y) - 5 * x - 6 * y);

            // Now, create the constraints
            List<LinearConstraint> constraints = new List<LinearConstraint>();
            constraints.Add(new LinearConstraint(f, () => x - y == 5));
            constraints.Add(new LinearConstraint(f, () => x >= 10));

            // Now we create the quadratic programming solver for 2 variables, using the constraints.
            GoldfarbIdnani solver = new GoldfarbIdnani(f, constraints);


            double[,] A = 
            {
                { 1, -1 }, 
                { 1,  0 }, 
            };

            double[] b = 
            {
                 5, 
                10, 
            };

            Assert.IsTrue(A.IsEqual(solver.ConstraintMatrix));
            Assert.IsTrue(b.IsEqual(solver.ConstraintValues));


            double[,] Q = 
            {   
                { +2*2,  -1   }, 
                {   -1,  +4*2 },
            };

            double[] d = { -5, -6 };


            var actualQ = f.QuadraticTerms;
            var actuald = f.LinearTerms;

            Assert.IsTrue(Q.IsEqual(actualQ));
            Assert.IsTrue(d.IsEqual(actuald));


            // And attempt to solve it.
            bool success = solver.Minimize();
            Assert.AreEqual(170, solver.Value);
            Assert.IsTrue(success);
        }