# JuMP constraint coefficients

**URL:** <https://discourse.julialang.org/t/jump-constraint-coefficients/113754>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization, math\
**Created:** [May 2, 2024, 7:08pm UTC](https://discourse.julialang.org/t/jump-constraint-coefficients/113754 "2024-05-02T19:08:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 2, 2024, 7:08pm UTC](https://discourse.julialang.org/t/jump-constraint-coefficients/113754/1 "2024-05-02T19:08:24Z")

</div>

What am I doing wrong here?

```julia
model = JuMP.Model()
JuMP.@variable(model, x[1:length(s)])
JuMP.@constraint(model, c, s .* x ∈ JuMP.MOI.Nonnegatives(10))
JuMP.normalized_coefficient(c, x[1])
ERROR: MethodError: no method matching normalized_coefficient(::JuMP.ConstraintRef{…}, ::JuMP.VariableRef)

Closest candidates are:
  normalized_coefficient(::JuMP.ConstraintRef{<:JuMP.AbstractModel, <:MathOptInterface.ConstraintIndex{F}}, ::JuMP.AbstractVariableRef, ::JuMP.AbstractVariableRef) where F<:MathOptInterface.ScalarQuadraticFunction
   @ JuMP ~/.julia/packages/JuMP/as6Ji/src/variables.jl:2857
  normalized_coefficient(::JuMP.ConstraintRef{<:JuMP.AbstractModel, <:MathOptInterface.ConstraintIndex{F}}, ::JuMP.AbstractVariableRef) where F<:Union{MathOptInterface.ScalarAffineFunction, MathOptInterface.ScalarQuadraticFunction}
   @ JuMP ~/.julia/packages/JuMP/as6Ji/src/variables.jl:2821

```

How do I access the coefficient of `x[1]` ?

I’m following [Constraints · JuMP](https://jump.dev/JuMP.jl/stable/manual/constraints/#Vector-constraints), but I guess there is something I’m missing.

---

<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:** [May 2, 2024, 8:17pm UTC](https://discourse.julialang.org/t/jump-constraint-coefficients/113754/2 "2024-05-02T20:17:38Z")

</div>

Your issue is that `c` is a vector-valued constraint. It does not have a single coefficient for `x[1]`. What do you expect the return value to be? The vector `[s[1], 0, ..., 0]`?

Do you intend instead:

```julia
julia> s = rand(10);

julia> model = JuMP.Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> JuMP.@variable(model, x[1:length(s)])
10-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]
 x[4]
 x[5]
 x[6]
 x[7]
 x[8]
 x[9]
 x[10]

julia> JuMP.@constraint(model, c, s .* x .>= 0)
10-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.GreaterThan{Float64}}, ScalarShape}}:
 c : 0.5709969834637747 x[1] ≥ 0
 c : 0.7619275184835252 x[2] ≥ 0
 c : 0.34898255811341505 x[3] ≥ 0
 c : 0.05870819653256942 x[4] ≥ 0
 c : 0.6044341953123104 x[5] ≥ 0
 c : 0.9001617090928788 x[6] ≥ 0
 c : 0.8589634852858642 x[7] ≥ 0
 c : 0.8241775821914167 x[8] ≥ 0
 c : 0.6172547651461359 x[9] ≥ 0
 c : 0.830700523487739 x[10] ≥ 0

julia> JuMP.normalized_coefficient(c[1], x[1])
0.5709969834637747

```

---

<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:** [May 2, 2024, 10:25pm UTC](https://discourse.julialang.org/t/jump-constraint-coefficients/113754/3 "2024-05-02T22:25:28Z")

</div>

@e3c6 did this answer your question? Or is it a feature that we could/should add to JuMP?

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 2, 2024, 11:00pm UTC](https://discourse.julialang.org/t/jump-constraint-coefficients/113754/4 "2024-05-02T23:00:38Z")

</div>

> [@odow](#):
>
> is it a feature that we could/should add to JuMP?

I believe supporting

```julia
JuMP.normalized_coefficient(c, x)

```

to return the vector of coefficients would be nice, don’t you think?

---

<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:** [May 2, 2024, 11:09pm UTC](https://discourse.julialang.org/t/jump-constraint-coefficients/113754/5 "2024-05-02T23:09:36Z")

</div>

I’ve opened an issue: [Support normalized\_coefficient for vector-valued constraints · Issue #3742 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/3742)
