# Obtaining variable scalar coefficients in objective

**URL:** <https://discourse.julialang.org/t/obtaining-variable-scalar-coefficients-in-objective/54128>\
**Category:** Optimization (Mathematical)\
**Created:** [January 28, 2021, 4:19pm UTC](https://discourse.julialang.org/t/obtaining-variable-scalar-coefficients-in-objective/54128 "2021-01-28T16:19:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [January 28, 2021, 4:19pm UTC](https://discourse.julialang.org/t/obtaining-variable-scalar-coefficients-in-objective/54128/1 "2021-01-28T16:19:03Z")

</div>

Hello! I would like to extend NREL’s [progressive hedging package](https://github.com/NREL/ProgressiveHedging.jl) 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:

```julia
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).

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [January 28, 2021, 6:59pm UTC](https://discourse.julialang.org/t/obtaining-variable-scalar-coefficients-in-objective/54128/2 "2021-01-28T18:59:12Z")

</div>

See [https://github.com/jump-dev/JuMP.jl/issues/2371](https://github.com/jump-dev/JuMP.jl/issues/2371).

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]`.

---

<div class="post-metadata">

**Author:** ![gobs](https://avatars.discourse-cdn.com/v4/letter/g/bc8723/32.png) [@gobs](https://discourse.julialang.org/u/gobs)\
**Post date:** [January 29, 2021, 7:59am UTC](https://discourse.julialang.org/t/obtaining-variable-scalar-coefficients-in-objective/54128/3 "2021-01-29T07:59:37Z")

</div>

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