Skip to content
Open
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
6 changes: 2 additions & 4 deletions src/ConicProgram/ConicProgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,8 @@ end
Method not supported for `DiffOpt.ConicProgram.Model` directly.
However, a fallback is provided in `DiffOpt`.
"""
function MOI.set(::Model, ::DiffOpt.ReverseObjectiveSensitivity, val)
return throw(
MOI.UnsupportedAttribute(DiffOpt.ReverseObjectiveSensitivity()),
)
function MOI.set(::Model, ::DiffOpt.ReverseObjectiveValue, val)
return throw(MOI.UnsupportedAttribute(DiffOpt.ReverseObjectiveValue()))
end

end
4 changes: 2 additions & 2 deletions src/NonLinearProgram/NonLinearProgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ function _lu_with_inertia_correction(
return K
end

_all_variables(form::Form) = MOI.VariableIndex.(1:form.num_variables)
_all_variables(form::Form) = MOI.VariableIndex.(1:(form.num_variables))
_all_variables(model::Model) = _all_variables(model.model)
_all_params(form::Form) = collect(keys(form.var2param))
_all_params(model::Model) = _all_params(model.model)
Expand Down Expand Up @@ -650,7 +650,7 @@ function MOI.get(model::Model, ::DiffOpt.ForwardObjectiveSensitivity)
return model.forw_grad_cache.objective_sensitivity_p
end

function MOI.set(model::Model, ::DiffOpt.ReverseObjectiveSensitivity, val)
function MOI.set(model::Model, ::DiffOpt.ReverseObjectiveValue, val)
model.input_cache.dobj = val
return
end
Expand Down
2 changes: 1 addition & 1 deletion src/NonLinearProgram/nlp_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function _create_evaluator(form::Form)
evaluator = MOI.Nonlinear.Evaluator(
nlp,
backend,
MOI.VariableIndex.(1:form.num_variables),
MOI.VariableIndex.(1:(form.num_variables)),
)
MOI.initialize(evaluator, [:Hess, :Jac, :Grad])
return evaluator
Expand Down
6 changes: 2 additions & 4 deletions src/QuadraticProgram/QuadraticProgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,8 @@ end
Method not supported for `DiffOpt.QuadraticProgram.Model` directly.
However, a fallback is provided in `DiffOpt`.
"""
function MOI.set(::Model, ::DiffOpt.ReverseObjectiveSensitivity, val)
return throw(
MOI.UnsupportedAttribute(DiffOpt.ReverseObjectiveSensitivity()),
)
function MOI.set(::Model, ::DiffOpt.ReverseObjectiveValue, val)
return throw(MOI.UnsupportedAttribute(DiffOpt.ReverseObjectiveValue()))
end

end
42 changes: 39 additions & 3 deletions src/diff_opt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ Currently, this only works for the set `MOI.Parameter`.
"""
struct ForwardConstraintSet <: MOI.AbstractConstraintAttribute end

"""
ForwardParameterValue <: MOI.AbstractVariableAttribute

A JuMP-level shortcut for forward differentiation with respect to a parameter
variable. Equivalent to setting
`ForwardConstraintSet()` on `ParameterRef(p)` with a wrapped `MOI.Parameter`:

```julia
set_attribute(p, DiffOpt.ForwardParameterValue(), value)
# same as
set_attribute(
ParameterRef(p), DiffOpt.ForwardConstraintSet(), MOI.Parameter(value),
)
```
"""
struct ForwardParameterValue <: MOI.AbstractVariableAttribute end

"""
ForwardVariablePrimal <: MOI.AbstractVariableAttribute

Expand Down Expand Up @@ -201,18 +218,20 @@ MOI.set(model, DiffOpt.ReverseConstraintDual(), ci, value)
struct ReverseConstraintDual <: MOI.AbstractConstraintAttribute end

"""
ReverseObjectiveSensitivity <: MOI.AbstractModelAttribute
ReverseObjectiveValue <: MOI.AbstractModelAttribute

A `MOI.AbstractModelAttribute` to set input data for reverse differentiation.

For instance, to set the sensitivity of the parameter perturbation with respect to the
objective function perturbation, do the following:

```julia
MOI.set(model, DiffOpt.ReverseObjectiveSensitivity(), value)
MOI.set(model, DiffOpt.ReverseObjectiveValue(), value)
```
"""
struct ReverseObjectiveSensitivity <: MOI.AbstractModelAttribute end
struct ReverseObjectiveValue <: MOI.AbstractModelAttribute end

Base.@deprecate_binding ReverseObjectiveSensitivity ReverseObjectiveValue

"""
ForwardConstraintDual <: MOI.AbstractConstraintAttribute
Expand Down Expand Up @@ -310,6 +329,23 @@ struct ReverseConstraintSet <: MOI.AbstractConstraintAttribute end

MOI.is_set_by_optimize(::ReverseConstraintSet) = true

"""
ReverseParameterValue <: MOI.AbstractVariableAttribute

A JuMP-level shortcut for reverse differentiation output with respect to a
parameter variable. Equivalent to reading `ReverseConstraintSet()` on
`ParameterRef(p)` and unwrapping `MOI.Parameter`:

