-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathIntervalToHyperRectangleBridge.jl
More file actions
132 lines (120 loc) · 4.12 KB
/
IntervalToHyperRectangleBridge.jl
File metadata and controls
132 lines (120 loc) · 4.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
# 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 TestConstraintIntervalToHyperRectangle
using Test
import MathOptInterface as MOI
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name) $T" for T in [Float32, Float64]
getfield(@__MODULE__, name)(T)
end
end
end
return
end
include("../utilities.jl")
function test_ScalarFunctionConstantNotZero(T)
mock = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}()),
)
bridged_mock = MOI.Bridges.Constraint.IntervalToHyperRectangle{T}(mock)
config = MOI.Test.Config(T)
MOI.Test.test_model_ScalarFunctionConstantNotZero(bridged_mock, config)
return
end
function test_basic(T)
mock = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}()),
)
bridged_mock = MOI.Bridges.Constraint.IntervalToHyperRectangle{T}(mock)
config = MOI.Test.Config()
MOI.Test.runtests(
bridged_mock,
config,
include = ["test_basic_ScalarAffineFunction_Interval"],
)
return
end
function test_linear_integration(T)
mock = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}()),
)
bridged_mock = MOI.Bridges.Constraint.IntervalToHyperRectangle{T}(mock)
config = MOI.Test.Config(T, exclude = Any[MOI.ConstraintBasisStatus])
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
T[5, 5],
(MOI.VectorAffineFunction{T}, MOI.HyperRectangle{T}) => [T[-1]],
),
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
T[5//2, 5//2],
(MOI.VectorAffineFunction{T}, MOI.HyperRectangle{T}) => [T[1]],
),
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
T[1, 1],
(MOI.VectorAffineFunction{T}, MOI.HyperRectangle{T}) => [T[1]],
),
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
T[6, 6],
(MOI.VectorAffineFunction{T}, MOI.HyperRectangle{T}) => [T[-1]],
),
)
MOI.Test.test_linear_integration_Interval(bridged_mock, config)
return
end
function test_runtests(T)
MOI.Bridges.runtests(
MOI.Bridges.Constraint.IntervalToHyperRectangleBridge,
model -> begin
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.Interval{T}(3, 5))
end,
model -> begin
x = MOI.add_variable(model)
MOI.add_constraint(
model,
MOI.VectorOfVariables([x]),
MOI.HyperRectangle(T[3], T[5]),
)
end,
eltype = T,
)
MOI.Bridges.runtests(
MOI.Bridges.Constraint.IntervalToHyperRectangleBridge,
model -> begin
x = MOI.add_variable(model)
MOI.add_constraint(model, T(2) * x, MOI.Interval{T}(3, 5))
end,
model -> begin
x = MOI.add_variable(model)
MOI.add_constraint(
model,
MOI.Utilities.vectorize([T(2) * x]),
MOI.HyperRectangle(T[3], T[5]),
)
end,
eltype = T,
)
return
end
function test_modify_ScalarCoefficientChange(T)
inner = MOI.Utilities.Model{T}()
model = MOI.Bridges.Constraint.IntervalToHyperRectangle{T}(inner)
x = MOI.add_variable(model)
c = MOI.add_constraint(model, T(1) * x, MOI.Interval(T(0), T(1)))
@test ≈(MOI.get(model, MOI.ConstraintFunction(), c), T(1) * x)
MOI.modify(model, c, MOI.ScalarCoefficientChange(x, T(2)))
@test ≈(MOI.get(model, MOI.ConstraintFunction(), c), T(2) * x)
return
end
end # module
TestConstraintIntervalToHyperRectangle.runtests()