-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# # Sampling basis | ||
|
||
#md # [![](https://mybinder.org/badge_logo.svg)](@__BINDER_ROOT_URL__/generated/Getting started/sampling.ipynb) | ||
#md # [![](https://img.shields.io/badge/show-nbviewer-579ACA.svg)](@__NBVIEWER_ROOT_URL__/generated/Getting started/sampling.ipynb) | ||
# **Contributed by**: Benoît Legat | ||
|
||
using Test #src | ||
using DynamicPolynomials | ||
using SumOfSquares | ||
import MultivariateBases as MB | ||
import SDPLR | ||
import Hypatia | ||
import SCS | ||
import BMSOS | ||
|
||
# In this tutorial, we show how to use a different polynomial basis | ||
# for enforcing the equality between the polynomial and its Sum-of-Squares decomposition. | ||
|
||
@polyvar x | ||
p = x^4 - 4x^3 - 2x^2 + 12x + 3 | ||
|
||
scs = SCS.Optimizer | ||
sdplr = optimizer_with_attributes(SDPLR.Optimizer, "maxrank" => (m, n) -> 4) | ||
hypatia = Hypatia.Optimizer | ||
bmsos = BMSOS.Optimizer | ||
function test(solver, feas::Bool) | ||
model = Model(solver) | ||
set_silent(model) | ||
if feas | ||
γ = -6 | ||
else | ||
@variable(model, γ) | ||
@objective(model, Max, γ) | ||
end | ||
@constraint(model, p - γ in SOSCone(), zero_basis = BoxSampling([-1], [1])) | ||
optimize!(model) | ||
@test primal_status == MOI.FEASIBLE_POINT | ||
if !feasible | ||
@test value(γ) ≈ -6 rtol=1e-4 | ||
end | ||
end | ||
test(scs) | ||
test(sdplr) | ||
test(hypatia) | ||
test(bmsos, true) | ||
|
||
function test_rand(solver, d, B) | ||
model = Model(solver) | ||
set_silent(model) | ||
p = MB.algebra_element(rand(2d+1), MB.SubBasis{B}(monomials(x, 0:2d))) | ||
@constraint(model, p in SOSCone(), zero_basis = BoxSampling([-1], [1])) | ||
optimize!(model) | ||
return solve_time(model) | ||
end | ||
|
||
test_rand(scs, 2, MultivariateBases.Trigonometric) | ||
test_rand(bmsos, 4, MultivariateBases.Trigonometric) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
struct LowRankBridge{T,M} <: MOI.Bridges.Variable.AbstractBridge | ||
affine::Vector{MOI.ScalarAffineFunction{T}} | ||
variables::Vector{Vector{MOI.VariableIndex}} | ||
constraints::Vector{MOI.ConstraintIndex{MOI.VectorOfVariables}} | ||
set::SOS.WeightedSOSCone{M} | ||
end | ||
|
||
import LinearAlgebra | ||
|
||
function MOI.Bridges.Variable.bridge_constrained_variable( | ||
::Type{LowRankBridge{T,M}}, | ||
model::MOI.ModelLike, | ||
set::SOS.WeightedSOSCone{M}, | ||
) where {T,M} | ||
variables = Vector{Vector{MOI.VariableIndex}}(undef, length(set.gram_bases)) | ||
constraints = Vector{MOI.ConstraintIndex{MOI.VectorOfVariables}}(undef, length(set.gram_bases)) | ||
for i in eachindex(set.gram_bases) | ||
U = MB.transformation_to(set.gram_bases[i], set.basis) | ||
weights = SA.coeffs(set.weights[i], set.basis) | ||
variables[i], constraints[i] = MOI.add_constrained_variables( | ||
model, | ||
MOI.SetWithDotProducts( | ||
SOS.matrix_cone(M, length(set.gram_bases[i])), | ||
[ | ||
MOI.TriangleVectorization( | ||
MOI.LowRankMatrix( | ||
[weights[j]], | ||
reshape(U[j, :], size(U, 2), 1), | ||
) | ||
) | ||
for j in eachindex(set.basis) | ||
], | ||
), | ||
) | ||
end | ||
return KernelBridge{T,M}( | ||
[ | ||
MOI.ScalarAffineFunction( | ||
[MOI.ScalarAffineTerm(one(T), variables[i][j]) for i in eachindex(set.gram_bases)], | ||
zero(T), | ||
) | ||
for j in eachindex(set.basis) | ||
], | ||
variables, | ||
constraints, | ||
set, | ||
) | ||
end | ||
|
||
function MOI.Bridges.Variable.supports_constrained_variable( | ||
::Type{<:LowRankBridge}, | ||
::Type{<:SOS.WeightedSOSCone{M,B}}, | ||
) where {M,B} | ||
# Could be made to work for non-LagrangeBasis but it's not low rank in | ||
# we we'll need a high bridge cost for them so that it's only UnsafeAddMul | ||
# if the other bridges are removed | ||
return B <: MB.LagrangeBasis | ||
end | ||
|
||
function MOI.Bridges.added_constrained_variable_types( | ||
::Type{LowRankBridge{T,M}}, | ||
) where {T,M} | ||
return Tuple{Type}[ | ||
(MOI.SetWithDotProducts{S[1],MOI.TriangleVectorization{MOI.LowRankMatrix{T}}},) | ||
for S in SOS.Bridges.Constraint.constrained_variable_types(M) | ||
if S[1] == MOI.PositiveSemidefiniteConeTriangle # FIXME hack | ||
] | ||
end | ||
|
||
function MOI.Bridges.added_constraint_types(::Type{<:LowRankBridge}) | ||
return Tuple{Type,Type}[] | ||
end | ||
|
||
function MOI.Bridges.Variable.concrete_bridge_type( | ||
::Type{<:LowRankBridge{T}}, | ||
::Type{<:SOS.WeightedSOSCone{M}}, | ||
) where {T,M} | ||
return LowRankBridge{T,M} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters