Skip to content

Improve type inference in Utilities #1316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Utilities/cachingoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ function reset_optimizer(m::CachingOptimizer, optimizer::MOI.AbstractOptimizer)
for attr in MOI.get(m.model_cache, MOI.ListOfOptimizerAttributesSet())
# Skip attributes which don't apply to the new optimizer.
if attr isa MOI.RawParameter
# Even if the optimizer claims to `supports` `attr`, the value
# Even if the optimizer claims to `supports` `attr`, the value
# might have a different meaning (e.g., two solvers with `logLevel`
# as a RawParameter). To be on the safe side, just skip all raw
# as a RawParameter). To be on the safe side, just skip all raw
# parameters.
continue
elseif !MOI.is_copyable(attr) || !MOI.supports(m.optimizer, attr)
Expand Down Expand Up @@ -277,7 +277,8 @@ function MOI.add_variables(m::CachingOptimizer, n)
if m.state == ATTACHED_OPTIMIZER
if m.mode == AUTOMATIC
try
vindices_optimizer = MOI.add_variables(m.optimizer, n)
vindices_optimizer =
MOI.add_variables(m.optimizer, n)::Vector{MOI.VariableIndex}
catch err
if err isa MOI.NotAllowedError
reset_optimizer(m)
Expand All @@ -286,7 +287,8 @@ function MOI.add_variables(m::CachingOptimizer, n)
end
end
else
vindices_optimizer = MOI.add_variables(m.optimizer, n)
vindices_optimizer =
MOI.add_variables(m.optimizer, n)::Vector{MOI.VariableIndex}
end
end
vindices = MOI.add_variables(m.model_cache, n)
Expand Down
28 changes: 12 additions & 16 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -614,34 +614,30 @@ function MOI.get(
return MOI.get(model.constraints, noc)
end

function _add_contraint_type(
function _add_constraint_type(
list,
model::AbstractModel,
S::Type{<:MOI.AbstractScalarSet},
)
flag = single_variable_flag(S)
flag = single_variable_flag(S)::UInt8
if any(mask -> !iszero(flag & mask), model.single_variable_mask)
push!(list, (MOI.SingleVariable, S))
end
return
end
function MOI.get(
model::AbstractModel{T},
loc::MOI.ListOfConstraintTypesPresent,
attr::MOI.ListOfConstraintTypesPresent,
) where {T}
list = copy(MOI.get(model.constraints, loc))
for S in (
MOI.EqualTo{T},
MOI.GreaterThan{T},
MOI.LessThan{T},
MOI.Interval{T},
MOI.Semicontinuous{T},
MOI.Semiinteger{T},
MOI.Integer,
MOI.ZeroOne,
)
_add_contraint_type(list, model, S)
end
list = MOI.get(model.constraints, attr)::Vector{Tuple{DataType,DataType}}
_add_constraint_type(list, model, MOI.EqualTo{T})
_add_constraint_type(list, model, MOI.GreaterThan{T})
_add_constraint_type(list, model, MOI.LessThan{T})
_add_constraint_type(list, model, MOI.Interval{T})
_add_constraint_type(list, model, MOI.Semicontinuous{T})
_add_constraint_type(list, model, MOI.Semiinteger{T})
_add_constraint_type(list, model, MOI.Integer)
_add_constraint_type(list, model, MOI.ZeroOne)
return list
end

Expand Down
24 changes: 12 additions & 12 deletions src/Utilities/vector_of_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ end
# Deletion of variables in vector of variables

function _remove_variable(v::VectorOfConstraints, vi::MOI.VariableIndex)
CleverDicts.map_values!(v.constraints) do func_set
return remove_variable(func_set..., vi)
CleverDicts.map_values!(v.constraints) do (f, s)
return remove_variable(f, s, vi)
end
return
end
function _filter_variables(keep::Function, v::VectorOfConstraints)
CleverDicts.map_values!(v.constraints) do func_set
return filter_variables(keep, func_set...)
function _filter_variables(keep::F, v::VectorOfConstraints) where {F<:Function}
CleverDicts.map_values!(v.constraints) do (f, s)
return filter_variables(keep, f, s)
end
return
end
Expand Down Expand Up @@ -203,10 +203,10 @@ function _delete_variables(
end

function _delete_variables(
callback::Function,
v::VectorOfConstraints{MOI.VectorOfVariables,S},
callback::F,
v::VectorOfConstraints{MOI.VectorOfVariables,<:MOI.AbstractVectorSet},
vis::Vector{MOI.VariableIndex},
) where {S<:MOI.AbstractVectorSet}
) where {F<:Function}
filter!(v.constraints) do p
f = p.second[1]
del = if length(f.variables) == 1
Expand All @@ -223,21 +223,21 @@ function _delete_variables(
end

function _deleted_constraints(
callback::Function,
callback::F,
v::VectorOfConstraints,
vi::MOI.VariableIndex,
)
) where {F<:Function}
vis = [vi]
_delete_variables(callback, v, vis)
_remove_variable(v, vi)
return
end

function _deleted_constraints(
callback::Function,
callback::F,
v::VectorOfConstraints,
vis::Vector{MOI.VariableIndex},
)
) where {F<:Function}
removed = Set(vis)
_delete_variables(callback, v, vis)
_filter_variables(vi -> !(vi in removed), v)
Expand Down