Skip to content

Commit

Permalink
[Bridges] add ModifyBridgeNotAllowed (#2307)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Oct 11, 2023
1 parent 0cfa93f commit eb0d3c1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
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

0 comments on commit eb0d3c1

Please sign in to comment.