Skip to content

Commit 8097841

Browse files
committed
Add more tests for modifications
1 parent db7b687 commit 8097841

File tree

2 files changed

+113
-10
lines changed

2 files changed

+113
-10
lines changed

src/Test/UnitTests/modifications.jl

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const modificationtests = Dict{String, Function}()
22

33
"""
4-
solve_modify_variable_bound(model::MOI.ModelLike, config::TestConfig)
4+
solve_set_singlevariable_lessthan(model::MOI.ModelLike, config::TestConfig)
55
66
Test set modification SingleVariable-in-LessThan constraint. If
77
`config.solve=true` confirm that it solves correctly, and if
88
`config.duals=true`, check that the duals are computed correctly.
99
"""
10-
function solve_modify_variable_bound(model::MOI.ModelLike, config::TestConfig)
10+
function solve_set_singlevariable_lessthan(model::MOI.ModelLike, config::TestConfig)
1111
MOI.empty!(model)
1212
MOIU.loadfromstring!(model,"""
1313
variables: x
@@ -36,16 +36,16 @@ function solve_modify_variable_bound(model::MOI.ModelLike, config::TestConfig)
3636
)
3737
end
3838
end
39-
modificationtests["solve_modify_variable_bound"] = solve_modify_variable_bound
39+
modificationtests["solve_set_singlevariable_lessthan"] = solve_set_singlevariable_lessthan
4040

4141
"""
42-
solve_modify_rhs(model::MOI.ModelLike, config::TestConfig)
42+
solve_set_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConfig)
4343
4444
Test modifying set of ScalarAffineFunction-in-LessThan constraint. If
4545
`config.solve=true` confirm that it solves correctly, and if
4646
`config.duals=true`, check that the duals are computed correctly.
4747
"""
48-
function solve_modify_rhs(model::MOI.ModelLike, config::TestConfig)
48+
function solve_set_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConfig)
4949
MOI.empty!(model)
5050
MOIU.loadfromstring!(model,"""
5151
variables: x
@@ -77,6 +77,83 @@ function solve_modify_rhs(model::MOI.ModelLike, config::TestConfig)
7777
)
7878
end
7979
end
80-
modificationtests["solve_modify_rhs"] = solve_modify_rhs
80+
modificationtests["solve_set_scalaraffine_lessthan"] = solve_set_scalaraffine_lessthan
81+
82+
"""
83+
solve_coef_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConfig)
84+
85+
Test modifying set of ScalarAffineFunction-in-LessThan constraint. If
86+
`config.solve=true` confirm that it solves correctly, and if
87+
`config.duals=true`, check that the duals are computed correctly.
88+
"""
89+
function solve_coef_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConfig)
90+
MOI.empty!(model)
91+
MOIU.loadfromstring!(model,"""
92+
variables: x
93+
maxobjective: 1.0x
94+
""")
95+
x = MOI.get(model, MOI.VariableIndex, "x")
96+
c = MOI.addconstraint!(model,
97+
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0),
98+
MOI.LessThan(1.0)
99+
)
100+
if config.solve
101+
test_model_solution(model, config;
102+
objective_value = 1.0,
103+
variable_primal = [(x, 1.0)],
104+
constraint_primal = [(c, 1.0)],
105+
constraint_dual = [(c, -1.0)]
106+
)
107+
end
108+
@test MOI.canmodify(model, typeof(c), MOI.ScalarCoefficientChange{Float64})
109+
MOI.modify!(model, c, MOI.ScalarCoefficientChange(x, 2.0))
110+
if config.solve
111+
test_model_solution(model, config;
112+
objective_value = 0.5,
113+
variable_primal = [(x, 0.5)],
114+
constraint_primal = [(c, 1.0)],
115+
constraint_dual = [(c, -0.5)]
116+
)
117+
end
118+
end
119+
modificationtests["solve_coef_scalaraffine_lessthan"] = solve_coef_scalaraffine_lessthan
120+
121+
"""
122+
solve_coef_scalar_objective(model::MOI.ModelLike, config::TestConfig)
123+
124+
Test modifying set of ScalarAffineFunction-in-LessThan constraint. If
125+
`config.solve=true` confirm that it solves correctly, and if
126+
`config.duals=true`, check that the duals are computed correctly.
127+
"""
128+
function solve_coef_scalar_objective(model::MOI.ModelLike, config::TestConfig)
129+
MOI.empty!(model)
130+
MOIU.loadfromstring!(model,"""
131+
variables: x
132+
maxobjective: 1.0x + 2.0
133+
c1: 1.0x <= 1.0
134+
""")
135+
x = MOI.get(model, MOI.VariableIndex, "x")
136+
if config.solve
137+
test_model_solution(model, config;
138+
objective_value = 3.0,
139+
variable_primal = [(x, 1.0)]
140+
)
141+
end
142+
@test MOI.canmodify(model,
143+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
144+
MOI.ScalarConstantChange{Float64}
145+
)
146+
MOI.modify!(model,
147+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
148+
MOI.ScalarConstantChange(3.0)
149+
)
150+
if config.solve
151+
test_model_solution(model, config;
152+
objective_value = 4.0,
153+
variable_primal = [(x, 1.0)]
154+
)
155+
end
156+
end
157+
modificationtests["solve_coef_scalar_objective"] = solve_coef_scalar_objective
81158

82159
@moitestset modification

test/Test/unit.jl

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ end
181181
@testset "modifications" begin
182182
mock = MOIU.MockOptimizer(Model{Float64}())
183183
config = MOIT.TestConfig()
184-
@testset "solve_modify_variable_bound" begin
184+
@testset "solve_set_singlevariable_lessthan" begin
185185
MOIU.set_mock_optimize!(mock,
186186
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
187187
MOI.Success, (MOI.FeasiblePoint, [1.0]),
@@ -194,9 +194,9 @@ end
194194
(MOI.SingleVariable, MOI.LessThan{Float64}) => [-1.0]
195195
)
196196
)
197-
MOIT.solve_modify_variable_bound(mock, config)
197+
MOIT.solve_set_singlevariable_lessthan(mock, config)
198198
end
199-
@testset "solve_modify_rhs" begin
199+
@testset "solve_set_scalaraffine_lessthan" begin
200200
MOIU.set_mock_optimize!(mock,
201201
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
202202
MOI.Success, (MOI.FeasiblePoint, [1.0]),
@@ -209,6 +209,32 @@ end
209209
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) => [-1.0]
210210
)
211211
)
212-
MOIT.solve_modify_rhs(mock, config)
212+
MOIT.solve_set_scalaraffine_lessthan(mock, config)
213+
end
214+
@testset "solve_coef_scalaraffine_lessthan" begin
215+
MOIU.set_mock_optimize!(mock,
216+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
217+
MOI.Success, (MOI.FeasiblePoint, [1.0]),
218+
MOI.FeasiblePoint,
219+
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) => [-1.0]
220+
),
221+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
222+
MOI.Success, (MOI.FeasiblePoint, [0.5]),
223+
MOI.FeasiblePoint,
224+
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) => [-0.5]
225+
)
226+
)
227+
MOIT.solve_coef_scalaraffine_lessthan(mock, config)
228+
end
229+
@testset "solve_coef_scalar_objective" begin
230+
MOIU.set_mock_optimize!(mock,
231+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
232+
MOI.Success, (MOI.FeasiblePoint, [1.0])
233+
),
234+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
235+
MOI.Success, (MOI.FeasiblePoint, [1.0])
236+
)
237+
)
238+
MOIT.solve_coef_scalar_objective(mock, config)
213239
end
214240
end

0 commit comments

Comments
 (0)