mechmotum / cyipopt

Cython interface for the interior point optimzer IPOPT
Eclipse Public License 2.0
223 stars 53 forks source link

Are sparsity of matrices computed automatically when JAX is used? #205

Closed brunomorampc closed 1 year ago

brunomorampc commented 1 year ago

I read in the documentation that I should provide the matrix sparsity or IPOPT will assume they are dense. Is this automatically taken care of when JAX is used like in the example here https://cyipopt.readthedocs.io/en/stable/tutorial.html#algorithmic-differentation or should I still provide them?

brocksam commented 1 year ago

No, they are not. JAX is not (yet) able to determine the structural sparsity of the derivatives it computes, although there has been some discussion on the matter.

You will need to determine the Jacobian and Hessian sparsity structures yourself and pass these to CyIpopt to take advantage of sparsity. I think you will also need to convert the dense NumPy-like arrays that JAX returns to sparse ones. You could try SciPy sparse array or it also looks like there's any experimental module in JAX for sparse arrays.

If you don't know or aren't able to produce the structural sparsity yourself then you can approximate it using a random sampling approach. Seed your derivative matrix with random values for all of its variables and detect which elements are zero. Do this for a few sets of random variable values and if an entry is zero for all then it's likely a structural zero. This approach isn't foolproof but gives a good approximation for structural sparsity.

brunomorampc commented 1 year ago

Thank you!