Skip to content

Commit

Permalink
Rebase and formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Mar 10, 2021
1 parent 53eb140 commit 8afdfe2
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 63 deletions.
1 change: 1 addition & 0 deletions src/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ function map_values!(f::Function, d::CleverDict)
d.dict[k] = f(v)
end
end
return
end

end
49 changes: 33 additions & 16 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ const AbstractModel{T} = Union{AbstractModelLike{T},AbstractOptimizer{T}}
# Variables
function MOI.get(model::AbstractModel, ::MOI.NumberOfVariables)::Int64
if model.variable_indices === nothing
model.num_variables_created
return model.num_variables_created
else
length(model.variable_indices)
return length(model.variable_indices)
end
end

function MOI.add_variable(model::AbstractModel{T}) where {T}
vi = VI(model.num_variables_created += 1)
push!(model.single_variable_mask, 0x0)
Expand All @@ -23,6 +24,7 @@ function MOI.add_variable(model::AbstractModel{T}) where {T}
end
return vi
end

function MOI.add_variables(model::AbstractModel, n::Integer)
return [MOI.add_variable(model) for i in 1:n]
end
Expand Down Expand Up @@ -119,6 +121,7 @@ function MOI.delete(model::AbstractModel, vi::MOI.VariableIndex)
model.name_to_con = nothing
return
end

function MOI.delete(model::AbstractModel, vis::Vector{MOI.VariableIndex})
if isempty(vis)
# In `keep`, we assume that `model.variable_indices !== nothing` so
Expand Down Expand Up @@ -152,13 +155,15 @@ function MOI.is_valid(
model.single_variable_mask[ci.value] & single_variable_flag(S),
)
end

function MOI.is_valid(model::AbstractModel, ci::CI{F,S}) where {F,S}
if MOI.supports_constraint(model, F, S)
return MOI.is_valid(constraints(model, ci), ci)
else
return false
end
end

function MOI.is_valid(model::AbstractModel, vi::VI)
if model.variable_indices === nothing
return 1 vi.value model.num_variables_created
Expand Down Expand Up @@ -187,8 +192,10 @@ MOI.get(model::AbstractModel, ::MOI.Name) = model.name
MOI.supports(::AbstractModel, ::MOI.VariableName, vi::Type{VI}) = true
function MOI.set(model::AbstractModel, ::MOI.VariableName, vi::VI, name::String)
model.var_to_name[vi] = name
return model.name_to_var = nothing # Invalidate the name map.
model.name_to_var = nothing # Invalidate the name map.
return
end

function MOI.get(model::AbstractModel, ::MOI.VariableName, vi::VI)
return get(model.var_to_name, vi, EMPTYSTRING)
end
Expand Down Expand Up @@ -252,8 +259,10 @@ function MOI.set(
name::String,
)
model.con_to_name[ci] = name
return model.name_to_con = nothing # Invalidate the name map.
model.name_to_con = nothing # Invalidate the name map.
return
end

function MOI.get(model::AbstractModel, ::MOI.ConstraintName, ci::CI)
return get(model.con_to_name, ci, EMPTYSTRING)
end
Expand Down Expand Up @@ -285,11 +294,7 @@ function MOI.get(model::AbstractModel, ConType::Type{<:CI}, name::String)
end
ci = get(model.name_to_con, name, nothing)
throw_if_multiple_with_name(ci, name)
if ci isa ConType
return ci
else
return nothing
end
return ci isa ConType ? ci : nothing
end

