Skip to content

Commit

Permalink
Move in methods from SumOfSquares
Browse files Browse the repository at this point in the history
  • Loading branch information
joehuchette committed Dec 29, 2017
1 parent 088d102 commit 34e9c2b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/constraint.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function addpolyconstraint!(m::JuMP.Model, p, s::ZeroPoly, domain::FullSpace)
constraints = JuMP.constructconstraint!.(coefficients(p), :(==))
JuMP.addVectorizedConstraint(m, constraints)
end

function addpolyconstraint!(m::JuMP.Model, p, s::ZeroPoly, domain::AbstractAlgebraicSet)
addpolyconstraint!(m, rem(p, ideal(domain)), s, FullSpace())
end

function addpolyconstraint!(m::JuMP.Model, p, s::ZeroPoly, domain::BasicSemialgebraicSet)
addpolyconstraint!(m, p, NonNegPoly(), domain)
addpolyconstraint!(m, -p, NonNegPoly(), domain)
nothing
end

addpolyconstraint!(m::JuMP.Model, args...) = error("PolyJuMP is just a JuMP extension for modelling Polynomial Optimization: it does not implement any reformulation. To use automatic sums of squares (SOS) reformulations, install the SumOfSquares Julia package and try \`using SumOfSquares\` and \`setpolymodule!(SumOfSquares)\` or use \`SOSModel\` instead of \`Model\`.")
11 changes: 1 addition & 10 deletions src/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@ setpolymodule!(data::PolyData, pm::Module) = data.polymodule = pm
getpolymodule(m::JuMP.Model) = getpolymodule(getpolydata(m))
function getpolymodule(data::PolyData)
if isnull(data.polymodule)
return DefaultModule
return PolyJuMP
end
get(data.polymodule)
end

module DefaultModule
import JuMP, PolyJuMP, MultivariatePolynomials
function createpoly(m::JuMP.Model, p::Union{PolyJuMP.Poly{false, :Default}, PolyJuMP.Poly{false, :Classic}}, category::Symbol)
MultivariatePolynomials.polynomial([JuMP.Variable(m, -Inf, Inf, category)*p.x[i] for i in 1:length(p.x)])
end

addpolyconstraint!(m::JuMP.Model, args...) = error("PolyJuMP is just a JuMP extension for modelling Polynomial Optimization: it does not implement any reformulation. To use automatic sums of squares (SOS) reformulations, install the SumOfSquares Julia package and try \`using SumOfSquares\` and \`setpolymodule!(SumOfSquares)\` or use \`SOSModel\` instead of \`Model\`.")
end
16 changes: 16 additions & 0 deletions src/variable.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# x should be sorted and without duplicates
function _createpoly(m::JuMP.Model, ::Poly{false}, x::AbstractVector{<:AbstractMonomial}, category::Symbol)
polynomial((i) -> Variable(m, -Inf, Inf, category), x)
end

function createpoly(m::JuMP.Model, p::Poly{false, :Gram}, category::Symbol)
_createpoly(m, p, monomials(sum(p.x)^2), category)
end

function createpoly(m::JuMP.Model, p::Union{Poly{false, :Default,Array{T}}, Poly{false, :Classic,Array{T}}}, category::Symbol) where T <: MultivariatePolynomials.AbstractMonomial
_createpoly(m, p, monovec(p.x), category)
end

function createpoly(m::JuMP.Model, p::Union{PolyJuMP.Poly{false, :Default}, PolyJuMP.Poly{false, :Classic}}, category::Symbol)
MultivariatePolynomials.polynomial([JuMP.Variable(m, -Inf, Inf, category)*p.x[i] for i in 1:length(p.x)])
end
2 changes: 1 addition & 1 deletion test/polymodule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
m = Model()
# Triggers the creation of polydata
@test isnull(PolyJuMP.getpolydata(m).polymodule)
@test PolyJuMP.getpolymodule(m) == PolyJuMP.DefaultModule
@test PolyJuMP.getpolymodule(m) == PolyJuMP
setpolymodule!(m, TestPolyModule)
@test PolyJuMP.getpolymodule(m) == TestPolyModule
end

0 comments on commit 34e9c2b

Please sign in to comment.