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

Add Objective.FunctionConversionBridge #2303

Merged
merged 7 commits into from
Oct 9, 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
3 changes: 2 additions & 1 deletion docs/src/submodules/Bridges/list_of_bridges.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ These bridges are subtypes of [`Bridges.Objective.AbstractBridge`](@ref).
```@docs
Bridges.Objective.FunctionizeBridge
Bridges.Objective.QuadratizeBridge
Bridges.Objective.SlackBridge
Bridges.Objective.VectorFunctionizeBridge
Bridges.Objective.FunctionConversionBridge
Bridges.Objective.SlackBridge
Bridges.Objective.VectorSlackBridge
```

Expand Down
4 changes: 1 addition & 3 deletions src/Bridges/Objective/Objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ include("bridge.jl")
include("map.jl")
include("single_bridge_optimizer.jl")

include("bridges/functionize.jl")
include("bridges/quadratize.jl")
include("bridges/conversion.jl")
include("bridges/slack.jl")
include("bridges/vector_functionize.jl")
include("bridges/vector_slack.jl")

"""
Expand Down
196 changes: 196 additions & 0 deletions src/Bridges/Objective/bridges/conversion.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

"""
FunctionConversionBridge{T,F,G} <: AbstractBridge
odow marked this conversation as resolved.
Show resolved Hide resolved

`FunctionConversionBridge` implements the following reformulations:

* ``\\min \\{g(x)\\}`` into ``\\min\\{f(x)\\}``
* ``\\max \\{g(x)\\}`` into ``\\max\\{f(x)\\}``

for these pairs of functions:

