From 470e90f82be25210e5751e9696cc85ea4833290a Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Sep 2023 09:13:51 +1200 Subject: [PATCH 1/5] Fix various JET errors --- src/FileFormats/CBF/CBF.jl | 2 +- src/FileFormats/MPS/MPS.jl | 2 +- src/FileFormats/NL/NL.jl | 14 ++--- src/FileFormats/SDPA/SDPA.jl | 42 +++++++------- src/Utilities/objective_container.jl | 84 +++++++++++++++++++--------- 5 files changed, 89 insertions(+), 55 deletions(-) diff --git a/src/FileFormats/CBF/CBF.jl b/src/FileFormats/CBF/CBF.jl index 3bd110f840..d1895d5d28 100644 --- a/src/FileFormats/CBF/CBF.jl +++ b/src/FileFormats/CBF/CBF.jl @@ -59,7 +59,7 @@ end Create an empty instance of `FileFormats.CBF.Model`. """ -Model() = Model{Float64}() +Model(; kwargs...) = Model{Float64}() Base.show(io::IO, ::Model) = print(io, "A Conic Benchmark Format (CBF) model") diff --git a/src/FileFormats/MPS/MPS.jl b/src/FileFormats/MPS/MPS.jl index 34ce4b941f..5bea825d7e 100644 --- a/src/FileFormats/MPS/MPS.jl +++ b/src/FileFormats/MPS/MPS.jl @@ -1319,7 +1319,7 @@ function parse_name_line(data::TempMPSModel, line::String) if m === nothing error("Malformed NAME line: ", line) end - data.name = strip(m[1]) + data.name = strip(m[1]::AbstractString) return end diff --git a/src/FileFormats/NL/NL.jl b/src/FileFormats/NL/NL.jl index b22bb8f539..cc0fa0e211 100644 --- a/src/FileFormats/NL/NL.jl +++ b/src/FileFormats/NL/NL.jl @@ -781,11 +781,11 @@ function _assert_has_model(::Nothing, attr) ) end -_assert_has_model(::MOI.Utilities.UniversalFallback, ::Any) = nothing +_assert_has_model(model::MOI.Utilities.UniversalFallback, ::Any) = model function MOI.get(model::Model, attr::MOI.AbstractModelAttribute) - _assert_has_model(model.model, attr) - return MOI.get(model.model, attr) + inner = _assert_has_model(model.model, attr) + return MOI.get(inner, attr) end function MOI.get( @@ -793,8 +793,8 @@ function MOI.get( attr::MOI.AbstractConstraintAttribute, ci::MOI.ConstraintIndex, ) - _assert_has_model(model.model, attr) - return MOI.get(model.model, attr, ci) + inner = _assert_has_model(model.model, attr) + return MOI.get(inner, attr, ci) end function MOI.get( @@ -802,8 +802,8 @@ function MOI.get( attr::MOI.AbstractVariableAttribute, x::MOI.VariableIndex, ) - _assert_has_model(model.model, attr) - return MOI.get(model.model, attr, x) + inner = _assert_has_model(model.model, attr) + return MOI.get(inner, attr, x) end end diff --git a/src/FileFormats/SDPA/SDPA.jl b/src/FileFormats/SDPA/SDPA.jl index f3f0b4837f..8599013bf5 100644 --- a/src/FileFormats/SDPA/SDPA.jl +++ b/src/FileFormats/SDPA/SDPA.jl @@ -302,20 +302,17 @@ function _dim_to_set(s::AbstractString) return MOI.Nonnegatives(-block_dim) end end -function _parse_dimensions(dims) +function _parse_dimensions(dims::AbstractString) isvalid(char) = isdigit(char) || char == '-' + is_delimiter(char) = isspace(char) || char == ',' start = findfirst(isvalid, dims) - stop = findlast(isvalid, dims) - if isnothing(start) + if start === nothing return Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[] end - function is_delimiter(char) - return isspace(char) || char == ',' - end + stop = findlast(isvalid, dims)::Int s = split(dims[start:stop], is_delimiter) - s = filter(!isempty, s) return Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[ - _dim_to_set(dim) for dim in s + _dim_to_set(dim) for dim in filter!(!isempty, s) ] end @@ -324,19 +321,20 @@ end Read `io` in the SDPA file format and store the result in `model`. """ -function Base.read!(io::IO, model::Model{T}) where {T} +function Base.read!(io::IO, model::Model{T}) where {T<:Real} if !MOI.is_empty(model) error("Cannot read in file because model is not empty.") end num_variables_read = false num_blocks = nothing - block_sets = nothing + block_sets = Union{MOI.PositiveSemidefiniteConeTriangle,MOI.Nonnegatives}[] + block_sets_read = false objective_read = false integer_read = false - scalar_vars = nothing + scalar_vars = MOI.VariableIndex[] intvar_idx = Int[] c = nothing - funcs = nothing + funcs = MOI.VectorAffineFunction{T}[] MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) while !eof(io) line = strip(readline(io)) @@ -375,22 +373,26 @@ function Base.read!(io::IO, model::Model{T}) where {T} # According to http://plato.asu.edu/ftp/sdpa_format.txt, # additional text after the number of blocks should be ignored. num_blocks = parse(Int, split(line)[1]) - elseif block_sets === nothing + elseif !block_sets_read if isempty(line) && !iszero(num_blocks) continue end block_sets = _parse_dimensions(line) + block_sets_read = true if length(block_sets) != num_blocks error( "The number of blocks ($num_blocks) does not match the length of the list of blocks dimensions ($(length(block_sets))).", ) end - funcs = [ - MOI.VectorAffineFunction( - MOI.VectorAffineTerm{T}[], - zeros(T, MOI.dimension(block_sets[i])), - ) for i in 1:num_blocks - ] + for i in 1:num_blocks + push!( + funcs, + MOI.VectorAffineFunction( + MOI.VectorAffineTerm{T}[], + zeros(T, MOI.dimension(block_sets[i])), + ), + ) + end elseif !objective_read num_vars = MOI.get(model, MOI.NumberOfVariables()) if isempty(line) && !iszero(num_vars) @@ -452,7 +454,7 @@ function Base.read!(io::IO, model::Model{T}) where {T} end end end - for block in 1:num_blocks + for block in 1:(num_blocks::Int) MOI.add_constraint(model, funcs[block], block_sets[block]) end for var_idx in intvar_idx diff --git a/src/Utilities/objective_container.jl b/src/Utilities/objective_container.jl index 2dafc71974..62272536f9 100644 --- a/src/Utilities/objective_container.jl +++ b/src/Utilities/objective_container.jl @@ -116,26 +116,58 @@ function MOI.supports( return true end +function _get_single_variable(o::ObjectiveContainer{T}) where {T} + return o.single_variable::MOI.VariableIndex +end + +function _get_scalar_affine(o::ObjectiveContainer{T}) where {T} + return o.scalar_affine::MOI.ScalarAffineFunction{T} +end + +function _get_scalar_quadratic(o::ObjectiveContainer{T}) where {T} + return o.scalar_quadratic::MOI.ScalarQuadraticFunction{T} +end + +function _get_scalar_nonlinear(o::ObjectiveContainer{T}) where {T} + return o.scalar_nonlinear::MOI.ScalarNonlinearFunction +end + +function _get_vector_variables(o::ObjectiveContainer{T}) where {T} + return o.vector_variables::MOI.VectorOfVariables +end + +function _get_vector_affine(o::ObjectiveContainer{T}) where {T} + return o.vector_affine::MOI.VectorAffineFunction{T} +end + +function _get_vector_quadratic(o::ObjectiveContainer{T}) where {T} + return o.vector_quadratic::MOI.VectorQuadraticFunction{T} +end + +function _get_vector_nonlinear(o::ObjectiveContainer{T}) where {T} + return o.vector_nonlinear::MOI.VectorNonlinearFunction +end + function MOI.get( o::ObjectiveContainer{T}, ::MOI.ObjectiveFunction{F}, ) where {T,F} if o.scalar_affine !== nothing - return convert(F, o.scalar_affine) + return convert(F, _get_scalar_affine(o)) elseif o.single_variable !== nothing - return convert(F, o.single_variable) + return convert(F, _get_single_variable(o)) elseif o.scalar_quadratic !== nothing - return convert(F, o.scalar_quadratic) + return convert(F, _get_scalar_quadratic(o)) elseif o.scalar_nonlinear !== nothing - return convert(F, o.scalar_nonlinear) + return convert(F, _get_scalar_nonlinear(o)) elseif o.vector_variables !== nothing - return convert(F, o.vector_variables) + return convert(F, _get_vector_variables(o)) elseif o.vector_affine !== nothing - return convert(F, o.vector_affine) + return convert(F, _get_vector_affine(o)) elseif o.vector_quadratic !== nothing - return convert(F, o.vector_quadratic) + return convert(F, _get_vector_quadratic(o)) elseif o.vector_nonlinear !== nothing - return convert(F, o.vector_nonlinear) + return convert(F, _get_vector_nonlinear(o)) end # The default if no objective is set. return convert(F, zero(MOI.ScalarAffineFunction{T})) @@ -262,11 +294,11 @@ function MOI.modify( change::MOI.AbstractFunctionModification, ) where {T} if o.single_variable !== nothing - o.single_variable = modify_function!(o.single_variable, change) + o.single_variable = modify_function!(_get_single_variable(o), change) elseif o.scalar_affine !== nothing - o.scalar_affine = modify_function!(o.scalar_affine, change) + o.scalar_affine = modify_function!(_get_scalar_affine(o), change) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = modify_function!(o.scalar_quadratic, change) + o.scalar_quadratic = modify_function!(_get_scalar_quadratic(o), change) elseif o.scalar_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -276,11 +308,11 @@ function MOI.modify( ), ) elseif o.vector_variables !== nothing - o.vector_variables = modify_function!(o.vector_variables, change) + o.vector_variables = modify_function!(_get_vector_variables(o), change) elseif o.vector_quadratic !== nothing - o.vector_quadratic = modify_function!(o.vector_quadratic, change) + o.vector_quadratic = modify_function!(_get_vector_quadratic(o), change) elseif o.vector_affine !== nothing - o.vector_affine = modify_function!(o.vector_affine, change) + o.vector_affine = modify_function!(_get_vector_affine(o), change) elseif o.vector_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -304,13 +336,13 @@ end function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) if o.single_variable !== nothing - if x == o.single_variable + if x == _get_single_variable(o) _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = remove_variable(o.scalar_affine, x) + o.scalar_affine = remove_variable(_get_scalar_affine(o), x) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = remove_variable(o.scalar_quadratic, x) + o.scalar_quadratic = remove_variable(_get_scalar_quadratic(o), x) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -320,14 +352,14 @@ function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) ), ) elseif o.vector_variables !== nothing - o.vector_variables = remove_variable(o.vector_variables, x) + o.vector_variables = remove_variable(_get_vector_variables(o), x) if isempty(o.vector_variables.variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing - o.vector_affine = remove_variable(o.vector_affine, x) + o.vector_affine = remove_variable(_get_vector_affine(o), x) elseif o.vector_quadratic !== nothing - o.vector_quadratic = remove_variable(o.vector_quadratic, x) + o.vector_quadratic = remove_variable(_get_vector_quadratic(o), x) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -343,13 +375,13 @@ end function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) keep = v -> !(v in x) if o.single_variable !== nothing - if o.single_variable in x + if _get_single_variable(o) in x _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = filter_variables(keep, o.scalar_affine) + o.scalar_affine = filter_variables(keep, _get_scalar_affine(o)) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = filter_variables(keep, o.scalar_quadratic) + o.scalar_quadratic = filter_variables(keep, _get_scalar_quadratic(o)) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -359,14 +391,14 @@ function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) ), ) elseif o.vector_variables !== nothing - o.vector_variables = filter_variables(keep, o.vector_variables) + o.vector_variables = filter_variables(keep, _get_vector_variables(o)) if isempty(o.vector_variables.variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing - o.vector_affine = filter_variables(keep, o.vector_affine) + o.vector_affine = filter_variables(keep, _get_vector_affine(o)) elseif o.vector_quadratic !== nothing - o.vector_quadratic = filter_variables(keep, o.vector_quadratic) + o.vector_quadratic = filter_variables(keep, _get_vector_quadratic(o)) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( From ca753396d00e450341ef4d6e66105ee810760775 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Sep 2023 09:26:34 +1200 Subject: [PATCH 2/5] Update --- src/Utilities/objective_container.jl | 97 ++++++++++++---------------- 1 file changed, 40 insertions(+), 57 deletions(-) diff --git a/src/Utilities/objective_container.jl b/src/Utilities/objective_container.jl index 62272536f9..5effee8f26 100644 --- a/src/Utilities/objective_container.jl +++ b/src/Utilities/objective_container.jl @@ -116,58 +116,29 @@ function MOI.supports( return true end -function _get_single_variable(o::ObjectiveContainer{T}) where {T} - return o.single_variable::MOI.VariableIndex -end - -function _get_scalar_affine(o::ObjectiveContainer{T}) where {T} - return o.scalar_affine::MOI.ScalarAffineFunction{T} -end - -function _get_scalar_quadratic(o::ObjectiveContainer{T}) where {T} - return o.scalar_quadratic::MOI.ScalarQuadraticFunction{T} -end - -function _get_scalar_nonlinear(o::ObjectiveContainer{T}) where {T} - return o.scalar_nonlinear::MOI.ScalarNonlinearFunction -end - -function _get_vector_variables(o::ObjectiveContainer{T}) where {T} - return o.vector_variables::MOI.VectorOfVariables -end - -function _get_vector_affine(o::ObjectiveContainer{T}) where {T} - return o.vector_affine::MOI.VectorAffineFunction{T} -end - -function _get_vector_quadratic(o::ObjectiveContainer{T}) where {T} - return o.vector_quadratic::MOI.VectorQuadraticFunction{T} -end - -function _get_vector_nonlinear(o::ObjectiveContainer{T}) where {T} - return o.vector_nonlinear::MOI.VectorNonlinearFunction -end +_not_nothing(x) = x +_not_nothing(::Nothing) = error("Unexpected internal error.") function MOI.get( o::ObjectiveContainer{T}, ::MOI.ObjectiveFunction{F}, ) where {T,F} if o.scalar_affine !== nothing - return convert(F, _get_scalar_affine(o)) + return convert(F, _not_nothing(o.scalar_affine)) elseif o.single_variable !== nothing - return convert(F, _get_single_variable(o)) + return convert(F, _not_nothing(o.single_variable)) elseif o.scalar_quadratic !== nothing - return convert(F, _get_scalar_quadratic(o)) + return convert(F, _not_nothing(o.scalar_quadratic)) elseif o.scalar_nonlinear !== nothing - return convert(F, _get_scalar_nonlinear(o)) + return convert(F, _not_nothing(o.scalar_nonlinear)) elseif o.vector_variables !== nothing - return convert(F, _get_vector_variables(o)) + return convert(F, _not_nothing(o.vector_variables)) elseif o.vector_affine !== nothing - return convert(F, _get_vector_affine(o)) + return convert(F, _not_nothing(o.vector_affine)) elseif o.vector_quadratic !== nothing - return convert(F, _get_vector_quadratic(o)) + return convert(F, _not_nothing(o.vector_quadratic)) elseif o.vector_nonlinear !== nothing - return convert(F, _get_vector_nonlinear(o)) + return convert(F, _not_nothing(o.vector_nonlinear)) end # The default if no objective is set. return convert(F, zero(MOI.ScalarAffineFunction{T})) @@ -294,11 +265,14 @@ function MOI.modify( change::MOI.AbstractFunctionModification, ) where {T} if o.single_variable !== nothing - o.single_variable = modify_function!(_get_single_variable(o), change) + o.single_variable = + modify_function!(_not_nothing(o.single_variable), change) elseif o.scalar_affine !== nothing - o.scalar_affine = modify_function!(_get_scalar_affine(o), change) + o.scalar_affine = + modify_function!(_not_nothing(o.scalar_affine), change) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = modify_function!(_get_scalar_quadratic(o), change) + o.scalar_quadratic = + modify_function!(_not_nothing(o.scalar_quadratic), change) elseif o.scalar_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -308,11 +282,14 @@ function MOI.modify( ), ) elseif o.vector_variables !== nothing - o.vector_variables = modify_function!(_get_vector_variables(o), change) + o.vector_variables = + modify_function!(_not_nothing(o.vector_variables), change) elseif o.vector_quadratic !== nothing - o.vector_quadratic = modify_function!(_get_vector_quadratic(o), change) + o.vector_quadratic = + modify_function!(_not_nothing(o.vector_quadratic), change) elseif o.vector_affine !== nothing - o.vector_affine = modify_function!(_get_vector_affine(o), change) + o.vector_affine = + modify_function!(_not_nothing(o.vector_affine), change) elseif o.vector_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -336,13 +313,14 @@ end function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) if o.single_variable !== nothing - if x == _get_single_variable(o) + if x == _not_nothing(o.single_variable) _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = remove_variable(_get_scalar_affine(o), x) + o.scalar_affine = remove_variable(_not_nothing(o.scalar_affine), x) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = remove_variable(_get_scalar_quadratic(o), x) + o.scalar_quadratic = + remove_variable(_not_nothing(o.scalar_quadratic), x) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -352,14 +330,16 @@ function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) ), ) elseif o.vector_variables !== nothing - o.vector_variables = remove_variable(_get_vector_variables(o), x) + o.vector_variables = + remove_variable(_not_nothing(o.vector_variables), x) if isempty(o.vector_variables.variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing - o.vector_affine = remove_variable(_get_vector_affine(o), x) + o.vector_affine = remove_variable(_not_nothing(o.vector_affine), x) elseif o.vector_quadratic !== nothing - o.vector_quadratic = remove_variable(_get_vector_quadratic(o), x) + o.vector_quadratic = + remove_variable(_not_nothing(o.vector_quadratic), x) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -375,13 +355,14 @@ end function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) keep = v -> !(v in x) if o.single_variable !== nothing - if _get_single_variable(o) in x + if _not_nothing(o.single_variable) in x _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = filter_variables(keep, _get_scalar_affine(o)) + o.scalar_affine = filter_variables(keep, _not_nothing(o.scalar_affine)) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = filter_variables(keep, _get_scalar_quadratic(o)) + o.scalar_quadratic = + filter_variables(keep, _not_nothing(o.scalar_quadratic)) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -391,14 +372,16 @@ function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) ), ) elseif o.vector_variables !== nothing - o.vector_variables = filter_variables(keep, _get_vector_variables(o)) + o.vector_variables = + filter_variables(keep, _not_nothing(o.vector_variables)) if isempty(o.vector_variables.variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing - o.vector_affine = filter_variables(keep, _get_vector_affine(o)) + o.vector_affine = filter_variables(keep, _not_nothing(o.vector_affine)) elseif o.vector_quadratic !== nothing - o.vector_quadratic = filter_variables(keep, _get_vector_quadratic(o)) + o.vector_quadratic = + filter_variables(keep, _not_nothing(o.vector_quadratic)) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( From 30d0a31740e8d0372449d40d2ac62a021a38270d Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Sep 2023 11:22:03 +1200 Subject: [PATCH 3/5] Update --- src/Utilities/objective_container.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Utilities/objective_container.jl b/src/Utilities/objective_container.jl index 5effee8f26..a23c321b5e 100644 --- a/src/Utilities/objective_container.jl +++ b/src/Utilities/objective_container.jl @@ -332,7 +332,7 @@ function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) elseif o.vector_variables !== nothing o.vector_variables = remove_variable(_not_nothing(o.vector_variables), x) - if isempty(o.vector_variables.variables) + if isempty(_not_nothing(o.vector_variables).variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing @@ -374,7 +374,7 @@ function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) elseif o.vector_variables !== nothing o.vector_variables = filter_variables(keep, _not_nothing(o.vector_variables)) - if isempty(o.vector_variables.variables) + if isempty(_not_nothing(o.vector_variables).variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing From 4fdfe636c64261a4e52384dea471e3a656debe15 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Sep 2023 11:43:12 +1200 Subject: [PATCH 4/5] Change to something --- src/Utilities/objective_container.jl | 64 ++++++++++++---------------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/src/Utilities/objective_container.jl b/src/Utilities/objective_container.jl index a23c321b5e..30fa031c52 100644 --- a/src/Utilities/objective_container.jl +++ b/src/Utilities/objective_container.jl @@ -116,29 +116,26 @@ function MOI.supports( return true end -_not_nothing(x) = x -_not_nothing(::Nothing) = error("Unexpected internal error.") - function MOI.get( o::ObjectiveContainer{T}, ::MOI.ObjectiveFunction{F}, ) where {T,F} if o.scalar_affine !== nothing - return convert(F, _not_nothing(o.scalar_affine)) + return convert(F, something(o.scalar_affine)) elseif o.single_variable !== nothing - return convert(F, _not_nothing(o.single_variable)) + return convert(F, something(o.single_variable)) elseif o.scalar_quadratic !== nothing - return convert(F, _not_nothing(o.scalar_quadratic)) + return convert(F, something(o.scalar_quadratic)) elseif o.scalar_nonlinear !== nothing - return convert(F, _not_nothing(o.scalar_nonlinear)) + return convert(F, something(o.scalar_nonlinear)) elseif o.vector_variables !== nothing - return convert(F, _not_nothing(o.vector_variables)) + return convert(F, something(o.vector_variables)) elseif o.vector_affine !== nothing - return convert(F, _not_nothing(o.vector_affine)) + return convert(F, something(o.vector_affine)) elseif o.vector_quadratic !== nothing - return convert(F, _not_nothing(o.vector_quadratic)) + return convert(F, something(o.vector_quadratic)) elseif o.vector_nonlinear !== nothing - return convert(F, _not_nothing(o.vector_nonlinear)) + return convert(F, something(o.vector_nonlinear)) end # The default if no objective is set. return convert(F, zero(MOI.ScalarAffineFunction{T})) @@ -266,13 +263,12 @@ function MOI.modify( ) where {T} if o.single_variable !== nothing o.single_variable = - modify_function!(_not_nothing(o.single_variable), change) + modify_function!(something(o.single_variable), change) elseif o.scalar_affine !== nothing - o.scalar_affine = - modify_function!(_not_nothing(o.scalar_affine), change) + o.scalar_affine = modify_function!(something(o.scalar_affine), change) elseif o.scalar_quadratic !== nothing o.scalar_quadratic = - modify_function!(_not_nothing(o.scalar_quadratic), change) + modify_function!(something(o.scalar_quadratic), change) elseif o.scalar_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -283,13 +279,12 @@ function MOI.modify( ) elseif o.vector_variables !== nothing o.vector_variables = - modify_function!(_not_nothing(o.vector_variables), change) + modify_function!(something(o.vector_variables), change) elseif o.vector_quadratic !== nothing o.vector_quadratic = - modify_function!(_not_nothing(o.vector_quadratic), change) + modify_function!(something(o.vector_quadratic), change) elseif o.vector_affine !== nothing - o.vector_affine = - modify_function!(_not_nothing(o.vector_affine), change) + o.vector_affine = modify_function!(something(o.vector_affine), change) elseif o.vector_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -313,14 +308,13 @@ end function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) if o.single_variable !== nothing - if x == _not_nothing(o.single_variable) + if x == something(o.single_variable) _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = remove_variable(_not_nothing(o.scalar_affine), x) + o.scalar_affine = remove_variable(something(o.scalar_affine), x) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = - remove_variable(_not_nothing(o.scalar_quadratic), x) + o.scalar_quadratic = remove_variable(something(o.scalar_quadratic), x) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -330,16 +324,14 @@ function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) ), ) elseif o.vector_variables !== nothing - o.vector_variables = - remove_variable(_not_nothing(o.vector_variables), x) - if isempty(_not_nothing(o.vector_variables).variables) + o.vector_variables = remove_variable(something(o.vector_variables), x) + if isempty(something(o.vector_variables).variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing - o.vector_affine = remove_variable(_not_nothing(o.vector_affine), x) + o.vector_affine = remove_variable(something(o.vector_affine), x) elseif o.vector_quadratic !== nothing - o.vector_quadratic = - remove_variable(_not_nothing(o.vector_quadratic), x) + o.vector_quadratic = remove_variable(something(o.vector_quadratic), x) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -355,14 +347,14 @@ end function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) keep = v -> !(v in x) if o.single_variable !== nothing - if _not_nothing(o.single_variable) in x + if something(o.single_variable) in x _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = filter_variables(keep, _not_nothing(o.scalar_affine)) + o.scalar_affine = filter_variables(keep, something(o.scalar_affine)) elseif o.scalar_quadratic !== nothing o.scalar_quadratic = - filter_variables(keep, _not_nothing(o.scalar_quadratic)) + filter_variables(keep, something(o.scalar_quadratic)) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -373,15 +365,15 @@ function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) ) elseif o.vector_variables !== nothing o.vector_variables = - filter_variables(keep, _not_nothing(o.vector_variables)) - if isempty(_not_nothing(o.vector_variables).variables) + filter_variables(keep, something(o.vector_variables)) + if isempty(something(o.vector_variables).variables) _empty_keeping_sense(o) end elseif o.vector_affine !== nothing - o.vector_affine = filter_variables(keep, _not_nothing(o.vector_affine)) + o.vector_affine = filter_variables(keep, something(o.vector_affine)) elseif o.vector_quadratic !== nothing o.vector_quadratic = - filter_variables(keep, _not_nothing(o.vector_quadratic)) + filter_variables(keep, something(o.vector_quadratic)) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( From 25db4a0206852ac4b19d65636522a34bd63ab0cb Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 19 Sep 2023 12:05:16 +1200 Subject: [PATCH 5/5] Add more something usage --- src/Bridges/Bridges.jl | 6 +++--- src/Bridges/Variable/map.jl | 9 +++++---- src/Nonlinear/evaluator.jl | 2 +- src/Nonlinear/operators.jl | 2 +- src/Utilities/universalfallback.jl | 8 ++++---- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/Bridges/Bridges.jl b/src/Bridges/Bridges.jl index ccfe3bd869..8251eb0d32 100644 --- a/src/Bridges/Bridges.jl +++ b/src/Bridges/Bridges.jl @@ -302,13 +302,13 @@ function runtests( end # Test other bridge functions for b in values(Constraint.bridges(model)) - _general_bridge_tests(b) + _general_bridge_tests(something(b)) end for b in values(Objective.bridges(model)) - _general_bridge_tests(b) + _general_bridge_tests(something(b)) end for b in values(Variable.bridges(model)) - _general_bridge_tests(b) + _general_bridge_tests(something(b)) end _test_delete(Bridge, model, inner) return diff --git a/src/Bridges/Variable/map.jl b/src/Bridges/Variable/map.jl index 7e664cc5b9..014c4847b3 100644 --- a/src/Bridges/Variable/map.jl +++ b/src/Bridges/Variable/map.jl @@ -71,7 +71,7 @@ function Base.empty!(map::Map) map.unbridged_function = Dict{MOI.VariableIndex,Tuple{Int64,MOI.AbstractScalarFunction}}() else - empty!(map.unbridged_function) + empty!(something(map.unbridged_function)) end empty!(map.parent_index) map.current_context = 0 @@ -371,13 +371,14 @@ function add_keys_for_bridge( MOI.VariableIndex(-(bridge_index - 1 + i)) for i in 1:MOI.dimension(set) ] if map.unbridged_function !== nothing - mappings = unbridged_map(map.bridges[bridge_index], variables) + mappings = + unbridged_map(something(map.bridges[bridge_index]), variables) if mappings === nothing map.unbridged_function = nothing else for mapping in mappings push!( - map.unbridged_function, + something(map.unbridged_function), mapping.first => (bridge_index, mapping.second), ) end @@ -441,7 +442,7 @@ Return the expression of `vi` in terms of bridged variables. """ function unbridged_function(map::Map, vi::MOI.VariableIndex) throw_if_cannot_unbridge(map) - context_func = get(map.unbridged_function, vi, nothing) + context_func = get(something(map.unbridged_function), vi, nothing) if context_func === nothing return nothing end diff --git a/src/Nonlinear/evaluator.jl b/src/Nonlinear/evaluator.jl index 21955c14d1..2414ac7db1 100644 --- a/src/Nonlinear/evaluator.jl +++ b/src/Nonlinear/evaluator.jl @@ -90,7 +90,7 @@ function MOI.objective_expr(evaluator::Evaluator) end return convert_to_expr( evaluator, - evaluator.model.objective; + something(evaluator.model.objective); moi_output_format = true, ) end diff --git a/src/Nonlinear/operators.jl b/src/Nonlinear/operators.jl index ee49768111..d318fdb241 100644 --- a/src/Nonlinear/operators.jl +++ b/src/Nonlinear/operators.jl @@ -575,7 +575,7 @@ function eval_univariate_hessian( ) where {T} id = registry.univariate_operator_to_id[op] if id <= registry.univariate_user_operator_start - return _eval_univariate_2nd_deriv(id, x) + return _eval_univariate_2nd_deriv(id, x)::T end offset = id - registry.univariate_user_operator_start operator = registry.registered_univariate_operators[offset] diff --git a/src/Utilities/universalfallback.jl b/src/Utilities/universalfallback.jl index 21842f53a5..c57df71d7b 100644 --- a/src/Utilities/universalfallback.jl +++ b/src/Utilities/universalfallback.jl @@ -280,7 +280,7 @@ function MOI.delete(uf::UniversalFallback, vi::MOI.VariableIndex) delete!(d, vi) end if uf.objective !== nothing - uf.objective = remove_variable(uf.objective, vi) + uf.objective = remove_variable(something(uf.objective), vi) end for constraints in values(uf.single_variable_constraints) _remove_variable(uf, constraints, vi) @@ -309,7 +309,7 @@ function MOI.delete(uf::UniversalFallback, vis::Vector{MOI.VariableIndex}) end end if uf.objective !== nothing - uf.objective = remove_variable(uf.objective, vis) + uf.objective = remove_variable(something(uf.objective), vis) end for constraints in values(uf.single_variable_constraints) for vi in vis @@ -678,7 +678,7 @@ function MOI.get( # to check for duplicate names. MOI.get(uf.model, MOI.ConstraintIndex, name) end - ci_uf = get(uf.name_to_con, name, nothing) + ci_uf = get(something(uf.name_to_con), name, nothing) throw_if_multiple_with_name(ci_uf, name) c = check_type_and_multiple_names(MOI.ConstraintIndex{F,S}, ci_uf, ci, name) return c @@ -692,7 +692,7 @@ function MOI.get( if uf.name_to_con === nothing uf.name_to_con = build_name_to_con_map(uf.con_to_name) end - ci_uf = get(uf.name_to_con, name, nothing) + ci_uf = get(something(uf.name_to_con), name, nothing) throw_if_multiple_with_name(ci_uf, name) return check_type_and_multiple_names( MOI.ConstraintIndex,