Skip to content

Parametrize CachingOptimizer and MockOptimizer on model type #323

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

Merged
merged 2 commits into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions perf/cachingoptimizer.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# See https://github.com/JuliaOpt/MathOptInterface.jl/issues/321 and https://github.com/JuliaOpt/MathOptInterface.jl/pull/323

using BenchmarkTools
using MathOptInterface
const MOI = MathOptInterface
const MOIU = MOI.Utilities

@MOIU.model Model () (Interval,) () () () (ScalarAffineFunction,) () ()
optimizer = MOIU.MockOptimizer(Model{Float64}())
caching_optimizer = MOIU.CachingOptimizer(Model{Float64}(), optimizer)
MOIU.resetoptimizer!(caching_optimizer) # detach optimizer
v = MOI.addvariables!(caching_optimizer, 2)
cf = MOI.ScalarAffineFunction(v, [0.0, 0.0], 0.0)
c = MOI.addconstraint!(caching_optimizer, cf, MOI.Interval(-Inf, 1.0))
@btime MOI.modifyconstraint!($caching_optimizer, $c, $(MOI.Interval(0.0, 2.0)))
MOIU.attachoptimizer!(caching_optimizer)
@btime MOI.modifyconstraint!($caching_optimizer, $c, $(MOI.Interval(0.0, 2.0)))
@btime MOI.modifyconstraint!($(caching_optimizer.model_cache), $c, $(MOI.Interval(0.0, 2.0)))
@btime MOI.modifyconstraint!($(caching_optimizer.optimizer), $(caching_optimizer.model_to_optimizer_map[c]), $(MOI.Interval(0.0, 2.0)))
4 changes: 2 additions & 2 deletions src/Utilities/cachingoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ A `CachingOptimizer` has two modes of operation (`CachingOptimizerMode`):
- `Manual`: The only methods that change the state of the `CachingOptimizer` are [`resetoptimizer!`](@ref), [`dropoptimizer!`](@ref), and [`attachoptimizer!`](@ref). Attempting to perform an operation in the incorrect state results in an error.
- `Automatic`: The `CachingOptimizer` changes its state when necessary. For example, `optimize!` will automatically call `attachoptimizer!` (an optimizer must have been previously set). Attempting to add a constraint or perform a modification not supported by the optimizer results in a drop to `EmptyOptimizer` mode.
"""
mutable struct CachingOptimizer <: MOI.AbstractOptimizer
model_cache::MOI.ModelLike
mutable struct CachingOptimizer{MT<:MOI.ModelLike} <: MOI.AbstractOptimizer
model_cache::MT
optimizer::Union{Void,MOI.AbstractOptimizer}
state::CachingOptimizerState
mode::CachingOptimizerMode
Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/mockoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ struct MockConstraintAttribute <: MOI.AbstractConstraintAttribute
end

# A mock optimizer used for testing.
mutable struct MockOptimizer <: MOI.AbstractOptimizer
inner_model::MOI.ModelLike
mutable struct MockOptimizer{MT<:MOI.ModelLike} <: MOI.AbstractOptimizer
inner_model::MT
attribute::Int # MockModelAttribute
varattribute::Dict{MOI.VariableIndex,Int} # MockVariableAttribute
conattribute::Dict{MOI.ConstraintIndex,Int} # MockConstraintAttribute
Expand Down