* [`MOI.ScalarAffineFunction`](@ref)` to [`MOI.ScalarQuadraticFunction`](@ref)
* [`MOI.ScalarQuadraticFunction`](@ref) to [`MOI.ScalarNonlinearFunction`](@ref)
* [`MOI.VectorAffineFunction`](@ref) to [`MOI.VectorQuadraticFunction`](@ref)

## Source node

`FunctionConversionBridge` supports:

* [`MOI.ObjectiveFunction{G}`](@ref)

## Target nodes

`FunctionConversionBridge` creates:

* One objective node: [`MOI.ObjectiveFunction{F}`](@ref)
"""
struct FunctionConversionBridge{T,F,G} <: AbstractBridge end

function bridge_objective(
::Type{FunctionConversionBridge{T,F,G}},
model::MOI.ModelLike,
func::G,
) where {T,F,G<:MOI.AbstractFunction}
MOI.set(model, MOI.ObjectiveFunction{F}(), convert(F, func))
return FunctionConversionBridge{T,F,G}()
end

function supports_objective_function(
::Type{<:FunctionConversionBridge{T,F}},
::Type{G},
) where {T,F,G<:MOI.AbstractFunction}
return isfinite(MOI.Bridges.Constraint.conversion_cost(F, G))
end

function MOI.Bridges.added_constrained_variable_types(
::Type{<:FunctionConversionBridge},
)
return Tuple{Type}[]
end

function MOI.Bridges.added_constraint_types(::Type{<:FunctionConversionBridge})
return Tuple{Type,Type}[]
end

function MOI.Bridges.set_objective_function_type(
::Type{<:FunctionConversionBridge{T,F}},
) where {T,F}
return F
end

function concrete_bridge_type(
::Type{<:FunctionConversionBridge{T,F}},
::Type{G},
) where {T,F,G<:MOI.AbstractFunction}
return FunctionConversionBridge{T,F,G}
end

# Attributes, Bridge acting as a model
MOI.get(::FunctionConversionBridge, ::MOI.NumberOfVariables)::Int64 = 0

function MOI.get(::FunctionConversionBridge, ::MOI.ListOfVariableIndices)
return MOI.VariableIndex[]
end

# No variables or constraints are created in this bridge so there is nothing to
# delete.
MOI.delete(::MOI.ModelLike, ::FunctionConversionBridge) = nothing

function MOI.set(
::MOI.ModelLike,
::MOI.ObjectiveSense,
::FunctionConversionBridge,
::MOI.OptimizationSense,
)
# `FunctionConversionBridge` is sense agnostic, therefore, we don't need to
# change anything.
return
end

function MOI.get(
model::MOI.ModelLike,
attr::MOI.Bridges.ObjectiveFunctionValue{G},
::FunctionConversionBridge{T,F,G},
) where {T,F,G}
attr_f = MOI.Bridges.ObjectiveFunctionValue{F}(attr.result_index)
return MOI.get(model, attr_f)
end

function MOI.get(
model::MOI.ModelLike,
::MOI.ObjectiveFunction{G},
::FunctionConversionBridge{T,F,G},
) where {T,F,G<:MOI.AbstractFunction}
func = MOI.get(model, MOI.ObjectiveFunction{F}())
return MOI.Utilities.convert_approx(G, func)
end

"""
FunctionizeBridge{T,G} <: FunctionConversionBridge{T,MOI.ScalarAffineFunction{T},G}

`FunctionizeBridge` implements the following reformulations:

* ``\\min \\{x\\}`` into ``\\min\\{1x + 0\\}``
* ``\\max \\{x\\}`` into ``\\max\\{1x + 0\\}``

where `T` is the coefficient type of `1` and `0`.

## Source node

`FunctionizeBridge` supports:

* [`MOI.ObjectiveFunction{G}`](@ref)

## Target nodes

`FunctionizeBridge` creates:

* One objective node: [`MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}`](@ref)
"""
const FunctionizeBridge{T,G} =
FunctionConversionBridge{T,MOI.ScalarAffineFunction{T},G}

const Functionize{T,OT<:MOI.ModelLike} =
SingleBridgeOptimizer{FunctionizeBridge{T},OT}

"""
QuadratizeBridge{T,G} <: FunctionConversionBridge{T,MOI.ScalarQuadraticFunction{T},G}

`QuadratizeBridge` implements the following reformulations:

* ``\\min \\{a^\\top x + b\\}`` into ``\\min\\{x^\\top \\mathbf{0} x + a^\\top x + b\\}``
* ``\\max \\{a^\\top x + b\\}`` into ``\\max\\{x^\\top \\mathbf{0} x + a^\\top x + b\\}``

where `T` is the coefficient type of `0`.

## Source node

`QuadratizeBridge` supports:

* [`MOI.ObjectiveFunction{G}`](@ref)

## Target nodes

`QuadratizeBridge` creates:

* One objective node: [`MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}`](@ref)
"""
const QuadratizeBridge{T,G} =
FunctionConversionBridge{T,MOI.ScalarQuadraticFunction{T},G}

const Quadratize{T,OT<:MOI.ModelLike} =
SingleBridgeOptimizer{QuadratizeBridge{T},OT}

"""
VectorFunctionizeBridge{T,G} <: FunctionConversionBridge{T,MOI.VectorAffineFunction{T},G}

`VectorFunctionizeBridge` implements the following reformulations:

* ``\\min \\{x\\}`` into ``\\min\\{1x + 0\\}``
* ``\\max \\{x\\}`` into ``\\max\\{1x + 0\\}``

where `T` is the coefficient type of `1` and `0`.

## Source node

`VectorFunctionizeBridge` supports:

* [`MOI.ObjectiveFunction{G}`](@ref)

## Target nodes

`VectorFunctionizeBridge` creates:

* One objective node: [`MOI.ObjectiveFunction{MOI.VectorAffineFunction{T}}`](@ref)
"""
const VectorFunctionizeBridge{T,G} =
FunctionConversionBridge{T,MOI.VectorAffineFunction{T},G}

const VectorFunctionize{T,OT<:MOI.ModelLike} =
SingleBridgeOptimizer{VectorFunctionizeBridge{T},OT}
107 changes: 0 additions & 107 deletions src/Bridges/Objective/bridges/functionize.jl

This file was deleted.

Loading
Loading