-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathtest_debug.jl
More file actions
437 lines (395 loc) · 14.5 KB
/
test_debug.jl
File metadata and controls
437 lines (395 loc) · 14.5 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# 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 TestBridgesDebug
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
# This model is chosen because it has a number of properties that need bridging.
MOI.Utilities.@model(
Model,
(),
(),
(MOI.Reals, MOI.Zeros, MOI.Nonnegatives, MOI.RotatedSecondOrderCone),
(),
(),
(),
(MOI.VectorOfVariables,),
(MOI.VectorAffineFunction,)
)
function MOI.supports_constraint(
::Model{T},
::Type{MOI.VariableIndex},
::Type{<:MOI.Utilities.SUPPORTED_VARIABLE_SCALAR_SETS{T}},
) where {T}
return false
end
MOI.supports(::Model, ::MOI.ObjectiveFunction{MOI.VariableIndex}) = false
function MOI.supports(
::Model{T},
::MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}},
) where {T}
return false
end
MOI.supports_add_constrained_variables(::Model, ::Type{MOI.Reals}) = false
MOI.supports_add_constrained_variables(::Model, ::Type{MOI.Nonnegatives}) = true
function test_print_active_bridges()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
xs, _ = MOI.add_constrained_variables(model, MOI.Nonpositives(2))
x = xs[1]
MOI.add_constraint.(model, 1.0 * x, MOI.Interval(1.0, 2.0))
MOI.add_constraint.(model, 1.0 * x, MOI.EqualTo(1.4))
MOI.add_constraint.(model, 1.0 * x * x, MOI.LessThan(2.0))
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
f = 1.0 * x * x
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
content = """
* Unsupported objective: MOI.ScalarQuadraticFunction{Float64}
| bridged by:
| MOIB.Objective.SlackBridge{Float64, MOI.ScalarQuadraticFunction{Float64}, MOI.ScalarQuadraticFunction{Float64}}
| may introduce:
| * Unsupported objective: MOI.VariableIndex
| | bridged by:
| | MOIB.Objective.FunctionConversionBridge{Float64, MOI.ScalarAffineFunction{Float64}, MOI.VariableIndex}
| | may introduce:
| | * Supported objective: MOI.ScalarAffineFunction{Float64}
| * Unsupported constraint: MOI.ScalarQuadraticFunction{Float64}-in-MOI.GreaterThan{Float64}
| | bridged by:
| | MOIB.Constraint.QuadtoSOCBridge{Float64}
| | may introduce:
| | * Supported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.RotatedSecondOrderCone
| * Unsupported constraint: MOI.ScalarQuadraticFunction{Float64}-in-MOI.LessThan{Float64}
| | bridged by:
| | MOIB.Constraint.QuadtoSOCBridge{Float64}
| | may introduce:
| | * Supported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.RotatedSecondOrderCone
| * Unsupported variable: MOI.Reals
| | bridged by:
| | MOIB.Variable.FreeBridge{Float64}
| | may introduce:
| | * Supported variable: MOI.Nonnegatives
* Unsupported constraint: MOI.ScalarAffineFunction{Float64}-in-MOI.EqualTo{Float64}
| bridged by:
| MOIB.Constraint.VectorizeBridge{Float64, MOI.VectorAffineFunction{Float64}, MOI.Zeros, MOI.ScalarAffineFunction{Float64}}
| may introduce:
| * Supported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.Zeros
* Unsupported constraint: MOI.ScalarAffineFunction{Float64}-in-MOI.Interval{Float64}
| bridged by:
| MOIB.Constraint.IntervalToHyperRectangleBridge{Float64, MOI.VectorAffineFunction{Float64}, MOI.ScalarAffineFunction{Float64}}
| may introduce:
| * Unsupported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.HyperRectangle{Float64}
| | bridged by:
| | MOIB.Constraint.SplitHyperRectangleBridge{Float64, MOI.VectorAffineFunction{Float64}, MOI.VectorAffineFunction{Float64}}
| | may introduce:
| | * Supported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.Nonnegatives
* Unsupported constraint: MOI.ScalarQuadraticFunction{Float64}-in-MOI.LessThan{Float64}
| bridged by:
| MOIB.Constraint.QuadtoSOCBridge{Float64}
| may introduce:
| * Supported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.RotatedSecondOrderCone
* Unsupported variable: MOI.Nonpositives
| bridged by:
| MOIB.Variable.NonposToNonnegBridge{Float64}
| may introduce:
| * Supported variable: MOI.Nonnegatives
"""
# Prints to stdout, but just check it doesn't error.
mktemp() do _, io
redirect_stdout(io) do
return MOI.Bridges.print_active_bridges(model)
end
seekstart(io)
@test read(io, String) == content
return
end
@test sprint(MOI.Bridges.print_active_bridges, model) == content
return
end
MOI.Utilities.@model(
ParameterModel,
(),
(MOI.EqualTo,),
(),
(),
(),
(MOI.ScalarAffineFunction,),
(),
()
)
function MOI.supports_constraint(
::ParameterModel,
::Type{MOI.VariableIndex},
::Type{<:MOI.Parameter},
)
return false
end
function test_print_active_bridges_parameter()
inner = ParameterModel{Float64}()
model = MOI.Bridges.full_bridge_optimizer(inner, Float64)
p, _ = MOI.add_constrained_variable(model, MOI.Parameter(2.5))
@test sprint(MOI.Bridges.print_active_bridges, model) === """
* Supported objective: MOI.ScalarAffineFunction{Float64}
* Unsupported variable: MOI.Parameter{Float64}
| bridged by:
| MOIB.Variable.ParameterToEqualToBridge{Float64}
| may introduce:
| * Supported variable: MOI.EqualTo{Float64}
"""
return
end
function test_print_active_bridges_objective_supported()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
F = MOI.ScalarAffineFunction{Float64}
@test sprint(MOI.Bridges.print_active_bridges, model, F) === """
* Supported objective: MOI.ScalarAffineFunction{Float64}
"""
return
end
function test_print_active_bridges_objective_bridged()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
@test sprint(MOI.Bridges.print_active_bridges, model, MOI.VariableIndex) ===
"""
* Unsupported objective: MOI.VariableIndex
| bridged by:
| MOIB.Objective.FunctionConversionBridge{Float64, MOI.ScalarAffineFunction{Float64}, MOI.VariableIndex}
| may introduce:
| * Supported objective: MOI.ScalarAffineFunction{Float64}
"""
return
end
function test_print_active_bridges_objective_unsupported()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
F = MOI.ScalarAffineFunction{Int}
@test_throws(
MOI.UnsupportedAttribute{MOI.ObjectiveFunction{F}},
MOI.Bridges.print_active_bridges(model, F),
)
return
end
function test_print_active_bridges_constraint_supported()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
F = MOI.VectorAffineFunction{Float64}
S = MOI.Nonnegatives
@test sprint(MOI.Bridges.print_active_bridges, model, F, S) === """
* Supported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.Nonnegatives
"""
return
end
function test_print_active_bridges_constraint_bridged()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
F = MOI.VectorAffineFunction{Float64}
S = MOI.Nonpositives
content = """
* Unsupported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.Nonpositives
| bridged by:
| MOIB.Constraint.NonposToNonnegBridge{Float64, MOI.VectorAffineFunction{Float64}, MOI.VectorAffineFunction{Float64}}
| may introduce:
| * Supported constraint: MOI.VectorAffineFunction{Float64}-in-MOI.Nonnegatives
"""
@test sprint(MOI.Bridges.print_active_bridges, model, F, S) === content
return
end
function test_print_active_bridges_constraint_unsupported()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
F = MOI.ScalarAffineFunction{Int}
S = MOI.ZeroOne
@test_throws(
MOI.UnsupportedConstraint{F,S},
MOI.Bridges.print_active_bridges(model, F, S),
)
return
end
function test_print_active_bridges_variable_supported()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
S = MOI.Nonnegatives
@test sprint(MOI.Bridges.print_active_bridges, model, S) === """
* Supported variable: MOI.Nonnegatives
"""
return
end
function test_print_active_bridges_variable_bridged()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
S = MOI.Reals
content = """
* Unsupported variable: MOI.Reals
| bridged by:
| MOIB.Variable.FreeBridge{Float64}
| may introduce:
| * Supported variable: MOI.Nonnegatives
"""
@test sprint(MOI.Bridges.print_active_bridges, model, S) === content
return
end
function test_print_active_bridges_variable_unsupported()
model = MOI.Bridges.full_bridge_optimizer(Model{Float64}(), Float64)
@test_throws(
MOI.UnsupportedConstraint{MOI.VariableIndex,MOI.ZeroOne},
MOI.Bridges.print_active_bridges(model, MOI.ZeroOne),
)
@test_throws(
MOI.UnsupportedConstraint{MOI.VectorOfVariables,MOI.ExponentialCone},
MOI.Bridges.print_active_bridges(model, MOI.ExponentialCone),
)
return
end
MOI.Utilities.@model(
ConstraintModel,
(MOI.ZeroOne, MOI.Integer),
(MOI.GreaterThan, MOI.LessThan, MOI.EqualTo),
(),
(),
(),
(MOI.ScalarAffineFunction,),
(),
()
)
function test_print_active_bridges_variable_bridged_with_constraint()
model =
MOI.Bridges.full_bridge_optimizer(ConstraintModel{Float64}(), Float64)
content = """
* Unsupported variable: MOI.AllDifferent
| adding as constraint:
| * Supported variable: MOI.Reals
| * Unsupported constraint: MOI.VectorOfVariables-in-MOI.AllDifferent
| | bridged by:
| | MOIB.Constraint.AllDifferentToCountDistinctBridge{Float64, MOI.VectorOfVariables}
| | may introduce:
| | * Unsupported constraint: MOI.VectorOfVariables-in-MOI.CountDistinct
| | | bridged by:
| | | MOIB.Constraint.CountDistinctToMILPBridge{Float64, MOI.VectorOfVariables}
| | | may introduce:
| | | * Supported constraint: MOI.ScalarAffineFunction{Float64}-in-MOI.EqualTo{Float64}
| | | * Supported constraint: MOI.ScalarAffineFunction{Float64}-in-MOI.LessThan{Float64}
| | | * Supported variable: MOI.ZeroOne
| | * Supported variable: MOI.EqualTo{Float64}
"""
S = MOI.AllDifferent
@test sprint(MOI.Bridges.print_active_bridges, model, S) === content
return
end
function test_print_graph_stdout()
model = MOI.Utilities.Model{Float64}()
bridged = MOI.Bridges.full_bridge_optimizer(model, Float64)
dir = mktempdir()
filename = joinpath(dir, "tmp.out")
open(filename, "w") do io
redirect_stdout(io) do
MOI.Bridges.print_graph(bridged)
return
end
return
end
@test read(filename, String) ==
"Bridge graph with 0 variable nodes, 0 constraint nodes and 0 objective nodes.\n"
return
end
struct ModelPOIIssue201{T} <: MOI.ModelLike
supports_equal_to::Bool
supports_parameter::Bool
parameter::Vector{T}
equal_to::Vector{T}
function ModelPOIIssue201{T}(;
supports_equal_to::Bool = true,
supports_parameter::Bool = true,
) where {T}
return new{T}(supports_equal_to, supports_parameter, T[], T[])
end
end
function MOI.supports_add_constrained_variable(
model::ModelPOIIssue201{T},
::Type{MOI.Parameter{T}},
) where {T}
return model.supports_parameter
end
function MOI.supports_add_constrained_variable(
model::ModelPOIIssue201{T},
::Type{MOI.EqualTo{T}},
) where {T}
return model.supports_equal_to
end
function MOI.supports_constraint(
model::ModelPOIIssue201{T},
::Type{MOI.ScalarAffineFunction{T}},
::Type{MOI.EqualTo{T}},
) where {T}
return model.supports_equal_to
end
MOI.get(::ModelPOIIssue201, ::MOI.ObjectiveFunctionType) = nothing
function MOI.get(
model::ModelPOIIssue201{T},
::MOI.ListOfConstraintTypesPresent,
) where {T}
ret = Tuple{Type,Type}[]
if !isempty(model.parameter)
push!(ret, (MOI.VariableIndex, MOI.Parameter{T}))
end
if !isempty(model.equal_to)
push!(ret, (MOI.VariableIndex, MOI.EqualTo{T}))
end
return ret
end
function MOI.get(
model::ModelPOIIssue201{T},
::MOI.NumberOfConstraints{MOI.VariableIndex,MOI.Parameter{T}},
) where {T}
return length(model.parameter)
end
function MOI.get(
model::ModelPOIIssue201{T},
::MOI.NumberOfConstraints{MOI.VariableIndex,MOI.EqualTo{T}},
) where {T}
return length(model.equal_to)
end
function MOI.add_constrained_variable(
model::ModelPOIIssue201{T},
set::MOI.Parameter{T},
) where {T}
push!(model.parameter, set.value)
x = MOI.VariableIndex(length(model.parameter))
return x, MOI.ConstraintIndex{MOI.VariableIndex,typeof(set)}(x.value)
end
function MOI.add_constrained_variable(
model::ModelPOIIssue201{T},
set::MOI.EqualTo{T},
) where {T}
push!(model.equal_to, set.value)
x = MOI.VariableIndex(length(model.equal_to))
return x, MOI.ConstraintIndex{MOI.VariableIndex,typeof(set)}(x.value)
end
function test_parametricoptinterface_issue_201_case_1()
model = ModelPOIIssue201{Float64}(; supports_equal_to = false)
model = MOI.Bridges.full_bridge_optimizer(model, Float64)
p, _ = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
@test sprint(MOI.Bridges.print_active_bridges, model) ==
" * Supported variable: MOI.Parameter{Float64}\n"
return
end
function test_parametricoptinterface_issue_201_case_2()
model = ModelPOIIssue201{Float64}(; supports_equal_to = true)
model = MOI.Bridges.full_bridge_optimizer(model, Float64)
p, _ = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
@test sprint(MOI.Bridges.print_active_bridges, model) ==
" * Supported variable: MOI.Parameter{Float64}\n"
return
end
function test_parametricoptinterface_issue_201_case_3()
model = ModelPOIIssue201{Float64}(; supports_parameter = false)
model = MOI.Bridges.full_bridge_optimizer(model, Float64)
p, _ = MOI.add_constrained_variable(model, MOI.Parameter(1.0))
@test sprint(MOI.Bridges.print_active_bridges, model) ==
" * Unsupported variable: MOI.Parameter{Float64}\n | bridged by:\n | MOIB.Variable.ParameterToEqualToBridge{Float64}\n | may introduce:\n | * Supported variable: MOI.EqualTo{Float64}\n"
return
end
end
TestBridgesDebug.runtests()