Skip to content

Commit ee597f1

Browse files
committed
Test errors
1 parent d88fa72 commit ee597f1

File tree

4 files changed

+77
-33
lines changed

4 files changed

+77
-33
lines changed

src/Bridges/bridge_optimizer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,9 @@ function MOI.add_constraint(b::AbstractBridgeOptimizer, f::MOI.AbstractFunction,
632632
# The index of the contraint will have positive value hence
633633
# it would clash with the index space of `b.model` since
634634
# the constraint type is normally not bridged.
635-
error("Cannot `VectorOfVariables`-in-`$(typeof(s))`for",
635+
error("Cannot `VectorOfVariables`-in-`$(typeof(s))` for",
636636
" which some variables are bridged but not the",
637-
" first one $(first(f.variables)).")
637+
" first one `$(first(f.variables))`.")
638638
end
639639
BridgeType = Constraint.concrete_bridge_type(
640640
Constraint.VectorFunctionizeBridge{Float64}, typeof(f), typeof(s))

test/Bridges/Variable/free.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ config = MOIT.TestConfig()
1313

1414
bridged_mock = MOIB.Variable.Free{Float64}(mock)
1515

16+
@testset "solve_multirow_vectoraffine_nonpos" begin
17+
MOIU.set_mock_optimize!(mock,
18+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
19+
MOI.OPTIMAL, (MOI.FEASIBLE_POINT, [0.5, 0.0])
20+
),
21+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
22+
MOI.OPTIMAL, (MOI.FEASIBLE_POINT, [0.25, 0.0])
23+
)
24+
)
25+
MOIT.solve_multirow_vectoraffine_nonpos(bridged_mock, config)
26+
end
27+
1628
@testset "Linear6" begin
1729
MOIU.set_mock_optimize!(mock,
1830
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [0, 0, 0, 0]),

test/Bridges/Variable/vectorize.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ vis = MOI.get(bridged_mock, MOI.ListOfVariableIndices())
2222
@test length(vis) == 2
2323
y = vis[2]
2424

25+
err = ErrorException(
26+
"Cannot add two `SingleVariable`-in-`MathOptInterface.LessThan{Float64}`" *
27+
" on the same variable MathOptInterface.VariableIndex(-1)."
28+
)
29+
@test_throws err MOI.add_constraint(bridged_mock, MOI.SingleVariable(y), MOI.LessThan(4.0))
30+
2531
cis = MOI.get(bridged_mock, MOI.ListOfConstraintIndices{
2632
MOI.VectorAffineFunction{Float64}, MOI.ExponentialCone}())
2733
@test length(cis) == 1

test/Bridges/Variable/zeros.jl

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,37 @@ config = MOIT.TestConfig()
1313

1414
bridged_mock = MOIB.Variable.Zeros{Float64}(mock)
1515

16-
MOIU.set_mock_optimize!(mock,
17-
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(
18-
mock, [1.0],
19-
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) => 0.0,
20-
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) => 1.0)
21-
)
22-
2316
x, cx = MOI.add_constrained_variable(bridged_mock, MOI.GreaterThan(0.0))
2417
yz, cyz = MOI.add_constrained_variables(bridged_mock, MOI.Zeros(2))
2518
y, z = yz
2619
fx = MOI.SingleVariable(x)
2720
fy = MOI.SingleVariable(y)
2821
fz = MOI.SingleVariable(z)
29-
c1 = MOI.add_constraint(bridged_mock, 1.0fy + 1.0fz, MOI.EqualTo(0.0))
30-
c2 = MOI.add_constraint(bridged_mock, 1.0fx + 1.0fy + 1.0fz, MOI.GreaterThan(1.0))
22+
c1, c2 = MOI.add_constraints(
23+
bridged_mock, [1.0fy + 1.0fz, 1.0fx + 1.0fy + 1.0fz],
24+
[MOI.EqualTo(0.0), MOI.GreaterThan(1.0)]
25+
)
26+
#c2 = MOI.add_constraint(bridged_mock, , )
3127
MOI.set(bridged_mock, MOI.ObjectiveSense(), MOI.MIN_SENSE)
3228
obj = 1.0fx - 1.0fy - 1.0fz
3329
MOI.set(bridged_mock, MOI.ObjectiveFunction{typeof(obj)}(), obj)
3430

3531
@test MOIB.Variable.unbridged_map(MOIB.bridge(bridged_mock, y), y, MOIB.Variable.IndexInVector(1)) === nothing
3632
@test MOIB.Variable.unbridged_map(MOIB.bridge(bridged_mock, z), z, MOIB.Variable.IndexInVector(2)) === nothing
3733

