-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathruntests.jl
More file actions
201 lines (187 loc) · 5.99 KB
/
Copy pathruntests.jl
File metadata and controls
201 lines (187 loc) · 5.99 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
# Copyright (c) 2019: Joaquim Dias Garcia, 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.
using SparseArrays, Test
import MatrixOptInterface
const MatOI = MatrixOptInterface
const MOI = MatOI.MOI
const MOIU = MatOI.MOIU
const MOIB = MOI.Bridges
const MOIT = MOI.Test
const ATOL = 1e-4
const RTOL = 1e-4
include("conic_form.jl")
const dense_A = [
1.0 2.0
3.0 4.0
]
@testset "Matrix $(typeof(A))" for A in [dense_A, sparse(dense_A)]
dense_b = [5.0, 6.0]
dense_c = [7.0, 8.0]
@testset "Vector $(typeof(b))" for (b, c) in zip(
[dense_b, sparsevec(dense_b)],
[dense_c, sparsevec(dense_c)],
)
@testset "Standard form LP" begin
s = """
variables: x1, x2
cx1: x1 >= 0.0
cx2: x2 >= 0.0
c1: x1 + 2x2 == 5.0
c2: 3x1 + 4x2 == 6.0
minobjective: 7x1 + 8x2
"""
expected = MOIU.Model{Float64}()
MOIU.loadfromstring!(expected, s)
var_names = ["x1", "x2"]
con_names = ["c1", "c2"]
vcon_names = ["cx1", "cx2"]
model = MOIU.Model{Float64}()
sense = MOI.MIN_SENSE
v_lb = [0.0, 0.0]
v_ub = [Inf, Inf]
function test_expected(form)
MOI.copy_to(
MOI.Bridges.Constraint.Scalarize{Float64}(model),
form,
)
MOI.set(
model,
MOI.VariableName(),
MOI.VariableIndex.(1:2),
var_names,
)
MOI.set(
model,
MOI.ConstraintName(),
MOI.ConstraintIndex{
MOI.ScalarAffineFunction{Float64},
MOI.EqualTo{Float64},
}.(
1:2,
),
con_names,
)
return MOIT.util_test_models_equal(
model,
expected,
var_names,
con_names,
)
end
@testset "change $(typeof(lp))" for lp in [
MatOI.LPStandardForm{Float64,typeof(A),typeof(c)}(
sense,
c,
A,
b,
),
MatOI.LPForm{Float64,typeof(A),typeof(c)}(
sense,
c,
A,
b,
b,
v_lb,
v_ub,
),
MatOI.LPSolverForm{Float64,typeof(A),typeof(c)}(
sense,
c,
A,
b,
[MatOI.EQUAL_TO, MatOI.EQUAL_TO],
v_lb,
v_ub,
),
]
test_expected(lp)
@testset "to $F" for F in [
#MatOI.LPStandardForm{Float64, typeof(A)}, # FIXME doesn't work as the form gets bloated in the conversion
MatOI.LPForm{Float64,typeof(A),typeof(c)},
MatOI.LPSolverForm{Float64,typeof(A),typeof(c)},
]
test_expected(MatOI.change_form(F, lp))
end
end
end
@testset "Geometric form LP" begin
s = """
variables: x1, x2
c1: x1 + 3x2 <= 7.0
c2: 2x1 + 4x2 <= 8.0
maxobjective: 5x1 + 6x2
"""
expected = MOIU.Model{Float64}()
MOIU.loadfromstring!(expected, s)
var_names = ["x1", "x2"]
con_names = ["c1", "c2"]
model = MOIU.Model{Float64}()
sense = MOI.MAX_SENSE
v_lb = [-Inf, -Inf]
v_ub = [Inf, Inf]
function test_expected(form)
MOI.copy_to(model, form)
MOI.set(
model,
MOI.VariableName(),
MOI.VariableIndex.(1:2),
var_names,
)
MOI.set(
model,
MOI.ConstraintName(),
MOI.ConstraintIndex{
MOI.ScalarAffineFunction{Float64},
MOI.LessThan{Float64},
}.(
1:2,
),
con_names,
)
return MOIT.util_test_models_equal(
model,
expected,
var_names,
con_names,
)
end
@testset "change $(typeof(lp))" for lp in [
MatOI.LPGeometricForm{Float64,typeof(A'),typeof(c)}(
sense,
b,
A',
c,
),
MatOI.LPForm{Float64,typeof(A'),typeof(c)}(
sense,
b,
A',
[-Inf, -Inf],
c,
v_lb,
v_ub,
),
MatOI.LPSolverForm{Float64,typeof(A'),typeof(c)}(
sense,
b,
A',
c,
[MatOI.LESS_THAN, MatOI.LESS_THAN],
v_lb,
v_ub,
),
]
test_expected(lp)
@testset "to $F" for F in [
MatOI.LPGeometricForm{Float64,typeof(A),typeof(c)},
MatOI.LPForm{Float64,typeof(A),typeof(c)},
MatOI.LPSolverForm{Float64,typeof(A),typeof(c)},
]
test_expected(MatOI.change_form(F, lp))
end
end
end
end
end