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 Objective.VectorFunctionizeBridge #2139

Merged
merged 4 commits into from
Apr 16, 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
1 change: 1 addition & 0 deletions docs/src/submodules/Bridges/list_of_bridges.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ These bridges are subtypes of [`Bridges.Objective.AbstractBridge`](@ref).
Bridges.Objective.FunctionizeBridge
Bridges.Objective.QuadratizeBridge
Bridges.Objective.SlackBridge
Bridges.Objective.VectorFunctionizeBridge
Bridges.Objective.VectorSlackBridge
```

Expand Down
2 changes: 2 additions & 0 deletions src/Bridges/Objective/Objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ include("single_bridge_optimizer.jl")
include("bridges/functionize.jl")
include("bridges/quadratize.jl")
include("bridges/slack.jl")
include("bridges/vector_functionize.jl")
include("bridges/vector_slack.jl")

"""
Expand All @@ -28,6 +29,7 @@ function add_all_bridges(model, ::Type{T}) where {T}
MOI.Bridges.add_bridge(model, FunctionizeBridge{T})
MOI.Bridges.add_bridge(model, QuadratizeBridge{T})
MOI.Bridges.add_bridge(model, SlackBridge{T})
MOI.Bridges.add_bridge(model, VectorFunctionizeBridge{T})
MOI.Bridges.add_bridge(model, VectorSlackBridge{T})
return
end
Expand Down
103 changes: 103 additions & 0 deletions src/Bridges/Objective/bridges/vector_functionize.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# 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.

"""
VectorFunctionizeBridge{T}

`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{MOI.VectorOfVariables}`](@ref)

## Target nodes

`VectorFunctionizeBridge` creates:

* One objective node: [`MOI.ObjectiveFunction{MOI.VectorAffineFunction{T}}`](@ref)
"""
struct VectorFunctionizeBridge{T} <: AbstractBridge end

const VectorFunctionize{T,OT<:MOI.ModelLike} =
SingleBridgeOptimizer{VectorFunctionizeBridge{T},OT}

function bridge_objective(
::Type{VectorFunctionizeBridge{T}},
model::MOI.ModelLike,
f::MOI.VectorOfVariables,
) where {T}
F = MOI.VectorAffineFunction{T}
MOI.set(model, MOI.ObjectiveFunction{F}(), convert(F, f))
return VectorFunctionizeBridge{T}()
end

function supports_objective_function(
::Type{<:VectorFunctionizeBridge},
::Type{MOI.VectorOfVariables},
)
return true
end

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

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

function MOI.Bridges.set_objective_function_type(
::Type{VectorFunctionizeBridge{T}},
) where {T}
return MOI.VectorAffineFunction{T}
end

MOI.get(::VectorFunctionizeBridge, ::MOI.NumberOfVariables)::Int64 = 0

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

MOI.delete(::MOI.ModelLike, ::VectorFunctionizeBridge) = nothing

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

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

function MOI.get(
model::MOI.ModelLike,
::MOI.ObjectiveFunction{MOI.VectorOfVariables},
::VectorFunctionizeBridge{T},
) where {T}
f = MOI.get(model, MOI.ObjectiveFunction{MOI.VectorAffineFunction{T}}())
return convert(MOI.VectorOfVariables, f)
end
31 changes: 31 additions & 0 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,26 @@ function Base.convert(::Type{VectorOfVariables}, g::VariableIndex)
return VectorOfVariables([g])
end

function Base.convert(::Type{VectorOfVariables}, f::VectorAffineFunction)
variables = Vector{VariableIndex}(undef, length(f.constants))
assigned = fill(false, length(variables))
if any(!iszero, f.constants)
throw(InexactError(:convert, VectorOfVariables, f))
end
for term in f.terms
if assigned[term.output_index] || !isone(term.scalar_term.coefficient)
throw(InexactError(:convert, VectorOfVariables, f))
end
x = convert(VariableIndex, term.scalar_term.variable)
variables[term.output_index] = x
assigned[term.output_index] = true
end
if !all(assigned)
throw(InexactError(:convert, VectorOfVariables, f))
end
return VectorOfVariables(variables)
end

# VectorAffineFunction

function Base.convert(
Expand Down Expand Up @@ -846,6 +866,17 @@ function Base.convert(
return VectorAffineFunction{T}(f.terms, f.constants)
end

function Base.convert(
::Type{VectorAffineFunction{T}},
f::VectorOfVariables,
) where {T}
terms = VectorAffineTerm{T}[
VectorAffineTerm{T}(i, ScalarAffineTerm{T}(one(T), x)) for
(i, x) in enumerate(f.variables)
]
return VectorAffineFunction{T}(terms, zeros(T, length(terms)))
end

# VectorQuadraticFunction

function Base.convert(
Expand Down
91 changes: 91 additions & 0 deletions test/Bridges/Objective/vector_functionize.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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.

module TestObjectiveVectorFunctionize

using Test

import MathOptInterface as MOI

function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end

function test_runtests()
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorFunctionizeBridge,
"""
variables: x, y
minobjective: [x, y]
""",
"""
variables: x, y
minobjective: [1.0 * x + 0.0, 1.0 * y + 0.0]
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Objective.VectorFunctionizeBridge,
"""
variables: x, y
maxobjective: [x, y]
""",
"""
variables: x, y
maxobjective: [1.0 * x + 0.0, 1.0 * y + 0.0]
""",
)
return
end

function test_objective_function_value()
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
model = MOI.Bridges.Objective.VectorFunctionize{Float64}(inner)
MOI.Utilities.loadfromstring!(
model,
"""
variables: x, y
minobjective: [x, y]
""",
)
MOI.Utilities.set_mock_optimize!(
inner,
mock -> MOI.Utilities.mock_optimize!(mock, [3.0, 5.6]),
)
MOI.optimize!(model)
@test MOI.get(model, MOI.ObjectiveValue()) ≈ [3.0, 5.6]
return
end

function test_set_objective_sense()
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
model = MOI.Bridges.Objective.VectorFunctionize{Float64}(inner)
MOI.Utilities.loadfromstring!(
model,
"""
variables: x, y
minobjective: [x, y]
""",
)
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.MIN_SENSE
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.MAX_SENSE
return
end

end # module

TestObjectiveVectorFunctionize.runtests()
19 changes: 19 additions & 0 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,25 @@ function test_isapprox_issue_1483()
return
end

function test_convert_vectorofvariables()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
f = MOI.VectorOfVariables([x, y])
g = MOI.Utilities.operate(vcat, Float64, 1.0 * x, 1.0 * y)
@test convert(MOI.VectorOfVariables, g) == f
for g in (
MOI.Utilities.operate(vcat, Float64, 1.2 * x, 1.0 * y),
MOI.Utilities.operate(vcat, Float64, 1.0 * y, 1.2 * x),
MOI.Utilities.operate(vcat, Float64, 1.0 * x, 0.0),
MOI.Utilities.operate(vcat, Float64, 0.0, 1.0 * x),
MOI.Utilities.operate(vcat, Float64, 1.0 * x, 1.0 * y + 1.0),
MOI.Utilities.operate(vcat, Float64, 1.0 * x + 1.0, 1.0 * y),
)
@test_throws InexactError convert(MOI.VectorOfVariables, g)
end
return
end

function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
Expand Down