Skip to content

Commit

Permalink
Add DefaultModule
Browse files Browse the repository at this point in the history
  • Loading branch information
joehuchette committed Dec 27, 2017
1 parent 2edda32 commit 8a3d567
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
12 changes: 1 addition & 11 deletions src/PolyJuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,9 @@ function getpolyconstr(m::JuMP.Model)
getpolydata(m).polyconstr
end

setpolymodule!(m::JuMP.Model, pm::Module) = setpolymodule!(getpolydata(m), pm)
setpolymodule!(data::PolyData, pm::Module) = data.polymodule = pm

getpolymodule(m::JuMP.Model) = getpolymodule(getpolydata(m))
function getpolymodule(data::PolyData)
if isnull(data.polymodule)
error("PolyJuMP is just a JuMP extension for modelling Polynomial Optimization but it does not implements any reformulation. You might want to run \`Pkg.add(\"SumOfSquares\")\` to install the Sum of Squares reformulation. If it is installed you can do \`using SumOfSquares\` and then \`setpolymodule!(SumOfSquares)\` to use it or use \`SOSModel\` instead of \`Model\`.")
end
get(data.polymodule)
end

include("macros.jl")
include("solve.jl")
include("module.jl")

include("deprecated.jl")

Expand Down
19 changes: 19 additions & 0 deletions src/module.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
setpolymodule!(m::JuMP.Model, pm::Module) = setpolymodule!(getpolydata(m), pm)
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
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 but it does not implements any reformulation. You might want to run \`Pkg.add(\"SumOfSquares\")\` to install the Sum of Squares reformulation. If it is installed you can do \`using SumOfSquares\` and then \`setpolymodule!(SumOfSquares)\` to use it or use \`SOSModel\` instead of \`Model\`.")
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_throws ErrorException PolyJuMP.getpolymodule(m)
@test PolyJuMP.getpolymodule(m) == PolyJuMP.DefaultModule
setpolymodule!(m, TestPolyModule)
@test PolyJuMP.getpolymodule(m) == TestPolyModule
end

0 comments on commit 8a3d567

Please sign in to comment.