Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNMY: updates for JuMP 0.21 #148

Merged
merged 5 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ SemialgebraicSets = "8e049039-38e8-557d-ae3a-bc521ccf6204"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
DynamicPolynomials = "0.3.6"
lkapelevich marked this conversation as resolved.
Show resolved Hide resolved
JuMP = "0.20"
lkapelevich marked this conversation as resolved.
Show resolved Hide resolved
MathOptInterface = "~0.9.9"
MultivariateBases = "0.1.1"
MultivariateMoments = "0.3"
lkapelevich marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
8 changes: 4 additions & 4 deletions examples/Polynomial_Optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"source": [
"using JuMP\n",
"using Ipopt\n",
"model = Model(with_optimizer(Ipopt.Optimizer, print_level=0))\n",
"model = Model(optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0))\n",
"@variable(model, a >= 0)\n",
"@variable(model, b >= 0)\n",
"@constraint(model, a + b >= 1)\n",
Expand Down Expand Up @@ -137,7 +137,7 @@
"source": [
"using JuMP\n",
"using Ipopt\n",
"model = Model(with_optimizer(Ipopt.Optimizer, print_level=0))\n",
"model = Model(optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0))\n",
"@variable(model, a >= 0)\n",
"@variable(model, b >= 0)\n",
"@constraint(model, a + b >= 1)\n",
Expand Down Expand Up @@ -188,7 +188,7 @@
"outputs": [],
"source": [
"using CSDP\n",
"optimizer = with_optimizer(CSDP.Optimizer, printlevel=0);"
"optimizer = optimizer_with_attributes(CSDP.Optimizer, "printlevel" => 0);"
]
},
{
Expand All @@ -200,7 +200,7 @@
"outputs": [],
"source": [
"using MosekTools\n",
"optimizer = with_optimizer(Mosek.Optimizer, QUIET=true);"
"optimizer = optimizer_with_attributes(Mosek.Optimizer, "QUIET" => true);"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/Sum-of-Squares Matrices.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"outputs": [],
"source": [
"using CSDP\n",
"factory = with_optimizer(CSDP.Optimizer, printlevel=0);"
"factory = optimizer_with_attributes(CSDP.Optimizer, "printlevel" => 0);"
]
},
{
Expand All @@ -87,7 +87,7 @@
"outputs": [],
"source": [
"using MosekTools\n",
"factory = with_optimizer(Mosek.Optimizer, QUIET=true);"
"factory = optimizer_with_attributes(Mosek.Optimizer, "QUIET" => true);"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/chordal_sparsity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function test(test_function, factory)
end

@testset "chordal_sos" begin
factory = with_optimizer(Mosek.Optimizer)
factory = Mosek.Optimizer

val = test(chained_singular(12), factory)
@test abs(val) < 1e-6
Expand Down
2 changes: 1 addition & 1 deletion examples/chordal_sparsity_with_domain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function sos_lower_bound(factory, sparse::Sparsity)
return objective_value(model)
end

const factory = with_optimizer(Mosek.Optimizer)
const factory = Mosek.Optimizer

# use chordal sparsity
sparse_value = sos_lower_bound(factory, VariableSparsity())
Expand Down
2 changes: 1 addition & 1 deletion examples/goldsteinprice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using MosekTools
@polyvar x1 x2

# Create a Sum of Squares JuMP model with the Mosek solver
model = SOSModel(with_optimizer(Mosek.Optimizer))
model = SOSModel(Mosek.Optimizer)

# Create a JuMP decision variable for the lower bound
@variable(model, γ)
Expand Down
4 changes: 2 additions & 2 deletions examples/motzkin.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"outputs": [],
"source": [
"using SumOfSquares, CSDP\n",
"factory = with_optimizer(CSDP.Optimizer);"
"factory = CSDP.Optimizer;"
]
},
{
Expand All @@ -65,7 +65,7 @@
"outputs": [],
"source": [
"using SumOfSquares, MosekTools\n",
"factory = with_optimizer(Mosek.Optimizer);"
"factory = Mosek.Optimizer;"
]
},
{
Expand Down
12 changes: 7 additions & 5 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ matrix_cone_type(::SDSOSPoly) = ScaledDiagonallyDominantConeTriangle
const PosPoly{PB} = Union{DSOSPoly{PB}, SDSOSPoly{PB}, SOSPoly{PB}}

JuMP.variable_type(m::JuMP.Model, p::PosPoly) = PolyJuMP.polytype(m, p, p.polynomial_basis)
lkapelevich marked this conversation as resolved.
Show resolved Hide resolved
function PolyJuMP.polytype(m::JuMP.Model, cone::PosPoly,
basis::AbstractPolynomialBasis)
return GramMatrix{JuMP.VariableRef, typeof(basis), JuMP.AffExpr}
end

# TODO can all commented code be deleted?
# function PolyJuMP.polytype(m::JuMP.Model, cone::PosPoly,
lkapelevich marked this conversation as resolved.
Show resolved Hide resolved
# basis::AbstractPolynomialBasis)
# return GramMatrix{JuMP.VariableRef, typeof(basis), JuMP.AffExpr}
# end

# Sum-of-Squares polynomial

_polytype(m::JuMP.Model, ::PosPoly, x::MVT) where {MT<:MP.AbstractMonomial, MVT<:AbstractVector{MT}} = GramMatrix{JuMP.VariableRef, MT, MVT}
# _polytype(m::JuMP.Model, ::PosPoly, x::MVT) where {MT<:MP.AbstractMonomial, MVT<:AbstractVector{MT}} = GramMatrix{JuMP.VariableRef, MT, MVT}
lkapelevich marked this conversation as resolved.
Show resolved Hide resolved

function moi_add_variable(model::MOI.ModelLike, set::MOI.AbstractVectorSet,
binary::Bool, integer::Bool)
Expand Down
2 changes: 1 addition & 1 deletion test/Mock/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function cached_mock(
# If we just return `cached`, it will be emptied in `_model` and the state
# will be `MOIU.ATTACHED_OPTIMIZER` which is not what we want. For this
# reason we return a `JuMP.OptimizerFactory` which returns `cached` instead.
with_optimizer(() -> begin
return (() -> begin
cached = MOIU.CachingOptimizer(cache(), MOIU.AUTOMATIC)
optimizer = bridged_mock(args...; kws...)
MOIU.reset_optimizer(cached, optimizer)
Expand Down
2 changes: 1 addition & 1 deletion test/Solvers/csdp_tests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include("solver_preamble.jl")
import CSDP
factory = with_optimizer(CSDP.Optimizer, printlevel=0)
factory = optimizer_with_attributes(CSDP.Optimizer, "printlevel" => 0)
config = MOI.Test.TestConfig(atol=1e-4, rtol=1e-4, query=false)
@testset "Linear" begin
Tests.linear_test(factory, config)
Expand Down
2 changes: 1 addition & 1 deletion test/Solvers/glpk_tests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include("solver_preamble.jl")
import GLPK
factory = with_optimizer(GLPK.Optimizer)
factory = GLPK.Optimizer
config = MOI.Test.TestConfig(atol=1e-5, rtol=1e-5, query=false)
@testset "Linear" begin
# With `dsos_horn_test`, the termination status is
Expand Down
4 changes: 2 additions & 2 deletions test/Tests/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function _model(optimizer::MOI.AbstractOptimizer)
return direct_model(optimizer)
end

function _model(factory::OptimizerFactory)
function _model(factory)
return Model(factory)
end

Expand Down Expand Up @@ -37,7 +37,7 @@ macro test_suite(setname, subsets=false)
runtest = :( f(model, config) )
end
esc(:(
function $testname(model::Union{$MOI.ModelLike, OptimizerFactory},
function $testname(model, # could be ModelLike or an optimizer constructor
config::$MOI.Test.TestConfig,
exclude::Vector{String} = String[])
for (name,f) in $testdict
Expand Down
2 changes: 1 addition & 1 deletion test/certificate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end
end

@testset "Random SOS should be SOS with $(factory.constructor)" for factory in sdp_factories
@testset "Random SOS should be SOS with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x y
x = [1, x, y, x^2, y^2, x*y]
@test_throws ArgumentError randsos(x, monotype=:Unknown)
Expand Down
2 changes: 1 addition & 1 deletion test/equalitypolyconstr.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testset "Polynomial equality constraint with domain with $(factory.constructor)" for factory in sdp_factories
@testset "Polynomial equality constraint with domain with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar(x, y)

m = SOSModel(factory)
Expand Down
2 changes: 1 addition & 1 deletion test/extract_domain_polyopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using JuMP
using SumOfSquares
using MultivariateMoments

@testset "Polynomial Optimization example with $(factory.constructor)" for factory in sdp_factories
@testset "Polynomial Optimization example with $(factory.optimizer_constructor)" for factory in sdp_factories
isscs(factory) && continue
@polyvar x y
p = x^3 - x^2 + 2x*y -y^2 + y^3
Expand Down
2 changes: 1 addition & 1 deletion test/simplematrixsos.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Example 3.77 and 3.79 of
# Blekherman, G., Parrilo, P. A., & Thomas, R. R. (Eds.).
# Semidefinite optimization and convex algebraic geometry SIAM 2013
@testset "Example 3.77 and 3.79 with $(factory.constructor)" for factory in sdp_factories
@testset "Example 3.77 and 3.79 with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x
P = [x^2-2x+2 x; x x^2]
# Example 3.77
Expand Down
6 changes: 3 additions & 3 deletions test/simplepolysos.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# Example 3.25, 3.35 and 3.38 of
# Blekherman, G., Parrilo, P. A., & Thomas, R. R. (Eds.).
# Semidefinite optimization and convex algebraic geometry SIAM 2013
@testset "Example 3.25 with $(factory.constructor)" for factory in sdp_factories
@testset "Example 3.25 with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x y
m = SOSModel(factory)
@constraint m x^2-x*y^2+y^4+1 >= 0
JuMP.optimize!(m)
@test JuMP.primal_status(m) == MOI.FEASIBLE_POINT
end
@testset "Example 3.35 with $(factory.constructor)" for factory in sdp_factories
@testset "Example 3.35 with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x
m = SOSModel(factory)
@constraint m x^4+4x^3+6x^2+4x+5 in SOSCone() # equivalent to >= 0
JuMP.optimize!(m)
@test JuMP.primal_status(m) == MOI.FEASIBLE_POINT
# p(x) = (x^2+2x)^2 + 2(1+x)^2 + 3
end
@testset "Example 3.38 with $(factory.constructor)" for factory in sdp_factories
@testset "Example 3.38 with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x y
m = SOSModel(factory)
@constraint m 2x^4 + 5y^4 - x^2*y^2 >= -2(x^3*y + x + 1)
Expand Down
17 changes: 6 additions & 11 deletions test/solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@ mos = try_import(:MosekTools)
csd = try_import(:CSDP)
scs = try_import(:SCS)

# See https://github.com/JuliaOpt/JuMP.jl/pull/1396
function with_opt(mod::Module, args...; kwargs...)
JuMP.OptimizerFactory(mod.Optimizer, args, kwargs)
end

# Semidefinite factories
sdp_factories = JuMP.OptimizerFactory[]
sdp_factories = MOI.OptimizerWithAttributes[]
# Need at least Mosek 8 for sosdemo3 to pass; see:
# https://github.com/JuliaOpt/Mosek.jl/issues/92
# and at least Mosek 8.0.0.41 for sosdemo6 to pass; see:
# https://github.com/JuliaOpt/Mosek.jl/issues/98
mos && push!(sdp_factories, with_optimizer(MosekTools.Mosek.Optimizer, QUIET=true))
mos && push!(sdp_factories, optimizer_with_attributes(MosekTools.Mosek.Optimizer, "QUIET" => true))
# Currently, need to create a file param.csdp to avoid printing, see https://github.com/JuliaOpt/CSDP.jl/issues/15
csd && push!(sdp_factories, with_opt(CSDP, printlevel=0))
iscsdp(factory) = occursin("CSDP", string(factory.constructor))
# csd && push!(sdp_factories, optimizer_with_attributes(CSDP.Optimizer, "printlevel" => 0))
lkapelevich marked this conversation as resolved.
Show resolved Hide resolved
iscsdp(factory) = occursin("CSDP", string(factory.optimizer_constructor))
# Need 54000 iterations for sosdemo3 to pass on Linux 64 bits
# With 55000, sosdemo3 passes for every platform except Windows 64 bits on AppVeyor
scs && push!(sdp_factories, with_opt(SCS, eps=1e-6, max_iters=60000, verbose=0))
isscs(factory) = occursin("SCS", string(factory.constructor))
# scs && push!(sdp_factories, optimizer_with_attributes(SCS.Optimizer, "eps" => 1e-6, "max_iters" => 60000, "verbose" => 0))
lkapelevich marked this conversation as resolved.
Show resolved Hide resolved
isscs(factory) = occursin("SCS", string(factory.optimizer_constructor))
2 changes: 1 addition & 1 deletion test/sosdemo1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SOSDEMO1 --- Sum of Squares Test
# Section 3.1 of SOSTOOLS User's Manual

@testset "SOSDEMO1 with $(factory.constructor)" for factory in sdp_factories
@testset "SOSDEMO1 with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x y

m = SOSModel(factory)
Expand Down
2 changes: 1 addition & 1 deletion test/sosdemo2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SOSDEMO2 --- Lyapunov Function Search
# Section 3.2 of SOSTOOLS User's Manual

@testset "SOSDEMO2 with $(factory.constructor)" for factory in sdp_factories
@testset "SOSDEMO2 with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x[1:3]

# Constructing the vector field dx/dt = f
Expand Down
2 changes: 1 addition & 1 deletion test/sosdemo3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SOSDEMO3 --- Bound on Global Extremum
# Section 3.3 of SOSTOOLS User's Manual

@testset "SOSDEMO3 with $(factory.constructor)" for factory in sdp_factories
@testset "SOSDEMO3 with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x1 x2

m = SOSModel(factory)
Expand Down
4 changes: 2 additions & 2 deletions test/sosdemo8.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SOSDEMO8 --- Bounds in Probability
# Section 3.8 of SOSTOOLS User's Manual

@testset "SOSDEMO8 with $(factory.constructor)" for factory in sdp_factories
@testset "SOSDEMO8 with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x

# The probability adds up to one.
Expand All @@ -28,7 +28,7 @@
P = a + b*x + c*x^2

# Nonnegative on the support
K = @set 0 <= x && x <= 5
K = @set 0 <= x && x <= 5
cons = @constraint(m, P >= 0, domain = K)

# Greater than one on the event
Expand Down
8 changes: 4 additions & 4 deletions test/sosquartic.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Test quadratic module when mindegree of polynomial is strictly bigger than 0

@testset "SOSquartic with $(factory.constructor)" for factory in sdp_factories
@testset "SOSquartic with $(factory.optimizer_constructor)" for factory in sdp_factories
@polyvar x y
m = SOSModel(factory)
p = x^4 - y^4
p = x^4 - y^4
S = @set - x^2 - y^4 >=0
soscon = @constraint m p >= 0 domain = S
soscon = @constraint m p >= 0 domain = S

JuMP.optimize!(m)

@test JuMP.primal_status(m) == MOI.FEASIBLE_POINT
Q = gram_matrix(soscon)
@test mindegree(monomials(Q)) == 2
Expand Down