-
Notifications
You must be signed in to change notification settings - Fork 213
/
MOI_wrapper.jl
388 lines (345 loc) · 12 KB
/
MOI_wrapper.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
import MathOptInterface as MOI
mutable struct Optimizer{T} <: MOI.AbstractOptimizer
# Problem data.
variables::MOI.Utilities.VariablesContainer{T}
starting_values::Vector{Union{Nothing,T}}
nlp_model::Union{MOI.Nonlinear.Model,Nothing}
sense::MOI.OptimizationSense
# Parameters.
method::Union{AbstractOptimizer,Nothing}
silent::Bool
options::Dict{Symbol,Any}
# Solution attributes.
results::Union{Nothing,MultivariateOptimizationResults}
end
function Optimizer{T}() where {T}
return Optimizer{T}(
MOI.Utilities.VariablesContainer{T}(),
Union{Nothing,T}[],
nothing,
MOI.FEASIBILITY_SENSE,
nothing,
false,
Dict{Symbol,Any}(),
nothing,
)
end
Optimizer() = Optimizer{Float64}()
MOI.supports(::Optimizer, ::MOI.NLPBlock) = true
function MOI.supports(::Optimizer, ::Union{MOI.ObjectiveSense,MOI.ObjectiveFunction})
return true
end
MOI.supports(::Optimizer, ::MOI.Silent) = true
function MOI.supports(::Optimizer, p::MOI.RawOptimizerAttribute)
return p.name == "method" || hasfield(Options, Symbol(p.name))
end
function MOI.supports(::Optimizer, ::MOI.VariablePrimalStart, ::Type{MOI.VariableIndex})
return true
end
const BOUNDS{T} = Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T},MOI.Interval{T}}
const _SETS{T} = Union{MOI.GreaterThan{T},MOI.LessThan{T},MOI.EqualTo{T}}
function MOI.supports_constraint(
::Optimizer{T},
::Type{MOI.VariableIndex},
::Type{<:BOUNDS{T}},
) where {T}
return true
end
function MOI.supports_constraint(
::Optimizer{T},
::Type{MOI.ScalarNonlinearFunction},
::Type{<:_SETS{T}},
) where {T}
return true
end
MOI.supports_incremental_interface(::Optimizer) = true
function MOI.copy_to(model::Optimizer, src::MOI.ModelLike)
return MOI.Utilities.default_copy_to(model, src)
end
MOI.get(::Optimizer, ::MOI.SolverName) = "Optim"
function MOI.set(model::Optimizer, ::MOI.ObjectiveSense, sense::MOI.OptimizationSense)
model.sense = sense
return
end
function MOI.set(model::Optimizer, ::MOI.ObjectiveFunction{F}, func::F) where {F}
nl = convert(MOI.ScalarNonlinearFunction, func)
if isnothing(model.nlp_model)
model.nlp_model = MOI.Nonlinear.Model()
end
MOI.Nonlinear.set_objective(model.nlp_model, nl)
return nothing
end
function MOI.set(model::Optimizer, ::MOI.Silent, value::Bool)
model.silent = value
return
end
MOI.get(model::Optimizer, ::MOI.Silent) = model.silent
const TIME_LIMIT = "time_limit"
MOI.supports(::Optimizer, ::MOI.TimeLimitSec) = true
function MOI.set(model::Optimizer, ::MOI.TimeLimitSec, value::Real)
MOI.set(model, MOI.RawOptimizerAttribute(TIME_LIMIT), Float64(value))
end
function MOI.set(model::Optimizer, attr::MOI.TimeLimitSec, ::Nothing)
delete!(model.options, Symbol(TIME_LIMIT))
end
function MOI.get(model::Optimizer, ::MOI.TimeLimitSec)
return get(model.options, Symbol(TIME_LIMIT), nothing)
end
MOI.Utilities.map_indices(::Function, opt::AbstractOptimizer) = opt
function MOI.set(model::Optimizer, p::MOI.RawOptimizerAttribute, value)
if p.name == "method"
model.method = value
else
model.options[Symbol(p.name)] = value
end
return
end
function MOI.get(model::Optimizer, p::MOI.RawOptimizerAttribute)
if p.name == "method"
return p.method
end
key = Symbol(p.name)
if haskey(model.options, key)
return model.options[key]
end
error("RawOptimizerAttribute with name $(p.name) is not set.")
end
MOI.get(model::Optimizer, ::MOI.SolveTimeSec) = time_run(model.results)
function MOI.empty!(model::Optimizer)
MOI.empty!(model.variables)
empty!(model.starting_values)
model.nlp_model = nothing
model.sense = MOI.FEASIBILITY_SENSE
model.results = nothing
return
end
function MOI.is_empty(model::Optimizer)
return MOI.is_empty(model.variables) &&
isempty(model.starting_values) &&
isnothing(model.nlp_model) &&
model.sense == MOI.FEASIBILITY_SENSE
end
function MOI.add_variable(model::Optimizer{T}) where {T}
push!(model.starting_values, nothing)
return MOI.add_variable(model.variables)
end
function MOI.is_valid(model::Optimizer, index::Union{MOI.VariableIndex,MOI.ConstraintIndex})
return MOI.is_valid(model.variables, index)
end
function MOI.add_constraint(model::Optimizer{T}, vi::MOI.VariableIndex, set::BOUNDS{T}) where {T}
return MOI.add_constraint(model.variables, vi, set)
end
function MOI.add_constraint(
model::Optimizer{T},
f::MOI.ScalarNonlinearFunction,
s::_SETS{T},
) where {T}
if model.nlp_model === nothing
model.nlp_model = MOI.Nonlinear.Model()
end
index = MOI.Nonlinear.add_constraint(model.nlp_model, f, s)
return MOI.ConstraintIndex{typeof(f),typeof(s)}(index.value)
end
function starting_value(optimizer::Optimizer{T}, i) where {T}
if optimizer.starting_values[i] !== nothing
return optimizer.starting_values[i]
else
v = optimizer.variables
return min(max(zero(T), v.lower[i]), v.upper[i])
end
end
function MOI.set(
model::Optimizer,
::MOI.VariablePrimalStart,
vi::MOI.VariableIndex,
value::Union{Real,Nothing},
)
MOI.throw_if_not_valid(model, vi)
model.starting_values[vi.value] = value
return
end
function requested_features(::ZerothOrderOptimizer, has_constraints)
return Symbol[]
end
function requested_features(::FirstOrderOptimizer, has_constraints)
features = [:Grad]
if has_constraints
push!(features, :Jac)
end
return features
end
function requested_features(::Union{IPNewton,SecondOrderOptimizer}, has_constraints)
features = [:Grad, :Hess]
if has_constraints
push!(features, :Jac)
end
return features
end
function sparse_to_dense!(A, I::Vector, nzval)
for k in eachindex(I)
i, j = I[k]
A[i, j] += nzval[k]
end
return A
end
function sym_sparse_to_dense!(A, I::Vector, nzval)
for k in eachindex(I)
i, j = I[k]
A[i, j] += nzval[k]
A[j, i] = A[i, j]
end
return A
end
function MOI.optimize!(model::Optimizer{T}) where {T}
backend = MOI.Nonlinear.SparseReverseMode()
vars = MOI.get(model.variables, MOI.ListOfVariableIndices())
evaluator = MOI.Nonlinear.Evaluator(model.nlp_model, backend, vars)
nlp_data = MOI.NLPBlockData(evaluator)
# load parameters
if isnothing(model.nlp_model)
error("An objective should be provided to Optim with `@objective`.")
end
objective_scale = model.sense == MOI.MAX_SENSE ? -one(T) : one(T)
zero_μ = zeros(T, length(nlp_data.constraint_bounds))
function f(x)
return objective_scale * MOI.eval_objective(evaluator, x)
end
function g!(G, x)
fill!(G, zero(T))
MOI.eval_objective_gradient(evaluator, G, x)
if model.sense == MOI.MAX_SENSE
rmul!(G, objective_scale)
end
return G
end
function h!(H, x)
fill!(H, zero(T))
MOI.eval_hessian_lagrangian(evaluator, H_nzval, x, objective_scale, zero_μ)
sym_sparse_to_dense!(H, hessian_structure, H_nzval)
return H
end
method = model.method
nl_constrained = !isempty(nlp_data.constraint_bounds)
features = MOI.features_available(evaluator)
has_bounds = any(vi -> isfinite(model.variables.lower[vi.value]) || isfinite(model.variables.upper[vi.value]), vars)
if method === nothing
if nl_constrained
method = IPNewton()
elseif :Grad in features
# FIXME `fallback_method(f, g!, h!)` returns `Newton` but if there
# are variable bounds, `Newton` is not supported. On the other hand,
# `fallback_method(f, g!)` returns `LBFGS` which is supported if `has_bounds`.
if :Hess in features && !has_bounds
method = fallback_method(f, g!, h!)
else
method = fallback_method(f, g!)
end
else
method = fallback_method(f)
end
end
used_features = requested_features(method, nl_constrained)
MOI.initialize(evaluator, used_features)
if :Hess in used_features
hessian_structure = MOI.hessian_lagrangian_structure(evaluator)
H_nzval = zeros(T, length(hessian_structure))
end
initial_x = starting_value.(model, eachindex(model.starting_values))
options = copy(model.options)
if !nl_constrained && has_bounds && !(method isa IPNewton)
options = Options(; options...)
model.results = optimize(f, g!, model.variables.lower, model.variables.upper, initial_x, Fminbox(method), options; inplace = true)
else
d = promote_objtype(method, initial_x, :finite, true, f, g!, h!)
add_default_opts!(options, method)
options = Options(; options...)
if nl_constrained || has_bounds
if nl_constrained
lc = [b.lower for b in nlp_data.constraint_bounds]
uc = [b.upper for b in nlp_data.constraint_bounds]
c!(c, x) = MOI.eval_constraint(evaluator, c, x)
if !(:Jac in features)
error("Nonlinear constraints should be differentiable to be used with Optim.")
end
if !(:Hess in features)
error("Nonlinear constraints should be twice differentiable to be used with Optim.")
end
jacobian_structure = MOI.jacobian_structure(evaluator)
J_nzval = zeros(T, length(jacobian_structure))
function jacobian!(J, x)
fill!(J, zero(T))
MOI.eval_constraint_jacobian(evaluator, J_nzval, x)
sparse_to_dense!(J, jacobian_structure, J_nzval)
return J
end
function con_hessian!(H, x, λ)
fill!(H, zero(T))
MOI.eval_hessian_lagrangian(evaluator, H_nzval, x, zero(T), λ)
sym_sparse_to_dense!(H, hessian_structure, H_nzval)
return H
end
c = TwiceDifferentiableConstraints(
c!, jacobian!, con_hessian!,
model.variables.lower, model.variables.upper, lc, uc,
)
else
@assert has_bounds
c = TwiceDifferentiableConstraints(
model.variables.lower, model.variables.upper)
end
model.results = optimize(d, c, initial_x, method, options)
else
model.results = optimize(d, initial_x, method, options)
end
end
return
end
function MOI.get(model::Optimizer, ::MOI.TerminationStatus)
if model.results === nothing
return MOI.OPTIMIZE_NOT_CALLED
elseif converged(model.results)
return MOI.LOCALLY_SOLVED
else
return MOI.OTHER_ERROR
end
end
function MOI.get(model::Optimizer, ::MOI.RawStatusString)
return summary(model.results)
end
# Ipopt always has an iterate available.
function MOI.get(model::Optimizer, ::MOI.ResultCount)
return model.results === nothing ? 0 : 1
end
function MOI.get(model::Optimizer, attr::MOI.PrimalStatus)
if !(1 <= attr.result_index <= MOI.get(model, MOI.ResultCount()))
return MOI.NO_SOLUTION
end
if converged(model.results)
return MOI.FEASIBLE_POINT
else
return MOI.UNKNOWN_RESULT_STATUS
end
end
MOI.get(::Optimizer, ::MOI.DualStatus) = MOI.NO_SOLUTION
function MOI.get(model::Optimizer, attr::MOI.ObjectiveValue)
MOI.check_result_index_bounds(model, attr)
val = minimum(model.results)
if model.sense == MOI.MAX_SENSE
val = -val
end
return val
end
function MOI.get(model::Optimizer, attr::MOI.VariablePrimal, vi::MOI.VariableIndex)
MOI.check_result_index_bounds(model, attr)
MOI.throw_if_not_valid(model, vi)
return minimizer(model.results)[vi.value]
end
function MOI.get(
model::Optimizer{T},
attr::MOI.ConstraintPrimal,
ci::MOI.ConstraintIndex{MOI.VariableIndex,<:BOUNDS{T}},
) where {T}
MOI.check_result_index_bounds(model, attr)
MOI.throw_if_not_valid(model, ci)
return minimizer(model.results)[ci.value]
end