-
Notifications
You must be signed in to change notification settings - Fork 87
/
scalarize.jl
295 lines (262 loc) · 7.69 KB
/
scalarize.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
# 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.
"""
ScalarizeBridge{T,F,S}
`ScalarizeBridge` implements the following reformulations:
* ``f(x) - a \\in \\mathbb{R}_+`` into ``f_i(x) \\ge a_i`` for all ``i``
* ``f(x) - a \\in \\mathbb{R}_-`` into ``f_i(x) \\le a_i`` for all ``i``
* ``f(x) - a \\in \\{0\\}`` into ``f_i(x) == a_i`` for all ``i``
## Source node
`ScalarizeBridge` supports:
* `G` in [`MOI.Nonnegatives{T}`](@ref)
* `G` in [`MOI.Nonpositives{T}`](@ref)
* `G` in [`MOI.Zeros{T}`](@ref)
## Target nodes
`ScalarizeBridge` creates:
* `F` in `S`, where `S` is one of [`MOI.GreaterThan{T}`](@ref),
[`MOI.LessThan{T}`](@ref), and [`MOI.EqualTo{T}`](@ref), depending on the
type of the input set.
"""
mutable struct ScalarizeBridge{T,F,S} <: AbstractBridge
scalar_constraints::Vector{MOI.ConstraintIndex{F,S}}
constants::Vector{T}
end
const Scalarize{T,OT<:MOI.ModelLike} =
SingleBridgeOptimizer{ScalarizeBridge{T},OT}
function bridge_constraint(
::Type{ScalarizeBridge{T,F,S}},
model::MOI.ModelLike,
f::MOI.AbstractVectorFunction,
set::MOI.Utilities.VectorLinearSet,
) where {T,F,S}
dimension = MOI.output_dimension(f)
constants = MOI.constant(f, T)
new_f = MOI.Utilities.scalarize(f, true)
constraints = Vector{MOI.ConstraintIndex{F,S}}(undef, dimension)
for i in 1:dimension
constraints[i] = MOI.add_constraint(model, new_f[i], S(-constants[i]))
end
return ScalarizeBridge{T,F,S}(constraints, constants)
end
function MOI.supports_constraint(
::Type{ScalarizeBridge{T}},
F::Type{<:MOI.AbstractVectorFunction},
S::Type{<:MOI.Utilities.VectorLinearSet},
) where {T}
# If `F` is `MOI.VectorAffineFunction{Complex{Float64}}`, `S` is `MOI.Zeros`
# and `T` is `Float64`, it would create a set `MOI.EqualTo{Float64}` which
# is incorrect hence we say we only support it if the coefficient type of
# `F` is `T`.
# If `S` is `MOI.Nonnegatives` or `MOI.Nonpositives` then `T` must be
# `Real` as otherwise, `scalar_set_type` will fail in `concrete_bridge_type`.
return MOI.Utilities.is_coefficient_type(F, T) &&
(S === MOI.Zeros || (T <: Real))
end
function MOI.Bridges.added_constrained_variable_types(::Type{<:ScalarizeBridge})
return Tuple{Type}[]
end
function MOI.Bridges.added_constraint_types(
::Type{ScalarizeBridge{T,F,S}},
) where {T,F,S}
return Tuple{Type,Type}[(F, S)]
end
function concrete_bridge_type(
::Type{<:ScalarizeBridge{T}},
F::Type{<:MOI.AbstractVectorFunction},
S::Type{<:MOI.Utilities.VectorLinearSet},
) where {T}
return ScalarizeBridge{
T,
MOI.Utilities.scalar_type(F),
MOI.Utilities.scalar_set_type(S, T),
}
end
function MOI.get(
bridge::ScalarizeBridge{T,F,S},
::MOI.NumberOfConstraints{F,S},
)::Int64 where {T,F,S}
return length(bridge.scalar_constraints)
end
function MOI.get(
bridge::ScalarizeBridge{T,F,S},
::MOI.ListOfConstraintIndices{F,S},
) where {T,F,S}
return copy(bridge.scalar_constraints)
end
function MOI.delete(model::MOI.ModelLike, bridge::ScalarizeBridge)
for ci in bridge.scalar_constraints
MOI.delete(model, ci)
end
return
end
function MOI.delete(
model::MOI.ModelLike,
bridge::ScalarizeBridge,
i::MOI.Bridges.IndexInVector,
)
MOI.delete(model, bridge.scalar_constraints[i.value])
deleteat!(bridge.scalar_constraints, i.value)
deleteat!(bridge.constants, i.value)
return
end
function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintFunction,
bridge::ScalarizeBridge{T},
) where {T}
func = MOI.Utilities.vectorize(
MOI.get.(model, attr, bridge.scalar_constraints),
)
if func isa MOI.VectorOfVariables
return func
end
# `func` is in terms of bridged variables here while in `bridge_constraint`
# it was in terms of the solver variables so `MOI.constant(set)` might be
# different than `bridge.constants[i]`.
for i in eachindex(bridge.scalar_constraints)
set = MOI.get(model, MOI.ConstraintSet(), bridge.scalar_constraints[i])
rhs = MOI.constant(set)
if !iszero(rhs)
func = MOI.Utilities.operate_output_index!(-, T, i, func, rhs)
end
end
return func
end
function MOI.get(
::MOI.ModelLike,
::MOI.ConstraintSet,
bridge::ScalarizeBridge{T,F,S},
) where {T,F,S}
return MOI.Utilities.vector_set_type(S)(length(bridge.scalar_constraints))
end
function MOI.supports(
model::MOI.ModelLike,
attr::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart},
::Type{ScalarizeBridge{T,F,S}},
) where {T,F,S}
return MOI.supports(model, attr, MOI.ConstraintIndex{F,S})
end
function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintPrimal,
bridge::ScalarizeBridge,
)
return MOI.get.(model, attr, bridge.scalar_constraints) .+ bridge.constants
end
function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintPrimalStart,
bridge::ScalarizeBridge,
)
values = MOI.get.(model, attr, bridge.scalar_constraints)
if any(isnothing, values)
return
end
return values .+ bridge.constants
end
function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintPrimalStart,
bridge::ScalarizeBridge,
::Nothing,
)
for ci in bridge.scalar_constraints
MOI.set(model, attr, ci, nothing)
end
return
end
function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintPrimalStart,
bridge::ScalarizeBridge,
value,
)
MOI.set.(model, attr, bridge.scalar_constraints, value .- bridge.constants)
return
end
function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintDual,
bridge::ScalarizeBridge,
)
return MOI.get.(model, attr, bridge.scalar_constraints)
end
function MOI.get(
model::MOI.ModelLike,
attr::MOI.ConstraintDualStart,
bridge::ScalarizeBridge,
)
values = MOI.get.(model, attr, bridge.scalar_constraints)
if any(isnothing, values)
return
end
return values
end
function MOI.set(
model::MOI.ModelLike,
attr::MOI.ConstraintDualStart,
bridge::ScalarizeBridge,
value,
)
MOI.set.(model, attr, bridge.scalar_constraints, value)
return
end
function MOI.modify(
model::MOI.ModelLike,
bridge::ScalarizeBridge{T,F,S},
change::MOI.VectorConstantChange{T},
) where {T,F,S}
bridge.constants = change.new_constant
MOI.set.(
model,
MOI.ConstraintSet(),
bridge.scalar_constraints,
S.(-change.new_constant),
)
return
end
function MOI.modify(
model::MOI.ModelLike,
bridge::ScalarizeBridge,
change::MOI.MultirowChange{T},
) where {T}
for (index, value) in change.new_coefficients
MOI.modify(
model,
bridge.scalar_constraints[index],
MOI.ScalarCoefficientChange{T}(change.variable, value),
)
end
return
end
function MOI.set(
model::MOI.ModelLike,
::MOI.ConstraintFunction,
bridge::ScalarizeBridge{T,F,S},
func,
) where {T,F,S}
old_constants = bridge.constants
bridge.constants = MOI.constant(func, T)
new_func = MOI.Utilities.scalarize(func, true)
MOI.set.(
model,
MOI.ConstraintFunction(),
bridge.scalar_constraints,
new_func,
)
for i in eachindex(bridge.constants)
if bridge.constants[i] != old_constants[i]
MOI.set(
model,
MOI.ConstraintSet(),
bridge.scalar_constraints[i],
S(-bridge.constants[i]),
)
end
end
return
end
# TODO implement transform