Skip to content

Commit

Permalink
Merge pull request #91 from tkoolen/tk/update-moi-0.6
Browse files Browse the repository at this point in the history
Update to MOI 0.6.
  • Loading branch information
tkoolen authored Nov 20, 2018
2 parents 45a4bdc + b465cf8 commit 2518951
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 44 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Here's the basic problem setup:

```julia
# create a MathOptInterface optimizer instance
using OSQP.MathOptInterfaceOSQP
optimizer = OSQPOptimizer()
using OSQP
optimizer = OSQP.Optimizer()

# create a Parametron.Model, which holds problem information
using Parametron
Expand Down Expand Up @@ -127,7 +127,7 @@ julia> value.(model, x)
Note that the solver is warm-started. Also note that updating the parameters and solving a new QP instance is quite fast:

```julia
julia> using MathOptInterface; MathOptInterface.set!(optimizer, OSQPSettings.Verbose(), false) # silence the optimizer
julia> using MathOptInterface; using OSQP.MathOptInterfaceOSQP: OSQPSettings; MathOptInterface.set!(optimizer, OSQPSettings.Verbose(), false) # silence the optimizer

julia> using BenchmarkTools

Expand Down Expand Up @@ -166,7 +166,7 @@ using Random
n, m = 5, 15
Xdata = randn(n, m)
pdata = Vector{Float64}(undef, m);
model = Model(OSQPOptimizer())
model = Model(OSQP.Optimizer())
X = Parameter(model, val=Xdata)
p = Parameter(model, val=pdata)
g = [Variable(model) for _ = 1:n]
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.7
MathOptInterface 0.5 0.6
MathOptInterface 0.6 0.7
MacroTools
FunctionWrappers 0.1.0
DocStringExtensions
8 changes: 4 additions & 4 deletions docs/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "2d9e14d19bad3f9ad5cc5e4cffabc3cfa59de825"
git-tree-sha1 = "ec61a16eed883ad0cfa002d7489b3ce6d039bb9a"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "1.3.0"
version = "1.4.0"

