-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathVectorSlackBridge.jl
More file actions
203 lines (192 loc) · 6 KB
/
VectorSlackBridge.jl
File metadata and controls
203 lines (192 loc) · 6 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
202
203
# 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 TestObjectiveVectorSlack
using Test
import MathOptInterface as MOI
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
function test_runtests()
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorSlackBridge,
"""
variables: x
minobjective: [1.1 * x + 2.2]
""",
"""
variables: x, y
minobjective: [y]
[1.0 * y + -1.1 * x + -2.2] in Nonnegatives(1)
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorSlackBridge,
"""
variables: x
maxobjective: [1.1 * x + 2.2]
""",
"""
variables: x, y
maxobjective: [y]
[-1.0 * y + 1.1 * x + 2.2] in Nonnegatives(1)
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorSlackBridge,
"""
variables: x
minobjective: [1.1 * x + 2.2, -1.0 * x]
""",
"""
variables: x, y, z
minobjective: [y, z]
[1.0 * y + -1.1 * x + -2.2, 1.0 * z + 1.0 * x] in Nonnegatives(2)
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorSlackBridge,
"""
variables: x
maxobjective: [1.1 * x + 2.2, -1.0 * x]
""",
"""
variables: x, y, z
maxobjective: [y, z]
[-1.0 * y + 1.1 * x + 2.2, -1.0 * z + -1.0 * x] in Nonnegatives(2)
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorSlackBridge,
"""
variables: x
maxobjective: [1.0 * x, -1.1 * x]
""",
"""
variables: x, y
maxobjective: [x, y]
[-1.0 * y + -1.1 * x] in Nonnegatives(1)
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorSlackBridge,
"""
variables: x
minobjective: [-1.1 * x, 1.0 * x]
""",
"""
variables: x, y
minobjective: [y, x]
[1.0 * y + 1.1 * x] in Nonnegatives(1)
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorSlackBridge,
"""
variables: x
maxobjective: [x]
""",
"""
variables: x
maxobjective: [x]
""",
)
return
end
function test_objective_sense_before_function()
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
model = MOI.Bridges.Objective.VectorSlack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.Utilities.operate(vcat, Float64, 1.0 * x, 1.0 * x)
attr = MOI.ObjectiveFunction{typeof(f)}()
@test_throws(
MOI.SetAttributeNotAllowed(
attr,
"Set `MOI.ObjectiveSense` before `MOI.ObjectiveFunction` when " *
"using `MOI.Bridges.Objective.VectorSlackBridge`.",
),
MOI.set(model, attr, f),
)
return
end
function test_objective_function_value()
for sense in ("min", "max")
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
model = MOI.Bridges.Objective.VectorSlack{Float64}(inner)
MOI.Utilities.loadfromstring!(
model,
"""
variables: x
$(sense)objective: [1.1 * x + 2.2, -1.0 * x]
""",
)
MOI.Utilities.set_mock_optimize!(
inner,
mock -> MOI.Utilities.mock_optimize!(mock, [3.0, 5.6, -3.0]),
)
MOI.optimize!(model)
# Test that we get 5.5 here, not 5.6 as set for the slack variable. This
# ensures we return the value of the f(x) objective, not the `y` slack.
@test MOI.get(model, MOI.ObjectiveValue()) ≈ [5.5, -3.0]
end
return
end
function test_modify_vector_constant_change()
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
model = MOI.Bridges.Objective.VectorSlack{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.Utilities.operate(vcat, Float64, -1.1 * x, 1.0 * x)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
attr = MOI.ObjectiveFunction{typeof(f)}()
MOI.set(model, attr, f)
@test MOI.get(model, attr) ≈ f
@test_throws(
MOI.ModifyObjectiveNotAllowed,
MOI.modify(model, attr, MOI.VectorConstantChange([1.0, 2.0])),
)
return
end
function test_SlackBridge_ObjectiveFunctionValue_2()
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Objective.VectorSlack{Float64}(inner)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(1.0))
f = MOI.Utilities.operate(vcat, Float64, 1.1 * x - 1.2)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.Utilities.set_mock_optimize!(
inner,
mock -> begin
MOI.set(mock, MOI.ResultCount(), 2)
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mock, MOI.PrimalStatus(1), MOI.FEASIBLE_POINT)
MOI.set(mock, MOI.PrimalStatus(2), MOI.FEASIBLE_POINT)
y = MOI.get(mock, MOI.ListOfVariableIndices())
MOI.set.(mock, MOI.VariablePrimal(1), y, [1.0, -0.1])
MOI.set.(mock, MOI.VariablePrimal(2), y, [2.0, 1.0])
end,
)
MOI.optimize!(model)
@test MOI.get(model, MOI.ObjectiveValue(1)) ≈ [-0.1]
@test MOI.get(model, MOI.VariablePrimal(1), x) ≈ 1.0
@test MOI.get(model, MOI.ObjectiveValue(2)) ≈ [1.0]
@test MOI.get(model, MOI.VariablePrimal(2), x) ≈ 2.0
return
end
end # module
TestObjectiveVectorSlack.runtests()