Skip to content

Commit 575d7a3

Browse files
authored
Avoid double cache (#311)
1 parent df4a7dc commit 575d7a3

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/moi_wrapper.jl

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,29 @@
44
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
55

66
"""
7-
diff_optimizer(optimizer_constructor)
7+
function diff_optimizer(
8+
optimizer_constructor;
9+
with_bridge_type = Float64,
10+
with_cache_type = Float64,
11+
with_outer_cache = !isnothing(with_bridge_type),
12+
allow_parametric_opt_interface = true,
13+
)
814
915
Creates a `DiffOpt.Optimizer`, which is an MOI layer with an internal optimizer
1016
and other utility methods. Results (primal, dual and slack values) are obtained
1117
by querying the internal optimizer instantiated using the
1218
`optimizer_constructor`. These values are required for find jacobians with respect to problem data.
1319
20+
The inner optimizer is instantiated with
21+
`MOI.instantiate(optimizer_constructor; with_bridge_type, with_cache_type)`;
22+
see the docs of `MOI.instantiate`.
23+
24+
If `allow_parametric_opt_interface` is `true` and the inner optimizer does not
25+
*natively* (in the sense, without the bridge layer) supports `ParameterSet`, then
26+
a `ParametricOptInterface.Optimizer` layer is added.
27+
28+
If `with_outer_cache` is `true`, an additional layer of cache is added.
29+
1430
One define a differentiable model by using any solver of choice. Example:
1531
1632
```julia
@@ -26,38 +42,37 @@ function diff_optimizer(
2642
optimizer_constructor;
2743
with_bridge_type = Float64,
2844
with_cache_type = Float64,
29-
with_outer_cache = true,
45+
with_outer_cache = !isnothing(with_bridge_type),
3046
allow_parametric_opt_interface = true,
3147
)
3248
optimizer = MOI.instantiate(
3349
optimizer_constructor;
3450
with_bridge_type,
3551
with_cache_type,
3652
)
37-
add_poi =
38-
allow_parametric_opt_interface &&
39-
!MOI.supports_add_constrained_variable(
40-
isnothing(with_bridge_type) ? optimizer : optimizer.model,
41-
MOI.Parameter{Float64},
42-
)
53+
if allow_parametric_opt_interface &&
54+
!MOI.supports_add_constrained_variable(
55+
isnothing(with_bridge_type) ? optimizer : optimizer.model,
56+
MOI.Parameter{Float64},
57+
)
58+
optimizer = POI.Optimizer(optimizer)
59+
end
4360
# When we do `MOI.copy_to(diff, optimizer)` we need to efficiently `MOI.get`
4461
# the model information from `optimizer`. However, 1) `optimizer` may not
4562
# implement some getters or it may be inefficient and 2) the getters may be
4663
# unimplemented or inefficient through some bridges.
4764
# For this reason we add a cache layer, the same cache JuMP adds.
48-
caching_opt = if with_outer_cache
49-
MOI.Utilities.CachingOptimizer(
65+
if with_outer_cache
66+
optimizer = MOI.Utilities.CachingOptimizer(
5067
MOI.Utilities.UniversalFallback(
5168
MOI.Utilities.Model{
5269
something(with_bridge_type, with_cache_type),
5370
}(),
5471
),
55-
add_poi ? POI.Optimizer(optimizer) : optimizer,
72+
optimizer,
5673
)
57-
else
58-
add_poi ? POI.Optimizer(optimizer) : optimizer
5974
end
60-
return Optimizer(caching_opt)
75+
return Optimizer(optimizer)
6176
end
6277

6378
mutable struct Optimizer{OT<:MOI.ModelLike} <: MOI.AbstractOptimizer

0 commit comments

Comments
 (0)