Skip to content

Commit b8fb3bf

Browse files
committed
Add test for modifying constraint function. There is a broken test
and one commented out as MockOptimizer fails getting the constraint function.
1 parent 32c0b5a commit b8fb3bf

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/Test/UnitTests/modifications.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,50 @@ function solve_coef_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConf
118118
end
119119
modificationtests["solve_coef_scalaraffine_lessthan"] = solve_coef_scalaraffine_lessthan
120120

121+
"""
122+
solve_func_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConfig)
123+
124+
Test setting the function in a ScalarAffineFunction-in-LessThan
125+
constraint. If `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_func_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConfig)
129+
MOI.empty!(model)
130+
MOIU.loadfromstring!(model,"""
131+
variables: x
132+
maxobjective: 1.0x
133+
""")
134+
x = MOI.get(model, MOI.VariableIndex, "x")
135+
c = MOI.addconstraint!(model,
136+
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0),
137+
MOI.LessThan(1.0)
138+
)
139+
if config.solve
140+
test_model_solution(model, config;
141+
objective_value = 1.0,
142+
variable_primal = [(x, 1.0)],
143+
constraint_primal = [(c, 1.0)],
144+
constraint_dual = [(c, -1.0)]
145+
)
146+
end
147+
@test MOI.canset(model, MOI.ConstraintFunction(), typeof(c))
148+
MOI.set!(model, MOI.ConstraintFunction(), c,
149+
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(2.0, x)], 0.0)
150+
)
151+
@test MOI.canget(model, MOI.ConstraintFunction(), typeof(c))
152+
foo = MOI.get(model, MOI.ConstraintFunction(), c)
153+
@test_broken foo == MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(2.0, x)], 0.0)
154+
if config.solve
155+
test_model_solution(model, config;
156+
objective_value = 0.5,
157+
variable_primal = [(x, 0.5)],
158+
# constraint_primal = [(c, 1.0)],
159+
constraint_dual = [(c, -0.5)]
160+
)
161+
end
162+
end
163+
modificationtests["solve_func_scalaraffine_lessthan"] = solve_func_scalaraffine_lessthan
164+
121165
"""
122166
solve_const_vectoraffine_nonpos(model::MOI.ModelLike, config::TestConfig)
123167

test/Test/unit.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,21 @@ end
226226
)
227227
MOIT.solve_coef_scalaraffine_lessthan(mock, config)
228228
end
229+
@testset "solve_func_scalaraffine_lessthan" begin
230+
MOIU.set_mock_optimize!(mock,
231+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
232+
MOI.Success, (MOI.FeasiblePoint, [1.0]),
233+
MOI.FeasiblePoint,
234+
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) => [-1.0]
235+
),
236+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,
237+
MOI.Success, (MOI.FeasiblePoint, [0.5]),
238+
MOI.FeasiblePoint,
239+
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) => [-0.5]
240+
)
241+
)
242+
MOIT.solve_func_scalaraffine_lessthan(mock, config)
243+
end
229244
@testset "solve_const_vectoraffine_nonpos" begin
230245
MOIU.set_mock_optimize!(mock,
231246
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock,

0 commit comments

Comments
 (0)