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/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/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/objective_container.jl b/src/Utilities/objective_container.jl index 2dafc71974..30fa031c52 100644 --- a/src/Utilities/objective_container.jl +++ b/src/Utilities/objective_container.jl @@ -121,21 +121,21 @@ function MOI.get( ::MOI.ObjectiveFunction{F}, ) where {T,F} if o.scalar_affine !== nothing - return convert(F, o.scalar_affine) + return convert(F, something(o.scalar_affine)) elseif o.single_variable !== nothing - return convert(F, o.single_variable) + return convert(F, something(o.single_variable)) elseif o.scalar_quadratic !== nothing - return convert(F, o.scalar_quadratic) + return convert(F, something(o.scalar_quadratic)) elseif o.scalar_nonlinear !== nothing - return convert(F, o.scalar_nonlinear) + return convert(F, something(o.scalar_nonlinear)) elseif o.vector_variables !== nothing - return convert(F, o.vector_variables) + return convert(F, something(o.vector_variables)) elseif o.vector_affine !== nothing - return convert(F, o.vector_affine) + return convert(F, something(o.vector_affine)) elseif o.vector_quadratic !== nothing - return convert(F, o.vector_quadratic) + return convert(F, something(o.vector_quadratic)) elseif o.vector_nonlinear !== nothing - return convert(F, 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})) @@ -262,11 +262,13 @@ 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!(something(o.single_variable), change) elseif o.scalar_affine !== nothing - o.scalar_affine = modify_function!(o.scalar_affine, change) + o.scalar_affine = modify_function!(something(o.scalar_affine), change) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = modify_function!(o.scalar_quadratic, change) + o.scalar_quadratic = + modify_function!(something(o.scalar_quadratic), change) elseif o.scalar_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -276,11 +278,13 @@ function MOI.modify( ), ) elseif o.vector_variables !== nothing - o.vector_variables = modify_function!(o.vector_variables, change) + o.vector_variables = + modify_function!(something(o.vector_variables), change) elseif o.vector_quadratic !== nothing - o.vector_quadratic = modify_function!(o.vector_quadratic, change) + o.vector_quadratic = + modify_function!(something(o.vector_quadratic), change) elseif o.vector_affine !== nothing - o.vector_affine = modify_function!(o.vector_affine, change) + o.vector_affine = modify_function!(something(o.vector_affine), change) elseif o.vector_nonlinear !== nothing throw( MOI.ModifyObjectiveNotAllowed( @@ -304,13 +308,13 @@ end function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) if o.single_variable !== nothing - if x == o.single_variable + if x == something(o.single_variable) _empty_keeping_sense(o) end elseif o.scalar_affine !== nothing - o.scalar_affine = remove_variable(o.scalar_affine, x) + o.scalar_affine = remove_variable(something(o.scalar_affine), x) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = remove_variable(o.scalar_quadratic, x) + o.scalar_quadratic = remove_variable(something(o.scalar_quadratic), x) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -320,14 +324,14 @@ function MOI.delete(o::ObjectiveContainer, x::MOI.VariableIndex) ), ) elseif o.vector_variables !== nothing - o.vector_variables = remove_variable(o.vector_variables, x) - if isempty(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(o.vector_affine, x) + o.vector_affine = remove_variable(something(o.vector_affine), x) elseif o.vector_quadratic !== nothing - o.vector_quadratic = remove_variable(o.vector_quadratic, x) + o.vector_quadratic = remove_variable(something(o.vector_quadratic), x) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -343,13 +347,14 @@ 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 something(o.single_variable) 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, something(o.scalar_affine)) elseif o.scalar_quadratic !== nothing - o.scalar_quadratic = filter_variables(keep, o.scalar_quadratic) + o.scalar_quadratic = + filter_variables(keep, something(o.scalar_quadratic)) elseif o.scalar_nonlinear !== nothing throw( MOI.DeleteNotAllowed( @@ -359,14 +364,16 @@ function MOI.delete(o::ObjectiveContainer, x::Vector{MOI.VariableIndex}) ), ) elseif o.vector_variables !== nothing - o.vector_variables = filter_variables(keep, o.vector_variables) - if isempty(o.vector_variables.variables) + o.vector_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, o.vector_affine) + o.vector_affine = filter_variables(keep, something(o.vector_affine)) elseif o.vector_quadratic !== nothing - o.vector_quadratic = filter_variables(keep, o.vector_quadratic) + o.vector_quadratic = + filter_variables(keep, something(o.vector_quadratic)) elseif o.vector_nonlinear !== nothing throw( MOI.DeleteNotAllowed( 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,