Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
94 changes: 94 additions & 0 deletions docs/src/reference/optimizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,97 @@ SAGE relaxation:
```@docs
PolyJuMP.SAGE.Optimizer
```

## Facial reduction for semidefinite programs

Wrap an SDP solver with `FacialReduction.Optimizer` when the constraints force
a positive semidefinite slack onto a proper face. For example, if a PSD matrix
has a zero diagonal entry, its entire corresponding row and column must vanish.

```@example facial-reduction
using SumOfSquares
import Clarabel

model = Model(() -> FacialReduction.Optimizer(Clarabel.Optimizer))
set_silent(model)
set_optimizer_attribute(model, FacialReduction.MaxReductions(), 5)
@variable(model, X[1:2, 1:2], PSD)
@constraint(model, X[1, 1] == 0)
@objective(model, Min, X[2, 2])
optimize!(model)

value.(X) # solution in original coordinates
dual(VariableInSetRef(X); result = 2) # original PSD exposing matrix
MOI.get(model, FacialReduction.ReductionCount()) # one successful pass
MOI.get(model, FacialReduction.ReducedFace(), VariableInSetRef(X)) # spans e₂
```

The same optimizer factory works with `SOSModel`. At each face the default
search tries diagonal dominance (DD) first. If it finds no verified direction,
it tries scaled diagonal dominance (SDD). To start directly with SDD, or
search the entire PSD cone, select the cone type when constructing the wrapper
or set the optimizer attribute before the solve.

```@example facial-reduction
const DD = SumOfSquares.DiagonallyDominantConeTriangle
const SDD = SumOfSquares.ScaledDiagonallyDominantConeTriangle
const PSDTriangle = MOI.PositiveSemidefiniteConeTriangle

model = Model(() -> FacialReduction.Optimizer(Clarabel.Optimizer; search_cones = SDD))
set_optimizer_attribute(model, FacialReduction.SearchCones(), PSDTriangle)
# Try all three in order at each face.
set_optimizer_attribute(model, FacialReduction.SearchCones(), (DD, SDD, PSDTriangle))
get_optimizer_attribute(model, FacialReduction.SearchCones())
```

| Search | Subproblem | Directions available |
|:-------|:-----------|:---------------------|
| DD | LP | Coordinate vectors and equally weighted pairs eᵢ ± eⱼ |
| SDD | SOCP | Arbitrarily weighted pairs a eᵢ + b eⱼ |
| PSD | SDP | Arbitrary vectors, including dense ones |

These directions generate the exposing matrices by sums of outer products
[PermenterParrilo2018; Section 3.2](@cite). For example, the constraint
vᵀXv = 0 on a PSD matrix forces Xv = 0. If this is the only constraint and
v = (1, 2, 0), SDD can find the exposing ray vvᵀ. DD misses it because its
first row fails diagonal dominance. For v = (1, 2, 3), only the full PSD
search can find the ray. DD and SDD use the same SumOfSquares bridges as
Gram matrix variables. The optimizer supplied with `search_solver` must
support the selected searches. An LP-only solver can use `search_cones = DD`.

The search normalizes the total trace of the exposing blocks to one. After
finding a witness it searches for positive trace on the remaining kernel,
adding independent witnesses before counting the pass. The wrapper verifies
cone membership and the exposing equations against the exact rational values
of the input and computes the kernel in the original coordinates. A full PSD
search removes the cone approximation, but a numerical solve or rational
reconstruction can still fail, including when every exposing ray requires
irrational coefficients. When all selected searches end without a verified
direction, or the pass limit is reached, the wrapper solves the current SDP.
A limit of zero skips the searches.

