-
Notifications
You must be signed in to change notification settings - Fork 92
add nuclear norm and spectral norm cone sets (for general/nonsymmetric matrices) #976
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
17 commits
Select commit
Hold shift + click to select a range
66ac26c
define nuclear and spectral norm cones in sets.jl
chriscoey 10bda57
add to apireference
chriscoey 505204a
add nuclear and spectral tests
chriscoey 87d677f
add Utilities tests and fix some apparent bugs in existing tests
chriscoey fe2aa25
clarify new cone definitions
chriscoey b83290f
start on bridge file
chriscoey 592a75c
add NormSpectralBridge; modify cone definition
chriscoey 393d761
add spectral bridge and start on bridge tests
chriscoey 1c1bfd7
get bridges tests working for spectral bridge
chriscoey 83f4e87
get nuclear bridge working
chriscoey 89a23b9
use better conic tests for spectral and nuclear cones
chriscoey 04e4c91
address comments
chriscoey cae2e41
try allowing rows >= columns
chriscoey e81356a
remove restriction on dimensions
chriscoey 6498687
Merge branch 'master' into addspectralnormcone
chriscoey ead2edd
add spec/nuc cones to MOF
chriscoey 395583b
add primalstart for spectral and dualstart for nuclear
chriscoey 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
""" | ||
NormSpectralBridge{T} | ||
|
||
The `NormSpectralCone` is representable with a PSD constraint, since | ||
``t \\ge \\sigma_1(X)`` if and only if ``[tI X^\\top; X tI] \\succ 0``. | ||
""" | ||
struct NormSpectralBridge{T, F, G} <: AbstractBridge | ||
row_dim::Int # row dimension of X | ||
column_dim::Int # column dimension of X | ||
psd_index::CI{F, MOI.PositiveSemidefiniteConeTriangle} | ||
end | ||
function bridge_constraint(::Type{NormSpectralBridge{T, F, G}}, model::MOI.ModelLike, f::G, s::MOI.NormSpectralCone) where {T, F, G} | ||
f_scalars = MOIU.eachscalar(f) | ||
t = f_scalars[1] | ||
row_dim = s.row_dim | ||
column_dim = s.column_dim | ||
side_dim = row_dim + column_dim | ||
psd_set = MOI.PositiveSemidefiniteConeTriangle(side_dim) | ||
psd_func = MOIU.zero_with_output_dimension(F, MOI.dimension(psd_set)) | ||
for i in 1:side_dim | ||
MOIU.operate_output_index!(+, T, trimap(i, i), psd_func, t) | ||
end | ||
X_idx = 2 | ||
for j in 1:column_dim, i in (column_dim + 1):side_dim | ||
MOIU.operate_output_index!(+, T, trimap(i, j), psd_func, f_scalars[X_idx]) | ||
X_idx += 1 | ||
end | ||
psd_index = MOI.add_constraint(model, psd_func, psd_set) | ||
return NormSpectralBridge{T, F, G}(row_dim, column_dim, psd_index) | ||
end | ||
|
||
MOI.supports_constraint(::Type{NormSpectralBridge{T}}, ::Type{<:MOI.AbstractVectorFunction}, ::Type{MOI.NormSpectralCone}) where T = true | ||
MOIB.added_constrained_variable_types(::Type{<:NormSpectralBridge}) = Tuple{DataType}[] | ||
MOIB.added_constraint_types(::Type{NormSpectralBridge{T, F, G}}) where {T, F, G} = [(F, MOI.PositiveSemidefiniteConeTriangle)] | ||
function concrete_bridge_type(::Type{<:NormSpectralBridge{T}}, G::Type{<:MOI.AbstractVectorFunction}, ::Type{MOI.NormSpectralCone}) where T | ||
F = MOIU.promote_operation(vcat, T, MOIU.scalar_type(G), T) | ||
return NormSpectralBridge{T, F, G} | ||
end | ||
|
||
# Attributes, Bridge acting as a model | ||
MOI.get(bridge::NormSpectralBridge{T, F, G}, ::MOI.NumberOfConstraints{F, MOI.PositiveSemidefiniteConeTriangle}) where {T, F, G} = 1 | ||
MOI.get(bridge::NormSpectralBridge{T, F, G}, ::MOI.ListOfConstraintIndices{F, MOI.PositiveSemidefiniteConeTriangle}) where {T, F, G} = [bridge.psd_index] | ||
|
||
# References | ||
MOI.delete(model::MOI.ModelLike, bridge::NormSpectralBridge) = MOI.delete(model, bridge.psd_index) | ||
|
||
# Attributes, Bridge acting as a constraint | ||
function MOI.get(model::MOI.ModelLike, ::MOI.ConstraintFunction, bridge::NormSpectralBridge{T, F, G}) where {T, F, G} | ||
psd_func = MOIU.eachscalar(MOI.get(model, MOI.ConstraintFunction(), bridge.psd_index)) | ||
t = psd_func[1] | ||
side_dim = bridge.row_dim + bridge.column_dim | ||
X = psd_func[[trimap(i, j) for j in 1:bridge.column_dim for i in (bridge.column_dim + 1):side_dim]] | ||
return MOIU.convert_approx(G, MOIU.operate(vcat, T, t, X)) | ||
end | ||
MOI.get(model::MOI.ModelLike, ::MOI.ConstraintSet, bridge::NormSpectralBridge) = MOI.NormSpectralCone(bridge.row_dim, bridge.column_dim) | ||
MOI.supports(::MOI.ModelLike, ::MOI.ConstraintPrimalStart, ::Type{<:NormSpectralBridge}) = true | ||
function MOI.get(model::MOI.ModelLike, attr::Union{MOI.ConstraintPrimal, MOI.ConstraintPrimalStart}, bridge::NormSpectralBridge) | ||
primal = MOI.get(model, attr, bridge.psd_index) | ||
t = primal[1] | ||
side_dim = bridge.row_dim + bridge.column_dim | ||
X = primal[[trimap(i, j) for j in 1:bridge.column_dim for i in (bridge.column_dim + 1):side_dim]] | ||
return vcat(t, X) | ||
end | ||
function MOI.set(model::MOI.ModelLike, ::MOI.ConstraintPrimalStart, bridge::NormSpectralBridge{T}, value) where T | ||
column_dim = bridge.column_dim | ||
side_dim = bridge.row_dim + column_dim | ||
primal = zeros(T, div(side_dim * (side_dim + 1), 2)) | ||
for i in 1:side_dim | ||
primal[trimap(i, i)] = value[1] | ||
end | ||
X_idx = 2 | ||
for j in 1:column_dim, i in (column_dim + 1):side_dim | ||
primal[trimap(i, j)] = value[X_idx] | ||
X_idx += 1 | ||
end | ||
MOI.set(model, MOI.ConstraintPrimalStart(), bridge.psd_index, primal) | ||
return | ||
end | ||
# Given [U X'; X V] is dual on PSD constraint, the dual on NormSpectralCone | ||
# constraint is (tr(U) + tr(V), 2X) in NormNuclearCone. | ||
function MOI.get(model::MOI.ModelLike, ::MOI.ConstraintDual, bridge::NormSpectralBridge) | ||
dual = MOI.get(model, MOI.ConstraintDual(), bridge.psd_index) | ||
column_dim = bridge.column_dim | ||
side_dim = bridge.row_dim + column_dim | ||
t = sum(dual[trimap(i, i)] for i in 1:side_dim) | ||
X = 2 * dual[[trimap(i, j) for j in 1:column_dim for i in (column_dim + 1):side_dim]] | ||
return vcat(t, X) | ||
end | ||
|
||
""" | ||
NormNuclearBridge{T} | ||
|
||
The `NormNuclearCone` is representable with an SDP constraint and extra variables, | ||
since ``t \\ge \\sum_i \\sigma_i (X)`` if and only if there exists symmetric | ||
matrices ``U, V`` such that ``[U X^\\top; X V] \\succ 0`` and ``t \\ge (tr(U) + tr(V)) / 2``. | ||
""" | ||
struct NormNuclearBridge{T, F, G, H} <: AbstractBridge | ||
row_dim::Int # row dimension of X | ||
column_dim::Int # column dimension of X | ||
U::Vector{MOI.VariableIndex} | ||
V::Vector{MOI.VariableIndex} | ||
ge_index::CI{F, MOI.GreaterThan{T}} | ||
psd_index::CI{G, MOI.PositiveSemidefiniteConeTriangle} | ||
end | ||
function bridge_constraint(::Type{NormNuclearBridge{T, F, G, H}}, model::MOI.ModelLike, f::H, s::MOI.NormNuclearCone) where {T, F, G, H} | ||
f_scalars = MOIU.eachscalar(f) | ||
row_dim = s.row_dim | ||
column_dim = s.column_dim | ||
side_dim = row_dim + column_dim | ||
U_dim = div(column_dim * (column_dim + 1), 2) | ||
V_dim = div(row_dim * (row_dim + 1), 2) | ||
U = MOI.add_variables(model, U_dim) | ||
V = MOI.add_variables(model, V_dim) | ||
diag_vars = vcat([U[trimap(i, i)] for i in 1:column_dim], [V[trimap(i, i)] for i in 1:row_dim]) | ||
ge_index = MOIU.normalize_and_add_constraint(model, MOIU.operate(-, T, f_scalars[1], MOIU.operate!(/, T, MOIU.operate(sum, T, diag_vars), T(2))), MOI.GreaterThan(zero(T)), allow_modify_function=true) | ||
psd_set = MOI.PositiveSemidefiniteConeTriangle(side_dim) | ||
psd_func = MOI.VectorOfVariables(U) | ||
nuc_dim = 1 + row_dim * column_dim | ||
for i in 1:row_dim | ||
row_i = (1 + i):row_dim:nuc_dim | ||
psd_func = MOIU.operate(vcat, T, psd_func, f_scalars[row_i]) | ||
psd_func = MOIU.operate(vcat, T, psd_func, MOI.VectorOfVariables(V[[trimap(i, j) for j in 1:i]])) | ||
end | ||
psd_index = MOI.add_constraint(model, psd_func, psd_set) | ||
return NormNuclearBridge{T, F, G, H}(row_dim, column_dim, U, V, ge_index, psd_index) | ||
end | ||
|
||
MOI.supports_constraint(::Type{NormNuclearBridge{T}}, ::Type{<:MOI.AbstractVectorFunction}, ::Type{MOI.NormNuclearCone}) where T = true | ||
MOIB.added_constrained_variable_types(::Type{<:NormNuclearBridge}) = Tuple{DataType}[] | ||
MOIB.added_constraint_types(::Type{NormNuclearBridge{T, F, G, H}}) where {T, F, G, H} = [(F, MOI.GreaterThan{T}), (G, MOI.PositiveSemidefiniteConeTriangle)] | ||
function concrete_bridge_type(::Type{<:NormNuclearBridge{T}}, H::Type{<:MOI.AbstractVectorFunction}, ::Type{MOI.NormNuclearCone}) where T | ||
S = MOIU.scalar_type(H) | ||
F = MOIU.promote_operation(-, T, S, MOIU.promote_operation(/, T, MOIU.promote_operation(+, T, T, T), T)) | ||
G = MOIU.promote_operation(vcat, T, MOI.VectorOfVariables, H) | ||
return NormNuclearBridge{T, F, G, H} | ||
end | ||
|
||
# Attributes, Bridge acting as a model | ||
MOI.get(bridge::NormNuclearBridge, ::MOI.NumberOfVariables) = length(bridge.U) + length(bridge.V) | ||
MOI.get(bridge::NormNuclearBridge, ::MOI.ListOfVariableIndices) = vcat(bridge.U, bridge.V) | ||
MOI.get(bridge::NormNuclearBridge{T, F, G, H}, ::MOI.NumberOfConstraints{F, MOI.GreaterThan{T}}) where {T, F, G, H} = 1 | ||
MOI.get(bridge::NormNuclearBridge{T, F, G, H}, ::MOI.NumberOfConstraints{G, MOI.PositiveSemidefiniteConeTriangle}) where {T, F, G, H} = 1 | ||
MOI.get(bridge::NormNuclearBridge{T, F, G, H}, ::MOI.ListOfConstraintIndices{F, MOI.GreaterThan{T}}) where {T, F, G, H} = [bridge.ge_index] | ||
MOI.get(bridge::NormNuclearBridge{T, F, G, H}, ::MOI.ListOfConstraintIndices{G, MOI.PositiveSemidefiniteConeTriangle}) where {T, F, G, H} = [bridge.psd_index] | ||
|
||
# References | ||
function MOI.delete(model::MOI.ModelLike, bridge::NormNuclearBridge) | ||
MOI.delete(model, bridge.ge_index) | ||
MOI.delete(model, bridge.psd_index) | ||
MOI.delete(model, bridge.U) | ||
MOI.delete(model, bridge.V) | ||
end | ||
|
||
# Attributes, Bridge acting as a constraint | ||
function MOI.get(model::MOI.ModelLike, ::MOI.ConstraintFunction, bridge::NormNuclearBridge{T, F, G, H}) where {T, F, G, H} | ||
ge_func = MOI.get(model, MOI.ConstraintFunction(), bridge.ge_index) | ||
psd_func = MOIU.eachscalar(MOI.get(model, MOI.ConstraintFunction(), bridge.psd_index)) | ||
column_dim = bridge.column_dim | ||
side_dim = bridge.row_dim + column_dim | ||
t = MOIU.operate(+, T, ge_func, MOIU.operate(/, T, MOIU.operate(+, T, [psd_func[trimap(i, i)] for i in 1:side_dim]...), T(2))) | ||
t = MOIU.remove_variable(MOIU.remove_variable(t, bridge.U), bridge.V) | ||
X = psd_func[[trimap(i, j) for j in 1:bridge.column_dim for i in (bridge.column_dim + 1):side_dim]] | ||
return MOIU.convert_approx(H, MOIU.operate(vcat, T, t, X)) | ||
end | ||
MOI.get(model::MOI.ModelLike, ::MOI.ConstraintSet, bridge::NormNuclearBridge) = MOI.NormNuclearCone(bridge.row_dim, bridge.column_dim) | ||
MOI.supports(::MOI.ModelLike, ::MOI.ConstraintDualStart, ::Type{<:NormNuclearBridge}) = true | ||
function MOI.get(model::MOI.ModelLike, ::MOI.ConstraintPrimal, bridge::NormNuclearBridge) | ||
ge_primal = MOI.get(model, MOI.ConstraintPrimal(), bridge.ge_index) | ||
psd_primal = MOI.get(model, MOI.ConstraintPrimal(), bridge.psd_index) | ||
side_dim = bridge.row_dim + bridge.column_dim | ||
t = ge_primal + sum(psd_primal[trimap(i, i)] for i in 1:side_dim) / 2 | ||
X = psd_primal[[trimap(i, j) for j in 1:bridge.column_dim for i in (bridge.column_dim + 1):side_dim]] | ||
return vcat(t, X) | ||
end | ||
# Given t is dual on GreaterThan constraint and [U X'; X V] is dual on PSD constraint, | ||
# the dual on NormNuclearCone constraint is (t, 2X) in NormNuclearCone. | ||
function MOI.get(model::MOI.ModelLike, attr::Union{MOI.ConstraintDual, MOI.ConstraintDualStart}, bridge::NormNuclearBridge) | ||
t = MOI.get(model, attr, bridge.ge_index) | ||
psd_dual = MOI.get(model, attr, bridge.psd_index) | ||
side_dim = bridge.row_dim + bridge.column_dim | ||
X = 2 * psd_dual[[trimap(i, j) for j in 1:bridge.column_dim for i in (bridge.column_dim + 1):side_dim]] | ||
return vcat(t, X) | ||
end | ||
function MOI.set(model::MOI.ModelLike, ::MOI.ConstraintDualStart, bridge::NormNuclearBridge{T}, value) where T | ||
MOI.set(model, MOI.ConstraintDualStart(), bridge.ge_index, value[1]) | ||
column_dim = bridge.column_dim | ||
side_dim = bridge.row_dim + column_dim | ||
dual = zeros(T, div(side_dim * (side_dim + 1), 2)) | ||
for i in 1:side_dim | ||
dual[trimap(i, i)] = value[1] | ||
end | ||
X_idx = 2 | ||
for j in 1:column_dim, i in (column_dim + 1):side_dim | ||
dual[trimap(i, j)] = value[X_idx] / 2 | ||
X_idx += 1 | ||
end | ||
MOI.set(model, MOI.ConstraintDualStart(), bridge.psd_index, dual) | ||
return | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For ConstraintDualStart, I would set all
trimap(i, i)
to the dual oft
divided by2side_dim
. It might not be the only possibility but it works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK should I make another PR for that?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wrong, that won't make the matrix PSD.
In fact, it's easier to discuss the ConstraintPrimalStart of NormNuclearBridge.
So suppose you have X and t set.
[A X'; X B] >= 0 is equivalent to A >= X' inv(B) X. If X = U S V', then, it's equivalent to
V' * A * V <= S * U' * inv(B) * U * S
then, by choosing A = V * S * V' and B = U * S * U', you get
S <= S * inv(S) * S which is satisfied.
As the trace must sum up to t, you set A = V * S * V' * (t / tr(S)) and B = U * S * U' * (t / tr(S).
The matrix [A X'; X B] >= will be PSD iff t >= sum sigma_i(X) so it seems like a good candidate for starting value :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if that works for constraint primal start of NormNuclear then should it also work for constraint dual start of NormSpectral?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was just easier to reason in the primal than in the dual ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK rather than having you explain the details to me, it's probably easier if you implement those new start functions