-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstructures.jl
More file actions
196 lines (168 loc) · 8.21 KB
/
Copy pathstructures.jl
File metadata and controls
196 lines (168 loc) · 8.21 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Copyright (c) 2017: Guilherme Bodin, 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.
MOI.Utilities.@model(
DualizableModel,
(),
(MOI.EqualTo, MOI.GreaterThan, MOI.LessThan),
(
MOI.Reals,
MOI.Zeros,
MOI.Nonnegatives,
MOI.Nonpositives,
MOI.SecondOrderCone,
MOI.RotatedSecondOrderCone,
MOI.ExponentialCone,
MOI.DualExponentialCone,
MOI.PositiveSemidefiniteConeTriangle,
),
(MOI.PowerCone, MOI.DualPowerCone),
(),
(MOI.ScalarAffineFunction,),
(MOI.VectorOfVariables,),
(MOI.VectorAffineFunction,)
)
"""
PrimalDualMap{T}
Maps information from all structures of the primal to the dual model.
Main user maps:
* `constrained_var_idx::Dict{MOI.VariableIndex,Tuple{MOI.ConstraintIndex,Int}}`:
maps primal constrained variables to their primal
constraints (the special ones that makes them constrained variables) and
their internal index from 1 to dimension(set) (if vector constraints:
VectorOfVariables-in-Set), 1 otherwise (scalar: VariableIndex-in-Set).
Future name: primal_convar_to_primal_convarcon_and_index
* `constrained_var_dual::Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}`: maps
the primal constraint index of constrained variables to the dual
model's constraint index of the associated dual constraint. This dual
constraint is a regular constraint (not a constrained variable constraint).
`VectorOfVariables`-in-`Zeros` and `VariableIndex`-in-`EqualTo(zero(T))`
are not in this map, as they are not dualized (See
primal_convarcon_to_dual_function).
Future name: primal_convarcon_to_dual_con
note: from the above two maps, we can get primal_convar_to_dual_con_and_index
* `primal_var_dual_con::Dict{MOI.VariableIndex,MOI.ConstraintIndex}`: maps
"free" primal variables to their associated dual (equality) constraints.
Free variables as opposed to constrained variables. Note that Dualization
will select automatically which variables are free and which are
constrained.
Future name: primal_var_to_dual_con
note: from the above three maps, we can get primal_var_to_dual_con_and_index
* `primal_con_dual_var::Dict{MOI.ConstraintIndex,Vector{MOI.VariableIndex}}`:
maps primal constraint indices to vectors of dual variable indices. For
scalar constraints those vectors will be single element vectors.
Primal Constrained variables constraints (the main ones) are not in this
map.
Future name: primal_con_to_dual_var_vec
* `primal_con_dual_con::Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}`: maps
primal constraints to dual constrained variable. If the primal
constraint's set is EqualTo or Zeros, no constraint is added in the dual
variable (the dual variable is said to be free).
The keys are similar to the (# primal_con_to_dual_var_vec) map, except
that `VariableIndex`-in-`EqualTo(zero(T))` and `VectorOfVariables`-in-`Zeros`
are not in this map.
Future name: primal_con_to_dual_convarcon
Additional helper maps:
* `primal_con_constants::Dict{MOI.ConstraintIndex,Vector{T}}`: maps primal
constraints to their respective constants, which might be inside the set.
This map is used in `MOI.get(::DualOptimizer,::MOI.ConstraintPrimal,ci)`
that requires extra information in the case that the scalar set constrains
a constant (`EqualtTo`, `GreaterThan`, `LessThan`).
Future name: primal_con_to_primal_constants_vec
* `primal_parameter::Dict{MOI.VariableIndex,MOI.VariableIndex}`: maps
parameters in the primal to parameters in the dual model.
Future name: primal_parameter_to_dual_parameter
* `constrained_var_zero::Dict{MOI.ConstraintIndex,Unions{MOI.ScalarAffineFunction,MOI.VectorAffineFunction}}`:
caches scalar affine functions or vector affine functions associated with
constrained variables of type `VectorOfVariables`-in-`Zeros` or
`VariableIndex`-in-`EqualTo(zero(T))` as their duals would be `func`-in-`Reals`,
which are "irrelevant" to the model. This information is cached for
completeness of the `DualOptimizer` for `get`ting `ConstraintDuals`.
Future name: primal_convarcon_to_dual_function
* `primal_var_dual_quad_slack::Dict{MOI.VariableIndex,MOI.VariableIndex}`:
maps primal variables (that appear in quadratic objective terms) to dual
"slack" variables. These primal variables might appear in other maps.
Future name: primal_var_in_quad_obj_to_dual_slack_var
"""
mutable struct PrimalDualMap{T}
constrained_var_idx::Dict{MOI.VariableIndex,Tuple{MOI.ConstraintIndex,Int}}
constrained_var_dual::Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}
constrained_var_zero::Dict{
MOI.ConstraintIndex,
Union{MOI.VectorAffineFunction{T},MOI.ScalarAffineFunction{T}},
}
primal_var_dual_con::Dict{MOI.VariableIndex,MOI.ConstraintIndex}
primal_con_dual_var::Dict{MOI.ConstraintIndex,Vector{MOI.VariableIndex}}
primal_con_dual_con::Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}
primal_con_constants::Dict{MOI.ConstraintIndex,Vector{T}}
primal_parameter::Dict{MOI.VariableIndex,MOI.VariableIndex}
primal_var_dual_quad_slack::Dict{MOI.VariableIndex,MOI.VariableIndex}
function PrimalDualMap{T}() where {T}
return new(
Dict{MOI.VariableIndex,Tuple{MOI.ConstraintIndex,Int}}(),
Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}(),
Dict{
MOI.ConstraintIndex,
Union{MOI.VectorAffineFunction{T},MOI.ScalarAffineFunction{T}},
}(),
Dict{MOI.VariableIndex,MOI.ConstraintIndex}(),
Dict{MOI.ConstraintIndex,Vector{MOI.VariableIndex}}(),
Dict{MOI.ConstraintIndex,MOI.ConstraintIndex}(),
Dict{MOI.ConstraintIndex,Vector{T}}(),
Dict{MOI.VariableIndex,MOI.VariableIndex}(),
Dict{MOI.VariableIndex,MOI.VariableIndex}(),
)
end
end
function is_empty(primal_dual_map::PrimalDualMap{T}) where {T}
return isempty(primal_dual_map.constrained_var_idx) &&
isempty(primal_dual_map.constrained_var_dual) &&
isempty(primal_dual_map.constrained_var_zero) &&
isempty(primal_dual_map.primal_var_dual_con) &&
isempty(primal_dual_map.primal_con_dual_var) &&
isempty(primal_dual_map.primal_con_dual_con) &&
isempty(primal_dual_map.primal_con_constants) &&
isempty(primal_dual_map.primal_parameter) &&
isempty(primal_dual_map.primal_var_dual_quad_slack)
end
function empty!(primal_dual_map::PrimalDualMap)
Base.empty!(primal_dual_map.constrained_var_idx)
Base.empty!(primal_dual_map.constrained_var_dual)
Base.empty!(primal_dual_map.constrained_var_zero)
Base.empty!(primal_dual_map.primal_var_dual_con)
Base.empty!(primal_dual_map.primal_con_dual_var)
Base.empty!(primal_dual_map.primal_con_dual_con)
Base.empty!(primal_dual_map.primal_con_constants)
Base.empty!(primal_dual_map.primal_parameter)
Base.empty!(primal_dual_map.primal_var_dual_quad_slack)
return
end
"""
DualProblem{T,OT<:MOI.ModelLike}
Result of the `dualize` function. Contains the fields:
* `dual_model::OT`: contaninf a optimizer or data structure with the
`MathOptInterface` definition of the resulting dual model.
* `primal_dual_map::PrimalDualMap{T}`: a data structure to hold information of
how the primal and dual model are related in terms of indices
(`VariableIndex` and `ConstraintIndex`) and other data.
"""
struct DualProblem{T,OT<:MOI.ModelLike}
dual_model::OT #It can be a model from an optimizer or a DualizableModel{T}
primal_dual_map::PrimalDualMap{T}
function DualProblem{T}(
dual_optimizer::OT,
pdmap::PrimalDualMap{T},
) where {T,OT<:MOI.ModelLike}
return new{T,OT}(dual_optimizer, pdmap)
end
end
function DualProblem{T}(dual_optimizer::OT) where {T,OT<:MOI.ModelLike}
return DualProblem{T}(dual_optimizer, PrimalDualMap{T}())
end
function DualProblem(dual_optimizer::OT) where {OT<:MOI.ModelLike}
return DualProblem{Float64}(dual_optimizer)
end
function DualProblem{T}() where {T}
return DualProblem{T}(DualizableModel{T}(), PrimalDualMap{T}())
end