-
Notifications
You must be signed in to change notification settings - Fork 87
/
map.jl
359 lines (301 loc) · 10.3 KB
/
map.jl
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# 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.
"""
Map <: AbstractDict{MOI.VariableIndex,AbstractBridge}
Mapping between bridged constraints and the bridge that bridged the constraint.
"""
struct Map <: AbstractDict{MOI.ConstraintIndex,AbstractBridge}
# Constraint Index of bridged constraint -> Bridge.
# It is set to `nothing` when the constraint is deleted.
bridges::Vector{Union{Nothing,AbstractBridge}}
# Constraint Index of bridged constraint -> Constraint type.
constraint_types::Vector{Tuple{Type,Type}}
# The order of the keys is used in `keys_of_type` which is used by
# `ListOfConstraintIndices`. Therefore they need to be in the order
# of creation so we need `OrderedDict` and not `Dict`.
# For `VariableIndex` constraints: (variable, set type) -> bridge
single_variable_constraints::OrderedDict{Tuple{Int64,Type},AbstractBridge}
needs_final_touch::OrderedDict{Type,OrderedSet}
function Map()
return new(
Union{Nothing,AbstractBridge}[],
Tuple{Type,Type}[],
OrderedDict{Tuple{Int64,Type},AbstractBridge}(),
OrderedDict{Type,OrderedSet}(),
)
end
end
function Base.show(io::IO, B::Map)
indent = " "^get(io, :indent, 0)
n = length(B)
s = n == 1 ? "" : "s"
return print(io, "\n$(indent)with $(n) constraint bridge$s")
end
# Implementation of `AbstractDict` interface.
function Base.isempty(map::Map)
return all(bridge -> bridge === nothing, map.bridges) &&
isempty(map.single_variable_constraints)
end
function Base.empty!(map::Map)
empty!(map.bridges)
empty!(map.constraint_types)
empty!(map.single_variable_constraints)
empty!(map.needs_final_touch)
return map
end
function Base.length(map::Map)
return count(bridge -> bridge !== nothing, map.bridges) +
length(map.single_variable_constraints)
end
_index(ci::MOI.ConstraintIndex) = ci.value
_index(ci::MOI.ConstraintIndex{MOI.VectorOfVariables}) = -ci.value
function Base.haskey(map::Map, ci::MOI.ConstraintIndex{F,S}) where {F,S}
return 1 <= _index(ci) <= length(map.bridges) &&
map.bridges[_index(ci)] !== nothing &&
(F, S) == map.constraint_types[_index(ci)]
end
Base.getindex(map::Map, ci::MOI.ConstraintIndex) = map.bridges[_index(ci)]
function Base.haskey(
map::Map,
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
) where {S}
return haskey(map.single_variable_constraints, (ci.value, S))
end
function Base.getindex(
map::Map,
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
) where {S}
return map.single_variable_constraints[(ci.value, S)]
end
function Base.delete!(map::Map, ci::MOI.ConstraintIndex)
_unregister_for_final_touch(map, map.bridges[_index(ci)])
map.bridges[_index(ci)] = nothing
return map
end
function Base.delete!(
map::Map,
ci::MOI.ConstraintIndex{MOI.VariableIndex,S},
) where {S}
bridge = map.single_variable_constraints[(ci.value, S)]
_unregister_for_final_touch(map, bridge)
delete!(map.single_variable_constraints, (ci.value, S))
return map
end
function Base.values(map::Map)
return Base.Iterators.flatten((
# See comment in `values(::Variable.Map)`.
Base.Iterators.Filter(bridge -> bridge !== nothing, map.bridges),
values(map.single_variable_constraints),
))
end
# Implementation of iterate: it should combine non-`VariableIndex` constraints
# and `VariableIndex` constraints.
function _iterate_sv(
map::Map,
elem_state = iterate(map.single_variable_constraints),
)
if elem_state === nothing
return nothing
end
(i, S), bridge = elem_state[1]
ci = MOI.ConstraintIndex{MOI.VariableIndex,S}(i)
return ci => bridge, (2, elem_state[2])
end
_index(index, F, S) = MOI.ConstraintIndex{F,S}(index)
function _index(index, F::Type{MOI.VectorOfVariables}, S)
return MOI.ConstraintIndex{F,S}(-index)
end
function _iterate(map::Map, state = 1)
while state ≤ length(map.bridges) && map.bridges[state] === nothing
state += 1
end
if state > length(map.bridges)
return _iterate_sv(map)
end
F, S = map.constraint_types[state]
return _index(state, F, S) => map.bridges[state], (1, state + 1)
end
Base.iterate(map::Map) = _iterate(map)
function Base.iterate(map::Map, state)
if state[1] == 1
return _iterate(map, state[2])
end
return _iterate_sv(map, iterate(map.single_variable_constraints, state[2]))
end
# Custom interface for information needed by `AbstractBridgeOptimizer`s that is
# not part of the `AbstractDict` interface.
"""
number_of_type(map::Map, C::Type{<:MOI.ConstraintIndex})
Return the number of keys of type `C` in `map`.
"""
function number_of_type(map::Map, ::Type{MOI.ConstraintIndex{F,S}}) where {F,S}
return count(i -> haskey(map, _index(i, F, S)), eachindex(map.bridges))
end
function number_of_type(
map::Map,
::Type{MOI.ConstraintIndex{MOI.VariableIndex,S}},
) where {S}
ret = 0
for (_, key) in keys(map.single_variable_constraints)
ret += Int(key == S)
end
return ret
end
"""
keys_of_type(map::Map, C::Type{<:MOI.ConstraintIndex})
Return a list of all the keys of type `C` in `map` in order order in which they
were created with `add_key_for_bridge`.
"""
function keys_of_type(map::Map, C::Type{MOI.ConstraintIndex{F,S}}) where {F,S}
return Base.Iterators.Filter(
ci -> haskey(map, ci),
MOI.Utilities.LazyMap{C}(i -> _index(i, F, S), eachindex(map.bridges)),
)
end
function keys_of_type(
map::Map,
C::Type{MOI.ConstraintIndex{MOI.VariableIndex,S}},
) where {S}
return MOI.Utilities.LazyMap{C}(
key -> C(key[1]),
Base.Iterators.Filter(
key -> key[2] == S,
keys(map.single_variable_constraints),
),
)
end
"""
list_of_key_types(map::Map)
Return a list of all the different concrete type of keys in `map`.
"""
function list_of_key_types(map::Map)
list = Set{Tuple{Type,Type}}()
for i in eachindex(map.bridges)
if map.bridges[i] !== nothing
push!(list, map.constraint_types[i])
end
end
for (_, key) in keys(map.single_variable_constraints)
push!(list, (MOI.VariableIndex, key))
end
return list
end
"""
variable_constraints(map::Map, vi::MOI.VariableIndex)
Return the list of all keys corresponding to [`MathOptInterface.VariableIndex`](@ref)
constraints on the variable `vi`.
"""
function variable_constraints(map::Map, vi::MOI.VariableIndex)
cis = MOI.ConstraintIndex{MOI.VariableIndex}[]
for key in keys(map.single_variable_constraints)
if key[1] == vi.value
push!(cis, MOI.ConstraintIndex{MOI.VariableIndex,key[2]}(vi.value))
end
end
return cis
end
"""
vector_of_variables_constraints(map::Map)
Return the list of all keys that correspond to
[`MathOptInterface.VectorOfVariables`](@ref) constraints.
"""
function vector_of_variables_constraints(map::Map)
return MOI.Utilities.LazyMap{MOI.ConstraintIndex{MOI.VectorOfVariables}}(
i -> MOI.ConstraintIndex{map.constraint_types[i]...}(-i),
Base.Iterators.Filter(
i ->
map.bridges[i] !== nothing &&
map.constraint_types[i][1] == MOI.VectorOfVariables,
eachindex(map.bridges),
),
)
end
"""
has_bridges(map::Map)::Bool
Return a `Bool` indicating whether any bridge was added yet.
Note that it returns `false` even if all bridges were deleted while `isempty`
would return `true`.
It is computed in `O(1)` while `isempty` needs `O(n)` hence it is used by
[`MOI.Bridges.AbstractBridgeOptimizer`](@ref) to shortcut operations in case
variable bridges are not used.
"""
function has_bridges(map::Map)
return !isempty(map.bridges) || !isempty(map.single_variable_constraints)
end
"""
add_key_for_bridge(
map::Map,
bridge::AbstractBridge,
func::MOI.AbstractFunction,
set::MOI.AbstractSet,
)
Return a new constraint index `ci` and store the mapping `ci => bridge`.
"""
function add_key_for_bridge end
function add_key_for_bridge(
map::Map,
bridge::AbstractBridge,
::F,
::S,
) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet}
_register_for_final_touch(map, bridge)
push!(map.bridges, bridge)
push!(map.constraint_types, (F, S))
return _index(length(map.bridges), F, S)
end
function add_key_for_bridge(
map::Map,
bridge::AbstractBridge,
func::MOI.VariableIndex,
::S,
) where {S<:MOI.AbstractScalarSet}
_register_for_final_touch(map, bridge)
map.single_variable_constraints[(func.value, S)] = bridge
return MOI.ConstraintIndex{MOI.VariableIndex,S}(func.value)
end
function _register_for_final_touch(map::Map, bridge::BT) where {BT}
if MOI.Bridges.needs_final_touch(bridge)
if !haskey(map.needs_final_touch, BT)
map.needs_final_touch[BT] = OrderedSet{BT}()
end
push!(map.needs_final_touch[BT], bridge)
end
return
end
function _unregister_for_final_touch(b::Map, bridge::BT) where {BT}
if MOI.Bridges.needs_final_touch(bridge)
delete!(b.needs_final_touch[BT], bridge)
end
return
end
# Function barrier to iterate over bridges of the same type in an efficient way.
function _final_touch(bridges, model)
for bridge in bridges
MOI.Bridges.final_touch(bridge, model)
end
return
end
function MOI.Bridges.final_touch(map::Map, model::MOI.ModelLike)
for bridges in values(map.needs_final_touch)
_final_touch(bridges, model)
end
return
end
"""
EmptyMap <: AbstractDict{MOI.ConstraintIndex, AbstractBridge}
Empty version of [`Map`](@ref). It is used by
[`MOI.Bridges.Variable.SingleBridgeOptimizer`](@ref) as it does
not bridge any constraint.
"""
struct EmptyMap <: AbstractDict{MOI.VariableIndex,AbstractBridge} end
Base.show(::IO, ::EmptyMap) = nothing
Base.isempty(::EmptyMap) = true
Base.empty!(::EmptyMap) = nothing
Base.keys(::EmptyMap) = MOI.Utilities.EmptyVector{MOI.VariableIndex}()
Base.values(::EmptyMap) = MOI.Utilities.EmptyVector{AbstractBridge}()
has_bridges(::EmptyMap) = false
number_of_type(::EmptyMap, ::Type{<:MOI.ConstraintIndex}) = 0
MOI.Bridges.final_touch(::EmptyMap, ::MOI.ModelLike) = nothing