/
set_map.jl
283 lines (245 loc) · 8.15 KB
/
set_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
# 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.
"""
abstract type MultiSetMapBridge{T,S1,G} <: AbstractBridge end
Same as `SetMapBridge` but the output constraint type does not only depend on
the input constraint type.
When subtyping `MultiSetMapBridge`, `added_constraint_types` and `supports`
should additionally be implemented by the bridge.
For example, if a bridge `BridgeType` may create either a constraint of type
`F2`-in-`S2` or `F3`-in-`S3`, these methods should be implemented as follows:
```julia
function MOI.Bridges.added_constraint_types(
::Type{<:BridgeType{T,F2,F3}},
) where {T,F2,F3}
return Tuple{Type,Type}[(F2, S2), (F3, S3)]
end
function MOI.supports(
model::MOI.ModelLike,
attr::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart},
::Type{<:BridgeType{T,F2,F3}},
) where {T,F2,F3}
return MOI.supports(model, attr, MOI.ConstraintIndex{F2,S2}) ||
MOI.supports(model, attr, MOI.ConstraintIndex{F3,S3})
end
```
"""
abstract type MultiSetMapBridge{T,S1,G} <: AbstractBridge end
function bridge_constraint(
BT::Type{<:MultiSetMapBridge{T,S1,G}},
model::MOI.ModelLike,
func::G,
set::S1,
) where {T,S1,G}
mapped_func = MOI.Bridges.map_function(BT, func)
mapped_set = MOI.Bridges.map_set(BT, set)
constraint = MOI.add_constraint(model, mapped_func, mapped_set)
return BT(constraint)
end
function MOI.supports_constraint(
::Type{<:MultiSetMapBridge{T,S1}},
::Type{<:MOI.AbstractScalarFunction},
::Type{S1},
) where {T,S1<:MOI.AbstractScalarSet}
return true
end
function MOI.supports_constraint(
::Type{<:MultiSetMapBridge{T,S1}},
::Type{<:MOI.AbstractVectorFunction},
::Type{S1},
) where {T,S1<:MOI.AbstractVectorSet}
return true
end
function MOI.Bridges.added_constrained_variable_types(
::Type{<:MultiSetMapBridge},
)
return Tuple{Type}[]
end
# Attributes, Bridge acting as a model
function MOI.get(
bridge::MultiSetMapBridge,
::MOI.NumberOfConstraints{F,S},
)::Int64 where {F,S}
return bridge.constraint isa MOI.ConstraintIndex{F,S} ? 1 : 0
end
function MOI.get(
bridge::MultiSetMapBridge,
::MOI.ListOfConstraintIndices{F,S},
) where {F,S}
if bridge.constraint isa MOI.ConstraintIndex{F,S}
return MOI.ConstraintIndex{F,S}[bridge.constraint]
end
return MOI.ConstraintIndex{F,S}[]
end
# References
function MOI.delete(model::MOI.ModelLike, bridge::MultiSetMapBridge)
MOI.delete(model, bridge.constraint)
return
end
# Attributes, Bridge acting as a constraint
function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintFunction,
bridge::MultiSetMapBridge{T,S1,G},
) where {T,S1,G}
mapped_func = MOI.get(model, attr, bridge.constraint)
func = MOI.Bridges.inverse_map_function(typeof(bridge), mapped_func)
return MOI.Utilities.convert_approx(G, func)
end
function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintFunction,
bridge::MultiSetMapBridge{T,S1,G},
func::G,
) where {T,S1,G}
new_f = MOI.Bridges.map_function(typeof(bridge), func)
MOI.set(model, attr, bridge.constraint, new_f)
return
end
function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintSet,
bridge::MultiSetMapBridge,
)
set = MOI.get(model, attr, bridge.constraint)
return MOI.Bridges.inverse_map_set(typeof(bridge), set)
end
function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintSet,
bridge::MultiSetMapBridge{T,S1},
set::S1,
) where {T,S1}
new_set = MOI.Bridges.map_set(typeof(bridge), set)
MOI.set(model, attr, bridge.constraint, new_set)
return
end
function MOI.get(
model::MOI.ModelLike,
attr::Union{MOI.ConstraintPrimal,MOI.ConstraintPrimalStart},
bridge::MultiSetMapBridge,
)
value = MOI.get(model, attr, bridge.constraint)
if value === nothing
return nothing
end
return MOI.Bridges.inverse_map_function(typeof(bridge), value)
end
function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintPrimalStart,
bridge::MultiSetMapBridge,
value,
)
if value === nothing
MOI.set(model, attr, bridge.constraint, nothing)
else
mapped_value = MOI.Bridges.map_function(typeof(bridge), value)
MOI.set(model, attr, bridge.constraint, mapped_value)
end
return
end
function MOI.get(
model::MOI.ModelLike,
attr::Union{MOI.ConstraintDual,MOI.ConstraintDualStart},
bridge::MultiSetMapBridge,
)
value = MOI.get(model, attr, bridge.constraint)
if value === nothing
return nothing
end
return MOI.Bridges.adjoint_map_function(typeof(bridge), value)
end
function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintDualStart,
bridge::BT,
value,
) where {BT<:MultiSetMapBridge}
if value === nothing
MOI.set(model, attr, bridge.constraint, nothing)
else
mapped_value = MOI.Bridges.inverse_adjoint_map_function(BT, value)
MOI.set(model, attr, bridge.constraint, mapped_value)
end
return
end
# By linearity of the map, we can just change the constant/coefficient
function MOI.modify(
model::MOI.ModelLike,
bridge::BT,
change::MOI.ScalarConstantChange,
) where {BT<:MultiSetMapBridge}
new_constant = MOI.Bridges.map_function(BT, change.new_constant)
MOI.modify(model, bridge.constraint, MOI.ScalarConstantChange(new_constant))
return
end
function MOI.modify(
model::MOI.ModelLike,
bridge::BT,
change::MOI.VectorConstantChange,
) where {BT<:MultiSetMapBridge}
new_constant = MOI.Bridges.map_function(BT, change.new_constant)
MOI.modify(model, bridge.constraint, MOI.VectorConstantChange(new_constant))
return
end
function MOI.modify(
model::MOI.ModelLike,
bridge::BT,
change::MOI.ScalarCoefficientChange,
) where {BT<:MultiSetMapBridge}
new_coefficient = MOI.Bridges.map_function(BT, change.new_coefficient)
new_change = MOI.ScalarCoefficientChange(change.variable, new_coefficient)
MOI.modify(model, bridge.constraint, new_change)
return
end
function MOI.modify(
model::MOI.ModelLike,
bridge::BT,
change::MOI.MultirowChange,
) where {BT<:MultiSetMapBridge}
# It is important here that `change.new_coefficients` contains the complete
# new sparse column associated to the variable. Calling modify twice with
# part of the column won't work since the linear map might reset all the
# column each time.
coefficients = MOI.Bridges.map_function(BT, change.new_coefficients)
new_change = MOI.MultirowChange(change.variable, coefficients)
MOI.modify(model, bridge.constraint, new_change)
return
end
"""
abstract type SetMapBridge{T,S2,S1,F,G} <: MultiSetMapBridge{T,S1,G} end
Consider two type of sets, `S1` and `S2`, and a linear mapping `A` such that
the image of a set of type `S1` under `A` is a set of type `S2`.
A `SetMapBridge{T,S2,S1,F,G}` is a bridge that maps `G`-in-`S1` constraints
into `F`-in-`S2` by mapping the function through `A`.
The linear map `A` is described by;
* [`MOI.Bridges.map_set`](@ref)
* [`MOI.Bridges.map_function`](@ref).
Implementing a method for these two functions is sufficient to bridge
constraints. However, in order for the getters and setters of attributes such as
dual solutions and starting values to work as well, a method for the following
functions must be implemented:
* [`MOI.Bridges.inverse_map_set`](@ref)
* [`MOI.Bridges.inverse_map_function`](@ref)
* [`MOI.Bridges.adjoint_map_function`](@ref)
* [`MOI.Bridges.inverse_adjoint_map_function`](@ref)
See the docstrings of each function to see which feature would be missing if it
was not implemented for a given bridge.
"""
abstract type SetMapBridge{T,S2,S1,F,G} <: MultiSetMapBridge{T,S1,G} end
function MOI.Bridges.added_constraint_types(
::Type{<:SetMapBridge{T,S2,S1,F}},
) where {T,S2,S1,F}
return Tuple{Type,Type}[(F, S2)]
end
function MOI.supports(
model::MOI.ModelLike,
attr::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart},
::Type{<:SetMapBridge{T,S2,S1,F}},
) where {T,S2,S1,F}
return MOI.supports(model, attr, MOI.ConstraintIndex{F,S2})
end