ConstrainedProblem<T>
Models & Types · AiDotNet.Solvers.Constrained
A general nonlinear optimization problem: minimize f(x) subject to h(x) = 0 and
g(x) ≤ 0.
For Beginners
Three pieces describe a constrained problem: what you are minimizing,
what must come out exactly right (the equalities), and what must not be exceeded (the
inequalities). Anything you leave out simply does not constrain the answer — pass null.
How It Works
This is the shape every constrained problem reduces to, and the one Chapter 6 of the optimization
literature states the KKT conditions over. Note the sign convention: inequalities are written
g(x) ≤ 0, so a limit like x ≤ 5 is supplied as g(x) = x − 5.
Each constraint block is one function returning both the constraint values and their Jacobian —
row i holding the gradient of constraint i. They are supplied together because a
solver needs both at the same point on every iteration, and evaluating them in one pass lets a
caller share the expensive intermediate work between them.
Example
// minimize x² + y² subject to x + y = 1
var problem = new ConstrainedProblem<double>(
objective: p => (p[0] * p[0] + p[1] * p[1],
new Vector<double>(new[] { 2 * p[0], 2 * p[1] })),
equalityConstraints: p =>
{
var values = new Vector<double>(new[] { p[0] + p[1] - 1.0 });
var jacobian = new Matrix<double>(1, 2);
jacobian[0, 0] = 1.0;
jacobian[0, 1] = 1.0;
return (values, jacobian);
});
Constructors
| Constructor | Summary |
|---|---|
ConstrainedProblem(Func<Vector<T>, ValueTuple<T, Vector<T>>> objective, Func<Vector<T>, ValueTuple<Vector<T>, Matrix<T>>> equalityConstraints, Func<Vector<T>, ValueTuple<Vector<T>, Matrix<T>>> inequalityConstraints) |
Creates a constrained problem. |
Properties
| Property | Summary |
|---|---|
Func<Vector<T>, ValueTuple<T, Vector<T>>> Objective { get; } |
Gets the objective, returning its value and gradient at a point. |
Func<Vector<T>, ValueTuple<Vector<T>, Matrix<T>>> EqualityConstraints { get; } |
Gets the equality constraints h(x) = 0 and their Jacobian, or null when there are none. |
Func<Vector<T>, ValueTuple<Vector<T>, Matrix<T>>> InequalityConstraints { get; } |
Gets the inequality constraints g(x) ≤ 0 and their Jacobian, or null when there are none. |