-
Notifications
You must be signed in to change notification settings - Fork 86
/
sol.jl
322 lines (301 loc) · 11.4 KB
/
sol.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
# 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.
module TestNonlinearSolFiles
using Test
import MathOptInterface as MOI
const NL = MOI.FileFormats.NL
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 _hs071()
model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
v = MOI.add_variables(model, 4)
l = [1.1, 1.2, 1.3, 1.4]
u = [5.1, 5.2, 5.3, 5.4]
start = [2.1, 2.2, 2.3, 2.4]
MOI.add_constraint.(model, v, MOI.GreaterThan.(l))
MOI.add_constraint.(model, v, MOI.LessThan.(u))
MOI.set.(model, MOI.VariablePrimalStart(), v, start)
lb, ub = [25.0, 40.0], [Inf, 40.0]
evaluator = MOI.Test.HS071(true)
block_data = MOI.NLPBlockData(MOI.NLPBoundsPair.(lb, ub), evaluator, true)
MOI.set(model, MOI.NLPBlock(), block_data)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
return model, v
end
"""
test_sol_hs071()
This function tests the model with a solution as obtained from Ipopt.
"""
function test_sol_hs071()
model, v = _hs071()
nl_model = NL.Model()
index_map = MOI.copy_to(nl_model, model)
sol = NL.SolFileResults(joinpath(@__DIR__, "data", "hs071.sol"), nl_model)
@test MOI.get(sol, MOI.ResultCount()) == 1
@test MOI.get(sol, MOI.RawStatusString()) ==
"Ipopt 3.14.4: Optimal Solution Found"
@test MOI.get(sol, MOI.TerminationStatus()) == MOI.LOCALLY_SOLVED
@test MOI.get(sol, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT
@test MOI.get(sol, MOI.DualStatus()) == MOI.FEASIBLE_POINT
@test ≈(MOI.get(sol, MOI.ObjectiveValue()), 34.4432046; atol = 1e-6)
x_p = [1.1, 1.573552, 2.6746838, 5.4]
for (i, vi) in enumerate(v)
v_dest = index_map[vi]
@test ≈(MOI.get(sol, MOI.VariablePrimal(), v_dest), x_p[i]; atol = 1e-6)
end
F = MOI.VariableIndex
for (i, vi) in enumerate(v)
ci = MOI.ConstraintIndex{F,MOI.GreaterThan{Float64}}(vi.value)
c_dest = index_map[ci]
if i == 1
dual = MOI.get(sol, MOI.ConstraintDual(), c_dest)
@test ≈(dual, 28.5907038; atol = 1e-6)
else
@test ≈(
MOI.get(sol, MOI.ConstraintDual(), c_dest),
0.0;
atol = 1e-6,
)
end
@test ≈(MOI.get(sol, MOI.ConstraintPrimal(), ci), x_p[i]; atol = 1e-6)
ci = MOI.ConstraintIndex{F,MOI.LessThan{Float64}}(vi.value)
c_dest = index_map[ci]
if i == 4
dual = MOI.get(sol, MOI.ConstraintDual(), c_dest)
@test ≈(dual, -5.58255055; atol = 1e-6)
else
@test ≈(
MOI.get(sol, MOI.ConstraintDual(), c_dest),
0.0;
atol = 1e-6,
)
end
@test ≈(MOI.get(sol, MOI.ConstraintPrimal(), ci), x_p[i]; atol = 1e-6)
end
nlp_block_dual = MOI.get(sol, MOI.NLPBlockDual())
@test ≈(nlp_block_dual, [0.1787618, 0.9850008]; atol = 1e-6)
return
end
function test_sol_hs071_variable_dual()
model, v = _hs071()
for (sign, sense) in [(1, MOI.MIN_SENSE), (-1, MOI.MAX_SENSE)]
MOI.set(model, MOI.ObjectiveSense(), sense)
nl_model = NL.Model()
index_map = MOI.copy_to(nl_model, model)
sol =
NL.SolFileResults(joinpath(@__DIR__, "data", "hs071.sol"), nl_model)
x1 = index_map[v[1]]
F = MOI.VariableIndex
dual = sign * 28.590703805487557
ci = MOI.ConstraintIndex{F,MOI.GreaterThan{Float64}}(x1.value)
@test ≈(MOI.get(sol, MOI.ConstraintDual(), ci), dual, atol = 1e-8)
ci = MOI.ConstraintIndex{F,MOI.LessThan{Float64}}(x1.value)
@test ≈(MOI.get(sol, MOI.ConstraintDual(), ci), 0.0, atol = 1e-8)
ci = MOI.ConstraintIndex{F,MOI.EqualTo{Float64}}(x1.value)
@test ≈(MOI.get(sol, MOI.ConstraintDual(), ci), dual, atol = 1e-8)
ci = MOI.ConstraintIndex{F,MOI.Interval{Float64}}(x1.value)
@test ≈(MOI.get(sol, MOI.ConstraintDual(), ci), dual, atol = 1e-8)
end
return
end
"""
test_sol_hs071_max_sense()
This function tests that the duals are negated if we switch the objective sense.
"""
function test_sol_hs071_max_sense()
model, v = _hs071()
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
nl_model = NL.Model()
index_map = MOI.copy_to(nl_model, model)
sol = NL.SolFileResults(joinpath(@__DIR__, "data", "hs071.sol"), nl_model)
F = MOI.VariableIndex
for (i, vi) in enumerate(v)
ci = MOI.ConstraintIndex{F,MOI.GreaterThan{Float64}}(vi.value)
c_dest = index_map[ci]
if i == 1
dual = MOI.get(sol, MOI.ConstraintDual(), c_dest)
@test ≈(dual, -28.5907038; atol = 1e-6)
else
@test ≈(
MOI.get(sol, MOI.ConstraintDual(), c_dest),
0.0;
atol = 1e-6,
)
end
ci = MOI.ConstraintIndex{F,MOI.LessThan{Float64}}(vi.value)
c_dest = index_map[ci]
if i == 4
dual = MOI.get(sol, MOI.ConstraintDual(), c_dest)
@test ≈(dual, 5.58255055; atol = 1e-6)
else
@test ≈(
MOI.get(sol, MOI.ConstraintDual(), c_dest),
0.0;
atol = 1e-6,
)
end
end
nlp_block_dual = MOI.get(sol, MOI.NLPBlockDual())
@test ≈(nlp_block_dual, -[0.1787618, 0.9850008]; atol = 1e-6)
return
end
function test_sol_quadratic_constraint()
model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
x = MOI.add_variable(model)
g = MOI.ScalarQuadraticFunction(
[MOI.ScalarQuadraticTerm(2.0, x, x)],
[MOI.ScalarAffineTerm(1.0, x)],
3.0,
)
c = MOI.add_constraint(model, g, MOI.Interval(1.0, 10.0))
nl_model = NL.Model()
index_map = MOI.copy_to(nl_model, model)
filename = joinpath(@__DIR__, "data", "quadratic.sol")
sol = NL.SolFileResults(filename, nl_model)
xv = MOI.get(sol, MOI.VariablePrimal(), index_map[x])
@test ≈(MOI.get(sol, MOI.ConstraintPrimal(), index_map[c]), xv^2 + xv + 3)
@test ≈(MOI.get(sol, MOI.ConstraintDual(), index_map[c]), 0.0)
return
end
function test_sol_linear_constraint()
model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
x = MOI.add_variable(model)
g = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.2, x)], 3.0)
c = MOI.add_constraint(model, g, MOI.Interval(1.0, 10.0))
nl_model = NL.Model()
index_map = MOI.copy_to(nl_model, model)
filename = joinpath(@__DIR__, "data", "quadratic.sol")
sol = NL.SolFileResults(filename, nl_model)
xv = MOI.get(sol, MOI.VariablePrimal(), index_map[x])
@test ≈(MOI.get(sol, MOI.ConstraintPrimal(), index_map[c]), 1.2xv + 3)
@test ≈(MOI.get(sol, MOI.ConstraintDual(), index_map[c]), 0.0)
return
end
function test_sol_attr_out_of_bounds()
model, v = _hs071()
nl_model = NL.Model()
index_map = MOI.copy_to(nl_model, model)
sol = NL.SolFileResults(joinpath(@__DIR__, "data", "hs071.sol"), nl_model)
@test MOI.get(sol, MOI.PrimalStatus(2)) == MOI.NO_SOLUTION
@test MOI.get(sol, MOI.DualStatus(2)) == MOI.NO_SOLUTION
@test_throws MOI.ResultIndexBoundsError MOI.get(sol, MOI.ObjectiveValue(2))
x = index_map[v[1]]
@test_throws MOI.ResultIndexBoundsError MOI.get(
sol,
MOI.VariablePrimal(2),
x,
)
return
end
function test_sol_infeasible()
model, _ = _hs071()
nl_model = NL.Model()
_ = MOI.copy_to(nl_model, model)
sol = NL.SolFileResults(
joinpath(@__DIR__, "data", "hs071_infeasible.sol"),
nl_model,
)
@test MOI.get(sol, MOI.TerminationStatus()) == MOI.LOCALLY_INFEASIBLE
@test MOI.get(sol, MOI.PrimalStatus()) == MOI.UNKNOWN_RESULT_STATUS
@test MOI.get(sol, MOI.DualStatus()) == MOI.NO_SOLUTION
return
end
function test_sol_vbtol()
model, _ = _hs071()
nl_model = NL.Model()
_ = MOI.copy_to(nl_model, model)
sol = NL.SolFileResults(
joinpath(@__DIR__, "data", "hs071_vbtol.sol"),
nl_model,
)
@test MOI.get(sol, MOI.TerminationStatus()) == MOI.LOCALLY_SOLVED
@test MOI.get(sol, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT
@test MOI.get(sol, MOI.DualStatus()) == MOI.FEASIBLE_POINT
return
end
function test_sol_badsuffix()
model, _ = _hs071()
nl_model = NL.Model()
_ = MOI.copy_to(nl_model, model)
sol = NL.SolFileResults(
joinpath(@__DIR__, "data", "hs071_badsuffix.sol"),
nl_model,
)
@test MOI.get(sol, MOI.TerminationStatus()) == MOI.LOCALLY_SOLVED
@test MOI.get(sol, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT
@test MOI.get(sol, MOI.DualStatus()) == MOI.NO_SOLUTION
return
end
function test_sol_no_duals()
model, _ = _hs071()
nl_model = NL.Model()
_ = MOI.copy_to(nl_model, model)
sol = NL.SolFileResults(
joinpath(@__DIR__, "data", "hs071_noduals.sol"),
nl_model,
)
@test MOI.get(sol, MOI.TerminationStatus()) == MOI.LOCALLY_SOLVED
@test MOI.get(sol, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT
@test MOI.get(sol, MOI.DualStatus()) == MOI.NO_SOLUTION
return
end
function test_statuses()
statuses = Dict(
(0, "Optimal Solution Found") =>
(MOI.LOCALLY_SOLVED, MOI.FEASIBLE_POINT),
(100, "Optimal Solution Found") =>
(MOI.LOCALLY_SOLVED, MOI.FEASIBLE_POINT),
(200, "Converged to a locally infeasible point.") =>
(MOI.LOCALLY_INFEASIBLE, MOI.UNKNOWN_RESULT_STATUS),
(300, "Model is Unbounded") =>
(MOI.DUAL_INFEASIBLE, MOI.UNKNOWN_RESULT_STATUS),
(400, "norm limit exceeded") =>
(MOI.OTHER_LIMIT, MOI.UNKNOWN_RESULT_STATUS),
(403, "Solver 10.0.1: time limit, feasible solution\n") =>
(MOI.TIME_LIMIT, MOI.FEASIBLE_POINT),
(500, "Solver error.") =>
(MOI.OTHER_ERROR, MOI.UNKNOWN_RESULT_STATUS),
(-1, "Optimal Solution Found") =>
(MOI.LOCALLY_SOLVED, MOI.FEASIBLE_POINT),
(-1, "Converged to a locally infeasible point.") =>
(MOI.LOCALLY_INFEASIBLE, MOI.UNKNOWN_RESULT_STATUS),
(-1, "Model is Unbounded") =>
(MOI.DUAL_INFEASIBLE, MOI.UNKNOWN_RESULT_STATUS),
(-1, "norm limit exceeded") =>
(MOI.OTHER_LIMIT, MOI.UNKNOWN_RESULT_STATUS),
(-1, "Solver error.") =>
(MOI.OTHER_ERROR, MOI.UNKNOWN_RESULT_STATUS),
(-1, "") => (MOI.OTHER_ERROR, MOI.UNKNOWN_RESULT_STATUS),
)
for ((a, b), (t, p)) in statuses
@test NL._interpret_status(a, b) == (t, p)
end
return
end
function test_sol_empty()
sol = NL.SolFileResults("optimize not called", MOI.OPTIMIZE_NOT_CALLED)
@test MOI.get(sol, MOI.TerminationStatus()) == MOI.OPTIMIZE_NOT_CALLED
@test MOI.get(sol, MOI.PrimalStatus()) == MOI.NO_SOLUTION
@test MOI.get(sol, MOI.DualStatus()) == MOI.NO_SOLUTION
return
end
function test_result_count()
results = NL.SolFileResults("raw", MOI.OTHER_LIMIT)
@test MOI.get(results, MOI.ResultCount()) == 0
results.variable_primal[MOI.VariableIndex(1)] = 0.0
@test MOI.get(results, MOI.ResultCount()) == 1
return
end
end
TestNonlinearSolFiles.runtests()