-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdiff_opt.jl
585 lines (498 loc) · 16.9 KB
/
diff_opt.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# Copyright (c) 2020: Akshay Sharma and contributors
#
# 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.
# Constructs a Differentiable Optimizer model from a MOI Optimizer.
# Supports `forward_differentiate!` and `reverse_differentiate!` methods for solving and differentiating the model respectectively.
# ## Note
# Currently supports differentiating linear and quadratic programs only.
const MOIDD = MOI.Utilities.DoubleDicts
Base.@kwdef mutable struct InputCache
dx::Dict{MOI.VariableIndex,Float64} = Dict{MOI.VariableIndex,Float64}()# dz for QP
# ds
# dy #= [d\lambda, d\nu] for QP
# FIXME Would it be possible to have a DoubleDict where the value depends
# on the function type ? Here, we need to create two dicts to have
# concrete value types.
# `scalar_constraints` and `vector_constraints` includes `A` and `b` for CPs
# or `G` and `h` for QPs
scalar_constraints::MOIDD.DoubleDict{MOI.ScalarAffineFunction{Float64}} =
MOIDD.DoubleDict{MOI.ScalarAffineFunction{Float64}}() # also includes G for QPs
vector_constraints::MOIDD.DoubleDict{MOI.VectorAffineFunction{Float64}} =
MOIDD.DoubleDict{MOI.VectorAffineFunction{Float64}}() # also includes G for QPs
objective::Union{Nothing,MOI.AbstractScalarFunction} = nothing
end
function Base.empty!(cache::InputCache)
empty!(cache.dx)
empty!(cache.scalar_constraints)
empty!(cache.vector_constraints)
cache.objective = nothing
return
end
"""
reverse_differentiate!(model::MOI.ModelLike)
Wrapper method for the backward pass / reverse differentiation.
This method will consider as input a currently solved problem and differentials
with respect to the solution set with the [`ReverseVariablePrimal`](@ref) attribute.
The output problem data differentials can be queried with the
attributes [`ReverseObjectiveFunction`](@ref) and [`ReverseConstraintFunction`](@ref).
"""
function reverse_differentiate! end
"""
forward_differentiate!(model::Optimizer)
Wrapper method for the forward pass.
This method will consider as input a currently solved problem and
differentials with respect to problem data set with
the [`ForwardObjectiveFunction`](@ref) and [`ForwardConstraintFunction`](@ref) attributes.
The output solution differentials can be queried with the attribute
[`ForwardVariablePrimal`](@ref).
"""
function forward_differentiate! end
"""
empty_input_sensitivities!(model::MOI.ModelLike)
Empty the input sensitivities of the model.
Sets to zero all the sensitivities set by the user with method such as:
- `MOI.set(model, DiffOpt.ReverseVariablePrimal(), variable_index, value)`
- `MOI.set(model, DiffOpt.ForwardObjectiveFunction(), expression)`
- `MOI.set(model, DiffOpt.ForwardConstraintFunction(), index, expression)`
"""
function empty_input_sensitivities! end
"""
ForwardObjectiveFunction <: MOI.AbstractModelAttribute
A `MOI.AbstractModelAttribute` to set input data to forward differentiation, that
is, problem input data.
The possible values are any `MOI.AbstractScalarFunction`.
A `MOI.ScalarQuadraticFunction` can only be used in linearly constrained
quadratic models.
For instance, if the objective contains `θ * (x + 2y)`, for the purpose of
computing the derivative with respect to `θ`, the following should be set:
```julia
MOI.set(model, DiffOpt.ForwardObjectiveFunction(), 1.0 * x + 2.0 * y)
```
where `x` and `y` are the relevant `MOI.VariableIndex`.
"""
struct ForwardObjectiveFunction <: MOI.AbstractModelAttribute end
"""
ForwardConstraintFunction <: MOI.AbstractConstraintAttribute
A `MOI.AbstractConstraintAttribute` to set input data to forward differentiation, that
is, problem input data.
For instance, if the scalar constraint of index `ci` contains `θ * (x + 2y) <= 5θ`,
for the purpose of computing the derivative with respect to `θ`, the following
should be set:
```julia
MOI.set(model, DiffOpt.ForwardConstraintFunction(), ci, 1.0 * x + 2.0 * y - 5.0)
```
Note that we use `-5` as the `ForwardConstraintFunction` sets the tangent of the
ConstraintFunction so we consider the expression `θ * (x + 2y - 5)`.
"""
struct ForwardConstraintFunction <: MOI.AbstractConstraintAttribute end
"""
ForwardVariablePrimal <: MOI.AbstractVariableAttribute
A `MOI.AbstractVariableAttribute` to get output data from forward
differentiation, that is, problem solution.
For instance, to get the tangent of the variable of index `vi` corresponding to
the tangents given to `ForwardObjectiveFunction` and `ForwardConstraintFunction`, do the
following:
```julia
MOI.get(model, DiffOpt.ForwardVariablePrimal(), vi)
```
"""
struct ForwardVariablePrimal <: MOI.AbstractVariableAttribute end
MOI.is_set_by_optimize(::ForwardVariablePrimal) = true
"""
ReverseVariablePrimal <: MOI.AbstractVariableAttribute
A `MOI.AbstractVariableAttribute` to set input data to
reverse differentiation, that is, problem solution.
For instance, to set the tangent of the variable of index `vi`, do the
following:
```julia
MOI.set(model, DiffOpt.ReverseVariablePrimal(), x)
```
"""
struct ReverseVariablePrimal <: MOI.AbstractVariableAttribute end
"""
ReverseObjectiveFunction <: MOI.AbstractModelAttribute
A `MOI.AbstractModelAttribute` to get output data to reverse differentiation,
that is, problem input data.
For instance, to get the tangent of the objective function corresponding to
the tangent given to `ReverseVariablePrimal`, do the
following:
```julia
func = MOI.get(model, DiffOpt.ReverseObjectiveFunction())
```
Then, to get the sensitivity of the linear term with variable `x`, do
```julia
JuMP.coefficient(func, x)
```
To get the sensitivity with respect to the quadratic term with variables `x`
and `y`, do either
```julia
JuMP.coefficient(func, x, y)
```
or
```julia
DiffOpt.quad_sym_half(func, x, y)
```
!!! warning
These two lines are **not** equivalent in case `x == y`, see
[`quad_sym_half`](@ref) for the details on the difference between these two
functions.
"""
struct ReverseObjectiveFunction <: MOI.AbstractModelAttribute end
MOI.is_set_by_optimize(::ReverseObjectiveFunction) = true
"""
ReverseConstraintFunction
An `MOI.AbstractConstraintAttribute` to get output data to reverse differentiation, that
is, problem input data.
For instance, if the following returns `x + 2y + 5`, it means that the tangent
has coordinate `1` for the coefficient of `x`, coordinate `2` for the
coefficient of `y` and `5` for the function constant.
If the constraint is of the form `func == constant` or `func <= constant`,
the tangent for the constant on the right-hand side is `-5`.
```julia
MOI.get(model, DiffOpt.ReverseConstraintFunction(), ci)
```
"""
struct ReverseConstraintFunction <: MOI.AbstractConstraintAttribute end
MOI.is_set_by_optimize(::ReverseConstraintFunction) = true
"""
DifferentiateTimeSec()
A model attribute for the total elapsed time (in seconds) for computing
the differentiation information.
"""
struct DifferentiateTimeSec <: MOI.AbstractModelAttribute end
MOI.attribute_value_type(::DifferentiateTimeSec) = Float64
MOI.is_set_by_optimize(::DifferentiateTimeSec) = true
"""
abstract type AbstractModel <: MOI.ModelLike end
Model supporting [`forward_differentiate!`](@ref) and
[`reverse_differentiate!`](@ref).
"""
abstract type AbstractModel <: MOI.ModelLike end
MOI.supports_incremental_interface(::AbstractModel) = true
function MOI.is_valid(model::AbstractModel, idx::MOI.Index)
return MOI.is_valid(model.model, idx)
end
function MOI.add_variable(model::AbstractModel)
return MOI.add_variable(model.model)
end
function MOI.add_variables(model::AbstractModel, n)
return MOI.add_variables(model.model, n)
end
function MOI.Utilities.pass_nonvariable_constraints(
dest::AbstractModel,
src::MOI.ModelLike,
idxmap::MOI.Utilities.IndexMap,
constraint_types,
)
return MOI.Utilities.pass_nonvariable_constraints(
dest.model,
src,
idxmap,
constraint_types,
)
end
function MOI.Utilities.final_touch(model::AbstractModel, index_map)
return MOI.Utilities.final_touch(model.model, index_map)
end
function MOI.supports_constraint(
model::AbstractModel,
::Type{F},
::Type{S},
) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet}
return MOI.supports_constraint(model.model, F, S)
end
function MOI.add_constraint(
model::AbstractModel,
func::MOI.AbstractFunction,
set::MOI.AbstractSet,
)
return MOI.add_constraint(model.model, func, set)
end
function _enlarge_set(vec::Vector, idx, value)
m = last(idx)
if length(vec) < m
n = length(vec)
resize!(vec, m)
fill!(view(vec, (n+1):m), NaN)
end
vec[idx] = value
return
end
# The following `supports` methods are needed because
# `MOI.set(::MOI.ModelLike, ::SlackBridgePrimalDualStart, ::SlackBridge, ::Nothing)`
# checks that the model supports these starting value attributes.
function MOI.supports(
::AbstractModel,
::MOI.VariablePrimalStart,
::Type{<:MOI.VariableIndex},
)
return true
end
function MOI.supports(
::AbstractModel,
::Union{MOI.ConstraintDualStart,MOI.ConstraintPrimalStart},
::Type{<:MOI.ConstraintIndex},
)
return true
end
function MOI.get(
model::AbstractModel,
::MOI.VariablePrimalStart,
vi::MOI.VariableIndex,
)
return model.x[vi.value]
end
function MOI.set(
model::AbstractModel,
::MOI.VariablePrimalStart,
vi::MOI.VariableIndex,
value,
)
MOI.throw_if_not_valid(model, vi)
return _enlarge_set(model.x, vi.value, value)
end
function MOI.set(model::AbstractModel, ::ForwardObjectiveFunction, objective)
model.input_cache.objective = objective
return
end
function MOI.set(
model::AbstractModel,
::ReverseVariablePrimal,
vi::MOI.VariableIndex,
val,
)
model.input_cache.dx[vi] = val
return
end
function MOI.set(
model::AbstractModel,
::ForwardConstraintFunction,
ci::MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},S},
func::MOI.ScalarAffineFunction{T},
) where {T,S}
model.input_cache.scalar_constraints[ci] = func
return
end
function MOI.set(
model::AbstractModel,
::ForwardConstraintFunction,
ci::MOI.ConstraintIndex{MOI.VectorAffineFunction{T},S},
func::MOI.VectorAffineFunction{T},
) where {T,S}
model.input_cache.vector_constraints[ci] = func
return
end
function lazy_combination(op::F, α, a, β, b) where {F<:Function}
return LazyArrays.ApplyArray(
op,
LazyArrays.@~(α .* a),
LazyArrays.@~(β .* b),
)
end
function lazy_combination(
op::F,
α,
a,
β,
b,
I::AbstractUnitRange,
) where {F<:Function}
return lazy_combination(op, α, view(a, I), β, view(b, I))
end
function lazy_combination(
op::F,
a,
b,
i::Integer,
args::Vararg{Any,N},
) where {F<:Function,N}
return lazy_combination(op, a[i], b, b[i], a, args...)
end
function lazy_combination(
op::F,
a,
b,
i::AbstractUnitRange,
I::AbstractUnitRange,
) where {F<:Function}
return lazy_combination(op, view(a, i), b, view(b, i), a, I)
end
function _lazy_affine(vector, constant::Number)
return VectorScalarAffineFunction(vector, constant)
end
_lazy_affine(matrix, vector) = MatrixVectorAffineFunction(matrix, vector)
function _get_db end
function _get_dA end
function MOI.get(
model::AbstractModel,
::ReverseConstraintFunction,
ci::MOI.ConstraintIndex,
)
return _lazy_affine(_get_dA(model, ci), _get_db(model, ci))
end
"""
π(v::Vector{Float64}, model::MOI.ModelLike, cones::ProductOfSets)
Given a `model`, its `cones`, find the projection of the vectors `v`
of length equal to the number of rows in the conic form onto the cartesian
product of the cones corresponding to these rows.
For more info, refer to https://github.com/matbesancon/MathOptSetDistances.jl
"""
function π(v::Vector{T}, model::MOI.ModelLike, cones::ProductOfSets) where {T}
return map_rows(model, cones, Flattened{T}()) do ci, r
return MOSD.projection_on_set(
MOSD.DefaultDistance(),
v[r],
MOI.dual_set(MOI.get(model, MOI.ConstraintSet(), ci)),
)
end
end
"""
Dπ(v::Vector{Float64}, model, cones::ProductOfSets)
Given a `model`, its `cones`, find the gradient of the projection of
the vectors `v` of length equal to the number of rows in the conic form onto the
cartesian product of the cones corresponding to these rows.
For more info, refer to https://github.com/matbesancon/MathOptSetDistances.jl
"""
function Dπ(v::Vector{T}, model::MOI.ModelLike, cones::ProductOfSets) where {T}
return BlockDiagonals.BlockDiagonal(
map_rows(model, cones, Nested{Matrix{T}}()) do ci, r
return MOSD.projection_gradient_on_set(
MOSD.DefaultDistance(),
v[r],
MOI.dual_set(MOI.get(model, MOI.ConstraintSet(), ci)),
)
end,
)
end
# See the docstring of `map_rows`.
struct Nested{T} end
struct Flattened{T} end
# Store in `x` the values `y` corresponding to the rows `r` and the `k`th
# constraint.
function _assign_mapped!(x, y, r, k, ::Nested)
return x[k] = y
end
function _assign_mapped!(x, y, r, k, ::Flattened)
return x[r] = y
end
# Map the rows corresponding to `F`-in-`S` constraints and store it in `x`.
function _map_rows!(
f::Function,
x::Vector,
model,
cones::ProductOfSets,
::Type{F},
::Type{S},
map_mode,
k,
) where {F,S}
for ci in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
r = MOI.Utilities.rows(cones, ci)
k += 1
_assign_mapped!(x, f(ci, r), r, k, map_mode)
end
return k
end
# Allocate a vector for storing the output of `map_rows`.
function _allocate_rows(cones, ::Nested{T}) where {T}
return Vector{T}(undef, length(cones.dimension))
end
function _allocate_rows(cones, ::Flattened{T}) where {T}
return Vector{T}(undef, MOI.dimension(cones))
end
"""
map_rows(f::Function, model, cones::ProductOfSets, map_mode::Union{Nested{T}, Flattened{T}})
Given a `model`, its `cones` and `map_mode` of type `Nested` (resp.
`Flattened`), return a `Vector{T}` of length equal to the number of cones (resp.
rows) in the conic form where the value for the index (resp. rows) corresponding
to each cone is equal to `f(ci, r)` where `ci` is the corresponding constraint
index in `model` and `r` is a `UnitRange` of the corresponding rows in the conic
form.
"""
function map_rows(
f::Function,
model,
cones::ProductOfSets,
map_mode::Union{Nested,Flattened},
)
x = _allocate_rows(cones, map_mode)
k = 0
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
# Function barrier for type unstability of `F` and `S`
# `con_map` is a `MOI.Utilities.DoubleDicts.MainIndexDoubleDict`, we index it at `F, S`
# which returns a `MOI.Utilities.DoubleDicts.IndexWithType{F, S}` which is type stable.
# If we have a small number of different constraint types and many
# constraint of each type, this mostly removes type unstabilities
# as most the time is in `_map_rows!` which is type stable.
k = _map_rows!(f, x, model, cones, F, S, map_mode, k)
end
return x
end
function _fill(neg::Function, gradient_cache, input_cache, cones, args...)
return _fill(S -> true, neg, gradient_cache, input_cache, cones, args...)
end
function _fill(
filter::Function,
neg::Function,
gradient_cache,
input_cache,
cones,
args...,
)
for (F, S) in keys(input_cache.scalar_constraints.dict)
filter(S) || continue
_fill(args..., neg(S), cones, input_cache.scalar_constraints[F, S])
end
for (F, S) in keys(input_cache.vector_constraints.dict)
_fill(args..., neg(S), cones, input_cache.vector_constraints[F, S])
end
return
end
function _fill(vector::Vector, neg::Bool, cones, dict)
for (ci, func) in dict
r = MOI.Utilities.rows(cones, ci)
vector[r] = neg ? -MOI.constant(func) : MOI.constant(func)
end
return
end
function _fill(I::Vector, J::Vector, V::Vector, neg::Bool, cones, dict)
for (ci, func) in dict
r = MOI.Utilities.rows(cones, ci)
for term in func.terms
_push_term(I, J, V, neg, r, term)
end
end
return
end
function _push_term(
I::Vector,
J::Vector,
V::Vector,
neg::Bool,
r::Integer,
term::MOI.ScalarAffineTerm,
)
push!(I, r)
push!(J, term.variable.value)
return push!(V, neg ? -term.coefficient : term.coefficient)
end
function _push_term(
I::Vector,
J::Vector,
V::Vector,
neg::Bool,
r::AbstractUnitRange,
term::MOI.VectorAffineTerm,
)
return _push_term(I, J, V, neg, r[term.output_index], term.scalar_term)
end
function MOI.supports(model::AbstractModel, attr::MOI.AbstractModelAttribute)
return MOI.supports(model.model, attr)
end
function MOI.set(model::AbstractModel, attr::MOI.AbstractModelAttribute, value)
return MOI.set(model.model, attr, value)
end
function MOI.get(model::AbstractModel, attr::MOI.AbstractModelAttribute)
return MOI.get(model.model, attr)
end