MadNLP / DynamicNLPModels.jl

NLPModels for dynamic optimization
MIT License
11 stars 1 forks source link

Added support to efficiently build `SparseLQDynamicModel` from sparse `A, B, Q, R, K, S` #42

Closed dlcole3 closed 2 years ago

dlcole3 commented 2 years ago

Added new _build and _set functions in the sparse.jl source code to support sparse A, B, Q, R, K, and S matrices. Currently, when the problem becomes very large, the sparse formulation is not very fast and it takes a lot of memory allocation. This is largely because the old code treated all A, B, Q, R, K, and S matrices as being dense. Now, the Hessian and Jacobian are set using the rowval, nzval, and colptr attributes of A, B, Q, R, K, and S. This significantly reduces the memory required to build the SparseLQDynamicModel.

For N = 50, nu = 10, ns = 2000, I got the following results using @time dense LQDynamicData: 18.737979 seconds (200.89 k allocations: 9.577 GiB, 9.12% gc time) sparse LQDynamicData: 0.671002 seconds (1.51 M allocations: 134.857 MiB, 2.67% gc time)

Also added tests to ensure that the Hessian and Jacobian resulting from the new sparse functions are the same as before.

dlcole3 commented 2 years ago

Note that the current formulation includes a K matrix in the sparse model. I now realize I don't need that, but I will fix that on a separate PR. I already had the new functions enabled with K when I learned I did not need to use K with the sparse form

codecov-commenter commented 2 years ago

Codecov Report

Merging #42 (ba38441) into main (0365640) will increase coverage by 0.51%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main      #42      +/-   ##
==========================================
+ Coverage   97.30%   97.81%   +0.51%     
==========================================
  Files           4        4              
  Lines        1188     1466     +278     
==========================================
+ Hits         1156     1434     +278     
  Misses         32       32              
Impacted Files Coverage Δ
src/LinearQuadratic/sparse.jl 100.00% <100.00%> (ø)

:mega: Codecov can now indicate which changes are the most critical in Pull Requests. Learn more