Obtaining variable scalar coefficients in objective

Hello! I would like to extend NREL’s progressive hedging package to include a scaled penalty parameter, i.e. one which is proportional to the coefficients of the variable being penalised in the objective function.

Long story short: if I have a JuMP model, is there a way of querying the scalar coefficient of that variable in the objective? Something like this:

using JuMP
m = Model()
x = @variable(m, lower_bound=0.0)
con = @constraint(m, x >= 1)
obj = @objective(m, Min, 2*x)
get_scalar_coefficient(obj, x) # Would return 2.0

I was thinking that there must be a a way of doing this through MathOptInterface, though I wasn’t able to find it in the documentation. If it makes it easier, for my particular use case the problem would be a QP (linear constraints with linear + quadratic objective terms).

See Feature request: implement getindex for expressions · Issue #2371 · jump-dev/JuMP.jl · GitHub.

You can look in the obj.terms dictionary if affine, or the obj.aff.terms if quadratic.

PRs accepted if you have time to implement the get so this works as obj[x].


Thanks! I wouldn’t mind trying, though I might make a bad job of it.