/
common.jl
270 lines (233 loc) · 9.17 KB
/
common.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""
_solve_mc_model(
data::Dict{String,<:Any},
model_type::Type,
optimizer,
build_method::Function;
multinetwork::Bool=false,
ref_extensions::Vector{<:Function}=Function[],
solution_processors::Vector{<:Function}=Function[],
relax_integrality::Bool=false,
kwargs...
)::Dict{String,Any}
Internal solver interface that uses [`instantiate_mc_model`](@ref instantiate_mc_model) directly and runs `optimize_model!`, returning a result
See [`solve_mc_model`](@ref solve_mc_model)
"""
function _solve_mc_model(
data::Dict{String,<:Any},
model_type::Type,
optimizer,
build_method::Function;
multinetwork::Bool=false,
ref_extensions::Vector{<:Function}=Function[],
solution_processors::Vector{<:Function}=Function[],
relax_integrality::Bool=false,
kwargs...
)::Dict{String,Any}
if multinetwork != ismultinetwork(data)
model_requirement = multinetwork ? "multi-network" : "single-network"
data_type = ismultinetwork(data) ? "multi-network" : "single-network"
error("attempted to build a $(model_requirement) model with $(data_type) data")
end
start_time = time()
pm = instantiate_mc_model(
data,
model_type,
build_method;
ref_extensions=ref_extensions,
multinetwork=multinetwork,
kwargs...
)
@debug "pm model build time: $(time() - start_time)"
start_time = time()
result = optimize_model!(
pm,
relax_integrality=relax_integrality,
optimizer=optimizer,
solution_processors=solution_processors
)
@debug "pm model solve and solution time: $(time() - start_time)"
return result
end
"""
instantiate_mc_model(
data::Dict{String,<:Any},
model_type::Type,
build_method::Function;
ref_extensions::Vector{<:Function}=Function[],
multinetwork::Bool=false,
global_keys::Set{String}=Set{String}(),
eng2math_extensions::Vector{<:Function}=Function[],
eng2math_passthrough::Dict{String,<:Vector{<:String}}=Dict{String,Vector{String}}(),
make_pu_extensions::Vector{<:Function}=Function[],
kwargs...
)
Takes data in either the ENGINEERING or MATHEMATICAL model, a model type (_e.g._, [`ACRUPowerModel`](@ref ACRUPowerModel)),
and model builder function (_e.g._, [`build_mc_opf`](@ref build_mc_opf)), and returns an
[`AbstractUnbalancedPowerModel`](@ref AbstractUnbalancedPowerModel) structure.
For an explanation of `multinetwork` and `global_keys`, see [`make_multinetwork`](@ref make_multinetwork)
For an explanation of `eng2math_extensions` and `eng2math_passthrough`, see [`transform_data_model`](@ref transform_data_model)
For an explanation of `make_pu_extensions`, see [`make_per_unit!`](@ref make_per_unit!)
# `ref_extensions`
Inside of the model structures, data can be quickly accessed via the [`ref`](@ref ref) function. By default, the only ref
objects available are created by [`ref_add_core!`](@ref ref_add_core!), but users can add their own custom ref objects by passing
functions via `ref_extensions` that have the signature:
ref_add!(ref::Dict{Symbol,Any}, data::Dict{String,Any})
See the [Beginners Guide](@ref Introduction-to-PowerModelsDistribution) for an example.
"""
function instantiate_mc_model(
data::Dict{String,<:Any},
model_type::Type,
build_method::Function;
ref_extensions::Vector{<:Function}=Function[],
multinetwork::Bool=ismultinetwork(data),
global_keys::Set{String}=Set{String}(),
eng2math_extensions::Vector{<:Function}=Function[],
eng2math_passthrough::Dict{String,<:Vector{<:String}}=Dict{String,Vector{String}}(),
make_pu_extensions::Vector{<:Function}=Function[],
kwargs...
)
if iseng(data)
@info "Converting ENGINEERING data model to MATHEMATICAL first to build JuMP model"
data = transform_data_model(
data;
multinetwork=multinetwork,
global_keys=global_keys,
eng2math_extensions=eng2math_extensions,
eng2math_passthrough=eng2math_passthrough,
make_pu_extensions=make_pu_extensions,
)
end
return _IM.instantiate_model(
data,
model_type,
build_method,
ref_add_core!,
union(_pmd_math_global_keys, global_keys),
pmd_it_sym;
ref_extensions=ref_extensions,
kwargs...
)
end
"""
solve_mc_model(
data::Dict{String,<:Any},
model_type::Type,
optimizer,
build_mc::Function;
ref_extensions::Vector{<:Function}=Function[],
multinetwork::Bool=false,
global_keys::Set{String}=Set{String}(),
eng2math_extensions::Vector{<:Function}=Function[],
eng2math_passthrough::Dict{String,<:Vector{<:String}}=Dict{String,Vector{String}}(),
make_si::Bool=!get(data, "per_unit", false),
make_si_extensions::Vector{<:Function}=Function[],
dimensionalize_math_extensions::Dict{String,Dict{String,Vector{String}}}=Dict{String,Dict{String,Vector{String}}}(),
kwargs...
)::Dict{String,Any}
Takes data in either the ENGINEERING or MATHEMATICAL model, a model type (_e.g._, [`ACRUPowerModel`](@ref ACRUPowerModel)),
and model builder function (_e.g._, [`build_mc_opf`](@ref build_mc_opf)), and returns a solution in the original data model
defined by `data`.
If `make_si` is false, data will remain in per-unit.
For an explanation of `multinetwork` and `global_keys`, see [`make_multinetwork`](@ref make_multinetwork)
For an explanation of `eng2math_extensions` and `eng2math_passthrough`, see [`transform_data_model`](@ref transform_data_model)
For an explanation of `make_pu_extensions`, see [`make_per_unit!`](@ref make_per_unit!)
For an explanation of `ref_extensions`, see [`instantiate_mc_model`](@ref instantiate_mc_model)
For an explanation of `map_math2eng_extensions`, `make_si`, `make_si_extensions`, and `dimensionalize_math_extensions`, see [`solution_make_si`](@ref solution_make_si)
"""
function solve_mc_model(
data::Dict{String,<:Any},
model_type::Type,
optimizer,
build_mc::Function;
ref_extensions::Vector{<:Function}=Function[],
multinetwork::Bool=false,
global_keys::Set{String}=Set{String}(),
eng2math_extensions::Vector{<:Function}=Function[],
eng2math_passthrough::Dict{String,<:Vector{<:String}}=Dict{String,Vector{String}}(),
make_pu_extensions::Vector{<:Function}=Function[],
map_math2eng_extensions::Dict{String,<:Function}=Dict{String,Function}(),
make_si::Bool=!get(data, "per_unit", false),
make_si_extensions::Vector{<:Function}=Function[],
dimensionalize_math_extensions::Dict{String,Dict{String,Vector{String}}}=Dict{String,Dict{String,Vector{String}}}(),
kwargs...
)::Dict{String,Any}
if iseng(data)
data_math = transform_data_model(
data;
multinetwork=multinetwork,
eng2math_extensions=eng2math_extensions,
eng2math_passthrough=eng2math_passthrough,
make_pu_extensions=make_pu_extensions,
global_keys=global_keys,
)
result = _solve_mc_model(
data_math,
model_type,
optimizer,
build_mc;
ref_extensions=ref_extensions,
multinetwork=multinetwork,
global_keys=global_keys,
kwargs...
)
result["solution"] = transform_solution(
result["solution"],
data_math;
map_math2eng_extensions=map_math2eng_extensions,
make_si=make_si,
make_si_extensions=make_si_extensions,
dimensionalize_math_extensions=dimensionalize_math_extensions
)
elseif ismath(data)
result = _solve_mc_model(
data,
model_type,
optimizer,
build_mc;
ref_extensions=ref_extensions,
multinetwork=multinetwork,
eng2math_extensions=eng2math_extensions,
eng2math_passthrough=eng2math_passthrough,
global_keys=global_keys,
kwargs...
)
else
error("unrecognized data model format '$(get(data, "data_model", missing))'")
end
return result
end
"""
solve_mc_model(
file::String,
model_type::Type,
optimizer,
build_mc::Function;
dss2eng_extensions::Vector{<:Function}=Function[],
multinetwork::Bool=false,
global_keys::Set{String}=Set{String}(),
kwargs...
)::Dict{String,Any}
Given a `file::String`, data will be parsed automatically from the file.
See [`solve_mc_model`](@ref solve_mc_model) for detailed explanation of function arguments.
"""
function solve_mc_model(
file::String,
model_type::Type,
optimizer,
build_mc::Function;
dss2eng_extensions::Vector{<:Function}=Function[],
multinetwork::Bool=false,
global_keys::Set{String}=Set{String}(),
kwargs...
)::Dict{String,Any}
return solve_mc_model(
parse_file(file; dss2eng_extensions=dss2eng_extensions, multinetwork=multinetwork, global_keys=global_keys),
model_type,
optimizer,
build_mc;
multinetwork=multinetwork,
global_keys=global_keys,
kwargs...
)
end