Skip to content

Commit 0b5cd4d

Browse files
authored
Merge pull request #1093 from martinbiel/scalarize_bugfix
Bugfix for set ConstraintFunction in ScalarizeBridge.
2 parents 43e4e1a + 097c25b commit 0b5cd4d

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/Bridges/Constraint/scalarize.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ function MOI.modify(model::MOI.ModelLike, bridge::ScalarizeBridge,
122122
nothing
123123
end
124124
function MOI.set(model::MOI.ModelLike, ::MOI.ConstraintFunction,
125-
bridge::ScalarizeBridge{T}, func) where T
125+
bridge::ScalarizeBridge{T,F,S}, func) where {T,F,S}
126126
old_constants = bridge.constants
127127
bridge.constants = MOI.constant(func, T)
128128
new_func = MOIU.scalarize(func, true)
129129
MOI.set.(model, MOI.ConstraintFunction(), bridge.scalar_constraints,
130130
new_func)
131131
for i in eachindex(bridge.constants)
132132
if bridge.constants[i] != old_constants[i]
133-
MOI.set(model, MOI.ConstraintSet(), bridge.scalar_constraints,
133+
MOI.set(model, MOI.ConstraintSet(), bridge.scalar_constraints[i],
134134
S(-bridge.constants[i]))
135135
end
136136
end

src/Test/UnitTests/modifications.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,51 @@ function solve_func_scalaraffine_lessthan(model::MOI.ModelLike, config::TestConf
196196
end
197197
modificationtests["solve_func_scalaraffine_lessthan"] = solve_func_scalaraffine_lessthan
198198

199+
"""
200+
solve_func_vectoraffine_nonneg(model::MOI.ModelLike, config::TestConfig)
201+
202+
Test setting the function in a VectorAffineFunction-in-Nonnegatives
203+
constraint. If `config.solve=true` confirm that it solves correctly, and if
204+
`config.duals=true`, check that the duals are computed correctly.
205+
"""
206+
function solve_func_vectoraffine_nonneg(model::MOI.ModelLike, config::TestConfig)
207+
MOI.empty!(model)
208+
MOIU.loadfromstring!(model,"""
209+
variables: x, y
210+
minobjective: 1.0x + 2.0y
211+
c: [1.0x, 2.0y] in Nonnegatives(2)
212+
""")
213+
x = MOI.get(model, MOI.VariableIndex, "x")
214+
y = MOI.get(model, MOI.VariableIndex, "y")
215+
c = MOI.get(model, MOI.ConstraintIndex, "c")
216+
test_model_solution(model, config;
217+
objective_value = 0.0,
218+
variable_primal = [(x, 0.0), (y, 0.0)],
219+
constraint_primal = [(c, [0.0, 0.0])]
220+
)
221+
MOI.set(model, MOI.ConstraintFunction(), c,
222+
MOI.VectorAffineFunction([
223+
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x)),
224+
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(2.0, y))
225+
],
226+
[-1.0, -1.5]
227+
)
228+
)
229+
foo = MOI.get(model, MOI.ConstraintFunction(), c)
230+
@test foo MOI.VectorAffineFunction([
231+
MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x)),
232+
MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(2.0, y))
233+
],
234+
[-1.0, -1.5]
235+
)
236+
test_model_solution(model, config;
237+
objective_value = 2.5,
238+
variable_primal = [(x, 1.0), (y, 0.75)],
239+
constraint_primal = [(c, [0.0, 0.0])]
240+
)
241+
end
242+
modificationtests["solve_func_vectoraffine_nonneg"] = solve_func_vectoraffine_nonneg
243+
199244
"""
200245
solve_const_vectoraffine_nonpos(model::MOI.ModelLike, config::TestConfig)
201246

test/Bridges/Constraint/scalarize.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ config = MOIT.TestConfig()
8181
((MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}, 0),
8282
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}, 0)))
8383

84+
# Test setting VectorAffineFunction with nonzero constants
85+
MOIU.set_mock_optimize!(mock,
86+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [0.0, 0.0]),
87+
(mock::MOIU.MockOptimizer) -> MOIU.mock_optimize!(mock, [1.0, 0.75]))
88+
MOIT.solve_func_vectoraffine_nonneg(bridged_mock, config)
89+
8490
# VectorOfVariables-in-Nonnegatives
8591
# VectorOfVariables-in-Nonpositives
8692
# VectorOfVariables-in-Zeros

0 commit comments

Comments
 (0)