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 ExponentialConeToScalarNonlinearFunctionBridge #2587

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions src/Bridges/Constraint/Constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ function add_all_bridges(bridged_model, ::Type{T}) where {T}
MOI.Bridges.add_bridge(bridged_model, SOS1ToMILPBridge{T})
MOI.Bridges.add_bridge(bridged_model, SOS2ToMILPBridge{T})
MOI.Bridges.add_bridge(bridged_model, IndicatorToMILPBridge{T})

MOI.Bridges.add_bridge(
bridged_model,
ExponentialConeToScalarNonlinearFunctionBridge{T},
)
return
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# 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.

"""
ExponentialConeToScalarNonlinearFunctionBridge{T,F} <:
Bridges.Constraint.AbstractBridge

`ExponentialConeToScalarNonlinearFunctionBridge` implements the following
reformulation:

* ``(x, y, z) \\in \\textsf{ExponentialCone}()`` to
``y \\cdot exp(x / y)) - z \\le 0``, ``y \\ge 0``.

## Source node

`ExponentialConeToScalarNonlinearFunctionBridge` supports:

* `F` in [`MOI.ExponentialCone`](@ref)

## Target nodes

`ExponentialConeToScalarNonlinearFunctionBridge` creates:

* [`MOI.ScalarNonlinearFunction`](@ref) in [`MOI.LessThan{T}`](@ref)
* [`MOI.ScalarAffineFunction`](@ref) in [`MOI.GreaterThan{T}`](@ref)
"""
mutable struct ExponentialConeToScalarNonlinearFunctionBridge{
T,
F<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}},
} <: AbstractBridge
f::F
ci::MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,MOI.LessThan{T}}
ci_y::MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}}
end

const ExponentialToScalarNonlinearFunction{T,OT<:MOI.ModelLike} =
odow marked this conversation as resolved.
Show resolved Hide resolved
SingleBridgeOptimizer{ExponentialConeToScalarNonlinearFunctionBridge{T},OT}

function bridge_constraint(

Check warning on line 42 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L42

Added line #L42 was not covered by tests
::Type{ExponentialConeToScalarNonlinearFunctionBridge{T,F}},
model::MOI.ModelLike,
f::F,
s::MOI.ExponentialCone,
) where {T,F<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}}
x, y, z = MOI.Utilities.scalarize(f)
g_x_div_y = MOI.ScalarNonlinearFunction(:/, Any[x, y])
g_exp_x_div_y = MOI.ScalarNonlinearFunction(:exp, Any[g_x_div_y])
g = MOI.ScalarNonlinearFunction(

Check warning on line 51 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L48-L51

Added lines #L48 - L51 were not covered by tests
:-,
Any[MOI.ScalarNonlinearFunction(:*, Any[y, g_exp_x_div_y]), z],
)
ci = MOI.add_constraint(model, g, MOI.LessThan(zero(T)))
ci_y = MOI.add_constraint(model, one(T) * y, MOI.GreaterThan(zero(T)))
odow marked this conversation as resolved.
Show resolved Hide resolved
odow marked this conversation as resolved.
Show resolved Hide resolved
return ExponentialConeToScalarNonlinearFunctionBridge{T,F}(f, ci, ci_y)

Check warning on line 57 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L55-L57

Added lines #L55 - L57 were not covered by tests
end

function MOI.supports_constraint(

Check warning on line 60 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L60

Added line #L60 was not covered by tests
::Type{<:ExponentialConeToScalarNonlinearFunctionBridge{T}},
::Type{<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}},
::Type{MOI.ExponentialCone},
) where {T}
return true

Check warning on line 65 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L65

Added line #L65 was not covered by tests
end

function MOI.Bridges.added_constrained_variable_types(

Check warning on line 68 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L68

Added line #L68 was not covered by tests
::Type{ExponentialConeToScalarNonlinearFunctionBridge{T,F}},
) where {T,F}
return Tuple{Type}[]

Check warning on line 71 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L71

Added line #L71 was not covered by tests
end

function MOI.Bridges.added_constraint_types(

Check warning on line 74 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L74

Added line #L74 was not covered by tests
::Type{ExponentialConeToScalarNonlinearFunctionBridge{T,F}},
) where {T,F}
return Tuple{Type,Type}[

Check warning on line 77 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L77

Added line #L77 was not covered by tests
(MOI.ScalarNonlinearFunction, MOI.LessThan{T}),
(MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}),
]
end

function concrete_bridge_type(

Check warning on line 83 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L83

Added line #L83 was not covered by tests
::Type{<:ExponentialConeToScalarNonlinearFunctionBridge{T}},
::Type{F},
::Type{MOI.ExponentialCone},
) where {T,F<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}}
return ExponentialConeToScalarNonlinearFunctionBridge{T,F}

Check warning on line 88 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L88

Added line #L88 was not covered by tests
end

function MOI.get(

Check warning on line 91 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L91

Added line #L91 was not covered by tests
::MOI.ModelLike,
::MOI.ConstraintFunction,
bridge::ExponentialConeToScalarNonlinearFunctionBridge,
)
return copy(bridge.f)

