-
Notifications
You must be signed in to change notification settings - Fork 5
/
update_parameters.jl
286 lines (267 loc) · 10.1 KB
/
update_parameters.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
# Copyright (c) 2020: Tomás Gutierrez 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.
function set_with_new_constant(s::MOI.LessThan{T}, val::T) where {T}
return MOI.LessThan{T}(s.upper - val)
end
function set_with_new_constant(s::MOI.GreaterThan{T}, val::T) where {T}
return MOI.GreaterThan{T}(s.lower - val)
end
function set_with_new_constant(s::MOI.EqualTo{T}, val::T) where {T}
return MOI.EqualTo{T}(s.value - val)
end
function set_with_new_constant(s::MOI.Interval{T}, val::T) where {T}
return MOI.Interval{T}(s.lower - val, s.upper - val)
end
# Affine
# change to use only inner_ci all around so tha tupdates are faster
# modifications should not be used any ways, afterall we have param all around
function update_parametric_affine_constraints!(model::Optimizer)
for (F, S) in keys(model.affine_constraint_cache.dict)
affine_constraint_cache_inner = model.affine_constraint_cache[F, S]
affine_constraint_cache_set_inner =
model.affine_constraint_cache_set[F, S]
if !isempty(affine_constraint_cache_inner)
# barrier to avoid type instability of inner dicts
update_parametric_affine_constraints!(
model,
affine_constraint_cache_inner,
affine_constraint_cache_set_inner,
)
end
end
return
end
# TODO: cache changes and then batch them instead
function update_parametric_affine_constraints!(
model::Optimizer,
affine_constraint_cache_inner::DoubleDictInner{F,S,V},
affine_constraint_cache_set_inner::DoubleDictInner{
F,
S,
MOI.AbstractScalarSet,
},
) where {F,S<:SIMPLE_SCALAR_SETS{T},V} where {T}
# cis = MOI.ConstraintIndex{F,S}[]
# sets = S[]
# sizehint!(cis, length(affine_constraint_cache_inner))
# sizehint!(sets, length(affine_constraint_cache_inner))
for (inner_ci, pf) in affine_constraint_cache_inner
delta_constant = delta_parametric_constant(model, pf)
if !iszero(delta_constant)
pf.current_constant += delta_constant
new_set = S(pf.set_constant - pf.current_constant)
# new_set = set_with_new_constant(set, param_constant)
MOI.set(model.optimizer, MOI.ConstraintSet(), inner_ci, new_set)
# push!(cis, inner_ci)
# push!(sets, new_set)
end
end
# if !isempty(cis)
# MOI.set(model.optimizer, MOI.ConstraintSet(), cis, sets)
# end
return
end
function update_parametric_affine_constraints!(
model::Optimizer,
affine_constraint_cache_inner::DoubleDictInner{F,S,V},
affine_constraint_cache_set_inner::DoubleDictInner{
F,
S,
MOI.AbstractScalarSet,
},
) where {F,S<:MOI.Interval{T},V} where {T}
for (inner_ci, pf) in affine_constraint_cache_inner
set = affine_constraint_cache_set_inner[inner_ci]::S
delta_constant = delta_parametric_constant(model, pf)
if !iszero(delta_constant)
pf.current_constant += delta_constant
# new_set = S(pf.set_constant - pf.current_constant)
new_set = set_with_new_constant(set, pf.current_constant)::S
MOI.set(model.optimizer, MOI.ConstraintSet(), inner_ci, new_set)
end
end
return
end
function update_parametric_vector_affine_constraints!(model::Optimizer)
for (F, S) in keys(model.vector_affine_constraint_cache.dict)
vector_affine_constraint_cache_inner =
model.vector_affine_constraint_cache[F, S]
if !isempty(vector_affine_constraint_cache_inner)
# barrier to avoid type instability of inner dicts
update_parametric_vector_affine_constraints!(
model,
vector_affine_constraint_cache_inner,
)
end
end
return
end
function update_parametric_vector_affine_constraints!(
model::Optimizer,
vector_affine_constraint_cache_inner::DoubleDictInner{F,S,V},
) where {F<:MOI.VectorAffineFunction{T},S,V} where {T}
for (inner_ci, pf) in vector_affine_constraint_cache_inner
delta_constant = delta_parametric_constant(model, pf)
if !iszero(delta_constant)
pf.current_constant .+= delta_constant
MOI.modify(
model.optimizer,
inner_ci,
MOI.VectorConstantChange(pf.current_constant),
)
end
end
return
end
function update_parametric_quadratic_constraints!(model::Optimizer)
for (F, S) in keys(model.quadratic_constraint_cache.dict)
quadratic_constraint_cache_inner =
model.quadratic_constraint_cache[F, S]
quadratic_constraint_cache_set_inner =
model.quadratic_constraint_cache_set[F, S]
if !isempty(quadratic_constraint_cache_inner)
# barrier to avoid type instability of inner dicts
update_parametric_quadratic_constraints!(
model,
quadratic_constraint_cache_inner,
quadratic_constraint_cache_set_inner,
)
end
end
return
end
function affine_build_change_and_up_param_func(
pf::ParametricQuadraticFunction{T},
delta_terms,
) where {T}
changes = Vector{MOI.ScalarCoefficientChange}(undef, length(delta_terms))
i = 1
for (var, coef) in delta_terms
base_coef = pf.current_terms_with_p[var]
new_coef = base_coef + coef
pf.current_terms_with_p[var] = new_coef
changes[i] = MOI.ScalarCoefficientChange(var, new_coef)
i += 1
end
return changes
end
function update_parametric_quadratic_constraints!(
model::Optimizer,
quadratic_constraint_cache_inner::DoubleDictInner{F,S,V},
quadratic_constraint_cache_set_inner::DoubleDictInner{
F,
S,
MOI.AbstractScalarSet,
},
) where {F,S<:SIMPLE_SCALAR_SETS{T},V} where {T}
# cis = MOI.ConstraintIndex{F,S}[]
# sets = S[]
# sizehint!(cis, length(quadratic_constraint_cache_inner))
# sizehint!(sets, length(quadratic_constraint_cache_inner))
for (inner_ci, pf) in quadratic_constraint_cache_inner
delta_constant = delta_parametric_constant(model, pf)
if !iszero(delta_constant)
pf.current_constant += delta_constant
new_set = S(pf.set_constant - pf.current_constant)
# new_set = set_with_new_constant(set, param_constant)
MOI.set(model.optimizer, MOI.ConstraintSet(), inner_ci, new_set)
# push!(cis, inner_ci)
# push!(sets, new_set)
end
delta_terms = delta_parametric_affine_terms(model, pf)
if !isempty(delta_terms)
changes = affine_build_change_and_up_param_func(pf, delta_terms)
cis = fill(inner_ci, length(changes))
MOI.modify(model.optimizer, cis, changes)
end
end
# if !isempty(cis)
# MOI.set(model.optimizer, MOI.ConstraintSet(), cis, sets)
# end
return
end
function update_parametric_quadratic_constraints!(
model::Optimizer,
quadratic_constraint_cache_inner::DoubleDictInner{F,S,V},
quadratic_constraint_cache_set_inner::DoubleDictInner{
F,
S,
MOI.AbstractScalarSet,
},
) where {F,S<:MOI.Interval{T},V} where {T}
for (inner_ci, pf) in quadratic_constraint_cache_inner
set = quadratic_constraint_cache_set_inner[inner_ci]::S
delta_constant = delta_parametric_constant(model, pf)
if !iszero(delta_constant)
pf.current_constant += delta_constant
# new_set = S(pf.set_constant - pf.current_constant)
new_set = set_with_new_constant(set, pf.current_constant)::S
MOI.set(model.optimizer, MOI.ConstraintSet(), inner_ci, new_set)
end
delta_terms = delta_parametric_affine_terms(model, pf)
if !isempty(delta_terms)
changes = affine_build_change_and_up_param_func(pf, delta_terms)
cis = fill(inner_ci, length(changes))
MOI.modify(model.optimizer, cis, changes)
end
end
return
end
function update_parametric_affine_objective!(model::Optimizer{T}) where {T}
if model.affine_objective_cache === nothing
return
end
pf = model.affine_objective_cache
delta_constant = delta_parametric_constant(model, pf)
if !iszero(delta_constant)
pf.current_constant += delta_constant
# F = MOI.get(model.optimizer, MOI.ObjectiveFunctionType())
MOI.modify(
model.optimizer,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}(),
MOI.ScalarConstantChange(pf.current_constant),
)
end
return
end
function update_parametric_quadratic_objective!(model::Optimizer{T}) where {T}
if model.quadratic_objective_cache === nothing
return
end
pf = model.quadratic_objective_cache
delta_constant = delta_parametric_constant(model, pf)
if !iszero(delta_constant)
pf.current_constant += delta_constant
F = MOI.get(model.optimizer, MOI.ObjectiveFunctionType())
MOI.modify(
model.optimizer,
MOI.ObjectiveFunction{F}(),
MOI.ScalarConstantChange(pf.current_constant),
)
end
delta_terms = delta_parametric_affine_terms(model, pf)
if !isempty(delta_terms)
F = MOI.get(model.optimizer, MOI.ObjectiveFunctionType())
changes = affine_build_change_and_up_param_func(pf, delta_terms)
MOI.modify(model.optimizer, MOI.ObjectiveFunction{F}(), changes)
end
return
end
function update_parameters!(model::Optimizer)
update_parametric_affine_constraints!(model)
update_parametric_vector_affine_constraints!(model)
update_parametric_quadratic_constraints!(model)
update_parametric_affine_objective!(model)
update_parametric_quadratic_objective!(model)
# Update parameters and put NaN to indicate that the parameter has been
# updated
for (parameter_index, val) in model.updated_parameters
if !isnan(val)
model.parameters[parameter_index] = val
model.updated_parameters[parameter_index] = NaN
end
end
return
end