Skip to content

Commit 0c25804

Browse files
committed
Add unit tests for setting ConstraintSet
1 parent 136b03e commit 0c25804

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

src/Test/UnitTests/modifications.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const modificationtests = Dict{String, Function}()
2+
3+
"""
4+
solve_modify_variable_bound(model::MOI.ModelLike, config::TestConfig)
5+
6+
Test set modification SingleVariable-in-LessThan constraint. If
7+
`config.solve=true` confirm that it solves correctly, and if
8+
`config.duals=true`, check that the duals are computed correctly.
9+
"""
10+
function solve_modify_variable_bound(model::MOI.ModelLike, config::TestConfig)
11+
MOI.empty!(model)
12+
MOIU.loadfromstring!(model,"""
13+
variables: x
14+
maxobjective: 1.0x
15+
""")
16+
x = MOI.get(model, MOI.VariableIndex, "x")
17+
c = MOI.addconstraint!(model, MOI.SingleVariable(x), MOI.LessThan(1.0))
18+
if config.solve
19+
test_model_solution(model, config;
20+
objective_value = 1.0,
21+
variable_primal = [(x, 1.0)],
22+
constraint_primal = [(c, 1.0)],
23+
constraint_dual = [(c, -1.0)]
24+
)
25+
end
26+
@test MOI.canset(model, MOI.ConstraintSet(), typeof(c))
27+
MOI.set!(model, MOI.ConstraintSet(), c, MOI.LessThan(2.0))
28+
@test MOI.canget(model, MOI.ConstraintSet(), typeof(c))
29+
@test MOI.get(model, MOI.ConstraintSet(), c) == MOI.LessThan(2.0)
30+
if config.solve
31+
test_model_solution(model, config;
32+
objective_value = 2.0,
33+
variable_primal = [(x, 2.0)],
34+
constraint_primal = [(c, 2.0)],
35+
constraint_dual = [(c, -1.0)]
36+
)
37+
end
38+
end
39+
modificationtests["solve_modify_variable_bound"] = solve_modify_variable_bound
40+
41+
"""
42+
solve_modify_rhs(model::MOI.ModelLike, config::TestConfig)
43+
44+
Test modifying set of ScalarAffineFunction-in-LessThan constraint. If
45+
`config.solve=true` confirm that it solves correctly, and if
46+
`config.duals=true`, check that the duals are computed correctly.
47+
"""
48+
function solve_modify_rhs(model::MOI.ModelLike, config::TestConfig)
49+
MOI.empty!(model)
50+
MOIU.loadfromstring!(model,"""
51+
variables: x
52+
maxobjective: 1.0x
53+
""")
54+
x = MOI.get(model, MOI.VariableIndex, "x")
55+
c = MOI.addconstraint!(model,
56+
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0),
57+
MOI.LessThan(1.0)
58+
)
59+
if config.solve
60+
test_model_solution(model, config;
61+
objective_value = 1.0,
62+
variable_primal = [(x, 1.0)],
63+
constraint_primal = [(c, 1.0)],
64+
constraint_dual = [(c, -1.0)]
65+
)
66+
end
67+
@test MOI.canset(model, MOI.ConstraintSet(), typeof(c))
68+
MOI.set!(model, MOI.ConstraintSet(), c, MOI.LessThan(2.0))
69+
@test MOI.canget(model, MOI.ConstraintSet(), typeof(c))
70+
@test MOI.get(model, MOI.ConstraintSet(), c) == MOI.LessThan(2.0)
71+
if config.solve
72+
test_model_solution(model, config;
73+
objective_value = 2.0,
74+
variable_primal = [(x, 2.0)],
75+
constraint_primal = [(c, 2.0)],
76+
constraint_dual = [(c, -1.0)]
77+
)
78+
end
79+
end
80+
modificationtests["solve_modify_rhs"] = solve_modify_rhs
81+
82+
@moitestset modification

src/Test/UnitTests/unit_tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ include("variables.jl")
9696
include("objectives.jl")
9797
include("constraints.jl")
9898
include("basic_constraint_tests.jl")
99-
99+
include("modifications.jl")
100100

101101
@moitestset unit

test/Test/unit.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,38 @@ end
177177
MOIT.solve_affine_deletion_edge_cases(mock, config)
178178
end
179179
end
180+
181+
@testset "modifications" begin
182+
mock = MOIU.MockOptimizer(Model{Float64}())
183+
config = MOIT.TestConfig()
184+
@testset "solve_modify_variable_bound" begin
185+
MOIU.set_mock_optimize!(mock,
186+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
187+
MOI.Success, (MOI.FeasiblePoint, [1.0]),
188+
MOI.FeasiblePoint,
189+
(MOI.SingleVariable, MOI.LessThan{Float64}) => [-1.0]
190+
),
191+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
192+
MOI.Success, (MOI.FeasiblePoint, [2.0]),
193+
MOI.FeasiblePoint,
194+
(MOI.SingleVariable, MOI.LessThan{Float64}) => [-1.0]
195+
)
196+
)
197+
MOIT.solve_modify_variable_bound(mock, config)
198+
end
199+
@testset "solve_modify_rhs" begin
200+
MOIU.set_mock_optimize!(mock,
201+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
202+
MOI.Success, (MOI.FeasiblePoint, [1.0]),
203+
MOI.FeasiblePoint,
204+
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) => [-1.0]
205+
),
206+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
207+
MOI.Success, (MOI.FeasiblePoint, [2.0]),
208+
MOI.FeasiblePoint,
209+
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) => [-1.0]
210+
)
211+
)
212+
MOIT.solve_modify_rhs(mock, config)
213+
end
214+
end

0 commit comments

Comments
 (0)