using JuMP
m = Model()
@variable(m, x[1:2])
@constraint(m, mycons, x .== 0)
cn = constraint_by_name(m, "mycons") # returns nothing
Should cn
be nothing
?
I expected it would return an array of constraint references.
using JuMP
m = Model()
@variable(m, x[1:2])
@constraint(m, mycons, x .== 0)
cn = constraint_by_name(m, "mycons") # returns nothing
Should cn
be nothing
?
I expected it would return an array of constraint references.
Names are currently ignored for vectorized constraints, see
Even when that issue is resolved, constraint_by_name(m, "mycons")
would not return an array of constraints. The name attribute applies to individual constraints, not to groups of them (Constraints · JuMP). m[:mycons]
is the way to look up Julia variables stored in the model scope (Point 3 at Constraints · JuMP).
Thanks for the clarification.
Part of my desired usage was to test for whether a model has a constraint in it.
I thought constraint_by_name was the right way to do this because:
using JuMP
m = Model()
@variable(m, x)
@constraint(m, mycons, x == 0)
a = constraint_by_name(m, "mycons")
b = m[:mycons]
println(a == b) # true
a = constraint_by_name(m, "mycons2")
println(isnothing(a)) # true
b = m[:mycons2] # KeyError
I couldn’t figure out from the documentation a way to check whether mycons
is defined using the symbol :mycons
. I thought perhaps haskey
but that’s not defined for a JuMP model. Any suggestions on the right way to do this?
You can do haskey(object_dictionary(model), :mycons)
Perfect, thanks. I didn’t see this function anywhere in the JuMP documentation.
It would be nice if haskey
was defined. I think it’s a pretty obvious thing that people are going to try and run into issues. I made an issue: Define haskey on model · Issue #1887 · jump-dev/JuMP.jl · GitHub