function MOI.get(
Expand All @@ -312,8 +317,10 @@ function MOI.set(
model.objective = zero(MOI.ScalarAffineFunction{T})
end
model.senseset = true
return model.sense = sense
model.sense = sense
return
end

function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunctionType)
return MOI.typeof(model.objective)
end
Expand All @@ -340,7 +347,8 @@ function MOI.set(
end
model.objectiveset = true
# f needs to be copied, see #2
return model.objective = copy(f)
model.objective = copy(f)
return
end

function MOI.modify(
Expand Down Expand Up @@ -529,23 +537,29 @@ function _delete_constraint(
ci::MOI.ConstraintIndex{MOI.SingleVariable,S},
) where {S}
MOI.throw_if_not_valid(model, ci)
return model.single_variable_mask[ci.value] &= ~single_variable_flag(S)
model.single_variable_mask[ci.value] &= ~single_variable_flag(S)
return
end

function _delete_constraint(model::AbstractModel, ci::MOI.ConstraintIndex)
return MOI.delete(constraints(model, ci), ci)
MOI.delete(constraints(model, ci), ci)
return
end

function MOI.delete(model::AbstractModel, ci::MOI.ConstraintIndex)
_delete_constraint(model, ci)
model.name_to_con = nothing
return delete!(model.con_to_name, ci)
delete!(model.con_to_name, ci)
return
end

function MOI.modify(
model::AbstractModel,
ci::MOI.ConstraintIndex,
change::MOI.AbstractFunctionModification,
)
return MOI.modify(constraints(model, ci), ci, change)
MOI.modify(constraints(model, ci), ci, change)
return
end

function MOI.set(
Expand All @@ -570,14 +584,16 @@ function MOI.set(
if !iszero(flag & UPPER_BOUND_MASK)
model.upper_bound[ci.value] = extract_upper_bound(set)
end
return
end
function MOI.set(
model::AbstractModel,
attr::Union{MOI.ConstraintFunction, MOI.ConstraintSet},
ci::MOI.ConstraintIndex,
func_or_set,
)
return MOI.set(constraints(model, ci), attr, ci, func_or_set)
MOI.set(constraints(model, ci), attr, ci, func_or_set)
return
end

function MOI.get(
Expand Down Expand Up @@ -733,6 +749,7 @@ function MOI.empty!(model::AbstractModel{T}) where {T}
empty!(model.con_to_name)
model.name_to_con = nothing
broadcastcall(MOI.empty!, model)
return
end


Expand Down
33 changes: 23 additions & 10 deletions src/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ function MOI.empty!(uf::UniversalFallback)
uf.name_to_con = nothing
empty!(uf.modattr)
empty!(uf.varattr)
return empty!(uf.conattr)
empty!(uf.conattr)
return
end

function MOI.copy_to(uf::UniversalFallback, src::MOI.ModelLike; kws...)
return MOIU.automatic_copy_to(uf, src; kws...)
end

function supports_default_copy_to(uf::UniversalFallback, copy_names::Bool)
return supports_default_copy_to(uf.model, copy_names)
end
Expand Down Expand Up @@ -116,9 +119,11 @@ function _delete(uf::UniversalFallback, ci::MOI.ConstraintIndex{MOI.SingleVariab
MOI.is_valid(uf, ci) || throw(MOI.InvalidIndex(ci))
delete!(uf.single_variable_constraints[S], ci)
end
return
end
function _delete(uf::UniversalFallback, ci::MOI.ConstraintIndex)
MOI.delete(constraints(uf, ci), ci)
return
end
function MOI.delete(uf::UniversalFallback, ci::MOI.ConstraintIndex{F,S}) where {F,S}
_delete(uf, ci)
Expand All @@ -129,6 +134,7 @@ function MOI.delete(uf::UniversalFallback, ci::MOI.ConstraintIndex{F,S}) where {
for d in values(uf.conattr)
delete!(d, ci)
end
return
end
function _remove_variable(
uf::UniversalFallback,
Expand Down Expand Up @@ -161,6 +167,7 @@ function MOI.delete(uf::UniversalFallback, vi::MOI.VariableIndex)
end
end
end
return
end
function MOI.delete(uf::UniversalFallback, vis::Vector{MOI.VariableIndex})
fast_in_vis = Set(vis)
Expand Down Expand Up @@ -190,6 +197,7 @@ function MOI.delete(uf::UniversalFallback, vis::Vector{MOI.VariableIndex})
end
end
end
return
end

# Attributes
Expand Down Expand Up @@ -225,9 +233,9 @@ function MOI.get(
attr::Union{MOI.AbstractOptimizerAttribute,MOI.AbstractModelAttribute},
)
if !MOI.is_copyable(attr) || MOI.supports(uf.model, attr)
MOI.get(uf.model, attr)
return MOI.get(uf.model, attr)
else
_get(uf, attr)
return _get(uf, attr)
end
end
function MOI.get(
Expand All @@ -237,9 +245,9 @@ function MOI.get(
) where {F,S}
if MOI.supports_constraint(uf.model, F, S) &&
(!MOI.is_copyable(attr) || MOI.supports(uf.model, attr, typeof(idx)))
MOI.get(uf.model, attr, idx)
return MOI.get(uf.model, attr, idx)
else
_get(uf, attr, idx)
return _get(uf, attr, idx)
end
end
function MOI.get(
Expand All @@ -248,9 +256,9 @@ function MOI.get(
idx::MOI.VariableIndex,
)
if !MOI.is_copyable(attr) || MOI.supports(uf.model, attr, typeof(idx))
MOI.get(uf.model, attr, idx)
return MOI.get(uf.model, attr, idx)
else
_get(uf, attr, idx)
return _get(uf, attr, idx)
end
end
function MOI.get(
Expand Down Expand Up @@ -280,9 +288,9 @@ function MOI.get(
) where {S}
F = MOI.SingleVariable
if MOI.supports_constraint(uf.model, F, S)
MOI.get(uf.model, listattr)
return MOI.get(uf.model, listattr)
else
collect(keys(get(
return collect(keys(get(
uf.single_variable_constraints,
S,
OrderedDict{CI{F,S},S}(),
Expand Down Expand Up @@ -359,7 +367,8 @@ function MOI.set(
if sense == MOI.FEASIBILITY_SENSE
uf.objective = nothing
end
return MOI.set(uf.model, attr, sense)
MOI.set(uf.model, attr, sense)
return
end
function MOI.get(uf::UniversalFallback, attr::MOI.ObjectiveFunctionType)
if uf.objective === nothing
Expand Down Expand Up @@ -394,6 +403,7 @@ function MOI.set(
MOI.set(uf.model, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
MOI.set(uf.model, MOI.ObjectiveSense(), sense)
end
return
end

function MOI.modify(
Expand All @@ -406,6 +416,7 @@ function MOI.modify(
else
uf.objective = modify_function(uf.objective, change)
end
return
end

# Name
Expand Down Expand Up @@ -628,6 +639,7 @@ function MOI.modify(
change::MOI.AbstractFunctionModification,
)
MOI.modify(constraints(uf, ci), ci, change)
return
end

function MOI.get(
Expand Down Expand Up @@ -688,6 +700,7 @@ function MOI.set(
MOI.throw_if_not_valid(uf, ci)
uf.single_variable_constraints[S][ci] = set
end
return
end

# Variables
Expand Down
Loading

0 comments on commit 8afdfe2

Please sign in to comment.