Constrained quadratic programming in Java
Run this example
Complete Getting Started, then run from the repository root:
mvn exec:java -Dexec.mainClass=dev.nm.examples.QuadraticProgrammingExample
Problem and mathematics
Minimize
subject to
Here H = diag(2,2), so the objective is strictly convex and a feasible optimum is unique. This problem is adapted from the NM Dev test identified as example 16.4 in Nocedal and Wright's Numerical Optimization.
Walk through the API
- Construct H and the linear coefficient vector p.
- Create
QuadraticFunction(H,p). The factor one half is part of the API's convention. - Put each inequality's coefficients in one row of A and its right-hand side in b.
- Use
LinearGreaterThanConstraints(A,b): the convention is A*x >= b. - Create
QPProblem(objective, null, constraints). The null argument means no equality constraints. - Call
solve(problem).search()onQPDualActiveSetMinimizer, thenminimizer().
The example uses a tolerance of 1e-10 and an iteration cap of 1,000, rather than the effectively unbounded cap in the test.
Expected result and independent validation
The constraint slacks A*x-b are [0,1.2,4,1.4,1.7]; only the first constraint is active. The executable checks the reference point, objective, every constraint and stationarity.
For the convention Ax >= b, the Lagrangian is f(x) minus lambda-transpose times (Ax-b). At the solution:
Nonnegative multipliers, feasibility, stationarity and complementary slackness certify optimality for this convex problem. This gives stronger evidence than simply comparing a printed vector to another solver's output.
Assumptions and mistakes to avoid
Do not reverse inequality signs without negating both sides. Do not drop the one-half convention when translating a mathematical objective. This example has a positive-definite Hessian; it does not demonstrate that the solver can handle every indefinite, singular or badly scaled problem.
Check feasibility even when the solver returns a point, and make solver failures visible. Tolerances should reflect data scale and application needs.
Try it
Remove the first inequality and solve again. The unconstrained minimizer is (1,2.5) and satisfies the remaining constraints. Explain how the original first constraint moved the optimum.
Source and API reference
Adapted from QPDualActiveSetMinimizerTest.java, test_0030. See source provenance for the exact NM Dev commit and test path. The full runnable Java source linked above includes imports, construction steps and checks. Browse the NM Dev Javadoc for the classes used here.