Skip to content

Commit

Permalink
Refactor Model and UniversalFallback (#1245)
Browse files Browse the repository at this point in the history
* Refactor Model and UniversalFallback

* Fixes

* Fixes

* Simplify

* Don't do breaking changes to CleverDicts

* Refactor UniversalFallback

* Fixes

* Rebase and formatting fixes

* Fix docs

Co-authored-by: odow <o.dowson@gmail.com>
  • Loading branch information
blegat and odow authored Mar 11, 2021
1 parent 2bd73ab commit e0f8b05
Show file tree
Hide file tree
Showing 9 changed files with 653 additions and 543 deletions.
1 change: 0 additions & 1 deletion docs/src/manual/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)
# output
MAX_SENSE::OptimizationSense = 1
```

We add the knapsack constraint and integrality constraints:
Expand Down
48 changes: 31 additions & 17 deletions src/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ function index_to_key(::Type{MathOptInterface.VariableIndex}, index::Int64)
return MathOptInterface.VariableIndex(index)
end

key_to_index(key::MathOptInterface.VariableIndex) = key.value
function index_to_key(::Type{MathOptInterface.ConstraintIndex{F,S}}, index::Int64) where {F,S}
return MathOptInterface.ConstraintIndex{F,S}(index)
end

key_to_index(key::MathOptInterface.Index) = key.value

# Now, on with `CleverDicts`.

Expand Down Expand Up @@ -62,22 +66,6 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
set::BitSet
vector::Vector{V}
dict::OrderedCollections.OrderedDict{K,V}
function CleverDict{K,V}(n::Integer = 0) where {K,V}
set = BitSet()
sizehint!(set, n)
vec = Vector{K}(undef, n)
inverse_hash = x -> index_to_key(K, x)
hash = key_to_index
return new{K,V,typeof(hash),typeof(inverse_hash)}(
0,
hash,
inverse_hash,
true,
set,
vec,
OrderedCollections.OrderedDict{K,V}(),
)
end
function CleverDict{K,V}(
hash::F,
inverse_hash::I,
Expand All @@ -97,6 +85,9 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
)
end
end
function CleverDict{K,V}(n::Integer = 0) where {K,V}
return CleverDict{K,V}(key_to_index, Base.Fix1(index_to_key, K), n)
end

"""
index_to_key(::Type{K}, index::Int)
Expand Down Expand Up @@ -151,6 +142,14 @@ 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{K}) where {K}
return if _is_dense(c)
[c.inverse_hash(Int64(index))::K for index in c.set]
else
collect(keys(c.dict))
end
end

function Base.get(c::CleverDict, key, default)
if _is_dense(c)
if !haskey(c, key)
Expand Down Expand Up @@ -363,4 +362,19 @@ function Base.resize!(c::CleverDict{K,V}, n) where {K,V}
return
end

Base.values(d::CleverDict) = _is_dense(d) ? d.vector : values(d.dict)

# TODO `map!(f, values(dict::AbstractDict))` requires Julia 1.2 or later,
# use `map_values` once we drop Julia 1.1 and earlier.
function map_values!(f::Function, d::CleverDict)
if _is_dense(d)
map!(f, d.vector, d.vector)
else
for (k, v) in d.dict
d.dict[k] = f(v)
end
end
return
end

end
2 changes: 1 addition & 1 deletion src/Utilities/DoubleDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Works as a `AbstractDict{CI, V}` with minimal differences.
Note that `CI` is not a concrete type, opposed to `CI{MOI.SingleVariable, MOI.Integers}`,
which is a concrete type.
When optimal performance or type stability is required its possible to obtain a
When optimal performance or type stability is required it is possible to obtain a
fully type stable dictionary with values of type `V` and keys of type
`CI{MOI.SingleVariable, MOI.Integers}` from the dictionary `dict`, for instance:
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ include("copy.jl")
include("results.jl")
include("variables.jl")

include("vector_of_constraints.jl")
include("model.jl")
include("parser.jl")
include("mockoptimizer.jl")
Expand Down
Loading

0 comments on commit e0f8b05

Please sign in to comment.