JuMP constraint coefficients

What am I doing wrong here?

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, but I guess there is something I’m missing.

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> 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
1 Like

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

1 Like

I believe supporting

JuMP.normalized_coefficient(c, x)

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

1 Like

I’ve opened an issue: Support normalized_coefficient for vector-valued constraints · Issue #3742 · jump-dev/JuMP.jl · GitHub

1 Like