-
Notifications
You must be signed in to change notification settings - Fork 92
Add type parameter for CachingOptimizer #390
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #390 +/- ##
==========================================
+ Coverage 96.48% 96.75% +0.27%
==========================================
Files 39 39
Lines 4917 5397 +480
==========================================
+ Hits 4744 5222 +478
- Misses 173 175 +2
Continue to review full report at Codecov.
|
src/Utilities/cachingoptimizer.jl
Outdated
@@ -22,9 +22,9 @@ 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{MT<:MOI.ModelLike} <: MOI.AbstractOptimizer | |||
mutable struct CachingOptimizer{OT, MT<:MOI.ModelLike} <: MOI.AbstractOptimizer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try more explicit names for the type parameters. Something like OptimizerType
and ModelType
maybe?
src/Utilities/cachingoptimizer.jl
Outdated
@@ -34,19 +34,24 @@ mutable struct CachingOptimizer{MT<:MOI.ModelLike} <: MOI.AbstractOptimizer | |||
# optimizer indices. | |||
end | |||
|
|||
CachingOptimizer(model_cache::MOI.ModelLike, mode::CachingOptimizerMode) = CachingOptimizer(model_cache, nothing, NoOptimizer, mode, IndexMap(), IndexMap()) | |||
function CachingOptimizer(model_cache::MOI.ModelLike, mode::CachingOptimizerMode) | |||
CachingOptimizer{Union{Nothing,MOI.AbstractOptimizer}, typeof(model_cache)}(nothing, model_cache, NoOptimizer, mode, IndexMap(), IndexMap()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Union{Nothing,MOI.AbstractOptimizer}
is a really unusual type parameter. Why not type the optimizer
as Union{Nothing, OptimizerType}
? My understanding is that there's very little overhead in 0.7 for Union{Nothing, T}
if T
is concrete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I have updated the description which a benchmark showing this
src/Utilities/cachingoptimizer.jl
Outdated
`CachingOptimizer`-specific functions (e.g. `dropoptimizer!`) are called on it. | ||
`CachingOptimizer`-specific functions (e.g. `resetoptimizer!`) are called on it. | ||
The type of the optimizer returned is `CachingOptimizer{typeof(optimizer), | ||
typeof(model_cache)}` so it does not support the function `dropoptimizer!` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so it does not support the function
dropoptimizer!
nor the method `resetoptimizer!.
Is this true? Can't the field be set to nothing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, fixed :)
src/Utilities/cachingoptimizer.jl
Outdated
model_cache::MT | ||
optimizer::Union{Nothing,MOI.AbstractOptimizer} | ||
mutable struct CachingOptimizer{OptimizerType, ModelType<:MOI.ModelLike} <: MOI.AbstractOptimizer | ||
optimizer::OptimizerType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you plan to update this to optimizer::Union{OptimizerType, Nothing}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ups, fixed
This PR adds a type parameter for the optimizer of
CachingOptimizer
. It adds this type parameters in the first place as it will be more useful than the second one (see e.g.dropoptimizer!
). It also changes the order of the cache and the optimizer fields for consistency.Note that it does not implement the idea of #321 (comment) of removing the possibility to have no optimizer or to change the optimizer.
Indeed, this would require JuMP to handle these cases and I initially thought that this was the solution because I didn't have the idea of simply using the non concrete type parameter
Union{Nothing, MOI.AbstractOptimizer}
in the case where we want to be able to drop the optimizer and to change it and use the concrete optimizer type when we don't want to change it and the type instability becomes the bottleneck (e.g. #321).We can now make a similar change in JuMP. Have a parameter for the
moibackend
type that the user can set it to the concrete type of the optimizer to have no overhead (but loosing the features of changing optimizer, droping it, ...)Benchmarks
See #323 for benchmarks before this change
With a CachingOptimizer with concrete field type for the optimizer field (i.e.
MockOptimizer{Model{Float64}}
):With a CachingOptimizer with
Union{Nothing, OptimizerType}
with concreteOptimizerType
for the `optimizer field:The overhead of the union with
Void
is negligible in v0.7 as suggested by #390 (comment) so it seems cleaner to use itCloses #321