-
Notifications
You must be signed in to change notification settings - Fork 7
/
duals.jl
136 lines (120 loc) · 4.18 KB
/
duals.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright (c) 2020: Tomás Gutierrez and contributors
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
function compute_dual_of_parameters!(model::Optimizer{T}) where {T}
model.dual_value_of_parameters =
zeros(T, model.number_of_parameters_in_model)
update_duals_from_affine_constraints!(model)
update_duals_from_vector_affine_constraints!(model)
update_duals_from_quadratic_constraints!(model)
if model.affine_objective_cache !== nothing
update_duals_from_objective!(model, model.affine_objective_cache)
end
if model.quadratic_objective_cache !== nothing
update_duals_from_objective!(model, model.quadratic_objective_cache)
end
return
end
function update_duals_from_affine_constraints!(model::Optimizer)
for (F, S) in keys(model.affine_constraint_cache.dict)
affine_constraint_cache_inner = model.affine_constraint_cache[F, S]
# barrier for type instability
compute_parameters_in_ci!(model, affine_constraint_cache_inner)
end
return
end
function update_duals_from_vector_affine_constraints!(model::Optimizer)
for (F, S) in keys(model.vector_affine_constraint_cache.dict)
vector_affine_constraint_cache_inner =
model.vector_affine_constraint_cache[F, S]
# barrier for type instability
compute_parameters_in_ci!(model, vector_affine_constraint_cache_inner)
end
return
end
function update_duals_from_quadratic_constraints!(model::Optimizer)
for (F, S) in keys(model.quadratic_constraint_cache.dict)
quadratic_constraint_cache_inner =
model.quadratic_constraint_cache[F, S]
# barrier for type instability
compute_parameters_in_ci!(model, quadratic_constraint_cache_inner)
end
return
end
function compute_parameters_in_ci!(
model::OT,
constraint_cache_inner::DoubleDictInner{F,S,V},
) where {OT,F,S,V}
for (inner_ci, pf) in constraint_cache_inner
compute_parameters_in_ci!(model, pf, inner_ci)
end
return
end
function compute_parameters_in_ci!(
model::Optimizer{T},
pf,
ci::MOI.ConstraintIndex{F,S},
) where {F,S} where {T}
cons_dual = MOI.get(model.optimizer, MOI.ConstraintDual(), ci)
for term in pf.p
model.dual_value_of_parameters[p_val(term.variable)] -=
cons_dual * term.coefficient
end
return
end
function compute_parameters_in_ci!(
model::Optimizer{T},
pf::ParametricVectorAffineFunction{T},
ci::MOI.ConstraintIndex{F,S},
) where {F<:MOI.VectorAffineFunction{T},S} where {T}
cons_dual = MOI.get(model.optimizer, MOI.ConstraintDual(), ci)
for term in pf.p
model.dual_value_of_parameters[p_val(term.scalar_term.variable)] -=
cons_dual[term.output_index] * term.scalar_term.coefficient
end
return
end
# this one seem to be the same as the next
function update_duals_from_objective!(model::Optimizer{T}, pf) where {T}
for param in pf.p
model.dual_value_of_parameters[p_val(param.variable)] -=
param.coefficient
end
return
end
"""
ParameterDual <: MOI.AbstractVariableAttribute
Attribute defined to get the dual values associated to parameters
# Example
```julia
MOI.get(model, POI.ParameterValue(), p)
```
"""
struct ParameterDual <: MOI.AbstractVariableAttribute end
MOI.is_set_by_optimize(::ParametricOptInterface.ParameterDual) = true
function MOI.get(model::Optimizer, ::ParameterDual, v::MOI.VariableIndex)
if !is_additive(
model,
MOI.ConstraintIndex{MOI.VariableIndex,Parameter}(v.value),
)
error("Cannot compute the dual of a multiplicative parameter")
end
return model.dual_value_of_parameters[p_val(v)]
end
function MOI.get(
model::Optimizer,
::MOI.ConstraintDual,
cp::MOI.ConstraintIndex{MOI.VariableIndex,Parameter},
)
if !is_additive(model, cp)
error("Cannot compute the dual of a multiplicative parameter")
end
return model.dual_value_of_parameters[p_val(cp)]
end
function is_additive(model::Optimizer, cp::MOI.ConstraintIndex)
if cp.value in model.multiplicative_parameters
return false
end
return true
end