Open lizcarlson opened 9 months ago
Thanks, in order to avoid accessing internal fields and use the API, I guess something like this would work
drop_almost_zero(a::Number, tol) = abs(a) > tol ? a : zero(a)
drop_almost_zero(a::JuMP.VariableRef, tol) = a
drop_almost_zero(a::JuMP.AbstractJuMPScalar, tol) = JuMP.map_coefficients(Base.Fix2(drop_almost_zero, tol), a)
drop_almost_zero(a::MP.AbstractPolynomialLike, tol) = MP.map_coefficients(Base.Fix2(drop_almost_zero, tol), a)
It has been requested that I post the following hack for a clean routine I wrote for polynomials with tunable coefficients in a SOS SDP program (minimal example at the end of this post). In particular, take a polynomial with tunable coefficients, which you would like to optimize the problem over. If the polynomial has coefficients which are monomial in the tunable quantities, the cleanup routine is trivial. However, if the polynomials has coefficients which are polynomial in the tunable quantities, the cleanup routine is not as straightforward due to the structure of the
@variable
macro. I missed this the first time I wrote the clean-up routine, which was quite important; my problem was able to be solved much more efficiently (I say this loosely) once I discovered this extra clean-up caveat.This is likely not the most efficient routine to use in this setting, but it would be particularly helpful to either have a note in the documentation that one should be careful how they do this or a reference to a new command for cleanup that does this under the hood. @blegat attempted to do the latter back in March but it didn't get pushed, see here for details.
Thanks, Elizabeth
(The basis is not taken from any real problem but rather is randomly chosen to make terms overlap in some way, which can occur when manipulating multiple functions with tunable coefficients, with constraints on their sum.)