[[Dates]]
deps = ["Printf"]
Expand Down Expand Up @@ -66,9 +66,9 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[MathOptInterface]]
deps = ["Compat", "Unicode"]
git-tree-sha1 = "6bb3530fb9ed16a2f9324d6de54ec5ab9e5822e0"
git-tree-sha1 = "e645d549092a3a9405eef089836440dd68feaf6b"
uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
version = "0.5.1"
version = "0.6.3"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
Expand Down
34 changes: 17 additions & 17 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ mutable struct Model{T, O<:MOI.AbstractOptimizer}
backend = ParametronMOIModel{T}()
initialized = false
objective = Objective(T, @expression zero(AffineFunction{T}))
MOI.set!(backend, MOI.ObjectiveSense(), MOI.OptimizationSense(Minimize))
MOI.set!(backend, MOI.ObjectiveFunction{typeof(objective.f)}(), objective.f)
MOI.set(backend, MOI.ObjectiveSense(), MOI.OptimizationSense(Minimize))
MOI.set(backend, MOI.ObjectiveFunction{typeof(objective.f)}(), objective.f)
constraints = Constraints{T}()
model_var_to_optimizer = Vector{MOI.VariableIndex}()
new{T, O}(params, backend, optimizer, initialized, objective, constraints, model_var_to_optimizer)
Expand Down Expand Up @@ -48,7 +48,7 @@ Create a new decision variable (`Variable`) associated with the model.
"""
function Variable(m::Model)
m.initialized && error("Model has already been initialized.")
index = MOI.addvariable!(m.backend)
index = MOI.add_variable(m.backend)
Variable(index)
end

Expand All @@ -60,38 +60,38 @@ Set the objective function and optimization sense (`Minimize` or `Maximize`).
function setobjective!(m::Model{T}, sense::Sense, expr) where T
m.initialized && error("Model was already initialized. setobjective! can only be called before initialization.")
m.objective = Objective(T, expr)
MOI.set!(m.backend, MOI.ObjectiveSense(), MOI.OptimizationSense(sense))
MOI.set!(m.backend, MOI.ObjectiveFunction{typeof(m.objective.f)}(), m.objective.f)
MOI.set(m.backend, MOI.ObjectiveSense(), MOI.OptimizationSense(sense))
MOI.set(m.backend, MOI.ObjectiveFunction{typeof(m.objective.f)}(), m.objective.f)
nothing
end

function addconstraint!(m::Model, constraint::Constraint)
m.initialized && error("Model was already initialized. addconstraint! can only be called before initialization.")
constraint.modelindex = MOI.addconstraint!(m.backend, constraint.f, constraint.set)
function add_constraint(m::Model, constraint::Constraint)
m.initialized && error("Model was already initialized. add_constraint can only be called before initialization.")
constraint.modelindex = MOI.add_constraint(m.backend, constraint.f, constraint.set)
push!(m.constraints, constraint)
nothing
end

_add_nonnegative_constraint!(m::Model{T}, expr, val::AffineFunction) where {T} =
addconstraint!(m, Constraint(T, expr, MOI.GreaterThan(zero(T))))
add_constraint(m, Constraint(T, expr, MOI.GreaterThan(zero(T))))
_add_nonpositive_constraint!(m::Model{T}, expr, val::AffineFunction) where {T} =
addconstraint!(m, Constraint(T, expr, MOI.LessThan(zero(T))))
add_constraint(m, Constraint(T, expr, MOI.LessThan(zero(T))))
_add_zero_constraint!(m::Model{T}, expr, val::AffineFunction) where {T} =
addconstraint!(m, Constraint(T, expr, MOI.EqualTo(zero(T))))
add_constraint(m, Constraint(T, expr, MOI.EqualTo(zero(T))))

_add_nonnegative_constraint!(m::Model{T}, expr, val::AbstractVector{<:AffineFunction}) where {T} =
addconstraint!(m, Constraint(T, expr, MOI.Nonnegatives(length(val))))
add_constraint(m, Constraint(T, expr, MOI.Nonnegatives(length(val))))
_add_nonpositive_constraint!(m::Model{T}, expr, val::AbstractVector{<:AffineFunction}) where {T} =
addconstraint!(m, Constraint(T, expr, MOI.Nonpositives(length(val))))
add_constraint(m, Constraint(T, expr, MOI.Nonpositives(length(val))))
_add_zero_constraint!(m::Model{T}, expr, val::AbstractVector{<:AffineFunction}) where {T} =
addconstraint!(m, Constraint(T, expr, MOI.Zeros(length(val))))
add_constraint(m, Constraint(T, expr, MOI.Zeros(length(val))))

add_nonnegative_constraint!(m::Model, expr) = _add_nonnegative_constraint!(m, expr, evalarg(expr))
add_nonpositive_constraint!(m::Model, expr) = _add_nonpositive_constraint!(m, expr, evalarg(expr))
add_zero_constraint!(m::Model, expr) = _add_zero_constraint!(m, expr, evalarg(expr))

add_integer_constraint!(m::Model, x::Variable) = addconstraint!(m, Constraint(MOI.SingleVariable(MOI.VariableIndex(x)), MOI.Integer()))
add_binary_constraint!(m::Model, x::Variable) = addconstraint!(m, Constraint(MOI.SingleVariable(MOI.VariableIndex(x)), MOI.ZeroOne()))
add_integer_constraint!(m::Model, x::Variable) = add_constraint(m, Constraint(MOI.SingleVariable(MOI.VariableIndex(x)), MOI.Integer()))
add_binary_constraint!(m::Model, x::Variable) = add_constraint(m, Constraint(MOI.SingleVariable(MOI.VariableIndex(x)), MOI.ZeroOne()))

function mapindices!(m::Model, idxmap)
mapindices!(m.constraints, idxmap)
Expand All @@ -111,7 +111,7 @@ Users should generally not need to call this function directly, as it is automat
called the first time [`solve!`](@ref) is called on a `Model`.
"""
@noinline function initialize!(m::Model)
indexmap = MOI.copy!(m.optimizer, m.backend)
indexmap = MOI.copy_to(m.optimizer, m.backend)
mapindices!(m, indexmap)
m.initialized = true
nothing
Expand Down
16 changes: 8 additions & 8 deletions src/moi_interop.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# MOI backend AbstractModel
MOIU.@model(ParametronMOIModel, # modelname
(ZeroOne, Integer), # scalarsets
(LessThan, GreaterThan, EqualTo), # typedscalarsets
(Zeros, Nonnegatives, Nonpositives), # vectorsets
(MOI.ZeroOne, MOI.Integer), # scalarsets
(MOI.LessThan, MOI.GreaterThan, MOI.EqualTo), # typedscalarsets
(MOI.Zeros, MOI.Nonnegatives, MOI.Nonpositives), # vectorsets
(), # typedvectorsets
(SingleVariable,), # scalarfunctions
(ScalarAffineFunction,), # typedscalarfunctions
(MOI.SingleVariable,), # scalarfunctions
(MOI.ScalarAffineFunction,), # typedscalarfunctions
(), # vectorfunctions
(VectorAffineFunction,) # typedvectorfunctions
(MOI.VectorAffineFunction,) # typedvectorfunctions
)


