Skip to content

Commit

Permalink
Make Optimizer.variable_info field a concrete type (#385)
Browse files Browse the repository at this point in the history
* Fix type stability of .variable_info by being explicit about the hash/inverse hash

* Formatting

* Formatting
  • Loading branch information
joehuchette authored Jan 6, 2021
1 parent 249022a commit a452044
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ end
Base.cconvert(::Type{Ptr{Cvoid}}, x::Env) = x
Base.unsafe_convert(::Type{Ptr{Cvoid}}, env::Env) = env.ptr_env::Ptr{Cvoid}

const _HASH = CleverDicts.key_to_index
const _INVERSE_HASH = x -> CleverDicts.index_to_key(MOI.VariableIndex, x)

mutable struct Optimizer <: MOI.AbstractOptimizer
# The low-level Gurobi model.
inner::Ptr{Cvoid}
Expand Down Expand Up @@ -135,7 +138,12 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
# A mapping from the MOI.VariableIndex to the Gurobi column. _VariableInfo
# also stores some additional fields like what bounds have been added, the
# variable type, and the names of SingleVariable-in-Set constraints.
variable_info::CleverDicts.CleverDict{MOI.VariableIndex, _VariableInfo}
variable_info::CleverDicts.CleverDict{
MOI.VariableIndex,
_VariableInfo,
typeof(_HASH),
typeof(_INVERSE_HASH),
}

# If you add variables to a model that had variables deleted AND has
# not called `update_model!` since the deletion, then the newly created
Expand Down Expand Up @@ -231,7 +239,8 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
model.params[string(name)] = value
end
model.silent = false
model.variable_info = CleverDicts.CleverDict{MOI.VariableIndex, _VariableInfo}()
model.variable_info =
CleverDicts.CleverDict{MOI.VariableIndex,_VariableInfo}(_HASH, _INVERSE_HASH)
model.next_column = 1
model.last_constraint_index = 1
model.columns_deleted_since_last_update = Int[]
Expand Down

0 comments on commit a452044

Please sign in to comment.