public void ConstructorTest4()
{
// Example code from
// https://groups.google.com/forum/#!topic/accord-net/an0sJGGrOuU
int nVariablesTest = 4; // number of variables
int nConstraintsTest = 2; // number of constraints
double constraintsTolerance = 1e-100;
double[,] ATest = new double[,] { { 1, 2, 3, 4 }, { 0, 4, 3, 1 } }; // arbitary A matrix. A*X = b
double[,] bTest = new double[,] { { 0 }, { 2 } }; // arbitary A matrix. A*X = b
double[,] XSolve = ATest.Solve(bTest); // uses the pseudoinverse to minimise norm(X) subject to A*X = b
// recreate Solve function using AugmentedLagrangian
var fTest = new NonlinearObjectiveFunction(nVariablesTest, ds => ds.InnerProduct(ds), ds => ds.Multiply(2.0)); // minimise norm(ds)
var nonlinearConstraintsTest = new List<NonlinearConstraint>(nConstraintsTest); // linear constraints A*X = b
for (int i = 0; i < nConstraintsTest; i++)
{
int j = i; // http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx
nonlinearConstraintsTest.Add(new NonlinearConstraint(fTest, ds => ATest.GetRow(j).InnerProduct(ds) - (double)bTest.GetValue(j, 0), ConstraintType.EqualTo, 0.0, ds => ATest.GetRow(j), constraintsTolerance));
}
var innerSolverTest = new ResilientBackpropagation(nVariablesTest);
innerSolverTest.Tolerance = constraintsTolerance;
innerSolverTest.Iterations = 1000;
var solverTest = new Accord.Math.Optimization.AugmentedLagrangian(innerSolverTest, fTest, nonlinearConstraintsTest);
solverTest.MaxEvaluations = 0;
bool didMinimise = solverTest.Minimize();
var errorConstraintRelative = XSolve.Subtract(solverTest.Solution, 1).ElementwiseDivide(XSolve); // relative error between .Solve and .Minimize
var errorConstraintAbsolute = XSolve.Subtract(solverTest.Solution, 1); // absolute error between .Solve and .Minimize
double[] errorConstraintsTest = new double[nConstraintsTest];
for (int i = 0; i < nConstraintsTest; i++)
{
errorConstraintsTest[i] = nonlinearConstraintsTest[i].Function(solverTest.Solution);
}
}
}