# Gurobi how to get variable coefficients in constraints

**URL:** <https://discourse.julialang.org/t/gurobi-how-to-get-variable-coefficients-in-constraints/54150>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [January 29, 2021, 12:16am UTC](https://discourse.julialang.org/t/gurobi-how-to-get-variable-coefficients-in-constraints/54150 "2021-01-29T00:16:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rezabayani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rezabayani/32/25954_2.png) [@rezabayani](https://discourse.julialang.org/u/rezabayani)\
**Post date:** [January 29, 2021, 12:16am UTC](https://discourse.julialang.org/t/gurobi-how-to-get-variable-coefficients-in-constraints/54150/1 "2021-01-29T00:16:58Z")

</div>

I am trying to extract the variable coefficients in a constraint. According to Gurobi documentation, [https://www.gurobi.com/documentation/9.1/refman/py\_model\_getcoeff.html](https://www.gurobi.com/documentation/9.1/refman/py_model_getcoeff.html), the function Model.getCoeff() should do the trick. However, this does not exist in Gurobi Julia package. The closest one I get is Gurobi.getcoeff(model::Gurobi.Model , cidx::Integer , vidx::Integer) at Gurobi\7YNJV\src\grb\_constrs.jl:367. However, I can not make it work.  
For example, in the following minimal code:

```julia
using Gurobi
using JuMP
# define a simple UC problem
n_u = 5
    n_t = 3
    U = collect(1:n_u) # num units
    T = collect(1:n_t) # num time steps
    p_g_min = round.(0.0*rand(n_u), digits=2)
    p_g_max = 1 .+ round.(rand(n_u), digits=2)
    cost_g = 10 .+ round.(10.0*rand(n_u), digits=1)
    cost_g0 = round.(1.0*rand(n_u), digits=1)
    d0 = rand((sum(p_g_min):.01:.6*sum(p_g_max)),n_t)

# basic_uc_model = Model(Gurobi.Optimizer)
basic_uc_model = direct_model(Gurobi.Optimizer())
    @variable(basic_uc_model, p_g_min[i] <= p_g[i in U, t in T] <= p_g_max[i])

    @variable(basic_uc_model, 0 <= I_g[i in U, t in T] <= 1)

    @constraint(basic_uc_model, lower_band[i in U, t in T], p_g[i,t] >= p_g_min[i]*I_g[i,t])
    @constraint(basic_uc_model, upper_band[i in U, t in T], p_g[i,t] <= p_g_max[i]*I_g[i,t])
    @constraint(basic_uc_model, demand[t in T], sum(p_g[i,t] for i in U) == d0[t])
    @objective(basic_uc_model, Min, sum(p_g[i,t]*cost_g[i] + I_g[i,t]*cost_g0[i] for i in U, t in T))

    set_optimizer_attribute(basic_uc_model, "PoolSearchMode", 1)

optimize!(basic_uc_model)
Gurobi.getcoeff(basic_uc_model, lower_band[1,1], p_g[1,1])

Error: MethodError: no method matching getcoeff(::Model, ::ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.GreaterThan{Float64}},ScalarShape}, ::VariableRef)
in top-level scope at test_gurobi.jl:42

```

I think I am not using the correct formatting and I am not sure about cidx and vidx. I would appreciate any help.

---

<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 29, 2021, 12:33am UTC](https://discourse.julialang.org/t/gurobi-how-to-get-variable-coefficients-in-constraints/54150/2 "2021-01-29T00:33:58Z")

</div>

You probably want something like

```nohighlight
con = constraint_object(lower_band[1, 1])
coef = con.func.terms[p_g[1, 1]]

```

Gurobi.j wraps the C API, not the Python API:  
[https://github.com/jump-dev/Gurobi.jl/blob/3e996da53396996ce40df4436eefccfa0a05b18a/src/gen91/libgrb\_api.jl#L169-L171](https://github.com/jump-dev/Gurobi.jl/blob/3e996da53396996ce40df4436eefccfa0a05b18a/src/gen91/libgrb_api.jl#L169-L171)  
However, I suggest you avoid using it if at all possible. It is mainly intended for callbacks, not for general users.

---

<div class="post-metadata">

**Author:** ![rezabayani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rezabayani/32/25954_2.png) [@rezabayani](https://discourse.julialang.org/u/rezabayani)\
**Post date:** [January 29, 2021, 12:57am UTC](https://discourse.julialang.org/t/gurobi-how-to-get-variable-coefficients-in-constraints/54150/3 "2021-01-29T00:57:54Z")

</div>

Thanks that does the trick. I wonder if such option is implemented within JuMP itself?

---

<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 29, 2021, 1:35am UTC](https://discourse.julialang.org/t/gurobi-how-to-get-variable-coefficients-in-constraints/54150/4 "2021-01-29T01:35:49Z")

</div>

It’s a known issue: [https://github.com/jump-dev/JuMP.jl/issues/2371#issuecomment-734519777](https://github.com/jump-dev/JuMP.jl/issues/2371#issuecomment-734519777).

It’s a good intro issue if you want to help contribute to JuMP.

---

<div class="post-metadata">

**Author:** ![rezabayani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rezabayani/32/25954_2.png) [@rezabayani](https://discourse.julialang.org/u/rezabayani)\
**Post date:** [January 29, 2021, 3:31am UTC](https://discourse.julialang.org/t/gurobi-how-to-get-variable-coefficients-in-constraints/54150/5 "2021-01-29T03:31:16Z")

</div>

Okay here is an interesting one. Your method returns an error if the constraint does not contain that variable. For example:

```julia
cc[1,1] # segment_power[1,1] : p_g_seg[1,1,1] + p_g_seg[1,2,1] + p_g_seg[1,3,1] - p_g[1,1] == 0.0
con = constraint_object(cc[1,1])
con.func.terms
#=
OrderedCollections.OrderedDict{VariableRef,Float64} with 4 entries:
  p_g_seg[1,1,1] => 1.0
  p_g_seg[1,2,1] => 1.0
  p_g_seg[1,3,1] => 1.0
  p_g[1,1] => -1.0
=#
haskey(con.func.terms, "p_g[1,1]") #false
haskey(con.func.terms, p_g[1,1]) #false
collect(keys(con.func.terms))
#=
4-element Array{VariableRef,1}:
 p_g_seg[1,1,1]
 p_g_seg[1,2,1]
 p_g_seg[1,3,1]
 p_g[1,1]
=#
p_g[1,1] in collect(keys(con.func.terms)) #false
typeof(collect(keys(con.func.terms))[4]) #VariableRef
typeof(p_g[1,1]) #VariableRef
con.func.terms[p_g[1,1]] # -1.00
con.func.terms[p_g[1,2]] #KeyError: key p_g[1,2] not found

```

Besides your suggestion, I also found [How do I extract the coefficients and RHS's of all constraints in a JuMP linear program? (Need for KKT) - #4 by odow](https://discourse.julialang.org/t/how-do-i-extract-the-coefficients-and-rhss-of-all-constraints-in-a-jump-linear-program-need-for-kkt/38525/4) and it suggested using some NLP package that returns “A” matrix. However, it is quite a challenge to figure out the ordering of that A matrix in large cases. Do you have any idea how to get around this issue?

---

<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 29, 2021, 3:51am UTC](https://discourse.julialang.org/t/gurobi-how-to-get-variable-coefficients-in-constraints/54150/6 "2021-01-29T03:51:56Z")

</div>

`con.func.terms` is just a dictionary, so you can use `get(con.func.terms, p_g[1, 2], 0.0)` to have a default of `0.0` if the key is not present.

The NLP package and A matrix was for something different.
