-
Notifications
You must be signed in to change notification settings - Fork 92
[Bridges] add ExponentialConeToScalarNonlinearFunctionBridge #2587
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
160bcdf
[Bridges] add ExponentialConeToScalarNonlinearFunctionBridge
odow ac93de6
Fix formatting
odow 2793b6b
Update src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinea…
odow daad7bd
Update src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinea…
odow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
src/Bridges/Constraint/bridges/ExponentialConeToScalarNonlinearFunctionBridge.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
# 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 ExponentialConeToScalarNonlinearFunction{T,OT<:MOI.ModelLike} = | ||
SingleBridgeOptimizer{ExponentialConeToScalarNonlinearFunctionBridge{T},OT} | ||
|
||
function bridge_constraint( | ||
::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( | ||
:-, | ||
Any[MOI.ScalarNonlinearFunction(:*, Any[y, g_exp_x_div_y]), z], | ||
) | ||
ci = MOI.add_constraint(model, g, MOI.LessThan(zero(T))) | ||
# We add this as a constraint to avoid conflicting with existing variable | ||
# bounds, of which there can be at most one of. | ||
ci_y = MOI.add_constraint(model, one(T) * y, MOI.GreaterThan(zero(T))) | ||
odow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ExponentialConeToScalarNonlinearFunctionBridge{T,F}(f, ci, ci_y) | ||
end | ||
|
||
function MOI.supports_constraint( | ||
::Type{<:ExponentialConeToScalarNonlinearFunctionBridge{T}}, | ||
::Type{<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}}, | ||
::Type{MOI.ExponentialCone}, | ||
) where {T} | ||
return true | ||
end | ||
|
||
function MOI.Bridges.added_constrained_variable_types( | ||
::Type{ExponentialConeToScalarNonlinearFunctionBridge{T,F}}, | ||
) where {T,F} | ||
return Tuple{Type}[] | ||
end | ||
|
||
function MOI.Bridges.added_constraint_types( | ||
::Type{ExponentialConeToScalarNonlinearFunctionBridge{T,F}}, | ||
) where {T,F} | ||
return Tuple{Type,Type}[ | ||
(MOI.ScalarNonlinearFunction, MOI.LessThan{T}), | ||
(MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}), | ||
] | ||
end | ||
|
||
function concrete_bridge_type( | ||
::Type{<:ExponentialConeToScalarNonlinearFunctionBridge{T}}, | ||
::Type{F}, | ||
::Type{MOI.ExponentialCone}, | ||
) where {T,F<:Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{T}}} | ||
return ExponentialConeToScalarNonlinearFunctionBridge{T,F} | ||
end | ||
|
||
function MOI.get( | ||
::MOI.ModelLike, | ||
::MOI.ConstraintFunction, | ||
bridge::ExponentialConeToScalarNonlinearFunctionBridge, | ||
) | ||
return copy(bridge.f) | ||
end | ||
|
||
function MOI.get( | ||
::MOI.ModelLike, | ||
::MOI.ConstraintSet, | ||
::ExponentialConeToScalarNonlinearFunctionBridge, | ||
) | ||
return MOI.ExponentialCone() | ||
end | ||
|
||
function MOI.delete( | ||
model::MOI.ModelLike, | ||
bridge::ExponentialConeToScalarNonlinearFunctionBridge, | ||
) | ||
MOI.delete(model, bridge.ci) | ||
MOI.delete(model, bridge.ci_y) | ||
return | ||
end | ||
|
||
function MOI.get( | ||
::ExponentialConeToScalarNonlinearFunctionBridge, | ||
::MOI.NumberOfVariables, | ||
)::Int64 | ||
return 0 | ||
end | ||
|
||
function MOI.get( | ||
::ExponentialConeToScalarNonlinearFunctionBridge, | ||
::MOI.ListOfVariableIndices, | ||
)::Vector{MOI.VariableIndex} | ||
return MOI.VariableIndex[] | ||
end | ||
|
||
function MOI.get( | ||
::ExponentialConeToScalarNonlinearFunctionBridge{T}, | ||
::MOI.NumberOfConstraints{MOI.ScalarNonlinearFunction,MOI.LessThan{T}}, | ||
)::Int64 where {T} | ||
return 1 | ||
end | ||
|
||
function MOI.get( | ||
bridge::ExponentialConeToScalarNonlinearFunctionBridge{T}, | ||
::MOI.ListOfConstraintIndices{MOI.ScalarNonlinearFunction,MOI.LessThan{T}}, | ||
) where {T} | ||
return [bridge.ci] | ||
end | ||
|
||
function MOI.get( | ||
::ExponentialConeToScalarNonlinearFunctionBridge{T}, | ||
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}}, | ||
)::Int64 where {T} | ||
return 1 | ||
end | ||
|
||
function MOI.get( | ||
bridge::ExponentialConeToScalarNonlinearFunctionBridge{T}, | ||
::MOI.ListOfConstraintIndices{ | ||
MOI.ScalarAffineFunction{T}, | ||
MOI.GreaterThan{T}, | ||
}, | ||
) where {T} | ||
return [bridge.ci_y] | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
test/Bridges/Constraint/ExponentialConeToScalarNonlinearFunctionBridge.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.