/
functions.jl
259 lines (240 loc) · 7.74 KB
/
functions.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
# 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 TestFunctions
using Test
using MathOptInterface
const MOI = MathOptInterface
"""
test_isbits()
Test isbit-ness of VariableIndex and terms.
It is important that these struct remain isbits as otherwise, the performance of
the corresponding function will be deteriored. These tests explicit this design
choice and makes sure that it remains the case.
"""
function test_isbits()
x = @inferred MOI.VariableIndex(1)
@test isbits(x)
at = @inferred MOI.ScalarAffineTerm(1.0, x)
@test isbits(at)
@test isbits(@inferred MOI.VectorAffineTerm(1, at))
qt = @inferred MOI.ScalarQuadraticTerm(1.0, x, x)
@test isbits(qt)
@test isbits(@inferred MOI.VectorQuadraticTerm(1, qt))
end
function test_functions_broadcast()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
z = MOI.VariableIndex(3)
function sum_indices(sv1::MOI.VariableIndex, sv2::MOI.VariableIndex)
return sv1.value + sv2.value
end
@test sum_indices.(x, [y, z]) == [3, 4]
end
function test_functions_copy_VectorOfVariables()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
z = MOI.VariableIndex(3)
f = MOI.VectorOfVariables([x, y])
f_copy = copy(f)
f_copy.variables[2] = z
@test f.variables[2] == y
end
function test_functions_convert_VariableIndex()
model = MOI.Utilities.Model{Float64}()
x = MOI.add_variable(model)
f_vov = convert(MOI.VectorOfVariables, x)
@test f_vov ≈ MOI.VectorOfVariables([x])
f_vaf = convert(MOI.VectorAffineFunction{Float64}, x)
@test f_vaf ≈ MOI.VectorAffineFunction(
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x))],
[0.0],
)
f_vqf = convert(MOI.VectorQuadraticFunction{Float64}, x)
@test f_vqf ≈ MOI.VectorQuadraticFunction(
MOI.VectorQuadraticTerm{Float64}[],
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x))],
[0.0],
)
end
function test_functions_convert_ScalarAffineFunction()
model = MOI.Utilities.Model{Float64}()
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(2.0, x)], 1.0)
@test_throws(MethodError, convert(MOI.VectorOfVariables, f))
f_vaf = convert(MOI.VectorAffineFunction{Float64}, f)
@test f_vaf ≈ MOI.VectorAffineFunction(
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(2.0, x))],
[1.0],
)
f_vqf = convert(MOI.VectorQuadraticFunction{Float64}, f)
@test f_vqf ≈ MOI.VectorQuadraticFunction(
MOI.VectorQuadraticTerm{Float64}[],
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(2.0, x))],
[1.0],
)
end
function test_functions_convert_ScalarQuadraticFunction()
model = MOI.Utilities.Model{Float64}()
x = MOI.add_variable(model)
f = MOI.ScalarQuadraticFunction(
[MOI.ScalarQuadraticTerm(3.0, x, x)],
[MOI.ScalarAffineTerm(2.0, x)],
1.0,
)
@test_throws(MethodError, convert(MOI.VectorOfVariables, f))
@test_throws(MethodError, convert(MOI.VectorAffineFunction{Float64}, f))
f_vqf = convert(MOI.VectorQuadraticFunction{Float64}, f)
@test f_vqf ≈ MOI.VectorQuadraticFunction(
MOI.VectorQuadraticTerm{Float64}[MOI.VectorQuadraticTerm(
1,
MOI.ScalarQuadraticTerm(3.0, x, x),
)],
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(2.0, x))],
[1.0],
)
end
function test_isapprox_VectorOfVariables()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
@test MOI.VectorOfVariables([x, y]) == MOI.VectorOfVariables([x, y])
@test MOI.VectorOfVariables([y, x]) != MOI.VectorOfVariables([x, y])
@test MOI.VectorOfVariables([x, x]) != MOI.VectorOfVariables([x])
@test MOI.VectorOfVariables([x]) != MOI.VectorOfVariables([y])
return
end
function test_isapprox_ScalarAffineFunction()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
z = MOI.VariableIndex(3)
@test MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([1, 1], [x, z]), 1) ≈
MOI.ScalarAffineFunction(
MOI.ScalarAffineTerm.([1.0, 1e-7, 1.0], [x, y, z]),
1.0,
) atol = 1e-6
@test MOI.ScalarAffineFunction(
MOI.ScalarAffineTerm.([1.0, 1e-7], [x, y]),
1.0,
) ≈ MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1, x)], 1) atol = 1e-6
f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2, 4], [x, y]), 6)
g = deepcopy(f)
@test g ≈ f
@test f ≈ g
f.terms[2] = MOI.ScalarAffineTerm(3, y)
@test !(g ≈ f)
@test !(f ≈ g)
return
end
function test_isapprox_VectorAffineFunction()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
f = MOI.VectorAffineFunction(
MOI.VectorAffineTerm.(
[1, 1, 2],
MOI.ScalarAffineTerm.([2, 4, 3], [x, y, y]),
),
[6, 8],
)
g = deepcopy(f)
@test f ≈ g
f.terms[3] = MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(9, y))
@test !(f ≈ g)
@test !(g ≈ f)
push!(f.terms, MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(-6, y)))
@test f ≈ g
@test g ≈ f
return
end
function test_isapprox_ScalarQuadraticFunction()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
z = MOI.VariableIndex(3)
f = MOI.ScalarQuadraticFunction(
MOI.ScalarQuadraticTerm.([1, 2, 3], [x, y, x], [x, y, y]),
[MOI.ScalarAffineTerm(3, x)],
8,
)
g = deepcopy(f)
@test f ≈ g
push!(f.affine_terms, MOI.ScalarAffineTerm(2, y))
@test !(f ≈ g)
g = deepcopy(f)
push!(f.quadratic_terms, MOI.ScalarQuadraticTerm(2, y, x))
@test !(f ≈ g)
@test !(g ≈ f)
push!(f.quadratic_terms, MOI.ScalarQuadraticTerm(-2, y, x))
@test f ≈ g
@test g ≈ f
return
end
function test_isapprox_VectorQuadraticFunction()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
z = MOI.VariableIndex(3)
f = MOI.VectorQuadraticFunction(
MOI.VectorQuadraticTerm.(
[1, 1, 2],
MOI.ScalarQuadraticTerm.([1, 2, 3], [x, y, x], [x, y, y]),
),
MOI.VectorAffineTerm.(
[1, 2, 1],
MOI.ScalarAffineTerm.([3, 1, 1], [x, x, y]),
),
[10, 11, 12],
)
g = deepcopy(f)
@test f ≈ g
@test g ≈ f
f.affine_terms[1] = MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(4, x))
@test !(f ≈ g)
@test !(g ≈ f)
push!(g.affine_terms, MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(-3, x)))
push!(g.affine_terms, MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(4, x)))
@test f ≈ g
@test g ≈ f
f.quadratic_terms[1] =
MOI.VectorQuadraticTerm(3, MOI.ScalarQuadraticTerm(1, x, x))
@test !(f ≈ g)
@test !(g ≈ f)
return
end
function test_isapprox_issue_1483()
x = MOI.ScalarQuadraticFunction(
MOI.ScalarQuadraticTerm{Float16}[],
MOI.ScalarAffineTerm{Float16}[],
Float16(0.0),
)
y = MOI.ScalarQuadraticFunction(
MOI.ScalarQuadraticTerm{Float16}[],
MOI.ScalarAffineTerm{Float16}[MOI.ScalarAffineTerm(
Float16(1.0),
MOI.VariableIndex(1234),
)],
Float16(0.0),
)
z = MOI.ScalarQuadraticFunction(
MOI.ScalarQuadraticTerm{Float16}[],
MOI.ScalarAffineTerm{Float16}[],
Float16(0.0),
)
@test !(x ≈ y)
@test !(y ≈ x)
@test x ≈ z
@test z ≈ x
@test !(y ≈ z)
@test !(z ≈ y)
return
end
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
end
end
TestFunctions.runtests()