From eec9d3e1fcaf5e7872d83b0d40904985044fee53 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 29 Oct 2021 13:28:24 +1300 Subject: [PATCH 1/3] [Utilities] Fix getter in UniversalFallback --- src/Utilities/universalfallback.jl | 6 ++++-- test/Utilities/universalfallback.jl | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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() From 416e887a3a84ffbb0dda44197067917af2e1ae5d Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 29 Oct 2021 13:42:45 +1300 Subject: [PATCH 2/3] Other fixes --- src/FileFormats/MOF/nonlinear.jl | 9 +++------ src/FileFormats/NL/NL.jl | 8 ++++---- src/Utilities/print.jl | 3 +++ 3 files changed, 10 insertions(+), 10 deletions(-) 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..ff7a12cdcc 100644 --- a/src/FileFormats/NL/NL.jl +++ b/src/FileFormats/NL/NL.jl @@ -243,10 +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 - MOI.get(model, MOI.NLPBlock()) - catch - MOI.NLPBlockData(MOI.NLPBoundsPair[], _LinearNLPEvaluator(), false) + nlp_block = MOI.get(model, MOI.NLPBlock()) + if nlp_block === nothing + nlp_block = + MOI.NLPBlockData(MOI.NLPBoundsPair[], _LinearNLPEvaluator(), false) end if !(:ExprGraph in MOI.features_available(nlp_block.evaluator)) error( 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 From 68f6ef617d2f7b79f97995c53c8bed2c21b96c57 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 29 Oct 2021 14:15:28 +1300 Subject: [PATCH 3/3] Fix --- src/FileFormats/NL/NL.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/FileFormats/NL/NL.jl b/src/FileFormats/NL/NL.jl index ff7a12cdcc..8cc160983d 100644 --- a/src/FileFormats/NL/NL.jl +++ b/src/FileFormats/NL/NL.jl @@ -243,10 +243,11 @@ 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 = MOI.get(model, MOI.NLPBlock()) - if nlp_block === nothing - nlp_block = - MOI.NLPBlockData(MOI.NLPBoundsPair[], _LinearNLPEvaluator(), false) + has_nlp = MOI.NLPBlock() in MOI.get(model, MOI.ListOfModelAttributesSet()) + nlp_block = if has_nlp + MOI.get(model, MOI.NLPBlock()) + else + MOI.NLPBlockData(MOI.NLPBoundsPair[], _LinearNLPEvaluator(), false) end if !(:ExprGraph in MOI.features_available(nlp_block.evaluator)) error(