diff --git a/src/FileFormats/MOF/nonlinear.jl b/src/FileFormats/MOF/nonlinear.jl index 6c84f2da2d..6f695fa555 100644 --- a/src/FileFormats/MOF/nonlinear.jl +++ b/src/FileFormats/MOF/nonlinear.jl @@ -62,12 +62,9 @@ function write_nlpblock( model::Model, name_map::Dict{MOI.VariableIndex,String}, ) where {T<:Object} - # TODO(odow): is there a better way of checking if the NLPBlock is set? - nlp_block = try - MOI.get(model, MOI.NLPBlock()) - catch ex - @assert isa(ex, KeyError) - return # No NLPBlock set. + nlp_block = MOI.get(model, MOI.NLPBlock()) + if nlp_block === nothing + return end MOI.initialize(nlp_block.evaluator, [:ExprGraph]) variables = MOI.get(model, MOI.ListOfVariableIndices()) diff --git a/src/FileFormats/NL/NL.jl b/src/FileFormats/NL/NL.jl index fa8b9ed517..8cc160983d 100644 --- a/src/FileFormats/NL/NL.jl +++ b/src/FileFormats/NL/NL.jl @@ -243,9 +243,10 @@ MOI.initialize(::_LinearNLPEvaluator, ::Vector{Symbol}) = nothing function MOI.copy_to(dest::Model, model::MOI.ModelLike) mapping = MOI.Utilities.IndexMap() # Initialize the NLP block. - nlp_block = try + has_nlp = MOI.NLPBlock() in MOI.get(model, MOI.ListOfModelAttributesSet()) + nlp_block = if has_nlp MOI.get(model, MOI.NLPBlock()) - catch + else MOI.NLPBlockData(MOI.NLPBoundsPair[], _LinearNLPEvaluator(), false) end if !(:ExprGraph in MOI.features_available(nlp_block.evaluator)) diff --git a/src/Utilities/print.jl b/src/Utilities/print.jl index 44f30f6d6d..d150a9291b 100644 --- a/src/Utilities/print.jl +++ b/src/Utilities/print.jl @@ -483,6 +483,9 @@ end function _nlp_block(model::MOI.ModelLike) try block = MOI.get(model, MOI.NLPBlock()) + if block === nothing + return + end if :ExprGraph in MOI.features_available(block.evaluator) MOI.initialize(block.evaluator, [:ExprGraph]) end diff --git a/src/Utilities/universalfallback.jl b/src/Utilities/universalfallback.jl index 29c56dd5d6..9a96a918a9 100644 --- a/src/Utilities/universalfallback.jl +++ b/src/Utilities/universalfallback.jl @@ -258,10 +258,12 @@ end # Attributes function _get(uf::UniversalFallback, attr::MOI.AbstractOptimizerAttribute) - return uf.optattr[attr] + return get(uf.optattr, attr, nothing) end -_get(uf::UniversalFallback, attr::MOI.AbstractModelAttribute) = uf.modattr[attr] +function _get(uf::UniversalFallback, attr::MOI.AbstractModelAttribute) + return get(uf.modattr, attr, nothing) +end function _get( uf::UniversalFallback, diff --git a/test/Utilities/universalfallback.jl b/test/Utilities/universalfallback.jl index 5df504866f..d3908caed4 100644 --- a/test/Utilities/universalfallback.jl +++ b/test/Utilities/universalfallback.jl @@ -373,6 +373,13 @@ function test_show() return end +function test_missing_attribute() + model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()) + @test MOI.get(model, MOI.Test.UnknownModelAttribute()) === nothing + @test MOI.get(model, MOI.TimeLimitSec()) === nothing + return +end + end # module TestUniversalFallback.runtests()