public void GoldfarbIdnaniConstructorTest7()
{
// Solve the following optimization problem:
//
// min f(x) = 3x² + 2xy + 3y² - y
//
// s.t. x >= 1
// y >= 1
//
double x = 0, y = 0;
// http://www.wolframalpha.com/input/?i=min+x%C2%B2+%2B+2xy+%2B+y%C2%B2+-+y%2C+x+%3E%3D+1%2C+y+%3E%3D+1
var f = new QuadraticObjectiveFunction(() => 3 * (x * x) + 2 * (x * y) + 3 * (y * y) - y);
List<LinearConstraint> constraints = new List<LinearConstraint>();
constraints.Add(new LinearConstraint(f, () => x >= 1));
constraints.Add(new LinearConstraint(f, () => y >= 1));
GoldfarbIdnani target = new GoldfarbIdnani(f, constraints);
double[,] A =
{
{ 1, 0 },
{ 0, 1 },
};
double[] b =
{
1,
1,
};
Assert.IsTrue(A.IsEqual(target.ConstraintMatrix));
Assert.IsTrue(b.IsEqual(target.ConstraintValues));
double[,] Q =
{
{ 6, 2 },
{ 2, 6 },
};
double[] d = { 0, -1 };
var actualQ = f.QuadraticTerms;
var actuald = f.LinearTerms;
Assert.IsTrue(Q.IsEqual(actualQ));
Assert.IsTrue(d.IsEqual(actuald));
bool success = target.Minimize();
double minValue = target.Value;
Assert.IsTrue(success);
double[] solution = target.Solution;
Assert.AreEqual(7, minValue);
Assert.AreEqual(1, solution[0]);
Assert.AreEqual(1, solution[1]);
Assert.AreEqual(8, target.Lagrangian[0], 1e-5);
Assert.AreEqual(7, target.Lagrangian[1], 1e-5);
foreach (double v in target.Solution)
Assert.IsFalse(double.IsNaN(v));
foreach (double v in target.Lagrangian)
Assert.IsFalse(double.IsNaN(v));
}