-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathSDPA.jl
More file actions
424 lines (389 loc) · 12 KB
/
SDPA.jl
File metadata and controls
424 lines (389 loc) · 12 KB
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
# 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 TestSDPA
using Test
import MathOptInterface as MOI
import MathOptInterface.Utilities as MOIU
import MathOptInterface.FileFormats: SDPA
const SDPA_TEST_FILE = "test.sdpa"
const SDPA_MODELS_DIR = joinpath(@__DIR__, "models")
function runtests()
for name in names(@__MODULE__, all = true)
if startswith("$(name)", "test_")
@testset "$name" begin
getfield(@__MODULE__, name)()
end
end
end
sleep(1.0) # Allow time for unlink to happen.
rm(SDPA_TEST_FILE, force = true)
return
end
function _set_var_and_con_names(model::MOI.ModelLike)
variable_names = String[]
for j in MOI.get(model, MOI.ListOfVariableIndices())
var_name_j = "v" * string(j.value)
push!(variable_names, var_name_j)
MOI.set(model, MOI.VariableName(), j, var_name_j)
end
idx = 0
constraint_names = String[]
single_variable_constraints = Tuple[]
for i in MOI.get(
model,
MOI.ListOfConstraintIndices{MOI.VariableIndex,MOI.Integer}(),
)
idx += 1
x = MOI.get(model, MOI.VariableName(), MOI.VariableIndex(i.value))
push!(single_variable_constraints, (x, MOI.Integer()))
end
for i in Iterators.flatten((
MOI.get(
model,
MOI.ListOfConstraintIndices{
MOI.VectorAffineFunction{Float64},
MOI.Nonnegatives,
}(),
),
MOI.get(
model,
MOI.ListOfConstraintIndices{
MOI.VectorAffineFunction{Float64},
MOI.PositiveSemidefiniteConeTriangle,
}(),
),
))
idx += 1
con_name_i = "c" * string(idx)
push!(constraint_names, con_name_i)
MOI.set(model, MOI.ConstraintName(), i, con_name_i)
end
return variable_names, constraint_names, single_variable_constraints
end
function _test_write_then_read(model_string::String)
model1 = SDPA.Model()
MOIU.loadfromstring!(model1, model_string)
args = _set_var_and_con_names(model1)
MOI.write_to_file(model1, SDPA_TEST_FILE)
model2 = SDPA.Model()
MOI.read_from_file(model2, SDPA_TEST_FILE)
_set_var_and_con_names(model2)
if MOI.get(model1, MOI.ObjectiveSense()) == MOI.MAX_SENSE
MOI.set(model2, MOI.ObjectiveSense(), MOI.MAX_SENSE)
attr = MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}()
obj = MOI.get(model2, attr)
MOI.set(model2, attr, MOIU.operate(-, Float64, obj))
end
return MOI.Test.util_test_models_equal(model1, model2, args...)
end
function _test_read(filename::String, model_string::String)
model1 = MOI.FileFormats.Model(filename = filename)
MOIU.loadfromstring!(model1, model_string)
args = _set_var_and_con_names(model1)
model2 = SDPA.Model()
MOI.read_from_file(model2, filename)
_set_var_and_con_names(model2)
return MOI.Test.util_test_models_equal(model1, model2, args...)
end
function test_show()
@test sprint(summary, SDPA.Model()) == "MOI.FileFormats.SDPA.Model"
return
end
function test_support()
for set in [
MOI.EqualTo(1.0),
MOI.LessThan(1.0),
MOI.GreaterThan(1.0),
MOI.Interval(1.0, 2.0),
MOI.Semiinteger(1.0, 2.0),
MOI.Semicontinuous(1.0, 2.0),
MOI.ZeroOne(),
]
model_string = """
variables: x
minobjective: 1x
x in $set
"""
model = SDPA.Model()
@test !MOI.supports_constraint(model, MOI.VariableIndex, typeof(set))
err = MOI.UnsupportedConstraint{MOI.VariableIndex,typeof(set)}
@test_throws err MOIU.loadfromstring!(model, model_string)
end
end
function test_delete()
for T in [Int, Float64]
model = SDPA.Model(; number_type = T)
x = MOI.add_variable(model)
MOI.delete(model, x)
y = MOI.add_variable(model)
MOI.add_constraint(
model,
MOIU.vectorize([one(T) * y]),
MOI.Nonnegatives(1),
)
err = ErrorException(
"Non-contiguous variable indices not supported. This might be due to deleted variables.",
)
@test_throws err MOI.write_to_file(model, SDPA_TEST_FILE)
end
end
function test_objective()
for T in [Int, Float64]
model = SDPA.Model(; number_type = T)
@test !MOI.supports(model, MOI.ObjectiveFunction{MOI.VariableIndex}())
@test !MOI.supports(
model,
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}(),
)
model = SDPA.Model(; coefficient_type = T)
@test !MOI.supports(model, MOI.ObjectiveFunction{MOI.VariableIndex}())
@test !MOI.supports(
model,
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}(),
)
end
end
function test_nonempty()
model = SDPA.Model()
MOI.add_variable(model)
err = ErrorException("Cannot read in file because model is not empty.")
@test_throws err MOI.read_from_file(
model,
joinpath(SDPA_MODELS_DIR, "example_A.dat-s"),
)
end
function test_bad_blocks()
model = SDPA.Model()
err = ErrorException(
"The number of blocks (3) does not match the length of the list of blocks dimensions (2).",
)
@test_throws err MOI.read_from_file(
model,
joinpath(SDPA_MODELS_DIR, "bad_blocks.sdpa"),
)
end
function test_bad_number_variables()
model = SDPA.Model()
err = ErrorException(
"The number of variables (3) does not match the length of the list of coefficients for the objective function vector of coefficients (2).",
)
@test_throws err MOI.read_from_file(
model,
joinpath(SDPA_MODELS_DIR, "bad_vars.sdpa"),
)
end
function test_wrong_number_of_values_in_entry()
model = SDPA.Model()
err = ErrorException(
"Invalid line specifying entry: 0 1 2 2. There are 4 values instead of 5.",
)
@test_throws err MOI.read_from_file(
model,
joinpath(SDPA_MODELS_DIR, "bad_entry.sdpa"),
)
end
function test_nondiagonal_entry()
model = SDPA.Model()
err = ErrorException(
"Invalid line specifying entry: 0 1 1 2 1.0. `1 != 2` while block 1 has dimension 2 so it is a diagonal block.",
)
@test_throws err MOI.read_from_file(
model,
joinpath(SDPA_MODELS_DIR, "bad_diag.sdpa"),
)
end
function test_nonzero_in_objective()
model = SDPA.Model()
MOIU.loadfromstring!(
model,
"""
variables: x
minobjective: x + 1
""",
)
err = ErrorException(
"Nonzero constant in objective function not supported. Note that " *
"the constant may be added by the substitution of a bridged variable.",
)
@test_throws err MOI.write_to_file(model, SDPA_TEST_FILE)
end
function test_model_name()
model = SDPA.Model()
MOI.set(model, MOI.Name(), "FooBar")
MOI.write_to_file(model, SDPA_TEST_FILE)
@test readlines(SDPA_TEST_FILE) == ["\"FooBar", "0", "0", "", ""]
end
const _WRITE_READ_MODELS = [
(
"min ScalarAffine",
"""
variables: x, y
minobjective: 1.2x + -1y
""",
),
(
"max ScalarAffine",
"""
variables: x, y
maxobjective: 1.2x + -1y
""",
),
(
"VectorAffineFunction in Nonnegatives",
"""
variables: x, y
minobjective: 1.2x
c1: [1.1 * x, y + 1] in Nonnegatives(2)
""",
),
(
"VectorAffineFunction in PositiveSemidefiniteConeTriangle",
"""
variables: x, y, z
minobjective: 1.2x
c1: [1.1x, y + 1, 2x + z] in PositiveSemidefiniteConeTriangle(2)
""",
),
]
function test_write_read_models()
for (model_name, model_string) in _WRITE_READ_MODELS
_test_write_then_read(model_string)
end
end
const _EXAMPLE_MODELS = [
(
"example_A.dat-s",
"""
variables: x, y
minobjective: 10x + 20y
c1: [x + -1, 0, x + y + -2] in PositiveSemidefiniteConeTriangle(2)
c2: [5y + -3, 2y, 6y + -4] in PositiveSemidefiniteConeTriangle(2)
""",
),
(
"example_B.sdpa",
"""
variables: x
minobjective: 1x
c1: [0, 1x + -1, 0] in PositiveSemidefiniteConeTriangle(2)
""",
),
(
"example_integer.sdpa",
"""
variables: x, y, z
minobjective: 1x + -2y + -1z
c1: [1x, 1y, 1z] in PositiveSemidefiniteConeTriangle(2)
c2: [1z, 1x, 2.1] in PositiveSemidefiniteConeTriangle(2)
c3: [1x + 1y + 1z + -1, -1x + -1y + -1z + 8] in Nonnegatives(2)
x in Integer()
y in Integer()
z in Integer()
""",
),
(
"issue_1541.dat-s",
"""
variables: x, y, z
minobjective: -1y + -2z
c1: [-1x + -1y + -1z + 1, 1x, 1y, 1z] in Nonnegatives(4)
c2: [1x + 3y, 4y + 2z, -1z] in PositiveSemidefiniteConeTriangle(2)
""",
),
]
function test_examples()
for (model_name, model_string) in _EXAMPLE_MODELS
_test_read(joinpath(SDPA_MODELS_DIR, model_name), model_string)
_test_write_then_read(model_string)
end
end
# See https://github.com/jump-dev/MathOptInterface.jl/issues/1541
_spacer(char) = [" ", "$char", " $char", "$char ", " $char "]
function test_dim_reader()
fn(line) = SDPA._dim_to_set.(SDPA._split(line))
for (a, b) in ['{' => '}', '(' => ')']
for pre in _spacer(a), sep in _spacer(','), suf in _spacer(b)
@test fn("$(pre)-4$(sep)2$suf") ==
[MOI.Nonnegatives(4), MOI.PositiveSemidefiniteConeTriangle(2)]
@test fn("$(pre)2$(sep)-4$suf") ==
[MOI.PositiveSemidefiniteConeTriangle(2), MOI.Nonnegatives(4)]
end
end
return
end
function test_integer_before_variables()
file = """
*INTEGER
*1
"""
io = IOBuffer()
print(io, file)
seekstart(io)
model = SDPA.Model()
@test_throws(
ErrorException(
"The number of variables should be given before *INTEGER section.",
),
read!(io, model),
)
return
end
function test_unsupported_variable_types()
model = SDPA.Model()
@test_throws(
MOI.UnsupportedConstraint,
MOI.add_constrained_variable(model, MOI.Parameter(2.0)),
)
@test_throws(
MOI.UnsupportedConstraint,
MOI.add_constrained_variable(model, MOI.Semicontinuous(2.0, 3.0)),
)
@test_throws(
MOI.UnsupportedConstraint,
MOI.add_constrained_variable(model, MOI.Semiinteger(2.0, 3.0)),
)
return
end
function test_example_A_integer_coefficients()
filename = joinpath(SDPA_MODELS_DIR, "example_A_int.dat-s")
model = MOI.FileFormats.SDPA.Model(; coefficient_type = Int)
MOI.read_from_file(model, filename)
for xi in MOI.get(model, MOI.ListOfVariableIndices())
MOI.set(model, MOI.VariableName(), xi, "v$(xi.value)")
end
F, S = MOI.VectorAffineFunction{Int}, MOI.PositiveSemidefiniteConeTriangle
for ci in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
MOI.set(model, MOI.ConstraintName(), ci, "c$(ci.value)")
end
input = """
variables: v1, v2
minobjective::Int: 10v1 + 20v2
c1::Int: [v1 + -1, 0, v1 + v2 + -2] in PositiveSemidefiniteConeTriangle(2)
c2::Int: [5v2 + -3, 2v2, 6v2 + -4] in PositiveSemidefiniteConeTriangle(2)
"""
target = MOI.Utilities.Model{Int}()
MOI.Utilities.loadfromstring!(target, input)
MOI.Test.util_test_models_equal(model, target, ["v1", "v2"], ["c1", "c2"])
return
end
function test_unsupported_objectives()
model = MPS.Model()
for (F, ret) in [
MOI.VariableIndex => false,
MOI.ScalarAffineFunction{Float64} => true,
MOI.ScalarQuadraticFunction{Float64} => false,
MOI.ScalarNonlinearFunction => false,
MOI.VectorOfVariables => false,
MOI.VectorAffineFunction{Float64} => false,
MOI.VectorQuadraticFunction{Float64} => false,
MOI.VectorNonlinearFunction => false,
]
@test MOI.supports(model, MOI.ObjectiveFunction{F}()) == ret
end
return
end
end # module
TestSDPA.runtests()