Skip to content

[Bridges] fix deleting variable in bridged objective #2150

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

Merged
merged 7 commits into from
Apr 25, 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "MathOptInterface"
uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
version = "1.15.0"
version = "1.15.1"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Expand Down
8 changes: 7 additions & 1 deletion docs/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ CurrentModule = MathOptInterface
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v1.15.1 (April 25, 2023)

### Fixed

- Fixed deleting a variable in a bridged objective (#2150)

## v1.15.0 (April 19, 2023)

### Added
Expand All @@ -21,7 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed [`modify`](@ref) in [`Bridges.Objective.VectorSlackBridge`](@ref) (#2144)
- Fixed NAME record with spaces in `FileFormats.MPS` (#2146)
- Fixed deleting a variable in a bridged objective (#2147)

### Other

- Add a test for variables in one-sided open [`Interval`](@ref) sets (#2133)
Expand Down
77 changes: 62 additions & 15 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -545,15 +545,67 @@ function _delete_variables_in_variables_constraints(
end
end

function MOI.delete(b::AbstractBridgeOptimizer, vis::Vector{MOI.VariableIndex})
function _delete_variables_in_bridged_objective(
b::AbstractBridgeOptimizer,
vis::Vector{MOI.VariableIndex},
)
F = MOI.get(b, MOI.ObjectiveFunctionType())
if is_objective_bridged(b) && F == MOI.VectorOfVariables
f = MOI.get(b, MOI.ObjectiveFunction{MOI.VectorOfVariables}())
discard = Base.Fix2(in, vis)
if any(discard, f.variables)
g = MOI.VectorOfVariables(filter(!discard, f.variables))
MOI.set(b, MOI.ObjectiveFunction{MOI.VectorOfVariables}(), g)
end
_delete_variables_in_bridged_objective(b, vis, F)
return
end

function _delete_variables_in_bridged_objective(
b::AbstractBridgeOptimizer,
vis::Vector{MOI.VariableIndex},
::Type{MOI.VectorOfVariables},
)
obj_f = MOI.get(b, MOI.ObjectiveFunction{MOI.VectorOfVariables}())
discard = Base.Fix2(in, vis)
if !any(discard, obj_f.variables)
# There are no variables in the objective function to delete, so we
# don't need to do anything.
return
end
new_obj_f = MOI.VectorOfVariables(filter(!discard, obj_f.variables))
if MOI.output_dimension(new_obj_f) == 0
# We've deleted all variables in the objective. Zero the objective by
# setting FEASIBILITY_SENSE, but restore the sense afterwards.
sense = MOI.get(b, MOI.ObjectiveSense())
MOI.set(b, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
MOI.set(b, MOI.ObjectiveSense(), sense)
else
MOI.set(b, MOI.ObjectiveFunction{MOI.VectorOfVariables}(), new_obj_f)
end
return
end

function _delete_variables_in_bridged_objective(
b::AbstractBridgeOptimizer,
vis::Vector{MOI.VariableIndex},
::Type{MOI.VariableIndex},
)
obj_f = MOI.get(b, MOI.ObjectiveFunction{MOI.VariableIndex}())
if obj_f in vis
sense = MOI.get(b, MOI.ObjectiveSense())
MOI.set(b, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
MOI.set(b, MOI.ObjectiveSense(), sense)
end
return
end

function _delete_variables_in_bridged_objective(
::AbstractBridgeOptimizer,
::Vector{MOI.VariableIndex},
::Type{F},
) where {F}
# Nothing to do here. The variables can be deleted without changing the type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose the objective is a quadratic function that is bridged with Objective.Slack and then Constraint.QuadtoSOC, we should still update the function so that the corresponding SOC constraint is updated.
This can be done in a separate PR though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that happens automatically. This is just a special case where deleting a variable can unset the objective, rather than just modify the existing function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it does. I think the variable deletion API kind of assumes that all bridges are affine so since the deletion commutes with affine operations then it's fine to skip the bridges and delete the solver model directly but there is one notable exception which is the QuadtoSOC bridge. We should maybe check but it's best we open a separate issue

# of the objective, or whether one exists.
return
end

function MOI.delete(b::AbstractBridgeOptimizer, vis::Vector{MOI.VariableIndex})
if is_objective_bridged(b)
_delete_variables_in_bridged_objective(b, vis)
end
if Constraint.has_bridges(Constraint.bridges(b))
_delete_variables_in_variables_constraints(b, vis)
Expand Down Expand Up @@ -584,13 +636,8 @@ function MOI.delete(b::AbstractBridgeOptimizer, vis::Vector{MOI.VariableIndex})
end

function MOI.delete(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
F = MOI.get(b, MOI.ObjectiveFunctionType())
if is_objective_bridged(b) && F == MOI.VectorOfVariables
f = MOI.get(b, MOI.ObjectiveFunction{MOI.VectorOfVariables}())
if any(isequal(vi), f.variables)
g = MOI.VectorOfVariables(filter(!isequal(vi), f.variables))
MOI.set(b, MOI.ObjectiveFunction{MOI.VectorOfVariables}(), g)
end
if is_objective_bridged(b)
_delete_variables_in_bridged_objective(b, [vi])
end
if Constraint.has_bridges(Constraint.bridges(b))
_delete_variables_in_variables_constraints(b, [vi])
Expand Down
59 changes: 59 additions & 0 deletions test/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,65 @@ function test_deleting_variables_in_bridged_objective()
return
end

function test_deleting_all_variables_in_bridged_objective()
# We need to make sure that the bridges have the same functionality as the
# base Utilities.Model.
model_1 = MOI.Utilities.Model{Float64}()
model_2 = MOI.Bridges.Objective.VectorFunctionize{Float64}(
MOI.Utilities.Model{Float64}(),
)
for model in (model_1, model_2)
x = MOI.add_variable(model)
f = MOI.VectorOfVariables([x])
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.delete(model, x)
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test length(attrs) == 1
@test attrs[1] == MOI.ObjectiveSense()
end
return
end

function test_deleting_all_vector_variables_in_bridged_objective()
# We need to make sure that the bridges have the same functionality as the
# base Utilities.Model.
model_1 = MOI.Utilities.Model{Float64}()
model_2 = MOI.Bridges.Objective.VectorFunctionize{Float64}(
MOI.Utilities.Model{Float64}(),
)
for model in (model_1, model_2)
x = MOI.add_variable(model)
f = MOI.VectorOfVariables([x])
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.delete(model, [x])
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test length(attrs) == 1
@test attrs[1] == MOI.ObjectiveSense()
end
return
end

function test_deleting_all_variables_in_bridged_functionize_objective()
# We need to make sure that the bridges have the same functionality as the
# base Utilities.Model.
model_1 = MOI.Utilities.Model{Float64}()
model_2 = MOI.Bridges.Objective.Functionize{Float64}(
MOI.Utilities.Model{Float64}(),
)
for model in (model_1, model_2)
x = MOI.add_variable(model)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(x)}(), x)
MOI.delete(model, x)
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
@test length(attrs) == 1
@test MOI.ObjectiveSense() in attrs
end
return
end

end # module

TestBridgeOptimizer.runtests()