Skip to content

Commit e713f25

Browse files
committed
suggestions
1 parent a98f548 commit e713f25

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/jump_moi_overloads.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
# Use of this source code is governed by an MIT-style license that can be found
44
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
55

6+
# FIXME
7+
# Some function in this file are overloads to skip JuMP dirty state.
8+
# Workaround for https://github.com/jump-dev/JuMP.jl/issues/2797
9+
# This workaround is necessary because once some attributes are set the JuMP
10+
# model changes to a dirty state, then getting some attributes is blocked.
11+
# However, getting and setting forward and backward sensitivities is
12+
# done after the model is optimized, so we add function to bypass the
13+
# dirty state.
14+
615
function MOI.set(
716
model::JuMP.Model,
817
attr::ForwardObjectiveFunction,
@@ -54,7 +63,7 @@ function MOI.get(
5463
return JuMP.jump_function(model, moi_func)
5564
end
5665

57-
# FIXME Workaround for https://github.com/jump-dev/JuMP.jl/issues/2797
66+
# see FIXME comment in the top of the file
5867
function _moi_get_result(model::MOI.ModelLike, args...)
5968
if MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMIZE_NOT_CALLED
6069
throw(OptimizeNotCalled())
@@ -80,8 +89,6 @@ function MOI.get(
8089
return _moi_get_result(JuMP.backend(model), attr, JuMP.index(var_ref))
8190
end
8291

83-
# extras to handle model_dirty
84-
8592
function MOI.get(
8693
model::JuMP.Model,
8794
attr::ReverseConstraintSet,
@@ -108,12 +115,7 @@ function MOI.set(
108115
set::JuMP.AbstractScalarSet,
109116
)
110117
JuMP.check_belongs_to_model(con_ref, model)
111-
return MOI.set(
112-
JuMP.backend(model),
113-
attr,
114-
JuMP.index(con_ref),
115-
JuMP.moi_set(set),
116-
)
118+
return MOI.set(model, attr, con_ref, JuMP.moi_set(set))
117119
end
118120

119121
"""

src/moi_wrapper.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ One define a differentiable model by using any solver of choice. Example:
1717
julia> import DiffOpt, HiGHS
1818
1919
julia> model = DiffOpt.diff_optimizer(HiGHS.Optimizer)
20+
julia> set_attribute(model, DiffOpt.ModelConstructor, DiffOpt.QuadraticProgram.Model) # optional selection of diff method
2021
julia> x = model.add_variable(model)
2122
julia> model.add_constraint(model, ...)
2223
```
2324
"""
2425
function diff_optimizer(
2526
optimizer_constructor;
26-
method = nothing,
2727
with_parametric_opt_interface::Bool = false,
2828
with_bridge_type = Float64,
2929
with_cache::Bool = true,
@@ -45,9 +45,9 @@ function diff_optimizer(
4545
optimizer
4646
end
4747
if with_parametric_opt_interface
48-
return POI.Optimizer(Optimizer(caching_opt; method = method))
48+
return POI.Optimizer(Optimizer(caching_opt))
4949
else
50-
return Optimizer(caching_opt; method = method)
50+
return Optimizer(caching_opt)
5151
end
5252
end
5353

@@ -64,16 +64,10 @@ mutable struct Optimizer{OT<:MOI.ModelLike} <: MOI.AbstractOptimizer
6464
# sensitivity input cache using MOI like sparse format
6565
input_cache::InputCache
6666

67-
function Optimizer(
68-
optimizer::OT;
69-
method = nothing,
70-
) where {OT<:MOI.ModelLike}
67+
function Optimizer(optimizer::OT) where {OT<:MOI.ModelLike}
7168
output =
7269
new{OT}(optimizer, Any[], nothing, nothing, nothing, InputCache())
7370
add_all_model_constructors(output)
74-
if method !== nothing
75-
output.model_constructor = method
76-
end
7771
return output
7872
end
7973
end
@@ -497,6 +491,14 @@ end
497491
Determines which subtype of [`DiffOpt.AbstractModel`](@ref) to use for
498492
differentiation. When set to `nothing`, the first one out of
499493
`model.model_constructors` that support the problem is used.
494+
495+
Examples:
496+
497+
```julia
498+
julia> MOI.set(model, DiffOpt.ModelConstructor(), DiffOpt.QuadraticProgram.Model)
499+
500+
julia> MOI.set(model, DiffOpt.ModelConstructor(), DiffOpt.ConicProgram.Model)
501+
```
500502
"""
501503
struct ModelConstructor <: MOI.AbstractOptimizerAttribute end
502504

@@ -616,7 +618,11 @@ function _diff(model::Optimizer)
616618
end
617619
if isnothing(model.diff)
618620
error(
619-
"No differentiation model supports the problem. If you believe it should be supported, say by `DiffOpt.QuadraticProgram.Model`, use `MOI.set(model, DiffOpt.ModelConstructor, DiffOpt.QuadraticProgram.Model)` and try again to see an error indicating why it is not supported.",
621+
"No differentiation model supports the problem. If you " *
622+
"believe it should be supported, say by " *
623+
"`DiffOpt.QuadraticProgram.Model`, use " *
624+
"`MOI.set(model, DiffOpt.ModelConstructor, DiffOpt.QuadraticProgram.Model)`" *
625+
"and try again to see an error indicating why it is not supported."
620626
)
621627
end
622628
else

0 commit comments

Comments
 (0)