-
Notifications
You must be signed in to change notification settings - Fork 5
/
runtests.jl
408 lines (330 loc) · 9.94 KB
/
runtests.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
using Base.Test
using ECOS, SCS, MathProgBase
import Convex
import JuMP
using ConicBenchmarkUtilities
@testset "example4.cbf" begin
dat = readcbfdata("example4.cbf")
c, A, b, con_cones, var_cones, vartypes, dat.sense, dat.objoffset = cbftompb(dat)
@test c ≈ [1.0, 0.64]
@test A ≈ [-50.0 -31; -3.0 2.0]
@test b ≈ [-250.0, 4.0]
@test vartypes == [:Cont, :Cont]
@test dat.sense == :Max
@test dat.objoffset == 0.0
@test con_cones == [(:NonPos,[1]),(:NonNeg,[2])]
m = MathProgBase.ConicModel(ECOSSolver(verbose=0))
MathProgBase.loadproblem!(m, -c, A, b, con_cones, var_cones)
MathProgBase.optimize!(m)
x_sol = MathProgBase.getsolution(m)
objval = MathProgBase.getobjval(m)
x = Convex.Variable(2)
pj = Convex.maximize(x[1] + 0.64x[2], 50x[1] + 31x[2] <= 250, 3x[1] - 2x[2] >= -4)
Convex.solve!(pj, ECOSSolver(verbose=0))
@test x_sol ≈ Convex.evaluate(x) atol=1e-6
@test -objval ≈ pj.optval atol=1e-6
# test CBF writer
newdat = mpbtocbf("example", c, A, b, con_cones, var_cones, vartypes, dat.sense)
writecbfdata("example_out.cbf",newdat,"# Example C.4 from the CBF documentation version 2")
@test strip(readstring("example4.cbf")) == strip(readstring("example_out.cbf"))
rm("example_out.cbf")
end
# test transformation utilities
@testset "dualize" begin
# max y + z
# st x <= 1
# (x,y,z) in SOC
# x in {0,1}
c = [0.0, -1.0, -1.0]
A = [1.0 0.0 0.0;
-1.0 0.0 0.0;
0.0 -1.0 0.0;
0.0 0.0 -1.0]
b = [1.0, 0.0, 0.0, 0.0]
con_cones = [(:NonNeg,1:1), (:SOC,2:4)]
var_cones = [(:Free,1:3)]
(c, A, b, con_cones, var_cones) = dualize(c, A, b, con_cones, var_cones)
@test c == [1.0, 0.0, 0.0, 0.0]
@test A == [-1.0 1.0 0.0 0.0;
0.0 0.0 1.0 0.0;
0.0 0.0 0.0 1.0]
@test b == [0.0, -1.0, -1.0]
@test con_cones == [(:Zero,1:3)]
@test var_cones == [(:NonNeg,1:1), (:SOC,2:4)]
end
@testset "socrotated_to_soc" begin
# SOCRotated1 from MathProgBase conic tests
c = [ 0.0, 0.0, -1.0, -1.0]
A = [ 1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0]
b = [ 0.5, 1.0]
con_cones = [(:Zero,1:2)]
var_cones = [(:SOCRotated,1:4)]
vartypes = fill(:Cont,4)
(c, A, b, con_cones, var_cones, vartypes) = socrotated_to_soc(c, A, b, con_cones, var_cones, vartypes)
@test c == [0.0,0.0,-1.0,-1.0]
@test b == [0.5,1.0,0.0,0.0,0.0,0.0]
@test A ≈ [1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
-1.0 -1.0 0.0 0.0
-1.0 1.0 0.0 0.0
0.0 0.0 -1.4142135623730951 0.0
0.0 0.0 0.0 -1.4142135623730951]
@test var_cones == [(:Free,1:4)]
@test con_cones == [(:Zero,1:2),(:SOC,3:6)]
c = [-1.0,-1.0]
A = [0.0 0.0; 0.0 0.0; -1.0 0.0; 0.0 -1.0]
b = [0.5, 1.0, 0.0, 0.0]
con_cones = [(:SOCRotated,1:4)]
var_cones = [(:Free,1:2)]
vartypes = fill(:Cont,2)
(c, A, b, con_cones, var_cones, vartypes) = socrotated_to_soc(c, A, b, con_cones, var_cones, vartypes)
@test c == [-1.0,-1.0,0.0,0.0,0.0,0.0]
@test b == [0.5,1.0,0.0,0.0,0.0,0.0,0.0,0.0]
@test A == [0.0 0.0 1.0 0.0 0.0 0.0
0.0 0.0 0.0 1.0 0.0 0.0
-1.0 0.0 0.0 0.0 1.0 0.0
0.0 -1.0 0.0 0.0 0.0 1.0
0.0 0.0 -1.0 -1.0 0.0 0.0
0.0 0.0 -1.0 1.0 0.0 0.0
0.0 0.0 0.0 0.0 -1.4142135623730951 0.0
0.0 0.0 0.0 0.0 0.0 -1.4142135623730951]
@test var_cones == [(:Free,1:2),(:Free,3:6)]
@test con_cones == [(:Zero,1:4),(:SOC,5:8)]
end
@testset "remove_ints_in_nonlinear_cones" begin
# SOCINT1
c = [ 0.0, -2.0, -1.0]
A = sparse([ 1.0 0.0 0.0])
b = [ 1.0]
con_cones = [(:Zero,1)]
var_cones = [(:SOC,1:3)]
vartypes = [:Cont,:Bin,:Bin]
(c, A, b, con_cones, var_cones, vartypes) = remove_ints_in_nonlinear_cones(c, A, b, con_cones, var_cones, vartypes)
@test c == [0.0,-2.0,-1.0,0.0,0.0]
@test b == [1.0,0.0,0.0]
@test A == [1.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 -1.0 0.0
0.0 0.0 1.0 0.0 -1.0]
@test var_cones == [(:SOC,[1,4,5]),(:Free,[2,3])]
@test con_cones == [(:Zero,[1]),(:Zero,[2,3])]
end
@testset "exponential cones with Convex.jl" begin
x = Convex.Variable()
problem = Convex.minimize( exp(x), x >= 1 )
ConicBenchmarkUtilities.convex_to_cbf(problem, "exptest", "exptest.cbf")
output = """
# Generated by ConicBenchmarkUtilities.jl
VER
2
OBJSENSE
MIN
VAR
3 1
F 3
CON
5 3
EXP 3
L= 1
L+ 1
OBJACOORD
1
2 1.0
ACOORD
5
2 0 1.0
4 0 1.0
0 1 1.0
3 1 1.0
3 2 -1.0
BCOORD
2
1 1.0
4 -1.0
"""
@test readstring("exptest.cbf") == output
(c, A, b, con_cones, var_cones, vartypes, sense, objoffset) = cbftompb(readcbfdata("exptest.cbf"))
@test sense == :Min
@test objoffset == 0.0
@test all(vartypes .== :Cont)
m = MathProgBase.ConicModel(ECOSSolver(verbose=0))
MathProgBase.loadproblem!(m, c, A, b, con_cones, var_cones)
MathProgBase.optimize!(m)
@test MathProgBase.status(m) == :Optimal
x_sol = MathProgBase.getsolution(m)
@test x_sol ≈ [1.0,exp(1),exp(1)] atol=1e-5
@test MathProgBase.getobjval(m) ≈ exp(1) atol=1e-5
rm("exptest.cbf")
end
# SDP tests
@testset "roundtrip read/write" begin
dat = readcbfdata("example1.cbf")
@test dat.sense == :Min
@test dat.objoffset == 0.0
@test isempty(dat.intlist)
writecbfdata("example_out.cbf",dat,"# Example C.1 from the CBF documentation version 2")
@test strip(readstring("example1.cbf")) == strip(readstring("example_out.cbf"))
rm("example_out.cbf")
end
@testset "roundtrip through MPB format" begin
dat = readcbfdata("example1.cbf")
(c, A, b, con_cones, var_cones, vartypes, dat.sense, dat.objoffset) = cbftompb(dat)
newdat = mpbtocbf("example", c, A, b, con_cones, var_cones, vartypes, dat.sense)
writecbfdata("example_out.cbf",newdat,"# Example C.1 from the CBF documentation version 2")
output = """
# Example C.1 from the CBF documentation version 2
VER
2
OBJSENSE
MIN
PSDVAR
1
3
VAR
3 1
F 3
CON
5 2
L= 2
Q 3
OBJFCOORD
5
0 0 0 2.0
0 0 1 1.0
0 1 1 2.0
0 1 2 1.0
0 2 2 2.0
OBJACOORD
1
1 1.0
FCOORD
9
0 0 0 0 1.0
1 0 0 0 1.0
1 0 0 1 1.0
1 0 0 2 1.0
0 0 1 1 1.0
1 0 1 1 1.0
1 0 1 2 1.0
0 0 2 2 1.0
1 0 2 2 1.0
ACOORD
6
1 0 1.0
3 0 1.0
0 1 1.0
2 1 1.0
1 2 1.0
4 2 1.0
BCOORD
2
0 -1.0
1 -0.5
"""
@test readstring("example_out.cbf") == output
rm("example_out.cbf")
end
@testset "Instance with only PSD variables" begin
dat = readcbfdata("psd_var_only.cbf")
(c, A, b, con_cones, var_cones, vartypes, dat.sense, dat.objoffset) = cbftompb(dat)
@test var_cones == [(:SDP, [1, 2, 3])]
end
SCSSOLVER = SCSSolver(eps=1e-6, verbose=0)
@testset "roundtrip through MPB solver" begin
dat = readcbfdata("example1.cbf")
(c, A, b, con_cones, var_cones, vartypes, dat.sense, dat.objoffset) = cbftompb(dat)
m = MathProgBase.ConicModel(SCSSOLVER)
MathProgBase.loadproblem!(m, c, A, b, con_cones, var_cones)
MathProgBase.optimize!(m)
@test MathProgBase.status(m) == :Optimal
(scalar_solution, psdvar_solution) = ConicBenchmarkUtilities.mpb_sol_to_cbf(dat,MathProgBase.getsolution(m))
jm = JuMP.Model(solver=SCSSOLVER)
@JuMP.variable(jm, x[1:3])
@JuMP.variable(jm, X[1:3,1:3], SDP)
@JuMP.objective(jm, Min, vecdot([2 1 0; 1 2 1; 0 1 2],X) + x[2])
@JuMP.constraint(jm, X[1,1]+X[2,2]+X[3,3]+x[2] == 1.0)
@JuMP.constraint(jm, vecdot(ones(3,3),X) + x[1] + x[3] == 0.5)
@JuMP.constraint(jm, norm([x[1],x[3]]) <= x[2])
@test JuMP.solve(jm) == :Optimal
@test JuMP.getobjectivevalue(jm) ≈ MathProgBase.getobjval(m) atol=1e-4
for i in 1:3
@test JuMP.getvalue(x[i]) ≈ scalar_solution[i] atol=1e-4
end
for i in 1:3, j in 1:3
@test JuMP.getvalue(X[i,j]) ≈ psdvar_solution[1][i,j] atol=1e-4
end
end
@testset "example3.cbf" begin
dat = readcbfdata("example3.cbf")
(c, A, b, con_cones, var_cones, vartypes, dat.sense, dat.objoffset) = cbftompb(dat)
@test dat.sense == :Min
@test dat.objoffset == 1.0
@test all(vartypes .== :Cont)
m = MathProgBase.ConicModel(SCSSOLVER)
MathProgBase.loadproblem!(m, c, A, b, con_cones, var_cones)
MathProgBase.optimize!(m)
@test MathProgBase.status(m) == :Optimal
scalar_solution, psdvar_solution = ConicBenchmarkUtilities.mpb_sol_to_cbf(dat,MathProgBase.getsolution(m))
jm = JuMP.Model(solver=SCSSOLVER)
@JuMP.variable(jm, x[1:2])
@JuMP.variable(jm, X[1:2,1:2], SDP)
@JuMP.objective(jm, Min, X[1,1] + X[2,2] + x[1] + x[2] + 1)
@JuMP.constraint(jm, X[1,2] + X[2,1] - x[1] - x[2] ≥ 0.0)
@JuMP.SDconstraint(jm, [0 1; 1 3]*x[1] + [3 1; 1 0]*x[2] - [1 0; 0 1] >= 0)
@test JuMP.solve(jm) == :Optimal
@test JuMP.getobjectivevalue(jm) ≈ MathProgBase.getobjval(m)+dat.objoffset atol=1e-4
for i in 1:2
@test JuMP.getvalue(x[i]) ≈ scalar_solution[i] atol=1e-4
end
for i in 1:2, j in 1:2
@test JuMP.getvalue(X[i,j]) ≈ psdvar_solution[1][i,j] atol=1e-4
end
# should match example3 modulo irrelevant changes
ConicBenchmarkUtilities.jump_to_cbf(jm, "example3", "sdptest.cbf")
output = """
# Generated by ConicBenchmarkUtilities.jl
VER
2
OBJSENSE
MIN
PSDVAR
1
2
VAR
2 1
F 2
PSDCON
1
2
CON
1 1
L- 1
OBJFCOORD
2
0 0 0 1.0
0 1 1 1.0
OBJACOORD
2
0 1.0
1 1.0
FCOORD
1
0 0 0 1 -0.9999999999999999
ACOORD
2
0 0 1.0
0 1 1.0
HCOORD
4
0 0 0 1 0.9999999999999999
0 0 1 1 3.0
0 1 0 0 3.0
0 1 0 1 0.9999999999999999
DCOORD
2
0 0 0 -1.0
0 1 1 -1.0
"""
@test readstring("sdptest.cbf") == output
rm("sdptest.cbf")
end