-
Notifications
You must be signed in to change notification settings - Fork 92
Add NormCone for representing the epigraph of a p-norm #2119
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
7 commits
Select commit
Hold shift + click to select a range
99eff90
Add NormCone for representing the epigraph of a p-norm
odow 7f31268
Add Bridges.Constraint.NormSpecialCaseBridge
odow 5c7f8fe
Fix tests
odow 3708be2
Fix bridge test
odow 10d8285
Convert to SetMap
odow 1d0d74a
Updates
odow 9d7aa9c
Update
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
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
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,114 @@ | ||
# 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. | ||
|
||
struct NormSpecialCaseBridge{S,T,F} <: SetMapBridge{T,MOI.NormCone,S,F,F} | ||
constraint::MOI.ConstraintIndex{F,MOI.NormCone} | ||
end | ||
|
||
MOI.Bridges.map_function(::Type{<:NormSpecialCaseBridge}, f) = f | ||
|
||
MOI.Bridges.inverse_map_function(::Type{<:NormSpecialCaseBridge}, f) = f | ||
|
||
MOI.Bridges.adjoint_map_function(::Type{<:NormSpecialCaseBridge}, f) = f | ||
|
||
MOI.Bridges.inverse_adjoint_map_function(::Type{<:NormSpecialCaseBridge}, f) = f | ||
|
||
function MOI.Bridges.map_set( | ||
::Type{<:NormSpecialCaseBridge{S}}, | ||
set::S, | ||
) where {S} | ||
return MOI.NormCone(set) | ||
end | ||
|
||
function MOI.Bridges.inverse_map_set( | ||
::Type{<:NormSpecialCaseBridge{S}}, | ||
set::MOI.NormCone, | ||
) where {S} | ||
return S(MOI.dimension(set)) | ||
end | ||
|
||
function concrete_bridge_type( | ||
::Type{<:NormSpecialCaseBridge{S,T}}, | ||
F::Type{<:MOI.AbstractVectorFunction}, | ||
::Type{S}, | ||
) where {T,S<:Union{MOI.NormOneCone,MOI.SecondOrderCone,MOI.NormInfinityCone}} | ||
return NormSpecialCaseBridge{S,T,F} | ||
end | ||
|
||
""" | ||
NormOneConeToNormConeBridge{T,F} <: Bridges.Constraint.AbstractBridge | ||
|
||
`NormOneConeToNormConeBridge` implements the following reformulations: | ||
|
||
* ``(t, x) in NormOneCone(d)`` into ``(t, x) in NormCone(1, d)`` | ||
|
||
## Source node | ||
|
||
`NormOneConeToNormConeBridge` supports: | ||
|
||
* `F` in [`MOI.NormOneCone`](@ref) | ||
|
||
## Target nodes | ||
|
||
`NormOneConeToNormConeBridge` creates: | ||
|
||
* `F` in [`MOI.NormCone`](@ref) | ||
""" | ||
const NormOneConeToNormConeBridge{T,F} = | ||
NormSpecialCaseBridge{MOI.NormOneCone,T,F} | ||
|
||
const NormOneConeToNormCone{T,OT<:MOI.ModelLike} = | ||
SingleBridgeOptimizer{NormOneConeToNormConeBridge{T},OT} | ||
|
||
""" | ||
SecondOrderConeToNormConeBridge{T,F} <: Bridges.Constraint.AbstractBridge | ||
|
||
`SecondOrderConeToNormConeBridge` implements the following reformulations: | ||
|
||
* ``(t, x) in SecondOrderCone(d)`` into ``(t, x) in NormCone(2, d)`` | ||
|
||
## Source node | ||
|
||
`SecondOrderConeToNormConeBridge` supports: | ||
|
||
* `F` in [`MOI.SecondOrderCone`](@ref) | ||
|
||
## Target nodes | ||
|
||
`SecondOrderConeToNormConeBridge` creates: | ||
|
||
* `F` in [`MOI.NormCone`](@ref) | ||
""" | ||
const SecondOrderConeToNormConeBridge{T,F} = | ||
NormSpecialCaseBridge{MOI.SecondOrderCone,T,F} | ||
|
||
const SecondOrderConeToNormCone{T,OT<:MOI.ModelLike} = | ||
SingleBridgeOptimizer{SecondOrderConeToNormConeBridge{T},OT} | ||
|
||
""" | ||
NormInfinityConeToNormConeBridge{T,F} <: Bridges.Constraint.AbstractBridge | ||
|
||
`NormInfinityConeToNormConeBridge` implements the following reformulations: | ||
|
||
* ``(t, x) in NormInfinityCone(d)`` into ``(t, x) in NormCone(Inf, d)`` | ||
|
||
## Source node | ||
|
||
`NormInfinityConeToNormConeBridge` supports: | ||
|
||
* `F` in [`MOI.NormInfinityCone`](@ref) | ||
|
||
## Target nodes | ||
|
||
`NormInfinityConeToNormConeBridge` creates: | ||
|
||
* `F` in [`MOI.NormCone`](@ref) | ||
""" | ||
const NormInfinityConeToNormConeBridge{T,F} = | ||
NormSpecialCaseBridge{MOI.NormInfinityCone,T,F} | ||
|
||
const NormInfinityConeToNormCone{T,OT<:MOI.ModelLike} = | ||
SingleBridgeOptimizer{NormInfinityConeToNormConeBridge{T},OT} |
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,163 @@ | ||
# 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. | ||
|
||
""" | ||
NormToPowerBridge{T,F} <: Bridges.Constraint.AbstractBridge | ||
|
||
`NormToPowerBridge` implements the following reformulation: | ||
|
||
* ``(t, x) \\in NormCone(p, 1+d)`` into ``(r_i, t, x_i) \\in PowerCone(1 / p)`` | ||
for all ``i``, and ``\\sum\\limits_i r_i == t``. | ||
|
||
For details, see Alizadeh, F., and Goldfarb, D. (2001). "Second-order cone | ||
programming." Mathematical Programming, Series B, 95:3-51. | ||
|
||
## Source node | ||
|
||
`NormToPowerBridge` supports: | ||
|
||
* `F` in [`MOI.NormCone`](@ref) | ||
|
||
## Target nodes | ||
|
||
`NormToPowerBridge` creates: | ||
|
||
* `F` in [`MOI.PowerCone{T}`](@ref) | ||
* [`MOI.ScalarAffineFunction`](@ref) in [`MOI.EqualTo`](@ref) | ||
""" | ||
struct NormToPowerBridge{T,F} <: AbstractBridge | ||
power::Vector{MOI.ConstraintIndex{F,MOI.PowerCone{T}}} | ||
r::Vector{MOI.VariableIndex} | ||
equal::MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}} | ||
set::MOI.NormCone | ||
end | ||
|
||
const NormToPower{T,OT<:MOI.ModelLike} = | ||
SingleBridgeOptimizer{NormToPowerBridge{T},OT} | ||
|
||
function bridge_constraint( | ||
::Type{NormToPowerBridge{T,F}}, | ||
model::MOI.ModelLike, | ||
f::F, | ||
s::MOI.NormCone, | ||
) where {T,F} | ||
d = MOI.dimension(s) | ||
fi_s = MOI.Utilities.eachscalar(f) | ||
r = MOI.add_variables(model, d - 1) | ||
power_ci = MOI.ConstraintIndex{F,MOI.PowerCone{T}}[ | ||
MOI.add_constraint( | ||
model, | ||
MOI.Utilities.operate(vcat, T, r[i], fi_s[1], fi_s[i+1]), | ||
MOI.PowerCone(T(1 / s.p)), | ||
) for i in 1:length(r) | ||
] | ||
f = zero(MOI.ScalarAffineFunction{T}) | ||
for ri in r | ||
f = MOI.Utilities.operate!(+, T, f, ri) | ||
end | ||
MOI.Utilities.operate!(-, T, f, fi_s[1]) | ||
equal_ci = MOI.add_constraint(model, f, MOI.EqualTo(zero(T))) | ||
return NormToPowerBridge{T,F}(power_ci, r, equal_ci, s) | ||
end | ||
|
||
function MOI.supports_constraint( | ||
::Type{<:NormToPowerBridge{T}}, | ||
::Type{<:MOI.AbstractVectorFunction}, | ||
::Type{MOI.NormCone}, | ||
) where {T} | ||
return true | ||
end | ||
|
||
function MOI.Bridges.added_constrained_variable_types( | ||
::Type{<:NormToPowerBridge}, | ||
) | ||
return Tuple{Type}[(MOI.Reals,)] | ||
end | ||
|
||
function MOI.Bridges.added_constraint_types( | ||
::Type{<:NormToPowerBridge{T,F}}, | ||
) where {T,F} | ||
return Tuple{Type,Type}[ | ||
(F, MOI.PowerCone{T}), | ||
(MOI.ScalarAffineFunction{T}, MOI.EqualTo{T}), | ||
] | ||
end | ||
|
||
function concrete_bridge_type( | ||
::Type{<:NormToPowerBridge{T}}, | ||
F::Type{<:MOI.AbstractVectorFunction}, | ||
::Type{MOI.NormCone}, | ||
) where {T} | ||
return NormToPowerBridge{T,F} | ||
end | ||
|
||
function MOI.get(bridge::NormToPowerBridge, ::MOI.NumberOfVariables)::Int64 | ||
return length(bridge.r) | ||
end | ||
|
||
function MOI.get(bridge::NormToPowerBridge, ::MOI.ListOfVariableIndices) | ||
return copy(bridge.r) | ||
end | ||
|
||
function MOI.get( | ||
bridge::NormToPowerBridge{T,F}, | ||
::MOI.NumberOfConstraints{F,MOI.PowerCone{T}}, | ||
)::Int64 where {T,F} | ||
return length(bridge.power) | ||
end | ||
|
||
function MOI.get( | ||
bridge::NormToPowerBridge{T,F}, | ||
::MOI.ListOfConstraintIndices{F,MOI.PowerCone{T}}, | ||
) where {T,F} | ||
return copy(bridge.power) | ||
end | ||
|
||
function MOI.get( | ||
bridge::NormToPowerBridge{T}, | ||
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}}, | ||
)::Int64 where {T} | ||
return 1 | ||
end | ||
|
||
function MOI.get( | ||
bridge::NormToPowerBridge{T}, | ||
::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}}, | ||
) where {T} | ||
return [bridge.equal] | ||
end | ||
|
||
function MOI.delete(model::MOI.ModelLike, bridge::NormToPowerBridge) | ||
MOI.delete(model, bridge.power) | ||
MOI.delete(model, bridge.equal) | ||
MOI.delete(model, bridge.r) | ||
return | ||
end | ||
|
||
function MOI.get( | ||
model::MOI.ModelLike, | ||
::MOI.ConstraintFunction, | ||
bridge::NormToPowerBridge{T,F}, | ||
) where {T,F} | ||
elements = MOI.Utilities.scalar_type(F)[] | ||
for ci in bridge.power | ||
f = MOI.get(model, MOI.ConstraintFunction(), ci) | ||
fi_s = MOI.Utilities.eachscalar(f) | ||
if isempty(elements) | ||
push!(elements, fi_s[2]) | ||
end | ||
push!(elements, fi_s[3]) | ||
end | ||
return MOI.Utilities.operate(vcat, T, elements...) | ||
end | ||
|
||
function MOI.get( | ||
::MOI.ModelLike, | ||
::MOI.ConstraintSet, | ||
bridge::NormToPowerBridge, | ||
) | ||
return bridge.set | ||
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
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
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.