Skip to content

Commit

Permalink
Fix various JET errors
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Sep 18, 2023
1 parent dc166f6 commit 470e90f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/FileFormats/CBF/CBF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion src/FileFormats/MPS/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions src/FileFormats/NL/NL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -781,29 +781,29 @@ 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(
model::Model,
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(
model::Model,
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
42 changes: 22 additions & 20 deletions src/FileFormats/SDPA/SDPA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
84 changes: 58 additions & 26 deletions src/Utilities/objective_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand Down Expand Up @@ -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)

Check warning on line 297 in src/Utilities/objective_container.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/objective_container.jl#L297

Added line #L297 was not covered by tests
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(
Expand All @@ -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)

Check warning on line 311 in src/Utilities/objective_container.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/objective_container.jl#L311

Added line #L311 was not covered by tests
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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 470e90f

Please sign in to comment.