After a reduction, the wrapper appends one result with dual status
`MOI.REDUCTION_CERTIFICATE`. When all passes admit a common original exposing
vector through the recovery procedure, this result exposes the final face.
Otherwise it exposes the deepest prefix recovered in the original coordinates.
For example, if X₁₁ = 0 and X₂₂ + 4X₁₃ = 0, the first pass removes row 1 and
the second removes row 2. In the original dual, a multiplier for the second
equation produces an off-diagonal entry in row 3 with a zero diagonal, so
only the first removal can appear in a single original PSD certificate.
`ReducedFace()` still returns the final face for each native block.

Primal values use the original variables. For a dual solution, the wrapper
applies the adjoints of the face equations and adds multiples of the exposing
vectors in reverse order to recover cone membership [PermenterParrilo2018](@cite).
If this recovery fails, the wrapper reports `MOI.NO_SOLUTION` for the original
dual. For example, minimizing 2X₁₂ subject to X ⪰ 0, X₁₁ = 0 and X₂₂ = 1
attains zero, but an optimal original dual slack would have diagonal entry
zero and off-diagonal entry one in its second row.

```@docs
SumOfSquares.FacialReduction.Optimizer
SumOfSquares.FacialReduction.MaxReductions
SumOfSquares.FacialReduction.SearchCones
SumOfSquares.FacialReduction.ReductionCount
SumOfSquares.FacialReduction.ReducedFace
```
10 changes: 10 additions & 0 deletions docs/src/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,13 @@ @Article{Parrilo2003
issn = {1436-4646},
doi = {10.1007/s10107-003-0387-5},
}

@Article{PermenterParrilo2018,
author = {Permenter, Frank and Parrilo, Pablo A.},
title = {Partial facial reduction: simplified, equivalent {SDPs} via approximations of the {PSD} cone},
journal = {Mathematical Programming},
year = {2018},
volume = {171},
pages = {1--54},
doi = {10.1007/s10107-017-1169-9},
}
53 changes: 53 additions & 0 deletions src/FacialReduction/FacialReduction.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module FacialReduction

import SumOfSquares as SOS
import JuMP
import LinearAlgebra as LA
import MathOptInterface as MOI
import SparseArrays as SP
const Q = Rational{BigInt}
const PSD = MOI.PositiveSemidefiniteConeTriangle
const DD = SOS.DiagonallyDominantConeTriangle
const SDD = SOS.ScaledDiagonallyDominantConeTriangle
const Cone = Union{PSD,MOI.Zeros,MOI.Nonnegatives}
const VF = Union{MOI.VectorOfVariables,MOI.VectorAffineFunction{Float64}}
const SF = Union{MOI.VariableIndex,MOI.ScalarAffineFunction{Float64}}

"""
SearchCones()

Ordered exposing-matrix cones to try at each face. Defaults to
`(SumOfSquares.DiagonallyDominantConeTriangle,
SumOfSquares.ScaledDiagonallyDominantConeTriangle)`. A single cone type
selects a direct search, including `MOI.PositiveSemidefiniteConeTriangle`.
"""
struct SearchCones <: MOI.AbstractOptimizerAttribute end

"""
MaxReductions()

Maximum number of successful reduction passes before solving the current SDP.
"""
struct MaxReductions <: MOI.AbstractOptimizerAttribute end

"""
ReducedFace()

Constraint attribute on native vector cones, available after optimization.
For a PSD block, the columns of the returned matrix span its retained face:
the original slack is `U * Z * U'` with `Z ⪰ 0`. For an orthant they select
the surviving coordinates. Each matrix is in the original block coordinates.
"""
struct ReducedFace <: MOI.AbstractConstraintAttribute end
MOI.is_set_by_optimize(::ReducedFace) = true

"""Number of successful reduction passes in the last solve."""
struct ReductionCount <: MOI.AbstractModelAttribute end
MOI.is_set_by_optimize(::ReductionCount) = true

include("faces.jl")
include("search.jl")
include("lifting.jl")
include("optimizer.jl")

end
114 changes: 114 additions & 0 deletions src/FacialReduction/faces.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# A face is parametrized by X = U Z U'. The representative rows of U
# form an identity matrix, so Z is a principal submatrix of X.
mutable struct Block
index::MOI.ConstraintIndex
set::Cone
rows::UnitRange{Int}
reps::Vector{Int}
basis::SP.SparseMatrixCSC{Q,Int}
end

