Skip to content

Commit 32c0b5a

Browse files
committed
Add more modification tests
1 parent bf46749 commit 32c0b5a

File tree

2 files changed

+107
-6
lines changed

2 files changed

+107
-6
lines changed

src/Test/UnitTests/modifications.jl

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ modificationtests["solve_set_scalaraffine_lessthan"] = solve_set_scalaraffine_le
8282
"""
8383
solve_coef_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConfig)
8484
85-
Test modifying set of ScalarAffineFunction-in-LessThan constraint. If
86-
`config.solve=true` confirm that it solves correctly, and if
85+
Test modifying a variable coefficient in a ScalarAffineFunction-in-LessThan
86+
constraint. If `config.solve=true` confirm that it solves correctly, and if
8787
`config.duals=true`, check that the duals are computed correctly.
8888
"""
8989
function solve_coef_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConfig)
@@ -119,13 +119,55 @@ end
119119
modificationtests["solve_coef_scalaraffine_lessthan"] = solve_coef_scalaraffine_lessthan
120120

121121
"""
122-
solve_coef_scalar_objective(model::MOI.ModelLike, config::TestConfig)
122+
solve_const_vectoraffine_nonpos(model::MOI.ModelLike, config::TestConfig)
123123
124-
Test modifying set of ScalarAffineFunction-in-LessThan constraint. If
125-
`config.solve=true` confirm that it solves correctly, and if
124+
Test modifying the constant term in a VectorAffineFunction-in-Nonpositives
125+
constraint. If `config.solve=true` confirm that it solves correctly, and if
126126
`config.duals=true`, check that the duals are computed correctly.
127127
"""
128-
function solve_coef_scalar_objective(model::MOI.ModelLike, config::TestConfig)
128+
function solve_const_vectoraffine_nonpos(model::MOI.ModelLike, config::TestConfig)
129+
MOI.empty!(model)
130+
MOIU.loadfromstring!(model,"""
131+
variables: x, y
132+
maxobjective: 1.0x + 2.0y
133+
""")
134+
x = MOI.get(model, MOI.VariableIndex, "x")
135+
y = MOI.get(model, MOI.VariableIndex, "y")
136+
c = MOI.addconstraint!(model,
137+
MOI.VectorAffineFunction([
138+
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x)),
139+
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(2.0, y))
140+
],
141+
[0.0, 0.0]
142+
),
143+
MOI.Nonpositives(2)
144+
)
145+
if config.solve
146+
test_model_solution(model, config;
147+
objective_value = 0.0,
148+
variable_primal = [(x, 0.0), (y, 0.0)],
149+
constraint_primal = [(c, [0.0, 0.0])]
150+
)
151+
end
152+
@test MOI.canmodify(model, typeof(c), MOI.VectorConstantChange{Float64})
153+
MOI.modify!(model, c, MOI.VectorConstantChange([-1.0, -1.5]))
154+
if config.solve
155+
test_model_solution(model, config;
156+
objective_value = 2.5,
157+
variable_primal = [(x, 1.0), (y, 0.75)],
158+
constraint_primal = [(c, [0.0, 0.0])]
159+
)
160+
end
161+
end
162+
modificationtests["solve_const_vectoraffine_nonpos"] = solve_const_vectoraffine_nonpos
163+
164+
"""
165+
solve_const_scalar_objective(model::MOI.ModelLike, config::TestConfig)
166+
167+
Test the constant of a scalaraffine objective. If `config.solve=true` confirm
168+
that it solves correctly.
169+
"""
170+
function solve_const_scalar_objective(model::MOI.ModelLike, config::TestConfig)
129171
MOI.empty!(model)
130172
MOIU.loadfromstring!(model,"""
131173
variables: x
@@ -154,6 +196,43 @@ function solve_coef_scalar_objective(model::MOI.ModelLike, config::TestConfig)
154196
)
155197
end
156198
end
199+
modificationtests["solve_const_scalar_objective"] = solve_const_scalar_objective
200+
201+
"""
202+
solve_coef_scalar_objective(model::MOI.ModelLike, config::TestConfig)
203+
204+
Test modifying a variable coefficient in a scalaraffine objective. If
205+
`config.solve=true` confirm that it solves correctly.
206+
"""
207+
function solve_coef_scalar_objective(model::MOI.ModelLike, config::TestConfig)
208+
MOI.empty!(model)
209+
MOIU.loadfromstring!(model,"""
210+
variables: x
211+
maxobjective: 1.0x
212+
c1: 1.0x <= 1.0
213+
""")
214+
x = MOI.get(model, MOI.VariableIndex, "x")
215+
if config.solve
216+
test_model_solution(model, config;
217+
objective_value = 1.0,
218+
variable_primal = [(x, 1.0)]
219+
)
220+
end
221+
@test MOI.canmodify(model,
222+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
223+
MOI.ScalarCoefficientChange{Float64}
224+
)
225+
MOI.modify!(model,
226+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
227+
MOI.ScalarCoefficientChange(x, 3.0)
228+
)
229+
if config.solve
230+
test_model_solution(model, config;
231+
objective_value = 3.0,
232+
variable_primal = [(x, 1.0)]
233+
)
234+
end
235+
end
157236
modificationtests["solve_coef_scalar_objective"] = solve_coef_scalar_objective
158237

159238
@moitestset modification

test/Test/unit.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,28 @@ end
226226
)
227227
MOIT.solve_coef_scalaraffine_lessthan(mock, config)
228228
end
229+
@testset "solve_const_vectoraffine_nonpos" begin
230+
MOIU.set_mock_optimize!(mock,
231+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
232+
MOI.Success, (MOI.FeasiblePoint, [0.0, 0.0])
233+
),
234+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
235+
MOI.Success, (MOI.FeasiblePoint, [1.0, 0.75])
236+
)
237+
)
238+
MOIT.solve_const_vectoraffine_nonpos(mock, config)
239+
end
240+
@testset "solve_const_scalar_objective" begin
241+
MOIU.set_mock_optimize!(mock,
242+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
243+
MOI.Success, (MOI.FeasiblePoint, [1.0])
244+
),
245+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
246+
MOI.Success, (MOI.FeasiblePoint, [1.0])
247+
)
248+
)
249+
MOIT.solve_const_scalar_objective(mock, config)
250+
end
229251
@testset "solve_coef_scalar_objective" begin
230252
MOIU.set_mock_optimize!(mock,
231253
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,

0 commit comments

Comments
 (0)