-
Notifications
You must be signed in to change notification settings - Fork 58
/
MOI_wrapper.jl
481 lines (454 loc) · 17.5 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
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
# Copyright (c) 2013: Iain Dunning, Miles Lubin, 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.
module TestMOIWrapper
using Ipopt
using Test
const MOI = Ipopt.MOI
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
function test_MOI_Test()
model = MOI.Utilities.CachingOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
MOI.Bridges.full_bridge_optimizer(Ipopt.Optimizer(), Float64),
)
MOI.set(model, MOI.Silent(), true)
# Without fixed_variable_treatment set, duals are not computed for variables
# that have lower_bound == upper_bound.
MOI.set(
model,
MOI.RawOptimizerAttribute("fixed_variable_treatment"),
"make_constraint",
)
MOI.Test.runtests(
model,
MOI.Test.Config(
atol = 1e-4,
rtol = 1e-4,
infeasible_status = MOI.LOCALLY_INFEASIBLE,
optimal_status = MOI.LOCALLY_SOLVED,
exclude = Any[
MOI.ConstraintBasisStatus,
MOI.DualObjectiveValue,
MOI.ObjectiveBound,
],
);
exclude = String[
# Tests purposefully excluded:
# - NORM_LIMIT when run on macOS-M1. See #315
"test_linear_transform",
# - Upstream: ZeroBridge does not support ConstraintDual
"test_conic_linear_VectorOfVariables_2",
# - Excluded because this test is optional
"test_model_ScalarFunctionConstantNotZero",
# - Excluded because Ipopt returns INVALID_MODEL instead of
# LOCALLY_SOLVED
"test_linear_VectorAffineFunction_empty_row",
# - CachingOptimizer does not throw if optimizer not attached
"test_model_copy_to_UnsupportedAttribute",
"test_model_copy_to_UnsupportedConstraint",
],
)
return
end
function test_Name()
model = Ipopt.Optimizer()
@test MOI.supports(model, MOI.Name())
@test MOI.get(model, MOI.Name()) == ""
MOI.set(model, MOI.Name(), "Model")
@test MOI.get(model, MOI.Name()) == "Model"
return
end
function test_ConstraintDualStart()
model = Ipopt.Optimizer()
x = MOI.add_variables(model, 2)
l = MOI.add_constraint(model, x[1], MOI.GreaterThan(1.0))
u = MOI.add_constraint(model, x[1], MOI.LessThan(1.0))
e = MOI.add_constraint(model, x[2], MOI.EqualTo(1.0))
c = MOI.add_constraint(
model,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.0),
MOI.LessThan(1.5),
)
@test MOI.get(model, MOI.ConstraintDualStart(), l) === nothing
@test MOI.get(model, MOI.ConstraintDualStart(), u) === nothing
@test MOI.get(model, MOI.ConstraintDualStart(), e) === nothing
@test MOI.get(model, MOI.ConstraintDualStart(), c) === nothing
@test MOI.get(model, MOI.NLPBlockDualStart()) === nothing
MOI.set(model, MOI.ConstraintDualStart(), l, 1.0)
MOI.set(model, MOI.ConstraintDualStart(), u, -1.0)
MOI.set(model, MOI.ConstraintDualStart(), e, -1.5)
MOI.set(model, MOI.ConstraintDualStart(), c, 2.0)
MOI.set(model, MOI.NLPBlockDualStart(), [1.0, 2.0])
@test MOI.get(model, MOI.ConstraintDualStart(), l) == 1.0
@test MOI.get(model, MOI.ConstraintDualStart(), u) == -1.0
@test MOI.get(model, MOI.ConstraintDualStart(), e) == -1.5
@test MOI.get(model, MOI.ConstraintDualStart(), c) == 2.0
@test MOI.get(model, MOI.NLPBlockDualStart()) == [1.0, 2.0]
MOI.set(model, MOI.ConstraintDualStart(), l, nothing)
MOI.set(model, MOI.ConstraintDualStart(), u, nothing)
MOI.set(model, MOI.ConstraintDualStart(), e, nothing)
MOI.set(model, MOI.ConstraintDualStart(), c, nothing)
MOI.set(model, MOI.NLPBlockDualStart(), nothing)
@test MOI.get(model, MOI.ConstraintDualStart(), l) === nothing
@test MOI.get(model, MOI.ConstraintDualStart(), u) === nothing
@test MOI.get(model, MOI.ConstraintDualStart(), e) === nothing
@test MOI.get(model, MOI.ConstraintDualStart(), c) === nothing
@test MOI.get(model, MOI.NLPBlockDualStart()) === nothing
return
end
function test_solve_time()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
MOI.add_variable(model)
@test isnan(MOI.get(model, MOI.SolveTimeSec()))
MOI.optimize!(model)
@test MOI.get(model, MOI.SolveTimeSec()) >= 0.0
return
end
# Model structure for test_check_derivatives_for_naninf()
struct Issue136 <: MOI.AbstractNLPEvaluator end
MOI.initialize(::Issue136, ::Vector{Symbol}) = nothing
MOI.features_available(::Issue136) = [:Grad, :Jac]
MOI.eval_objective(::Issue136, x) = x[1]
MOI.eval_constraint(::Issue136, g, x) = (g[1] = x[1]^(1 / 3))
MOI.eval_objective_gradient(::Issue136, grad_f, x) = (grad_f[1] = 1.0)
MOI.jacobian_structure(::Issue136) = Tuple{Int64,Int64}[(1, 1)]
function MOI.eval_constraint_jacobian(::Issue136, J, x)
J[1] = (1 / 3) * x[1]^(1 / 3 - 1)
return
end
function test_check_derivatives_for_naninf()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.set(
model,
MOI.NLPBlock(),
MOI.NLPBlockData(MOI.NLPBoundsPair.([-Inf], [0.0]), Issue136(), false),
)
# Failure to set check_derivatives_for_naninf="yes" may cause Ipopt to
# segfault or return a NUMERICAL_ERROR status. Check that it is set to "yes"
# by obtaining an INVALID_MODEL status.
# MOI.set(model, MOI.RawOptimizerAttribute("check_derivatives_for_naninf"), "no")
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INVALID_MODEL
return
end
function test_callback()
model = Ipopt.Optimizer()
MOI.set(model, MOI.RawOptimizerAttribute("print_level"), 0)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(1.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.5)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
x_vals = Float64[]
function my_callback(
alg_mod::Cint,
iter_count::Cint,
obj_value::Float64,
inf_pr::Float64,
inf_du::Float64,
mu::Float64,
d_norm::Float64,
regularization_size::Float64,
alpha_du::Float64,
alpha_pr::Float64,
ls_trials::Cint,
)
push!(x_vals, MOI.get(model, MOI.CallbackVariablePrimal(model), x))
@test isapprox(obj_value, 1.0 * x_vals[end] + 0.5, atol = 1e-1)
return iter_count < 1
end
MOI.set(model, Ipopt.CallbackFunction(), my_callback)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INTERRUPTED
@test length(x_vals) == 2
@test x_vals[1] !== x_vals[2]
return
end
function test_callback_2()
model = Ipopt.Optimizer()
MOI.set(model, MOI.RawOptimizerAttribute("print_level"), 0)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(1.0))
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.5)
MOI.add_constraint(model, f, MOI.LessThan(2.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
x_vals = Float64[]
function my_callback(
alg_mod::Cint,
iter_count::Cint,
obj_value::Float64,
inf_pr::Float64,
inf_du::Float64,
mu::Float64,
d_norm::Float64,
regularization_size::Float64,
alpha_du::Float64,
alpha_pr::Float64,
ls_trials::Cint,
)
push!(x_vals, MOI.get(model, MOI.CallbackVariablePrimal(model), x))
@test isapprox(obj_value, 1.0 * x_vals[end] + 0.5, atol = 1e-1)
return iter_count < 1
end
MOI.set(model, Ipopt.CallbackFunction(), my_callback)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INTERRUPTED
@test length(x_vals) == 2
@test x_vals[1] !== x_vals[2]
return
end
function test_empty_optimize()
model = Ipopt.Optimizer()
@test MOI.get(model, MOI.RawStatusString()) == "Optimize not called"
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INVALID_MODEL
@test MOI.get(model, MOI.DualStatus()) == MOI.NO_SOLUTION
@test MOI.get(model, MOI.PrimalStatus()) == MOI.NO_SOLUTION
@test MOI.get(model, MOI.RawStatusString()) == "The model has no variable"
return
end
"""
test_get_model()
Test various getters for ConstraintFunction etc. We need this test because the
normal MOI ones require the solver to support VariableName and ConstraintName.
"""
function test_get_model()
model = MOI.Utilities.Model{Float64}()
MOI.Utilities.loadfromstring!(
model,
"""
variables: x, y, z
minobjective: 1.0 * x * x + 2.0 * y + 3.0
x >= 1.0
y <= 2.0
z == 3.0
1.0 * x >= 1.0
2.0 * y <= 4.0
3.0 * z == 9.0
1.0 * x * x + x >= 1.0
2.0 * y * y + y <= 8.0
3.0 * z * z + z == 27.0
""",
)
ipopt = Ipopt.Optimizer()
index_map = MOI.copy_to(ipopt, model)
attr = MOI.ListOfConstraintTypesPresent()
@test sort(MOI.get(model, attr); by = string) ==
sort(MOI.get(ipopt, attr); by = string)
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
cis = MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
@test length(cis) == 1
f_model = MOI.get(model, MOI.ConstraintFunction(), cis[1])
s_model = MOI.get(model, MOI.ConstraintSet(), cis[1])
cis = MOI.get(ipopt, MOI.ListOfConstraintIndices{F,S}())
@test length(cis) == MOI.get(ipopt, MOI.NumberOfConstraints{F,S}()) == 1
f_ipopt = MOI.get(ipopt, MOI.ConstraintFunction(), cis[1])
s_ipopt = MOI.get(ipopt, MOI.ConstraintSet(), cis[1])
@test s_model == s_ipopt
if F == MOI.VariableIndex
@test index_map[f_model] == f_ipopt
else
@test ≈(
MOI.Utilities.substitute_variables(x -> index_map[x], f_model),
f_ipopt,
)
end
end
F_model = MOI.get(model, MOI.ObjectiveFunctionType())
F_ipopt = MOI.get(ipopt, MOI.ObjectiveFunctionType())
@test F_model == F_ipopt
obj_model = MOI.get(model, MOI.ObjectiveFunction{F_model}())
obj_ipopt = MOI.get(ipopt, MOI.ObjectiveFunction{F_ipopt}())
@test ≈(
MOI.Utilities.substitute_variables(x -> index_map[x], obj_model),
obj_ipopt,
)
return
end
function test_supports_ConstraintDualStart_VariableIndex()
ipopt = Ipopt.Optimizer()
bridged = MOI.Bridges.full_bridge_optimizer(Ipopt.Optimizer(), Float64)
sets =
(MOI.LessThan{Float64}, MOI.GreaterThan{Float64}, MOI.EqualTo{Float64})
for model in (ipopt, bridged), S in sets
@test MOI.supports(
model,
MOI.ConstraintDualStart(),
MOI.ConstraintIndex{MOI.VariableIndex,S},
)
end
return
end
function test_parameter_number_of_variables()
model = Ipopt.Optimizer()
x = MOI.add_variable(model)
p, ci = MOI.add_constrained_variable(model, MOI.Parameter(2.0))
@test MOI.get(model, MOI.NumberOfVariables()) == 2
return
end
function test_parameter_list_of_variable_indices()
model = Ipopt.Optimizer()
x = MOI.add_variable(model)
p, ci = MOI.add_constrained_variable(model, MOI.Parameter(2.0))
@test MOI.get(model, MOI.ListOfVariableIndices()) == [x, p]
# Now reversed
model = Ipopt.Optimizer()
p, ci = MOI.add_constrained_variable(model, MOI.Parameter(2.0))
x = MOI.add_variable(model)
@test MOI.get(model, MOI.ListOfVariableIndices()) == [p, x]
return
end
function test_scalar_nonlinear_function_is_valid()
model = Ipopt.Optimizer()
x = MOI.add_variable(model)
F, S = MOI.ScalarNonlinearFunction, MOI.EqualTo{Float64}
@test MOI.is_valid(model, MOI.ConstraintIndex{F,S}(1)) == false
f = MOI.ScalarNonlinearFunction(:sin, Any[x])
c = MOI.add_constraint(model, f, MOI.EqualTo(0.0))
@test c isa MOI.ConstraintIndex{F,S}
@test MOI.is_valid(model, c) == true
return
end
function test_parameter()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
p, ci = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
x = MOI.add_variable(model)
fi = MOI.ScalarNonlinearFunction(:-, Any[x, p])
f = MOI.ScalarNonlinearFunction(:^, Any[fi, 2])
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
@test MOI.get(model, MOI.NumberOfVariables()) == 2
@test MOI.get(model, MOI.ListOfVariableIndices()) == [p, x]
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ 1
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ 1
MOI.set(model, MOI.ConstraintSet(), ci, MOI.Parameter(-2.5))
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ -2.5
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ -2.5
return
end
function test_parameter_replace_parameters()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
p, ci = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
x = MOI.add_variable(model)
t = MOI.add_variable(model)
lhs = MOI.ScalarNonlinearFunction(
:+,
Any[
x,
p,
1.0*x,
1.0*p,
1.0*x*x,
1.0*p*x,
MOI.ScalarNonlinearFunction(:^, Any[1.0*x-p, 2]),
],
)
f = MOI.ScalarNonlinearFunction(:-, Any[lhs, t])
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(t)}(), t)
MOI.add_constraint(model, f, MOI.LessThan(0.0))
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ 1.0
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ -0.25
return
end
function test_parameter_reverse()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
p, ci = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
fi = MOI.ScalarNonlinearFunction(:-, Any[x, p])
f = MOI.ScalarNonlinearFunction(:^, Any[fi, 2])
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
@test MOI.get(model, MOI.NumberOfVariables()) == 2
@test MOI.get(model, MOI.ListOfVariableIndices()) == [x, p]
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ 1
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ 1
MOI.set(model, MOI.ConstraintSet(), ci, MOI.Parameter(-2.5))
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ -2.5
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ -2.5
return
end
function test_parameter_scalar_affine_objective()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
p, ci = MOI.add_constrained_variable(model, MOI.Parameter(2.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
# f = (x - p)^2 + x + p + 1.0
f = (1.0 * x - 1.0 * p) * (1.0 * x - 1.0 * p) + x + p + 1.0
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ 1.5
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ 2.0
@test MOI.get(model, MOI.ObjectiveValue()) ≈ (1.5 - 2.0)^2 + 4.5
MOI.set(model, MOI.ConstraintSet(), ci, MOI.Parameter(2.2))
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ 1.7
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ 2.2
@test MOI.get(model, MOI.ObjectiveValue()) ≈ (1.7 - 2.2)^2 + 4.9
MOI.add_constraint(model, x, MOI.LessThan(1.5))
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ 1.5
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ 2.2
@test MOI.get(model, MOI.ObjectiveValue()) ≈ (1.5 - 2.2)^2 + 4.7
return
end
function test_parameter_scalar_affine_objective()
model = Ipopt.Optimizer()
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
p, ci = MOI.add_constrained_variable(model, MOI.Parameter(2.0))
t = MOI.add_variable(model)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(t)}(), t)
# f = (x - p)^2 + x + p + 1.0
f = (1.0 * x - 1.0 * p) * (1.0 * x - 1.0 * p) + x + p + 1.0
MOI.add_constraint(model, f - t, MOI.LessThan(0.0))
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ 1.5
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ 2.0
@test MOI.get(model, MOI.ObjectiveValue()) ≈ (1.5 - 2.0)^2 + 4.5
MOI.set(model, MOI.ConstraintSet(), ci, MOI.Parameter(2.2))
MOI.optimize!(model)
@test MOI.get(model, MOI.VariablePrimal(), x) ≈ 1.7
@test MOI.get(model, MOI.VariablePrimal(), p) ≈ 2.2
@test MOI.get(model, MOI.ObjectiveValue()) ≈ (1.7 - 2.2)^2 + 4.9
return
end
function test_ListOfSupportedNonlinearOperators()
model = Ipopt.Optimizer()
ops = MOI.get(model, MOI.ListOfSupportedNonlinearOperators())
@test ops isa Vector{Symbol}
@test :|| in ops
@test :ifelse in ops
@test :sin in ops
@test !(:f in ops)
f(x) = x^2
MOI.set(model, MOI.UserDefinedFunction(:f, 1), (f,))
@test :f in MOI.get(model, MOI.ListOfSupportedNonlinearOperators())
return
end
end # module TestMOIWrapper
TestMOIWrapper.runtests()