Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #2452 #2464

Merged
merged 10 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1456,8 +1456,8 @@ end
function MOI.get(
b::AbstractBridgeOptimizer,
attr::MOI.ConstraintSet,
ci::MOI.ConstraintIndex{<:MOI.AbstractScalarFunction},
)
ci::MOI.ConstraintIndex{<:MOI.AbstractScalarFunction,S},
) where {S}
set = if is_bridged(b, ci)
MOI.throw_if_not_valid(b, ci)
call_in_context(MOI.get, b, ci, attr)
Expand All @@ -1467,17 +1467,21 @@ function MOI.get(
# This is a scalar function, so if there are variable bridges, it might
# contain constants that have been moved into the set.
if !Variable.has_bridges(Variable.bridges(b))
# If there are no variable bridges, return the set.
return set
elseif !MOI.Utilities.supports_shift_constant(S)
# If it doesn't support shift_constant, then return the set
return set
end
# The function constant of the bridged function was moved to the set,
# we need to remove it.
func = if is_bridged(b, ci)
call_in_context(MOI.get, b, ci, MOI.ConstraintFunction())
else
MOI.get(b.model, MOI.ConstraintFunction(), ci)
end
f = unbridged_function(b, func)
return MOI.Utilities.shift_constant(set, -MOI.constant(f))
# The function constant of the bridged function (if it exists) was moved to
# the set, we need to remove it.
f = MOI.get(b, MOI.ConstraintFunction(), ci)
# f is the "un-bridged" function in terms of the user-variables. We need to
# substitute the variable bridges:
g = bridged_function(b, f)
# g is now the "bridged" function. Add its constant to recover the user's
odow marked this conversation as resolved.
Show resolved Hide resolved
# original set (since the constant was shifted by -constant(g) when adding).
odow marked this conversation as resolved.
Show resolved Hide resolved
return MOI.Utilities.shift_constant(set, MOI.constant(g))
end

## Other constraint attributes
Expand Down
15 changes: 15 additions & 0 deletions src/Test/test_modification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,18 @@ function test_modification_constraint_scalarquadraticcoefficientchange(
@test ≈(MOI.get(model, MOI.ConstraintFunction(), c), g, config)
return
end

function test_modification_mathoptinterface_issue_2452(
model::MOI.ModelLike,
config::Config{T},
) where {T}
F, S = MOI.ScalarAffineFunction{T}, MOI.EqualTo{T}
@requires MOI.supports_constraint(model, F, S)
x, _ = MOI.add_constrained_variable(model, MOI.GreaterThan(T(1)))
c = MOI.add_constraint(model, T(2) * x, MOI.EqualTo(T(3)))
@test ≈(MOI.get(model, MOI.ConstraintFunction(), c), T(2) * x, config)
@test MOI.get(model, MOI.ConstraintSet(), c) == MOI.EqualTo(T(3))
MOI.set(model, MOI.ConstraintSet(), c, MOI.EqualTo(T(2)))
@test MOI.get(model, MOI.ConstraintSet(), c) == MOI.EqualTo(T(2))
return
end
75 changes: 75 additions & 0 deletions test/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,81 @@ function test_cannot_unbridge_variable_function()
return
end

MOI.Utilities.@model(
Model2452,
(),
(),
(MOI.Nonnegatives, MOI.Zeros),
(),
(),
(),
(MOI.VectorOfVariables,),
(MOI.VectorAffineFunction,)
)

function MOI.supports_constraint(
::Model2452{T},
::Type{MOI.VariableIndex},
::Type{
<:Union{
MOI.GreaterThan{T},
MOI.LessThan{T},
MOI.EqualTo{T},
MOI.Interval{T},
MOI.ZeroOne,
},
},
) where {T}
return false
end

function MOI.supports_constraint(
::Model2452{T},
::Type{MOI.VectorOfVariables},
::Type{MOI.Reals},
) where {T}
return false
end

function test_issue_2452()
src = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
x = MOI.add_variable(src)
MOI.add_constraint(src, x, MOI.GreaterThan(1.0))
c = MOI.add_constraint(src, 2.0 * x, MOI.EqualTo(3.0))
dest = MOI.instantiate(Model2452{Float64}; with_bridge_type = Float64)
index_map = MOI.copy_to(dest, src)
set = MOI.get(dest, MOI.ConstraintSet(), index_map[c])
@test set == MOI.EqualTo(3.0)
MOI.set(dest, MOI.ConstraintSet(), index_map[c], set)
@test MOI.get(dest, MOI.ConstraintSet(), index_map[c]) == set
new_set = MOI.EqualTo(2.0)
MOI.set(dest, MOI.ConstraintSet(), index_map[c], new_set)
@test MOI.get(dest, MOI.ConstraintSet(), index_map[c]) == new_set
return
end

function test_issue_2452_with_constant()
src = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
x = MOI.add_variable(src)
MOI.add_constraint(src, x, MOI.GreaterThan(1.0))
MOI.add_constraint(src, 2.0 * x + 1.0, MOI.EqualTo(3.0))
dest = MOI.instantiate(Model2452{Float64}; with_bridge_type = Float64)
@test_throws MOI.ScalarFunctionConstantNotZero MOI.copy_to(dest, src)
return
end

function test_issue_2452_integer()
src = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
x = MOI.add_variable(src)
MOI.add_constraint(src, x, MOI.GreaterThan(1.0))
y = MOI.add_variable(src)
c = MOI.add_constraint(src, 1.0 * y, MOI.Integer())
dest = MOI.instantiate(Model2452{Float64}; with_bridge_type = Float64)
index_map = MOI.copy_to(dest, src)
@test MOI.get(dest, MOI.ConstraintSet(), index_map[c]) == MOI.Integer()
return
end

end # module

TestBridgeOptimizer.runtests()
Loading