-
Notifications
You must be signed in to change notification settings - Fork 86
/
isapprox.jl
72 lines (72 loc) · 3.73 KB
/
isapprox.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
@testset "Functions isapprox" begin
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
z = MOI.VariableIndex(3)
@testset "Variable" begin
@testset "Single" begin
@test MOI.SingleVariable(x) == MOI.SingleVariable(x)
@test MOI.SingleVariable(x) != MOI.SingleVariable(y)
end
@testset "Vector" begin
@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])
end
end
@testset "Affine" begin
@testset "Scalar" begin
@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
f.terms[2] = MOI.ScalarAffineTerm(3, y)
@test !(g ≈ f)
end
@testset "Vector" begin
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)
push!(f.terms, MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(-6, y)))
@test f ≈ g
end
end
@testset "Quadratic" begin
@testset "Affine" begin
f = MOI.ScalarQuadraticFunction([MOI.ScalarAffineTerm(3, x)],
MOI.ScalarQuadraticTerm.([1, 2, 3],
[x, y, x],
[x, y, y]), 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)
push!(f.quadratic_terms, MOI.ScalarQuadraticTerm(-2, y, x))
@test f ≈ g
end
@testset "Vector" begin
f = MOI.VectorQuadraticFunction(MOI.VectorAffineTerm.([1, 2, 1], MOI.ScalarAffineTerm.([3, 1, 1],
[x, x, y])),
MOI.VectorQuadraticTerm.([1, 1, 2], MOI.ScalarQuadraticTerm.([1, 2, 3],
[x, y, x],
[x, y, y])), [10, 11, 12])
g = deepcopy(f)
@test f ≈ g
f.affine_terms[1] = MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(4, x))
@test !(f ≈ g)
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
f.quadratic_terms[1] = MOI.VectorQuadraticTerm(3, MOI.ScalarQuadraticTerm(1, x, x))
@test !(f ≈ g)
end
end
end