Linear solvers
We suppose that the KKT system has been assembled previously into a given AbstractKKTSystem. Then, it remains to compute the Newton step by solving the KKT system for a given right-hand-side (given as a AbstractKKTVector). That's exactly the role of the linear solver.
If we do not assume any structure, the KKT system writes in generic form
\[K x = b\]
with $K$ the KKT matrix and $b$ the current right-hand-side. MadNLP provides a suite of specialized linear solvers to solve the linear system.
Inertia detection
If the matrix $K$ has negative eigenvalues, we have no guarantee that the solution of the KKT system is a descent direction with regards to the original nonlinear problem. That's the reason why most of the linear solvers compute the inertia of the linear system when factorizing the matrix $K$. The inertia counts the number of positive, negative and zero eigenvalues in the matrix. If the inertia does not meet a given criteria, then the matrix $K$ is regularized by adding a multiple of the identity to it: $K_r = K + \alpha I$.
We recall that the inertia of a matrix $K$ is given as a triplet $(n_+, n_-, n_0)$, with $n_+$ the number of positive eigenvalues, $n_-$ the number of negative eigenvalues and $n_0$ the number of zero eigenvalues.
Factorization algorithm
In nonlinear programming, it is common to employ an $LBL^\top$ factorization (with $L$ lower triangular and $B$ block-diagonal) to decompose the symmetric indefinite matrix $K$, as this algorithm returns the inertia of the matrix directly as a result of the factorization.
When MadNLP runs in inertia-free mode, the algorithm does not require to compute the inertia when factorizing the matrix $K$. In that case, MadNLP can use a classical LU or QR factorization to solve the linear system $Kx = b$.
Solving a KKT system with MadNLP
We suppose available a AbstractKKTSystem kkt, properly assembled following the procedure presented previously. We can query the assembled matrix $K$ as
K = MadNLP.get_kkt(kkt)6×6 SparseArrays.SparseMatrixCSC{Float64, Int32} with 13 stored entries:
2.0 ⋅ ⋅ ⋅ ⋅ ⋅
0.0 200.0 ⋅ ⋅ ⋅ ⋅
⋅ ⋅ 0.0 ⋅ ⋅ ⋅
⋅ ⋅ ⋅ 0.0 ⋅ ⋅
0.0 0.0 -1.0 ⋅ 0.0 ⋅
1.0 0.0 ⋅ -1.0 ⋅ 0.0Then, if we want to pass the KKT matrix K to Lapack, this translates to
linear_solver = LapackCPUSolver(K)LapackCPUSolver{Float64, SparseArrays.SparseMatrixCSC{Float64, Int32}, MadNLP.BUNCHKAUFMAN}(sparse(Int32[1, 2, 5, 6, 2, 5, 6, 3, 5, 4, 6, 5, 6], Int32[1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6], [2.0, 0.0, 0.0, 1.0, 200.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 0.0, 0.0], 6, 6), [6.21944439511105e-310 6.2193816143321e-310 … 2.0e-323 6.21938161437006e-310; 6.21938161430524e-310 6.21945060589447e-310 … 0.0 6.2194418101655e-310; … ; 6.21938161434477e-310 6.21938161433686e-310 … 6.21945060589447e-310 6.2193820083973e-310; 6.2193816143274e-310 6.2193816143495e-310 … 6.2193816143669e-310 1.5e-323], 6, Float64[], Float64[], Float64[], [384.0, 4.89060827e-315, 1.396151737735937e-309, NaN, 2.124003295e-314, 0.0, 0.0, 1.395e-320, 1.35807730622e-312, 1.3910743614899e-309 … 3.16e-322, 0.0, 0.0, 2.0e-323, 0.0, 2.121995801e-314, 1.3262474e-315, NaN, 3.16e-322, 1.966e-321], 384, [0], -1, Base.RefValue{Int64}(0), [125881655262144, 125883119203664, 125881655262160, 125883119203664, 125881655262176, 125883119203664], MadNLP.LapackOptions(MadNLP.BUNCHKAUFMAN), MadNLP.MadNLPLogger(MadNLP.INFO, MadNLP.INFO, nothing))The instance linear_solver does not copy the matrix $K$ and instead keep a reference to it.
linear_solver.A === KtrueThat way every time we re-assemble the matrix $K$ in kkt, the values are directly updated inside linear_solver.
To compute the factorization inside linear_solver, one simply has to call:
MadNLP.factorize!(linear_solver)LapackCPUSolver{Float64, SparseArrays.SparseMatrixCSC{Float64, Int32}, MadNLP.BUNCHKAUFMAN}(sparse(Int32[1, 2, 5, 6, 2, 5, 6, 3, 5, 4, 6, 5, 6], Int32[1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6], [2.0, 0.0, 0.0, 1.0, 200.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 0.0, 0.0], 6, 6), [2.0 0.0 … 0.0 0.0; 0.0 200.0 … 0.0 0.0; … ; 0.0 0.0 … 0.0 0.0; 0.5 0.0 … -1.0 -0.5], 6, Float64[], Float64[], Float64[], [384.0, 4.89060827e-315, 1.396151737735937e-309, NaN, 2.124003295e-314, 0.0, 0.0, 1.395e-320, 1.35807730622e-312, 1.3910743614899e-309 … 3.16e-322, 0.0, 0.0, 2.0e-323, 0.0, 2.121995801e-314, 1.3262474e-315, NaN, 3.16e-322, 1.966e-321], 384, [0], -1, Base.RefValue{Int64}(0), [1, 2, -5, -5, -6, -6], MadNLP.LapackOptions(MadNLP.BUNCHKAUFMAN), MadNLP.MadNLPLogger(MadNLP.INFO, MadNLP.INFO, nothing))Once the factorization computed, computing the backsolve for a right-hand-side b amounts to
nk = size(kkt, 1)
b = rand(nk)
MadNLP.solve_linear_system!(linear_solver, b)6-element Vector{Float64}:
0.10117786014243502
0.003181191259268898
-0.1518922233779787
-0.5545626014656571
-0.6998323987956749
-0.1917892807863918The values of b being modified inplace to store the solution $x$ of the linear system $Kx =b$.