Expand Down Expand Up @@ -127,7 +127,7 @@ end

function update!(objective::Objective{E, F}, optimizer::MOI.AbstractOptimizer, varmap) where {E, F}
update!(objective.f, objective.expr(), varmap)
MOI.set!(optimizer, MOI.ObjectiveFunction{F}(), objective.f)
MOI.set(optimizer, MOI.ObjectiveFunction{F}(), objective.f)
nothing
end

Expand Down Expand Up @@ -160,7 +160,7 @@ end

function update!(constraint::Constraint, optimizer::MOI.AbstractOptimizer, varmap)
update!(constraint.f, constraint.expr(), varmap)
MOI.set!(optimizer, MOI.ConstraintFunction(), constraint.optimizerindex, constraint.f)
MOI.set(optimizer, MOI.ConstraintFunction(), constraint.optimizerindex, constraint.f)
nothing
end
update!(constraint::Constraint{Nothing}, optimizer::MOI.AbstractOptimizer, varmap) = nothing
Expand Down
21 changes: 11 additions & 10 deletions test/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ using Test
using Random
using LinearAlgebra
using Parametron
using OSQP.MathOptInterfaceOSQP
using OSQP
using OSQP.MathOptInterfaceOSQP: OSQPSettings
using GLPK
using StaticArrays: SVector

Expand All @@ -13,12 +14,12 @@ import MathOptInterface
const MOI = MathOptInterface

function defaultoptimizer()
optimizer = OSQPOptimizer()
MOI.set!(optimizer, OSQPSettings.Verbose(), false)
MOI.set!(optimizer, OSQPSettings.EpsAbs(), 1e-8)
MOI.set!(optimizer, OSQPSettings.EpsRel(), 1e-16)
MOI.set!(optimizer, OSQPSettings.MaxIter(), 10000)
MOI.set!(optimizer, OSQPSettings.AdaptiveRhoInterval(), 25) # required for deterministic behavior
optimizer = OSQP.Optimizer()
MOI.set(optimizer, OSQPSettings.Verbose(), false)
MOI.set(optimizer, OSQPSettings.EpsAbs(), 1e-8)
MOI.set(optimizer, OSQPSettings.EpsRel(), 1e-16)
MOI.set(optimizer, OSQPSettings.MaxIter(), 10000)
MOI.set(optimizer, OSQPSettings.AdaptiveRhoInterval(), 25) # required for deterministic behavior
optimizer
end

Expand Down Expand Up @@ -235,11 +236,11 @@ end
glpk_works = false
try
optimizer = GLPK.Optimizer()
x = MOI.addvariable!(optimizer)
x = MOI.add_variable(optimizer)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0)
s = MOI.GreaterThan(0.0)
ci = MOI.addconstraint!(optimizer, f, s)
MOI.set!(optimizer, MOI.ConstraintFunction(), ci, f)
ci = MOI.add_constraint(optimizer, f, s)
MOI.set(optimizer, MOI.ConstraintFunction(), ci, f)
global glpk_works = true
catch e
if !(e isa MOI.UnsupportedAttribute{MOI.ConstraintFunction})
Expand Down

0 comments on commit 2518951

Please sign in to comment.