Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Feb 22, 2021
1 parent 796edb8 commit dac42f8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ function Base.haskey(c::CleverDict{K}, key::K) where {K}
return _is_dense(c) ? c.hash(key)::Int64 in c.set : haskey(c.dict, key)
end

function Base.keys(c::CleverDict)
function Base.keys(c::CleverDict{K}) where {K}
return if _is_dense(c)
map(c.inverse_hash, c.set)
[c.inverse_hash(K, index) for index in c.set]
else
keys(c.dict)
collect(keys(c.dict))
end
end

Expand Down
21 changes: 13 additions & 8 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ function MOI.is_valid(
)
end
function MOI.is_valid(model::AbstractModel, ci::CI{F,S}) where {F,S}
return MOI.is_valid(constraints(model, ci), ci)
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
Expand Down Expand Up @@ -626,9 +630,9 @@ end

function MOI.get(model::AbstractModel, loc::MOI.ListOfConstraintIndices{F,S}) where {F,S}
if MOI.supports_constraint(model, F, S)
return MOI.ConstraintIndex{F,S}[]
return MOI.get(constraints(model, F, S), loc)
else
return MOI.get(constraints(model, loc), loc)
return MOI.ConstraintIndex{F,S}[]
end
end

Expand All @@ -640,8 +644,12 @@ function MOI.get(
MOI.throw_if_not_valid(model, ci)
return MOI.SingleVariable(MOI.VariableIndex(ci.value))
end
function MOI.get(model::AbstractModel, ::MOI.ConstraintFunction, ci::CI)
return _getfunction(model, ci, getconstrloc(model, ci))
function MOI.get(
model::AbstractModel,
attr::Union{MOI.ConstraintFunction, MOI.ConstraintSet},
ci::MOI.ConstraintIndex
)
return MOI.get(constraints(model, ci), attr, ci)
end

function _get_single_variable_set(
Expand Down Expand Up @@ -688,9 +696,6 @@ function MOI.get(
MOI.throw_if_not_valid(model, ci)
return _get_single_variable_set(model, S, ci.value)
end
function MOI.get(model::AbstractModel, ::MOI.ConstraintSet, ci::CI)
return _getset(model, ci, getconstrloc(model, ci))
end

function MOI.is_empty(model::AbstractModel)
return isempty(model.name) &&
Expand Down
34 changes: 31 additions & 3 deletions src/Utilities/vector_of_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ function MOI.add_constraint(
func::F,
set::S
) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet}
return CleverDicts.add_item(v.constraints, (func, set))
# f needs to be copied, see #2
# We canonicalize the constraint so that solvers can avoid having to canonicalize
# it most of the time (they can check if they need to with `is_canonical`.
# Note that the canonicalization is not guaranteed if for instance
# `modify` is called and adds a new term.
# See https://github.com/jump-dev/MathOptInterface.jl/pull/1118
return CleverDicts.add_item(v.constraints, (canonical(func), copy(set)))
end
function MOI.is_valid(
v::VectorOfConstraints{F,S},
Expand All @@ -44,11 +50,33 @@ function MOI.delete(v::VectorOfConstraints{F,S}, ci::MOI.ConstraintIndex{F,S}) w
delete!(v.constraints, ci)
end
function MOI.get(v::VectorOfConstraints{F,S}, ::MOI.ConstraintFunction, ci::MOI.ConstraintIndex{F,S}) where {F,S}
MOI.throw_if_not_valid(v, ci)
return v.constraints[ci][1]
end
function MOI.get(v::VectorOfConstraints{F,S}, ::MOI.ConstraintSet, ci::MOI.ConstraintIndex{F,S}) where {F,S}
MOI.throw_if_not_valid(v, ci)
return v.constraints[ci][2]
end
function MOI.set(
v::VectorOfConstraints{F,S},
::MOI.ConstraintFunction,
ci::MOI.ConstraintIndex{F,S},
func::F,
) where {F,S}
MOI.throw_if_not_valid(v, ci)
v.constraints[ci] = (func, v.constraints[ci][2])
return
end
function MOI.set(
v::VectorOfConstraints{F,S},
::MOI.ConstraintSet,
ci::MOI.ConstraintIndex{F,S},
set::S,
) where {F,S}
MOI.throw_if_not_valid(v, ci)
v.constraints[ci] = (v.constraints[ci][1], set)
return
end

function MOI.get(
v::VectorOfConstraints{F,S},
Expand All @@ -67,8 +95,8 @@ function MOI.modify(
ci::MOI.ConstraintIndex{F,S},
change::MOI.AbstractFunctionModification,
) where {F,S}
func, set = constraint[ci]
constraint[ci] = (modify_function(func, change), set)
func, set = v.constraints[ci]
v.constraints[ci] = (modify_function(func, change), set)
return
end

Expand Down

0 comments on commit dac42f8

Please sign in to comment.