-
Notifications
You must be signed in to change notification settings - Fork 92
Add Bridges.Constraint.ComplexNormInfinityToSecondOrderConeBridge #2451
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 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8781063
WIP: add bridge for Complex NormInfinityCone
odow 8eebab3
Update
odow a4f9954
Fix format
odow aaf93f7
Update
odow 72a3efc
Update lazy_bridge_optimizer.jl
odow d8346c6
Add test
odow 98f3fa9
Update test/Bridges/Constraint/complex_norm_infinity.jl
odow 79e7435
Merge branch 'master' into od/complex-infinity-bridge
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
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
152 changes: 152 additions & 0 deletions
152
src/Bridges/Constraint/bridges/complex_norm_infinity.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,152 @@ | ||
# 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. | ||
|
||
""" | ||
ComplexNormInfinityToSecondOrderConeBridge{T} <: Bridges.Constraint.AbstractBridge | ||
|
||
`ComplexNormInfinityToSecondOrderConeBridge` implements the following | ||
reformulation: | ||
|
||
* ``(t, x) \\in NormInfinity(1+d)`` into ``(t, real(x_i), imag(x_i)) \\in SecondOrderCone()`` | ||
for all ``i``. | ||
|
||
## Source node | ||
|
||
`ComplexNormInfinityToSecondOrderConeBridge` supports: | ||
|
||
* [`MOI.VectorAffineFunction{Complex{T}}`](@ref) in [`MOI.NormInfinityCone`](@ref) | ||
|
||
## Target nodes | ||
|
||
`ComplexNormInfinityToSecondOrderConeBridge` creates: | ||
|
||
* [`MOI.VectorAffineFunction{T}`](@ref) in [`MOI.SecondOrderCone`](@ref) | ||
""" | ||
struct ComplexNormInfinityToSecondOrderConeBridge{T} <: AbstractBridge | ||
ci::Vector{ | ||
MOI.ConstraintIndex{MOI.VectorAffineFunction{T},MOI.SecondOrderCone}, | ||
} | ||
end | ||
|
||
const ComplexNormInfinityToSecondOrderCone{T,OT<:MOI.ModelLike} = | ||
SingleBridgeOptimizer{ComplexNormInfinityToSecondOrderConeBridge{T},OT} | ||
|
||
function bridge_constraint( | ||
::Type{ComplexNormInfinityToSecondOrderConeBridge{T}}, | ||
model::MOI.ModelLike, | ||
f::MOI.VectorAffineFunction{Complex{T}}, | ||
s::MOI.NormInfinityCone, | ||
) where {T} | ||
fi_s = MOI.Utilities.scalarize(f) | ||
if !iszero(imag(fi_s[1])) | ||
error( | ||
"The epigraph variable `t` in `[t; x] in NormInfinityCone()` " * | ||
"must be real. It is: $(fi_s[1])", | ||
) | ||
end | ||
t = real(fi_s[1]) | ||
cis = MOI.ConstraintIndex{MOI.VectorAffineFunction{T},MOI.SecondOrderCone}[] | ||
for fi in fi_s[2:end] | ||
ci = MOI.add_constraint( | ||
model, | ||
MOI.Utilities.operate(vcat, T, t, real(fi), imag(fi)), | ||
MOI.SecondOrderCone(3), | ||
) | ||
push!(cis, ci) | ||
end | ||
return ComplexNormInfinityToSecondOrderConeBridge{T}(cis) | ||
end | ||
|
||
function MOI.supports_constraint( | ||
::Type{<:ComplexNormInfinityToSecondOrderConeBridge{T}}, | ||
::Type{MOI.VectorAffineFunction{Complex{T}}}, | ||
::Type{MOI.NormInfinityCone}, | ||
) where {T} | ||
return true | ||
end | ||
|
||
function MOI.Bridges.added_constrained_variable_types( | ||
::Type{<:ComplexNormInfinityToSecondOrderConeBridge}, | ||
) | ||
return Tuple{Type}[] | ||
end | ||
|
||
function MOI.Bridges.added_constraint_types( | ||
::Type{<:ComplexNormInfinityToSecondOrderConeBridge{T}}, | ||
) where {T} | ||
return Tuple{Type,Type}[(MOI.VectorAffineFunction{T}, MOI.SecondOrderCone)] | ||
end | ||
|
||
function concrete_bridge_type( | ||
::Type{<:ComplexNormInfinityToSecondOrderConeBridge{T}}, | ||
::Type{MOI.VectorAffineFunction{Complex{T}}}, | ||
::Type{MOI.NormInfinityCone}, | ||
) where {T} | ||
return ComplexNormInfinityToSecondOrderConeBridge{T} | ||
end | ||
|
||
function MOI.get( | ||
::ComplexNormInfinityToSecondOrderConeBridge, | ||
::MOI.NumberOfVariables, | ||
)::Int64 | ||
return 0 | ||
end | ||
|
||
function MOI.get( | ||
bridge::ComplexNormInfinityToSecondOrderConeBridge{T}, | ||
::MOI.NumberOfConstraints{MOI.VectorAffineFunction{T},MOI.SecondOrderCone}, | ||
)::Int64 where {T} | ||
return length(bridge.ci) | ||
end | ||
|
||
function MOI.get( | ||
bridge::ComplexNormInfinityToSecondOrderConeBridge{T}, | ||
::MOI.ListOfConstraintIndices{ | ||
MOI.VectorAffineFunction{T}, | ||
MOI.SecondOrderCone, | ||
}, | ||
) where {T} | ||
return copy(bridge.ci) | ||
end | ||
|
||
function MOI.delete( | ||
model::MOI.ModelLike, | ||
bridge::ComplexNormInfinityToSecondOrderConeBridge, | ||
) | ||
MOI.delete(model, bridge.ci) | ||
return | ||
end | ||
|
||
function MOI.get( | ||
model::MOI.ModelLike, | ||
::MOI.ConstraintFunction, | ||
bridge::ComplexNormInfinityToSecondOrderConeBridge{T}, | ||
) where {T} | ||
elements = MOI.ScalarAffineFunction{Complex{T}}[] | ||
for ci in bridge.ci | ||
f = MOI.get(model, MOI.ConstraintFunction(), ci) | ||
fi_s = MOI.Utilities.scalarize(f) | ||
if isempty(elements) | ||
push!(elements, fi_s[1]) | ||
end | ||
fi = MOI.Utilities.operate( | ||
+, | ||
Complex{T}, | ||
(one(T) + zero(T) * im) * fi_s[2], | ||
(zero(T) + one(T) * im) * fi_s[3], | ||
) | ||
push!(elements, fi) | ||
end | ||
return MOI.Utilities.operate(vcat, Complex{T}, elements...) | ||
end | ||
|
||
function MOI.get( | ||
::MOI.ModelLike, | ||
::MOI.ConstraintSet, | ||
bridge::ComplexNormInfinityToSecondOrderConeBridge, | ||
) | ||
return MOI.NormInfinityCone(1 + length(bridge.ci)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 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 TestConstraintComplexNormInfinityToSecondOrderCone | ||
|
||
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.Constraint.ComplexNormInfinityToSecondOrderConeBridge, | ||
""" | ||
variables: t, x | ||
::Complex{Float64}: [t, (1 + 2im) * x + (3 + 4im)] in NormInfinityCone(2) | ||
""", | ||
""" | ||
variables: t, x | ||
::Float64: [t, 1 * x + 3, 2 * x + 4] in SecondOrderCone(3) | ||
""", | ||
) | ||
MOI.Bridges.runtests( | ||
MOI.Bridges.Constraint.ComplexNormInfinityToSecondOrderConeBridge, | ||
""" | ||
variables: t, x, y | ||
::Complex{Float64}: [2.0 * t + 3.0, x + im * y] in NormInfinityCone(2) | ||
""", | ||
""" | ||
variables: t, x, y | ||
::Float64: [2.0 * t + 3.0, 1.0 * x, 1.0 * y] in SecondOrderCone(3) | ||
""", | ||
) | ||
return | ||
end | ||
|
||
function test_imag_t() | ||
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()) | ||
model = | ||
MOI.Bridges.Constraint.ComplexNormInfinityToSecondOrderCone{Float64}( | ||
inner, | ||
) | ||
x = MOI.add_variables(model, 2) | ||
f_t = (1.0 + 2.0im) * x[1] | ||
f_x = (1.0 + 2.0im) * x[2] + (3.0 + 4.0im) | ||
f = MOI.Utilities.operate(vcat, Complex{Float64}, f_t, f_x) | ||
@test_throws( | ||
ErrorException( | ||
"The epigraph variable `t` in `[t; x] in NormInfinityCone()` " * | ||
"must be real. It is: $f_t", | ||
), | ||
MOI.add_constraint(model, f, MOI.NormInfinityCone(2)) | ||
) | ||
return | ||
end | ||
|
||
end # module | ||
|
||
TestConstraintComplexNormInfinityToSecondOrderCone.runtests() |
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
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.