Skip to content

[Utilities] add distance_to_set for PositiveSemidefiniteCone #2729

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 11 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
51 changes: 51 additions & 0 deletions src/Utilities/distance_to_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,54 @@ function distance_to_set(
elements = [x[i] for i in eachindex(x) if !(i in pairs[k])]
return LinearAlgebra.norm(elements, 2)
end

function _reshape(x::AbstractVector, set::MOI.PositiveSemidefiniteConeSquare)
n = MOI.side_dimension(set)
return reshape(x, (n, n))
end

function _reshape(x::AbstractVector, set::MOI.PositiveSemidefiniteConeTriangle)
n = MOI.side_dimension(set)
X = zeros(eltype(x), n, n)
k = 1
for i in 1:n
for j in 1:i
X[j, i] = X[i, j] = x[k]
k += 1
end
end
return LinearAlgebra.Symmetric(X)
end

"""
distance_to_set(
::ProjectionUpperBoundDistance,
x::AbstractVector,
set::Union{
MOI.PositiveSemidefiniteConeSquare,
MOI.PositiveSemidefiniteConeTriangle,
},
)

Let ``X`` be `x` reshaped into the appropriate matrix. The returned distance is
``||X - Y||_2^2`` where ``Y`` is the eigen decomposition of ``X`` with negative
eigen values removed.
"""
function distance_to_set(
::ProjectionUpperBoundDistance,
x::AbstractVector{T},
set::Union{
MOI.PositiveSemidefiniteConeSquare,
MOI.PositiveSemidefiniteConeTriangle,
},
) where {T<:Real}
_check_dimension(x, set)
λ, U = LinearAlgebra.eigen(_reshape(x, set))
if minimum(λ) >= zero(T)
return 0.0
end
λ_negative = LinearAlgebra.Diagonal(min.(zero(T), λ))
A = LinearAlgebra.Symmetric(U * λ_negative * U')
# Σ A^2 is needed to match the definition of Utilities.set_dot
return sum(Base.Fix2(^, 2), A)
end
26 changes: 25 additions & 1 deletion test/Utilities/distance_to_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function _test_set(set, pairs...; mismatch = nothing)
)
end
for (x, d) in pairs
@test MOI.Utilities.distance_to_set(x, set) ≈ d
@test ≈(MOI.Utilities.distance_to_set(x, set), d; atol = 1e-12)
end
return
end
Expand Down Expand Up @@ -308,6 +308,30 @@ function test_sos2()
return
end

function test_positivesemidefiniteconesquare()
_test_set(
MOI.PositiveSemidefiniteConeSquare(2),
[1.0, 0.0, 0.0, 1.0] => 0.0,
[1.0, -1.0, -1.0, 1.0] => 0.0,
[1.0, -2.0, -2.0, 1.0] => 1.0,
[1.0 1.1; 1.1 -2.3] => 2.6330532015051946;
mismatch = [1.0],
)
return
end

function test_positivesemidefiniteconetriangle()
_test_set(
MOI.PositiveSemidefiniteConeTriangle(2),
[1.0, 0.0, 1.0] => 0.0,
[1.0, -1.0, 1.0] => 0.0,
[1.0, -2.0, 1.0] => 1.0
[1.0, 1.1, -2.3] => 2.6330532015051946;
mismatch = [1.0],
)
return
end

end

TestFeasibilityChecker.runtests()
Loading