-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathindex_map.jl
More file actions
112 lines (92 loc) · 3.38 KB
/
index_map.jl
File metadata and controls
112 lines (92 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
# IndexMap is defined here because there is a boostrapping problem.
# * IndexMap requires `Utilities.CleverDicts` and `Utilities.DoubleDicts`, so
# if it were to be defined in MOI proper, it must be included after
# Utilities.
# * However, Utilities requires IndexMap, so it must be defined before
# Utilities.jl is included.
# To work around this issue, we define `IndexMap` here.
struct IndexMap <: AbstractDict{MOI.Index,MOI.Index}
var_map::CleverDicts.CleverDict{
MOI.VariableIndex,
MOI.VariableIndex,
typeof(CleverDicts.key_to_index),
typeof(CleverDicts.index_to_key),
}
con_map::DoubleDicts.IndexDoubleDict
nl_cache::Dict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction}
end
"""
IndexMap()
The dictionary-like object returned by [`MOI.copy_to`](@ref).
"""
function IndexMap()
var_map = CleverDicts.CleverDict{MOI.VariableIndex,MOI.VariableIndex}()
con_map = DoubleDicts.IndexDoubleDict()
nl_cache = Dict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction}()
return IndexMap(var_map, con_map, nl_cache)
end
function _identity_constraints_map(
model,
map::DoubleDicts.IndexDoubleDictInner{F,S},
) where {F,S}
for c in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
map[c] = c
end
return
end
"""
identity_index_map(model::MOI.ModelLike)
Return an [`IndexMap`](@ref) that maps all variable and constraint indices of
`model` to themselves.
"""
function identity_index_map(model::MOI.ModelLike)
variables = MOI.get(model, MOI.ListOfVariableIndices())
map = IndexMap()
for x in variables
map[x] = x
end
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
_identity_constraints_map(model, map.con_map[F, S])
end
return map
end
Base.getindex(map::IndexMap, key::MOI.VariableIndex) = map.var_map[key]
function Base.getindex(map::IndexMap, key::MOI.ConstraintIndex{F,S}) where {F,S}
return map.con_map[key]::MOI.ConstraintIndex{F,S}
end
function Base.getindex(map::IndexMap, ::Type{F}, ::Type{S}) where {F,S}
return map.con_map[F, S]
end
function Base.setindex!(
map::IndexMap,
value::MOI.VariableIndex,
key::MOI.VariableIndex,
)
return map.var_map[key] = value
end
function Base.setindex!(
map::IndexMap,
value::MOI.ConstraintIndex{F,S},
key::MOI.ConstraintIndex{F,S},
) where {F,S}
return map.con_map[key] = value
end
Base.delete!(map::IndexMap, x::MOI.VariableIndex) = delete!(map.var_map, x)
Base.delete!(map::IndexMap, c::MOI.ConstraintIndex) = delete!(map.con_map, c)
Base.haskey(map::IndexMap, c::MOI.ConstraintIndex) = haskey(map.con_map, c)
Base.haskey(map::IndexMap, x::MOI.VariableIndex) = haskey(map.var_map, x)
function Base.keys(map::IndexMap)
return Iterators.flatten((keys(map.var_map), keys(map.con_map)))
end
Base.length(map::IndexMap) = length(map.var_map) + length(map.con_map)
function Base.iterate(map::IndexMap, args...)
return iterate(Base.Iterators.flatten((map.var_map, map.con_map)), args...)
end
function map_indices(index_map::IndexMap, f::MOI.ScalarNonlinearFunction)
return map_indices(Base.Fix1(getindex, index_map), f, index_map.nl_cache)
end