Quadratic Programming
Mathwrist Code Example Series

Copyright ©Mathwrist LLC 2023
(October 5, 2023)
Abstract

This document gives some code examples on how to use the quadratic programming (QP) features in Mathwrist’s C++ Numerical Programming Library (NPL).

Indefinite QP

This example is taken from chapter 16.5 of [1] to solve the following indefinite QP problem.

argminx1,x2[x1,x2][100-1][x1x2] s.t. 
x1+2x2 2
-5x1+4x2 10
x1 3
x2 0
1 // 2 general linear constraints, 2 unknowns
2 //    x1 + 2 x2 >= 2
3 // -5 x1 + 4 x2 <= 10
4 LinearProg::Constraints constr(2, 2);
5 constr.A(0, 0) = 1;
6 constr.A(0, 1) = 2;
7 constr.A(1, 0) = -5;
8 constr.A(1, 1) = 4;
9 constr.general_bounds[0] = Bound::lower(2);
10 constr.general_bounds[1] = Bound::upper(10);
12 // simple bounds x1 <= 3, x2 >= 0
13 constr.simple_bounds[0] = Bound::upper(3);
14 constr.simple_bounds[1] = Bound::lower(0);
16 // Indefinite Hessian
17 Matrix G(2,2);
18 G(0, 0) = 1;
19 G(1, 1) = -1;
21 Matrix d;
22 QP_ActiveSet qp(G, d, constr);
24 qp.set_crash_start_radius(0.1);
26 Matrix x(2, 1);
27 x(0, 0) = 2;
28 x(1, 0) = 0;
30 qp(x);
32 // Compare with known solution.
33 assert(std::abs(x(0, 0) - 3) < 1.e-10);
34 assert(std::abs(x(1, 0) - 25. / 4) < 1.e-10);

References

  • [1] Jorge Nocedal and Stephen J. Wright: Numerical Optimization, Springer, 1999