Skip to content

Commit

Permalink
Update to MOI v0.10 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Oct 28, 2021
1 parent 306227c commit eceba3b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
MATLAB = "0.7, 0.8"
MathOptInterface = "0.9.7"
MathOptInterface = "0.10.4"
julia = "1"

[extras]
Expand Down
33 changes: 21 additions & 12 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,32 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
Dict{String, Any}(), Dict{String, Any}(),
nothing, NaN,
false, Dict{Symbol, Any}())
if !isempty(kwargs)
@warn("""Passing optimizer attributes as keyword arguments to
SDPT3.Optimizer is deprecated. Use
MOI.set(model, MOI.RawOptimizerAttribute("key"), value)
or
JuMP.set_optimizer_attribute(model, "key", value)
instead.
""")
end
for (key, value) in kwargs
MOI.set(optimizer, MOI.RawParameter(string(key)), value)
MOI.set(optimizer, MOI.RawOptimizerAttribute(string(key)), value)
end
return optimizer
end
end

function MOI.supports(optimizer::Optimizer, param::MOI.RawParameter)
function MOI.supports(optimizer::Optimizer, param::MOI.RawOptimizerAttribute)
return param.name in ALLOWED_OPTIONS
end
function MOI.set(optimizer::Optimizer, param::MOI.RawParameter, value)
function MOI.set(optimizer::Optimizer, param::MOI.RawOptimizerAttribute, value)
if !MOI.supports(optimizer, param)
throw(MOI.UnsupportedAttribute(param))
end
optimizer.options[Symbol(param.name)] = value
end
function MOI.get(optimizer::Optimizer, param::MOI.RawParameter)
function MOI.get(optimizer::Optimizer, param::MOI.RawOptimizerAttribute)
# TODO: This gives a poor error message if the name of the parameter is invalid.
return optimizer.options[Symbol(param.name)]
end
Expand All @@ -115,7 +124,7 @@ end
MOI.get(optimizer::Optimizer, ::MOI.Silent) = optimizer.silent

MOI.get(::Optimizer, ::MOI.SolverName) = "SDPT3"
function MOI.get(optimizer::Optimizer, ::MOI.SolveTime)
function MOI.get(optimizer::Optimizer, ::MOI.SolveTimeSec)
return optimizer.solve_time
end

Expand Down Expand Up @@ -203,10 +212,10 @@ function MOI.supports_constraint(
return true
end

function MOI.copy_to(dest::Optimizer, src::MOI.ModelLike; kws...)
return MOIU.automatic_copy_to(dest, src; kws...)
function MOI.copy_to(dest::Optimizer, src::MOI.ModelLike)
return MOIU.default_copy_to(dest, src)
end
MOIU.supports_default_copy_to(::Optimizer, copy_names::Bool) = !copy_names
MOI.supports_incremental_interface(::Optimizer) = true

# Variables
function MOI.add_variable(optimizer::Optimizer)
Expand Down Expand Up @@ -297,7 +306,7 @@ function MOI.set(optimizer::Optimizer, ::MOI.ObjectiveFunction{MOI.ScalarAffineF
end
sign = sense_to_sign(optimizer.objective_sense)
for term in func.terms
info = optimizer.variable_info[term.variable_index.value]
info = optimizer.variable_info[term.variable.value]
if info.variable_type == FREE
push!(optimizer.free_Cvar, info.index_in_cone)
push!(optimizer.free_Cval, sign * term.coefficient)
Expand Down Expand Up @@ -332,7 +341,7 @@ function MOI.add_constraint(optimizer::Optimizer, func::MOI.ScalarAffineFunction
push!(optimizer.b, MOI.constant(set))
con = length(optimizer.b)
for term in func.terms
info = optimizer.variable_info[term.variable_index.value]
info = optimizer.variable_info[term.variable.value]
if info.variable_type == FREE
push!(optimizer.free_Avar, info.index_in_cone)
push!(optimizer.free_Acon, con)
Expand Down Expand Up @@ -515,7 +524,7 @@ function MOI.get(optimizer::Optimizer, ::MOI.TerminationStatus)
end

function MOI.get(optimizer::Optimizer, attr::MOI.PrimalStatus)
if attr.N > MOI.get(optimizer, MOI.ResultCount())
if attr.result_index > MOI.get(optimizer, MOI.ResultCount())
return MOI.NO_SOLUTION
end
status = optimizer.status
Expand All @@ -530,7 +539,7 @@ function MOI.get(optimizer::Optimizer, attr::MOI.PrimalStatus)
end

function MOI.get(optimizer::Optimizer, attr::MOI.DualStatus)
if attr.N > MOI.get(optimizer, MOI.ResultCount())
if attr.result_index > MOI.get(optimizer, MOI.ResultCount())
return MOI.NO_SOLUTION
end
status = optimizer.status
Expand Down
17 changes: 8 additions & 9 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Test

using MathOptInterface
const MOI = MathOptInterface
const MOIT = MOI.Test
const MOIT = MOI.DeprecatedTest
const MOIU = MOI.Utilities
const MOIB = MOI.Bridges

Expand All @@ -14,22 +14,21 @@ MOI.set(OPTIMIZER, MOI.Silent(), true)
@test MOI.get(OPTIMIZER, MOI.SolverName()) == "SDPT3"
end

@testset "supports_default_copy_to" begin
@test MOIU.supports_default_copy_to(OPTIMIZER, false)
@test !MOIU.supports_default_copy_to(OPTIMIZER, true)
@testset "supports_incremental_interface" begin
@test MOI.supports_incremental_interface(OPTIMIZER)
end

# UniversalFallback is needed for starting values, even if they are ignored by SDPT3
const CACHE = MOIU.UniversalFallback(MOIU.Model{Float64}())
const CACHED = MOIU.CachingOptimizer(CACHE, OPTIMIZER)
const BRIDGED = MOIB.full_bridge_optimizer(CACHED, Float64)
const CONFIG = MOIT.TestConfig(atol=1e-4, rtol=1e-4)
const CONFIG = MOIT.Config(atol=1e-4, rtol=1e-4)

@testset "Options" begin
optimizer = SDPT3.Optimizer(printlevel = 1)
@test MOI.get(optimizer, MOI.RawParameter("printlevel")) == 1
@test MOI.get(optimizer, MOI.RawOptimizerAttribute("printlevel")) == 1

param = MOI.RawParameter("bad_option")
param = MOI.RawOptimizerAttribute("bad_option")
err = MOI.UnsupportedAttribute(param)
@test_throws err SDPT3.Optimizer(bad_option = 1)
end
Expand Down Expand Up @@ -75,11 +74,11 @@ end
@testset "Continuous Conic" begin
MOIT.contconictest(BRIDGED, CONFIG, [
# `MOI.OTHER_ERROR`
"lin2f",
"lin2f", "geomean3v",
# `MOI.NUMERICAL_ERROR`
"lin4",
# `ExponentialCone` and `PowerCone` not supported.
"exp", "dualexp", "pow", "dualpow", "logdet",
"exp", "dualexp", "pow", "dualpow", "logdet", "relentr",
# `RootDetConeSquare` -> `RootDetConeTriangle` bridge missing.
"rootdets"
])
Expand Down

0 comments on commit eceba3b

Please sign in to comment.