Skip to content
Merged
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
125 changes: 108 additions & 17 deletions src/optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ feasibility_tolerance::T
solve_time::Float64
```
and implement [`nonnegativity_cone`](@ref) and [`recover_solutions`](@ref).
The constraint of the Lagrangian can be customized by implementing
[`nonnegativity_constraint`](@ref).

[HL06] Henrion, Didier and Lasserre, Jean-Bernard.
*Convergent relaxations of polynomial matrix inequalities and static output
Expand Down Expand Up @@ -240,8 +242,9 @@ end
) where {T}

Return a vector of candidate [`Solution`](@ref)s recovered from the solution
of `relaxation`, where `cref` is the constraint of the `lagrangian` polynomial
in the cone [`nonnegativity_cone`](@ref). Return an empty vector if no
of `relaxation`, where `cref` is the constraint returned by
[`nonnegativity_constraint`](@ref) and `lagrangian` is the polynomial
constrained by `cref`. Return an empty vector if no
solution is recovered; `PolyJuMP.SAGE.Optimizer` implements the recovery from
the dual of `cref` of [MCW21, Section 4.2].

Expand Down Expand Up @@ -345,6 +348,68 @@ function _matrix_multiplier(relaxation::JuMP.GenericModel, x, n, degree)
return Σ
end

"""
nonnegativity_constraint(
model::AbstractRelaxationOptimizer,
relaxation::JuMP.GenericModel,
p,
domain::SS.AbstractSemialgebraicSet,
maxdegree::Int,
)

Add to `relaxation` the constraint that the polynomial `p` is nonnegative on
`domain` and return its reference. The set `domain` is either
`SemialgebraicSets.FullSpace()`, an algebraic set truncated at `maxdegree`
with `SemialgebraicSets.truncated` or a basic semialgebraic set whose
algebraic set is one of these two. The multipliers of the equalities and
inequalities of `domain` should be such that their product with the
corresponding equality or inequality has degree at most `maxdegree`.

By default, a free polynomial multiplier `μ_j` of degree
`maxdegree - maxdegree(h_j)` is created for each equality `h_j`, a multiplier
`σ_i` of degree `maxdegree - maxdegree(g_i)` constrained to belong to
[`nonnegativity_cone`](@ref) is created for each inequality `g_i` and
`p - Σ_i σ_i * g_i - Σ_j μ_j * h_j` is constrained to belong to
[`nonnegativity_cone`](@ref). It can be implemented to exploit the structure
of the domain, e.g., `SumOfSquares.Optimizer` gives the domain to the
Sum-of-Squares constraint so that its sparsity pattern takes the supports of
the equalities and inequalities into account.
"""
function nonnegativity_constraint(
model::AbstractRelaxationOptimizer,
relaxation::JuMP.GenericModel,
p,
domain::SS.AbstractSemialgebraicSet,
maxdegree::Int,
)
x = MP.variables(model.model)
cone = nonnegativity_cone(model)
for h in _equalities(domain)
p -= _multiplier(relaxation, x, maxdegree - MP.maxdegree(h)) * h
end
for g in _inequalities(domain)
σ = _multiplier(relaxation, x, maxdegree - MP.maxdegree(g))
JuMP.@constraint(relaxation, σ in cone)
p -= σ * g
end
return JuMP.@constraint(relaxation, p in cone)
end

# The constraints whose multiplier has degree `maxdegree` minus their degree
# are given in the domain of `nonnegativity_constraint`, the others have an
# explicit multiplier.
function _domain(equalities, inequalities, maxdegree)
domain = if isempty(equalities)
SS.FullSpace()
else
SS.truncated(SS.algebraic_set(equalities), maxdegree)
end
if isempty(inequalities)
return domain
end
return SS.BasicSemialgebraicSet(domain, inequalities)
end

function _optimize!(model::AbstractRelaxationOptimizer{T}) where {T}
pop = model.model
x = MP.variables(pop)
Expand All @@ -364,9 +429,9 @@ function _optimize!(model::AbstractRelaxationOptimizer{T}) where {T}
maximum(_maxdegree, mats; init = 0),
)
maxdeg += isodd(maxdeg)
eq_degrees = Dict{Int,Int}()
ineq_degrees = Dict{Int,Int}()
mat_degrees = Dict{Int,Int}()
eq_degrees = [maxdeg - MP.maxdegree(h) for h in eqs]
ineq_degrees = [maxdeg - MP.maxdegree(g) for g in ineqs]
mat_degrees = [maxdeg - _maxdegree(G) for G in mats]
for (ci, d) in model.multiplier_maxdegree
if ci isa _MatrixIndex
mat_degrees[ci.value] = d
Expand All @@ -376,35 +441,61 @@ function _optimize!(model::AbstractRelaxationOptimizer{T}) where {T}
ineq_degrees[ci.value] = d
end
end
# The degree of the products of the multipliers with the polynomials
maxdegree = max(
maxdeg,
maximum(eq_degrees .+ MP.maxdegree.(eqs); init = 0),
maximum(ineq_degrees .+ MP.maxdegree.(ineqs); init = 0),
)
maxdegree += isodd(maxdegree)
relaxation = JuMP.GenericModel{T}(model.solver)
cone = nonnegativity_cone(model)
t = JuMP.@variable(relaxation, base_name = "t")
lagrangian = f - t
if pop.objective_sense == MOI.MAX_SENSE
lagrangian = -lagrangian
end
for (i, h) in enumerate(eqs)
d = get(eq_degrees, i, maxdeg - MP.maxdegree(h))
lagrangian -= _multiplier(relaxation, x, d) * h
domain_eqs = eltype(eqs)[]
for (h, d) in zip(eqs, eq_degrees)
if d == maxdegree - MP.maxdegree(h)
push!(domain_eqs, h)
else
lagrangian -= _multiplier(relaxation, x, d) * h
end
end
for (i, g) in enumerate(ineqs)
d = get(ineq_degrees, i, maxdeg - MP.maxdegree(g))
σ = _multiplier(relaxation, x, d)
JuMP.@constraint(relaxation, σ in cone)
lagrangian -= σ * g
domain_ineqs = eltype(ineqs)[]
for (g, d) in zip(ineqs, ineq_degrees)
if d == maxdegree - MP.maxdegree(g)
push!(domain_ineqs, g)
else
σ = _multiplier(relaxation, x, d)
JuMP.@constraint(relaxation, σ in cone)
lagrangian -= σ * g
end
end
for (i, G) in enumerate(mats)
d = get(mat_degrees, i, maxdeg - _maxdegree(G))
for (G, d) in zip(mats, mat_degrees)
Σ = _matrix_multiplier(relaxation, x, size(G, 1), d)
JuMP.@constraint(relaxation, Σ in matrix_nonnegativity_cone(model))
lagrangian -= sum(G .* Σ)
end
cref = JuMP.@constraint(relaxation, lagrangian in cone)
cref = nonnegativity_constraint(
model,
relaxation,
lagrangian,
_domain(domain_eqs, domain_ineqs, maxdegree),
maxdegree,
)
sense = pop.objective_sense == MOI.MAX_SENSE ? MOI.MIN_SENSE : MOI.MAX_SENSE
JuMP.set_objective(relaxation, sense, t)
JuMP.optimize!(relaxation)
model.relaxation = relaxation
model.solutions = recover_solutions(model, relaxation, cref, lagrangian)
constraint = JuMP.constraint_object(cref)
model.solutions = recover_solutions(
model,
relaxation,
cref,
JuMP.reshape_vector(JuMP.jump_function(constraint), constraint.shape),
)
postprocess!(model.solutions, pop, nothing)
return
end
Expand Down
16 changes: 16 additions & 0 deletions test/sage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ function test_optimizer_equality_max(x, y, T, solver)
@test objective_value(model) ≈ 1 rtol = 1e-3
end

function test_optimizer_explicit_multipliers(x, y, T, solver)
model = Model(() -> PolyJuMP.SAGE.Optimizer{T}(solver))
@variable(model, a)
@objective(model, Min, a^2)
@constraint(model, con, a^2 >= 1)
@constraint(model, a + 2 >= 0)
@constraint(model, a^2 == 1)
# The product of the multiplier of `con` with `a^2 - 1` has degree 4 so
# the multipliers of the other constraints, whose product has degree 2,
# are not given in the domain of `PolyJuMP.nonnegativity_constraint`
MOI.set(model, PolyJuMP.MultiplierMaxdegree(), con, 2)
optimize!(model)
@test termination_status(model) == MOI.OPTIMAL
@test objective_bound(model) ≈ 1 rtol = 1e-3
end

import ECOS
const SOLVERS =
[optimizer_with_attributes(ECOS.Optimizer, MOI.Silent() => true)]
Expand Down
Loading