34+
err = ErrorException(
35+
"Cannot add two `VectorOfVariables`-in-`MathOptInterface.Zeros` on the" *
36+
" same first variable MathOptInterface.VariableIndex(-1)."
37+
)
38+
@test_throws err MOI.add_constraint(bridged_mock, MOI.VectorOfVariables(yz), MOI.Zeros(2))
39+
40+
err = ErrorException(
41+
"Cannot `VectorOfVariables`-in-`MathOptInterface.Zeros` for" *
42+
" which some variables are bridged but not the first one" *
43+
" `MathOptInterface.VariableIndex(12345679)`."
44+
)
45+
@test_throws err MOI.add_constraint(bridged_mock, MOI.VectorOfVariables([x, y]), MOI.Zeros(2))
46+
3847
err = ErrorException(
3948
"Cannot unbridge function because some variables are bridged by" *
4049
" variable bridges that do not support reverse mapping, e.g.," *
@@ -52,28 +61,45 @@ err = ArgumentError(
5261
)
5362
@test_throws err MOI.get(bridged_mock, MOIT.UnknownVariableAttribute(), y)
5463

55-
MOI.optimize!(bridged_mock)
56-
@test MOI.get(bridged_mock, MOI.VariablePrimal(), x) == 1.0
57-
@test MOI.get(bridged_mock, MOI.VariablePrimal(), y) == 0.0
58-
@test MOI.get(bridged_mock, MOI.VariablePrimal(), z) == 0.0
64+
@testset "Results" begin
65+
MOIU.set_mock_optimize!(mock,
66+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(
67+
mock, [1.0],
68+
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) => 0.0,
69+
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) => 1.0)
70+
)
71+
MOI.optimize!(bridged_mock)
72+
@test MOI.get(bridged_mock, MOI.VariablePrimal(), x) == 1.0
73+
@test MOI.get(bridged_mock, MOI.VariablePrimal(), y) == 0.0
74+
@test MOI.get(bridged_mock, MOI.VariablePrimal(), z) == 0.0
5975

60-
@test MOI.get(bridged_mock, MOI.ConstraintPrimal(), cyz) == zeros(2)
76+
@test MOI.get(bridged_mock, MOI.ConstraintPrimal(), cyz) == zeros(2)
6177

62-
@test MOI.get(bridged_mock, MOI.ConstraintDual(), cx) == 0.0
63-
@test MOI.get(bridged_mock, MOI.ConstraintDual(), c1) == 0.0
64-
@test MOI.get(bridged_mock, MOI.ConstraintDual(), c2) == 1.0
78+
@test MOI.get(bridged_mock, MOI.ConstraintDual(), cx) == 0.0
79+
@test MOI.get(bridged_mock, MOI.ConstraintDual(), c1) == 0.0
80+
@test MOI.get(bridged_mock, MOI.ConstraintDual(), c2) == 1.0
6581

66-
err = ArgumentError(
67-
"Bridge of type `MathOptInterface.Bridges.Variable.ZerosBridge{Float64}`" *
68-
" does not support accessing the attribute" *
69-
" `MathOptInterface.ConstraintDual(1)`."
70-
)
71-
@test_throws err MOI.get(bridged_mock, MOI.ConstraintDual(), cyz)
72-
73-
@test MOI.get(mock, MOI.NumberOfVariables()) == 1
74-
@test MOI.get(mock, MOI.ListOfVariableIndices()) == [x]
75-
@test MOI.get(bridged_mock, MOI.NumberOfVariables()) == 3
76-
@test MOI.get(bridged_mock, MOI.ListOfVariableIndices()) == [x, y, z]
77-
@test MOI.get(mock, MOI.NumberOfConstraints{MOI.VectorOfVariables, MOI.Zeros}()) == 0
78-
@test MOI.get(bridged_mock, MOI.NumberOfConstraints{MOI.VectorOfVariables, MOI.Zeros}()) == 1
79-
@test MOI.get(bridged_mock, MOI.ListOfConstraintIndices{MOI.VectorOfVariables, MOI.Zeros}()) == [cyz]
82+
err = ArgumentError(
83+
"Bridge of type `MathOptInterface.Bridges.Variable.ZerosBridge{Float64}`" *
84+
" does not support accessing the attribute" *
85+
" `MathOptInterface.ConstraintDual(1)`."
86+
)
87+
@test_throws err MOI.get(bridged_mock, MOI.ConstraintDual(), cyz)
88+
end
89+
90+
@testset "Query" begin
91+
@test MOI.get(mock, MOI.NumberOfVariables()) == 1
92+
@test MOI.get(mock, MOI.ListOfVariableIndices()) == [x]
93+
@test MOI.get(bridged_mock, MOI.NumberOfVariables()) == 3
94+
@test MOI.get(bridged_mock, MOI.ListOfVariableIndices()) == [x, y, z]
95+
@test MOI.get(mock, MOI.NumberOfConstraints{MOI.VectorOfVariables, MOI.Zeros}()) == 0
96+
@test MOI.get(bridged_mock, MOI.NumberOfConstraints{MOI.VectorOfVariables, MOI.Zeros}()) == 1
97+
@test MOI.get(bridged_mock, MOI.ListOfConstraintIndices{MOI.VectorOfVariables, MOI.Zeros}()) == [cyz]
98+
end
99+
100+
@testset "SingleVariable objective" begin
101+
err = ErrorException("Using bridged variable in `SingleVariable` function.")
102+
@test_throws err MOI.set(bridged_mock, MOI.ObjectiveFunction{typeof(fy)}(), fy)
103+
MOI.set(bridged_mock, MOI.ObjectiveFunction{typeof(fx)}(), fx)
104+
@test MOI.get(bridged_mock, MOI.ObjectiveFunction{typeof(fx)}()) == fx
105+
end

0 commit comments

Comments
 (0)