Skip to content

Commit 9c478de

Browse files
committed
[Bridges] improve test coverage
1 parent 0c9ba0f commit 9c478de

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

test/Bridges/bridge_optimizer.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,96 @@ function test_issue_2452_integer()
12861286
return
12871287
end
12881288

1289+
struct EmptyBridgeOptimizer <: MOI.Bridges.AbstractBridgeOptimizer end
1290+
1291+
function test_supports_bridging_constrained_variable_fallback()
1292+
@test !MOI.Bridges.supports_bridging_constrained_variable(
1293+
EmptyBridgeOptimizer(),
1294+
MOI.ZeroOne,
1295+
)
1296+
return
1297+
end
1298+
1299+
function test_supports_bridging_constraint_fallback()
1300+
@test !MOI.Bridges.supports_bridging_constraint(
1301+
EmptyBridgeOptimizer(),
1302+
MOI.ScalarAffineFunction{Float64},
1303+
MOI.EqualTo{Float64},
1304+
)
1305+
return
1306+
end
1307+
1308+
function test_delete_variable_from_bridged_objective()
1309+
model = MOI.Utilities.Model{Float64}()
1310+
b = MOI.Bridges.Objective.VectorFunctionize{Float64}(model)
1311+
x = MOI.add_variables(b, 2)
1312+
y = MOI.add_variable(b)
1313+
f = MOI.VectorOfVariables(x)
1314+
MOI.set(b, MOI.ObjectiveSense(), MOI.MIN_SENSE)
1315+
MOI.set(b, MOI.ObjectiveFunction{typeof(f)}(), f)
1316+
G = MOI.VectorAffineFunction{Float64}
1317+
g = MOI.get(model, MOI.ObjectiveFunction{G}())
1318+
MOI.delete(b, y)
1319+
@test g MOI.get(model, MOI.ObjectiveFunction{G}())
1320+
MOI.delete(b, x[1])
1321+
@test (
1322+
MOI.Utilities.vectorize([MOI.Utilities.scalarize(g)[2]]),
1323+
MOI.get(model, MOI.ObjectiveFunction{G}()),
1324+
)
1325+
MOI.delete(b, x[2])
1326+
@test (
1327+
MOI.Utilities.vectorize([zero(MOI.ScalarAffineFunction{Float64})]),
1328+
MOI.get(model, MOI.ObjectiveFunction{G}()),
1329+
)
1330+
return
1331+
end
1332+
1333+
function test_get_ObjectiveFunctionValue()
1334+
model = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
1335+
b = MOI.Bridges.full_bridge_optimizer(model, Float64)
1336+
x = MOI.add_variable(b)
1337+
MOI.set(b, MOI.ObjectiveSense(), MOI.MIN_SENSE)
1338+
MOI.set(b, MOI.ObjectiveFunction{MOI.VariableIndex}(), x)
1339+
y = only(MOI.get(model, MOI.ListOfVariableIndices()))
1340+
MOI.set(model, MOI.VariablePrimal(1), y, 1.23)
1341+
@test MOI.get(b, MOI.Bridges.ObjectiveFunctionValue{typeof(x)}(1)) == 1.23
1342+
return
1343+
end
1344+
1345+
function test_bridged_function()
1346+
model = MOI.Utilities.Model{Float64}()
1347+
b = MOI.Bridges.Variable.Free{Float64}(model)
1348+
x = MOI.add_variable(b)
1349+
y, _ = MOI.add_constrained_variable(b, MOI.GreaterThan(1.0))
1350+
@test_throws(
1351+
ErrorException("Using bridged variable in `VariableIndex` function."),
1352+
MOI.Bridges.bridged_function(b, x),
1353+
)
1354+
@test MOI.Bridges.bridged_function(b, y) == y
1355+
return
1356+
end
1357+
1358+
function test_bridged_constraint_function_no_variable_bridges()
1359+
model = MOI.Utilities.Model{Float64}()
1360+
bridged = MOI.Bridges.full_bridge_optimizer(model, Float64)
1361+
x = MOI.add_variable(bridged)
1362+
set = MOI.ZeroOne()
1363+
@test MOI.Bridges.bridged_constraint_function(bridged, x, set) == (x, set)
1364+
return
1365+
end
1366+
1367+
function test_bridged_constraint_function_with_variable_bridges()
1368+
model = MOI.Utilities.Model{Float64}()
1369+
b = MOI.Bridges.Variable.Free{Float64}(model)
1370+
x = MOI.add_variable(b)
1371+
set = MOI.ZeroOne()
1372+
f, set = MOI.Bridges.bridged_constraint_function(b, 1.0 * x, set)
1373+
@test set == MOI.ZeroOne()
1374+
y = MOI.get(model, MOI.ListOfVariableIndices())
1375+
@test f 1.0 * y[1] - 1.0 * y[2]
1376+
return
1377+
end
1378+
12891379
end # module
12901380

12911381
TestBridgeOptimizer.runtests()

test/Bridges/debug.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,23 @@ function test_print_active_bridges_variable_bridged_with_constraint()
314314
return
315315
end
316316

317+
function test_print_graph_stdout()
318+
model = MOI.Utilities.Model{Float64}()
319+
bridged = MOI.Bridges.full_bridge_optimizer(model, Float64)
320+
dir = mktempdir()
321+
filename = joinpath(dir, "tmp.out")
322+
open(filename, "w") do io
323+
redirect_stdout(io) do
324+
print("Abc")
325+
return
326+
end
327+
return
328+
end
329+
@test read(filename, String) ==
330+
"Bridge graph with 0 variable nodes, 0 constraint nodes and 0 objective nodes."
331+
return
332+
end
333+
317334
end
318335

319336
TestBridgesDebug.runtests()

0 commit comments

Comments
 (0)