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

[Bridges] add ModifyBridgeNotAllowed #2307

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 27 additions & 10 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1154,17 +1154,38 @@ function MOI.set(
return
end

struct ModifyBridgeNotAllowed{C<:MOI.AbstractFunctionModification} <:
MOI.NotAllowedError
change::C
message::String

function ModifyBridgeNotAllowed(
change::MOI.AbstractFunctionModification,
msg::String = "",
)
return new{typeof(change)}(change, msg)
end
end

function MOI.operation_name(err::ModifyBridgeNotAllowed)
return "Modifying the bridge with $(err.change)"
end

function MOI.modify(
::MOI.ModelLike,
::AbstractBridge,
change::MOI.AbstractFunctionModification,
)
return throw(ModifyBridgeNotAllowed(change))
end

function _modify_bridged_function(
b::AbstractBridgeOptimizer,
ci_or_obj,
change::MOI.AbstractFunctionModification,
)
if is_bridged(b, ci_or_obj)
try
MOI.modify(recursive_model(b), bridge(b, ci_or_obj), change)
catch
MOI.throw_modify_not_allowed(ci_or_obj, change)
end
MOI.modify(recursive_model(b), bridge(b, ci_or_obj), change)
else
MOI.modify(b.model, ci_or_obj, change)
end
Expand Down Expand Up @@ -1902,11 +1923,7 @@ function MOI.modify(
modify_bridged_change(b, ci, change)
else
if is_bridged(b, ci)
try
call_in_context(MOI.modify, b, ci, change)
catch
MOI.throw_modify_not_allowed(ci, change)
end
call_in_context(MOI.modify, b, ci, change)
else
MOI.modify(b.model, ci, change)
end
Expand Down
18 changes: 16 additions & 2 deletions test/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,10 @@ function test_modify_objective_scalar_quadratic_coefficient_change()
attr = MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}()
MOI.set(model, attr, T(1) * x * x + T(2) * x + T(3))
change = MOI.ScalarQuadraticCoefficientChange(x, x, T(4))
@test_throws MOI.ModifyObjectiveNotAllowed MOI.modify(model, attr, change)
@test_throws(
MOI.Bridges.ModifyBridgeNotAllowed,
MOI.modify(model, attr, change),
)
return
end

Expand All @@ -1071,7 +1074,18 @@ function test_modify_constraint_scalar_quadratic_coefficient_change()
x = MOI.add_variable(model)
c = MOI.add_constraint(model, T(1) * x * x + T(2) * x, MOI.LessThan(T(1)))
change = MOI.ScalarQuadraticCoefficientChange(x, x, T(4))
@test_throws MOI.ModifyConstraintNotAllowed MOI.modify(model, c, change)
@test_throws(
MOI.Bridges.ModifyBridgeNotAllowed,
MOI.modify(model, c, change),
)
return
end

function test_show_modify_bridge_not_allowed()
x = MOI.VariableIndex(1)
change = MOI.ScalarQuadraticCoefficientChange(x, x, 2.0)
err = MOI.Bridges.ModifyBridgeNotAllowed(change)
@test occursin("cannot be performed", sprint(showerror, err))
return
end

Expand Down
Loading