Skip to content

Commit

Permalink
Merge pull request #2363 from andrewrosemberg/ar/has_variable
Browse files Browse the repository at this point in the history
Define haskey for JuMP AbstractModel
  • Loading branch information
joaquimg authored Nov 10, 2020
2 parents af2355d + 99147d1 commit 4b677ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,15 @@ function Base.setindex!(m::JuMP.Model, value, name::Symbol)
m.obj_dict[name] = value
end

"""
haskey(model::AbstractModel, name::Symbol)
Determine whether the model has a mapping for a given name.
"""
function Base.haskey(model::AbstractModel, name::Symbol)
return haskey(object_dictionary(model), name)
end

"""
operator_warn(model::AbstractModel)
operator_warn(model::Model)
Expand Down
7 changes: 7 additions & 0 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ function test_copy_direct_mode()
@test_throws ErrorException JuMP.copy(model)
end

function test_haskey()
model = Model()
@variable(model, p[i=1:10] >= 0)
@test haskey(model, :p)
@test !haskey(model, :i)
end

function runtests()
for name in names(@__MODULE__; all = true)
if !startswith("$(name)", "test_")
Expand Down

0 comments on commit 4b677ef

Please sign in to comment.