Check warning on line 96 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L96

Added line #L96 was not covered by tests
end

function MOI.get(

Check warning on line 99 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L99

Added line #L99 was not covered by tests
::MOI.ModelLike,
::MOI.ConstraintSet,
::ExponentialConeToScalarNonlinearFunctionBridge,
)
return MOI.ExponentialCone()

Check warning on line 104 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L104

Added line #L104 was not covered by tests
end

function MOI.delete(

Check warning on line 107 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L107

Added line #L107 was not covered by tests
model::MOI.ModelLike,
bridge::ExponentialConeToScalarNonlinearFunctionBridge,
)
MOI.delete(model, bridge.ci)
MOI.delete(model, bridge.ci_y)
return

Check warning on line 113 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L111-L113

Added lines #L111 - L113 were not covered by tests
end

function MOI.get(

Check warning on line 116 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L116

Added line #L116 was not covered by tests
::ExponentialConeToScalarNonlinearFunctionBridge,
::MOI.NumberOfVariables,
)::Int64
return 0

Check warning on line 120 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L120

Added line #L120 was not covered by tests
end

function MOI.get(

Check warning on line 123 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L123

Added line #L123 was not covered by tests
::ExponentialConeToScalarNonlinearFunctionBridge,
::MOI.ListOfVariableIndices,
)::Vector{MOI.VariableIndex}
return MOI.VariableIndex[]

Check warning on line 127 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L127

Added line #L127 was not covered by tests
end

function MOI.get(

Check warning on line 130 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L130

Added line #L130 was not covered by tests
::ExponentialConeToScalarNonlinearFunctionBridge{T},
::MOI.NumberOfConstraints{MOI.ScalarNonlinearFunction,MOI.LessThan{T}},
)::Int64 where {T}
return 1

Check warning on line 134 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L134

Added line #L134 was not covered by tests
end

function MOI.get(

Check warning on line 137 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L137

Added line #L137 was not covered by tests
bridge::ExponentialConeToScalarNonlinearFunctionBridge{T},
::MOI.ListOfConstraintIndices{MOI.ScalarNonlinearFunction,MOI.LessThan{T}},
) where {T}
return [bridge.ci]

Check warning on line 141 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L141

Added line #L141 was not covered by tests
end

function MOI.get(

Check warning on line 144 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L144

Added line #L144 was not covered by tests
::ExponentialConeToScalarNonlinearFunctionBridge{T},
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}},
)::Int64 where {T}
return 1

Check warning on line 148 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L148

Added line #L148 was not covered by tests
end

function MOI.get(

Check warning on line 151 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L151

Added line #L151 was not covered by tests
bridge::ExponentialConeToScalarNonlinearFunctionBridge{T},
::MOI.ListOfConstraintIndices{
MOI.ScalarAffineFunction{T},
MOI.GreaterThan{T},
},
) where {T}
return [bridge.ci_y]

Check warning on line 158 in src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl#L158

Added line #L158 was not covered by tests
end
2 changes: 2 additions & 0 deletions src/Utilities/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@
return _parsed_scalar_to_moi(model, s.args[2])
elseif Meta.isexpr(s, :call, 2) && s.args[1] == :VectorNonlinearFunction
return _parsed_vector_to_moi(model, s.args[2])
elseif Meta.isexpr(s, :call, 2) && s.args[1] == :esc
return _parsed_to_moi(model, _parse_function(s.args[2], Float64))

Check warning on line 248 in src/Utilities/parser.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/parser.jl#L248

Added line #L248 was not covered by tests
end
args = Any[_parsed_to_moi(model, arg) for arg in s.args[2:end]]
return MOI.ScalarNonlinearFunction(s.args[1], args)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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 TestConstraintExponentialConeToScalarNonlinearFunctionBridge

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_VectorOfVariables()
MOI.Bridges.runtests(
MOI.Bridges.Constraint.ExponentialConeToScalarNonlinearFunctionBridge,
"""
variables: x, y, z
[x, y, z] in ExponentialCone()
""",
"""
variables: x, y, z
ScalarNonlinearFunction(y * exp(x / y) - z) <= 0.0
1.0 * y >= 0.0
""",
)
return
end

function test_runtests_VectorAffineFunction()
MOI.Bridges.runtests(
MOI.Bridges.Constraint.ExponentialConeToScalarNonlinearFunctionBridge,
"""
variables: x, y, z
[1.0 * x, 2.0 * y, 3.0 * z + 1.0] in ExponentialCone()
""",
"""
variables: x, y, z
ScalarNonlinearFunction(esc(2.0 * y) * exp(esc(1.0 * x) / esc(2.0 * y)) - esc(3.0 * z + 1.0)) <= 0.0
2.0 * y >= 0.0
""",
)
return
end

end # module

TestConstraintExponentialConeToScalarNonlinearFunctionBridge.runtests()
Loading