Managing unnecessary 0's in JuMP


I have a rather ugly quadratic objective function that requires a fair amount of manipulation of variables, and I suspect that one of the things that’s making this process incredibly slow is that I have lots of GenericAffExpr that are carrying around lots of unnecessary variables that have 0.0 coefficients. Is there an elegant way of dropping these?


May you post a MWE?


Here’s the (incredibly hackish) code I wrote for doing this, I guess my question was whether there was an equivalent function already available in JuMP, which perhaps would have been better to use depending on the context

function _dropzeros(ex::T) where T<:JuMP.GenericAffExpr
    mask = Bool[x > eps(Float32) for x ∈ ex.ceoffs]
    T(ex.vars[mask], ex.coeffs[mask], ex.constant)

This seems to work quite well, by the way, and is much faster than I was expecting it to be.


I’m planning on changing AffExpr and QuadExpr to use a dictionary instead of a list of coefficients for JuMP 0.19 (discussion). That would resolve this issue.


That sounds wonderful. I’ve been noticing that things start getting really ugly with quadratic expressions, it gets to the point that everything requires custom code to do efficiently.