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 3 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 @@
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))

Check warning on line 495 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L493-L495

Added lines #L493 - L495 were not covered by tests
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)

Check warning on line 508 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L498-L508

Added lines #L498 - L508 were not covered by tests
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(

Check warning on line 525 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L525

Added line #L525 was not covered by tests
::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

Check warning on line 536 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L533-L536

Added lines #L533 - L536 were not covered by tests
end
λ_negative = LinearAlgebra.Diagonal(min.(zero(T), λ))
A = LinearAlgebra.Symmetric(U * λ_negative * U')

Check warning on line 539 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L538-L539

Added lines #L538 - L539 were not covered by tests
# Σ A^2 is needed to match the definition of Utilities.set_dot
return sum(Base.Fix2(^, 2), A)

Check warning on line 541 in src/Utilities/distance_to_set.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/distance_to_set.jl#L541

Added line #L541 was not covered by tests
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