Skip to content

Commit

Permalink
WIP: fix querying dual of symmetric equality constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 2, 2024
1 parent bf662d4 commit 85dbb01
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/sd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,29 @@ function reshape_set(
return PSDCone()
end

"""
DualSymmetricMatrixShape(side_dimension)
A shape used when quering the dual of a symmetic matrix.
"""
struct DualSymmetricMatrixShape <: AbstractShape
side_dimension::Int
end

function reshape_vector(v::Vector{T}, shape::DualSymmetricMatrixShape) where {T}
matrix = Matrix{T}(undef, shape.side_dimension, shape.side_dimension)
k = 0
for j in 1:shape.side_dimension
for i in 1:j-1
k += 1
matrix[j, i] = matrix[i, j] = 0.5 * v[k]
end
k += 1
matrix[j, j] = v[k]
end
return LinearAlgebra.Symmetric(matrix)

Check warning on line 197 in src/sd.jl

View check run for this annotation

Codecov / codecov/patch

src/sd.jl#L186-L197

Added lines #L186 - L197 were not covered by tests
end

"""
triangle_vec(matrix::Matrix)
Expand Down Expand Up @@ -479,6 +502,34 @@ function reshape_vector(v::Vector{T}, shape::HermitianMatrixShape) where {T}
return LinearAlgebra.Hermitian(matrix)
end

"""
DualHermitianMatrixShape(side_dimension)
A shape used when quering the dual of a symmetic matrix.
"""
struct DualHermitianMatrixShape <: AbstractShape
side_dimension::Int
end

function reshape_vector(v::Vector{T}, shape::DualHermitianMatrixShape) where {T}
NewType = _MA.promote_operation(_MA.add_mul, T, Complex{Bool}, T)
n = shape.side_dimension
matrix = Matrix{NewType}(undef, n, n)
real_k = 0
imag_k = MOI.dimension(MOI.PositiveSemidefiniteConeTriangle(n))
for j in 1:n
for i in 1:(j-1)
real_k += 1
imag_k += 1
matrix[i, j] = (v[real_k] + im * v[imag_k]) / 2
matrix[j, i] = (v[real_k] - im * v[imag_k]) / 2
end
real_k += 1
matrix[j, j] = v[real_k]
end
return LinearAlgebra.Hermitian(matrix)

Check warning on line 530 in src/sd.jl

View check run for this annotation

Codecov / codecov/patch

src/sd.jl#L514-L530

Added lines #L514 - L530 were not covered by tests
end

function _vectorize_complex_variables(error_fn::Function, matrix::Matrix)
if any(_is_binary, matrix) || any(_is_integer, matrix)
# We would then need to fix the imaginary value to zero. Let's wait to
Expand Down Expand Up @@ -557,6 +608,20 @@ function build_constraint(
return VectorConstraint(x, MOI.Zeros(length(x)), shape)
end

function dual(

Check warning on line 611 in src/sd.jl

View check run for this annotation

Codecov / codecov/patch

src/sd.jl#L611

Added line #L611 was not covered by tests
con_ref::ConstraintRef{
<:AbstractModel,
MOI.ConstraintIndex{F,MOI.Zeros},
HermitianMatrixShape,
};
result::Int = 1,
) where {F}
return reshape_vector(

Check warning on line 619 in src/sd.jl

View check run for this annotation

Codecov / codecov/patch

src/sd.jl#L619

Added line #L619 was not covered by tests
_constraint_dual(con_ref, result),
DualHermitianMatrixShape(con_ref.shape.side_dimension),
)
end

reshape_set(s::MOI.Zeros, ::HermitianMatrixShape) = Zeros()

function build_constraint(
Expand All @@ -570,6 +635,20 @@ function build_constraint(
return VectorConstraint(x, MOI.Zeros(length(x)), shape)
end

function dual(

Check warning on line 638 in src/sd.jl

View check run for this annotation

Codecov / codecov/patch

src/sd.jl#L638

Added line #L638 was not covered by tests
con_ref::ConstraintRef{
<:AbstractModel,
MOI.ConstraintIndex{F,MOI.Zeros},
SymmetricMatrixShape,
};
result::Int = 1,
) where {F}
return reshape_vector(

Check warning on line 646 in src/sd.jl

View check run for this annotation

Codecov / codecov/patch

src/sd.jl#L646

Added line #L646 was not covered by tests
_constraint_dual(con_ref, result),
DualSymmetricMatrixShape(con_ref.shape.side_dimension),
)
end

function build_constraint(error_fn::Function, ::AbstractMatrix, ::Nonnegatives)
return error_fn(
"Unsupported matrix in vector-valued set. Did you mean to use the " *
Expand Down

0 comments on commit 85dbb01

Please sign in to comment.