```julia
get_attribute(p, DiffOpt.ReverseParameterValue())
# same as
get_attribute(ParameterRef(p), DiffOpt.ReverseConstraintSet()).value
```
"""
struct ReverseParameterValue <: MOI.AbstractVariableAttribute end

MOI.is_set_by_optimize(::ReverseParameterValue) = true

"""
DifferentiateTimeSec()

Expand Down
84 changes: 71 additions & 13 deletions src/jump_moi_overloads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# done after the model is optimized, so we add function to bypass the
# dirty state.

# DEPRECATE
function MOI.set(
model::JuMP.Model,
attr::ForwardObjectiveFunction,
Expand All @@ -38,7 +37,6 @@ function MOI.set(
return MOI.set(JuMP.backend(model), attr, allow)
end

# DEPRECATE
function MOI.set(
model::JuMP.Model,
attr::ForwardObjectiveFunction,
Expand All @@ -47,28 +45,71 @@ function MOI.set(
return MOI.set(model, attr, JuMP.AffExpr(func))
end

# DEPRECATE
function MOI.set(
model::JuMP.Model,
attr::ForwardConstraintFunction,
con_ref::JuMP.ConstraintRef,
con_ref::JuMP.ConstraintRef{
<:JuMP.AbstractModel,
<:MOI.ConstraintIndex{<:MOI.AbstractScalarFunction},
},
func::JuMP.AbstractJuMPScalar,
)
JuMP.check_belongs_to_model(con_ref, model)
JuMP.check_belongs_to_model(func, model)
return MOI.set(model, attr, con_ref, JuMP.moi_function(func))
return MOI.set(
JuMP.backend(model),
attr,
JuMP.index(con_ref),
JuMP.moi_function(func),
)
end

# DEPRECATE
function MOI.set(
model::JuMP.Model,
attr::ForwardConstraintFunction,
con_ref::JuMP.ConstraintRef,
con_ref::JuMP.ConstraintRef{
<:JuMP.AbstractModel,
<:MOI.ConstraintIndex{<:MOI.AbstractScalarFunction},
},
func::Number,
)
return MOI.set(model, attr, con_ref, JuMP.AffExpr(func))
end

# DEPRECATE - then modify
# Similar to `JuMP.set_start_value` for vector `ConstraintRef` in
# JuMP/src/constraints.jl
function MOI.set(
model::JuMP.Model,
attr::ForwardConstraintFunction,
con_ref::JuMP.ConstraintRef{
<:JuMP.AbstractModel,
<:MOI.ConstraintIndex{<:MOI.AbstractVectorFunction},
},
value::AbstractArray{<:JuMP.AbstractJuMPScalar},
)
JuMP.check_belongs_to_model(con_ref, model)
JuMP.check_belongs_to_model.(value, model)
v = JuMP.vectorize(value, con_ref.shape)
return MOI.set(
JuMP.backend(model),
attr,
JuMP.index(con_ref),
JuMP.moi_function(v),
)
end

function MOI.set(
model::JuMP.Model,
attr::ForwardConstraintFunction,
con_ref::JuMP.ConstraintRef{
<:JuMP.AbstractModel,
<:MOI.ConstraintIndex{<:MOI.AbstractVectorFunction},
},
value::AbstractArray{<:Number},
)
return MOI.set(model, attr, con_ref, JuMP.AffExpr.(value))
end

function MOI.get(
model::JuMP.Model,
attr::ForwardConstraintDual,
Expand All @@ -79,13 +120,11 @@ function MOI.get(
return JuMP.jump_function(model, moi_func)
end

# DEPRECATE - then modify
function MOI.get(model::JuMP.Model, attr::ReverseObjectiveFunction)
func = MOI.get(JuMP.backend(model), attr)
return JuMP.jump_function(model, func)
end

# DEPRECATE - then modify
function MOI.get(
model::JuMP.Model,
attr::ReverseConstraintFunction,
Expand Down Expand Up @@ -113,7 +152,6 @@ function _moi_get_result(model::MOI.Utilities.CachingOptimizer, args...)
return MOI.get(model, args...)
end

# DEPRECATE
function MOI.get(
model::JuMP.Model,
attr::ForwardVariablePrimal,
Expand All @@ -123,7 +161,6 @@ function MOI.get(
return _moi_get_result(JuMP.backend(model), attr, JuMP.index(var_ref))
end

# REVIEW
function MOI.get(
model::JuMP.Model,
attr::ReverseConstraintSet,
Expand All @@ -143,9 +180,30 @@ function MOI.set(
return MOI.set(JuMP.backend(model), attr, JuMP.index(con_ref), set)
end

function MOI.set(
model::JuMP.Model,
::ForwardParameterValue,
p::JuMP.VariableRef,
value::Number,
)
return MOI.set(
model,
ForwardConstraintSet(),
JuMP.ParameterRef(p),
MOI.Parameter(value),
)
end

function MOI.get(
model::JuMP.Model,
::ReverseParameterValue,
p::JuMP.VariableRef,
)
return MOI.get(model, ReverseConstraintSet(), JuMP.ParameterRef(p)).value
end

# there is no set_forward_constraint_set because there is set_forward_parameter

# DEPRECATE
function MOI.set(
model::JuMP.Model,
attr::ForwardConstraintSet,
Expand Down
Loading
Loading