_triangle(i, j) = MOI.Utilities.trimap(i, j)
_embedding(b) = b.basis

function _identity(n)
return SP.sparse(1:n, 1:n, ones(Q, n), n, n)
end

# Keep the representative entries and impose E*X = 0 on the rest.
# E' then lifts the corresponding equality multipliers.
function _maps(b)
n = length(b.rows)
if !(b.set isa PSD)
return b.reps, _identity(n)[setdiff(1:n, b.reps), :]
end
keep =
[_triangle(b.reps[i], j) for (k, j) in enumerate(b.reps) for i in 1:k]
drop = setdiff(1:n, keep)
E = _identity(n)[drop, :]
# Visit only the nonzeros of each pair of basis rows. Their symmetric
# outer product gives the coefficients of (U Z U')[i,j] in packed Z.
Ut = copy(transpose(b.basis))
I, J, V = Int[], Int[], Q[]
for (r, k) in enumerate(drop)
i, j = MOI.Utilities.inverse_trimap(k)
for p in SP.nzrange(Ut, i), q in SP.nzrange(Ut, j)
u, v = SP.rowvals(Ut)[p], SP.rowvals(Ut)[q]
push!(I, r)
push!(J, keep[_triangle(u, v)])
push!(V, SP.nonzeros(Ut)[p] * SP.nonzeros(Ut)[q])
end
end
return keep, E - SP.sparse(I, J, V, length(drop), n)
end

function _data(model, vars)
col = Dict(x => i + 1 for (i, x) in enumerate(vars))
I, J, V = Int[], Int[], Q[]
blocks = Block[]
nrows = 0
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
for ci in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
set = MOI.get(model, MOI.ConstraintSet(), ci)
f = convert(
MOI.VectorAffineFunction{Float64},
MOI.get(model, MOI.ConstraintFunction(), ci),
)
rows = nrows .+ (1:length(f.constants))
for (i, v) in enumerate(f.constants)
if !iszero(v)
push!(I, rows[i])
push!(J, 1)
push!(V, v)
end
end
for t in f.terms
push!(I, rows[t.output_index])
push!(J, col[t.scalar_term.variable])
push!(V, t.scalar_term.coefficient)
end
n = set isa PSD ? set.side_dimension : length(rows)
reps = set isa MOI.Zeros ? Int[] : collect(1:n)
push!(blocks, Block(ci, set, rows, reps, _identity(n)[:, reps]))
nrows += length(rows)
end
end
return blocks, SP.sparse(I, J, V, nrows, length(vars) + 1)
end

# Sparse exact column elimination. The dependency for a free column has
# that column's coefficient equal to one and uses only earlier pivots.
function _eliminate(A, coefficients = nothing)
basis = Dict{Int,Tuple{Dict{Int,Q},Dict{Int,Q}}}()
pivots = Int[]
x = zeros(Q, size(A, 2))
for j in axes(A, 2)
rows, vals = SP.findnz(A[:, j])
v = Dict(i => Q(a) for (i, a) in zip(rows, vals) if !iszero(a))
w = isnothing(coefficients) ? Dict{Int,Q}() : Dict(j => one(Q))
while !isempty(v)
i = minimum(keys(v))
a = v[i]
if !haskey(basis, i)
for d in (v, w), k in keys(d)
d[k] /= a
end
basis[i] = (v, w)
push!(pivots, j)
break
end
for (d, p) in zip((v, w), basis[i]), (k, value) in p
d[k] = get(d, k, zero(Q)) - a * value
if iszero(d[k])
delete!(d, k)
end
end
end
if isempty(v) && !isnothing(coefficients)
for (k, a) in w
x[k] += a * coefficients[j]
end
end
end
return pivots, x
end
Loading
Loading