-
Notifications
You must be signed in to change notification settings - Fork 12
/
examodels.jl
executable file
·285 lines (240 loc) · 9.74 KB
/
examodels.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
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env julia
###### AC-OPF using ExaModels ######
#
# implementation reference: https://exanauts.github.io/ExaModels.jl/stable/guide/
# only the built-in AD library is supported
#
import PowerModels
import ExaModels
import NLPModelsIpopt
import LinearAlgebra
function solve_opf(file_name)
time_data_start = time()
data = PowerModels.parse_file(file_name)
PowerModels.standardize_cost_terms!(data, order=2)
PowerModels.calc_thermal_limits!(data)
ref = PowerModels.build_ref(data)[:it][:pm][:nw][0]
arcdict = Dict(a => k for (k, a) in enumerate(ref[:arcs]))
busdict = Dict(k => i for (i, (k, v)) in enumerate(ref[:bus]))
gendict = Dict(k => i for (i, (k, v)) in enumerate(ref[:gen]))
branchdict = Dict(k => i for (i, (k, v)) in enumerate(ref[:branch]))
data = (
bus = [
begin
bus_loads = [ref[:load][l] for l in ref[:bus_loads][k]]
bus_shunts = [ref[:shunt][s] for s in ref[:bus_shunts][k]]
pd = sum(load["pd"] for load in bus_loads; init = 0.0)
gs = sum(shunt["gs"] for shunt in bus_shunts; init = 0.0)
qd = sum(load["qd"] for load in bus_loads; init = 0.0)
bs = sum(shunt["bs"] for shunt in bus_shunts; init = 0.0)
(i = busdict[k], j = k, pd = pd, gs = gs, qd = qd, bs = bs)
end for (k, v) in ref[:bus]
],
gen = [
(
i = gendict[k], j = k,
cost1 = v["cost"][1],
cost2 = v["cost"][2],
cost3 = v["cost"][3],
bus = busdict[v["gen_bus"]],
) for (k, v) in ref[:gen]
],
arc = [
(i = k, rate_a = ref[:branch][l]["rate_a"], bus = busdict[i]) for
(k, (l, i, j)) in enumerate(ref[:arcs])
],
branch = [
begin
f_idx = arcdict[i, branch["f_bus"], branch["t_bus"]]
t_idx = arcdict[i, branch["t_bus"], branch["f_bus"]]
g, b = PowerModels.calc_branch_y(branch)
tr, ti = PowerModels.calc_branch_t(branch)
ttm = tr^2 + ti^2
g_fr = branch["g_fr"]
b_fr = branch["b_fr"]
g_to = branch["g_to"]
b_to = branch["b_to"]
c1 = (-g * tr - b * ti) / ttm
c2 = (-b * tr + g * ti) / ttm
c3 = (-g * tr + b * ti) / ttm
c4 = (-b * tr - g * ti) / ttm
c5 = (g + g_fr) / ttm
c6 = (b + b_fr) / ttm
c7 = (g + g_to)
c8 = (b + b_to)
(
i = branchdict[i],
j = 1,
f_idx = f_idx,
t_idx = t_idx,
f_bus = busdict[branch["f_bus"]],
t_bus = busdict[branch["t_bus"]],
c1 = c1,
c2 = c2,
c3 = c3,
c4 = c4,
c5 = c5,
c6 = c6,
c7 = c7,
c8 = c8,
rate_a_sq = branch["rate_a"]^2,
)
end for (i, branch) in ref[:branch]
],
ref_buses = [busdict[i] for (i, k) in ref[:ref_buses]],
vmax = [v["vmax"] for (k, v) in ref[:bus]],
vmin = [v["vmin"] for (k, v) in ref[:bus]],
pmax = [v["pmax"] for (k, v) in ref[:gen]],
pmin = [v["pmin"] for (k, v) in ref[:gen]],
qmax = [v["qmax"] for (k, v) in ref[:gen]],
qmin = [v["qmin"] for (k, v) in ref[:gen]],
rate_a = [ref[:branch][l]["rate_a"] for (k, (l, i, j)) in enumerate(ref[:arcs])],
angmax = [b["angmax"] for (i, b) in ref[:branch]],
angmin = [b["angmin"] for (i, b) in ref[:branch]],
)
data_load_time = time() - time_data_start
time_model_start = time()
w = ExaModels.ExaCore()
va = ExaModels.variable(w, length(data.bus);)
vm = ExaModels.variable(
w,
length(data.bus);
start = fill!(similar(data.bus, Float64), 1.0),
lvar = data.vmin,
uvar = data.vmax,
)
pg = ExaModels.variable(w, length(data.gen); lvar = data.pmin, uvar = data.pmax)
qg = ExaModels.variable(w, length(data.gen); lvar = data.qmin, uvar = data.qmax)
p = ExaModels.variable(w, length(data.arc); lvar = -data.rate_a, uvar = data.rate_a)
q = ExaModels.variable(w, length(data.arc); lvar = -data.rate_a, uvar = data.rate_a)
o = ExaModels.objective(
w,
g.cost1 * pg[g.i]^2 + g.cost2 * pg[g.i] + g.cost3 for g in data.gen
)
c1 = ExaModels.constraint(w, va[i] for i in data.ref_buses)
c2 = ExaModels.constraint(
w,
p[b.f_idx] - b.c5 * vm[b.f_bus]^2 -
b.c3 * (vm[b.f_bus] * vm[b.t_bus] * cos(va[b.f_bus] - va[b.t_bus])) -
b.c4 * (vm[b.f_bus] * vm[b.t_bus] * sin(va[b.f_bus] - va[b.t_bus])) for
b in data.branch
)
c3 = ExaModels.constraint(
w,
q[b.f_idx] +
b.c6 * vm[b.f_bus]^2 +
b.c4 * (vm[b.f_bus] * vm[b.t_bus] * cos(va[b.f_bus] - va[b.t_bus])) -
b.c3 * (vm[b.f_bus] * vm[b.t_bus] * sin(va[b.f_bus] - va[b.t_bus])) for
b in data.branch
)
c4 = ExaModels.constraint(
w,
p[b.t_idx] - b.c7 * vm[b.t_bus]^2 -
b.c1 * (vm[b.t_bus] * vm[b.f_bus] * cos(va[b.t_bus] - va[b.f_bus])) -
b.c2 * (vm[b.t_bus] * vm[b.f_bus] * sin(va[b.t_bus] - va[b.f_bus])) for
b in data.branch
)
c5 = ExaModels.constraint(
w,
q[b.t_idx] +
b.c8 * vm[b.t_bus]^2 +
b.c2 * (vm[b.t_bus] * vm[b.f_bus] * cos(va[b.t_bus] - va[b.f_bus])) -
b.c1 * (vm[b.t_bus] * vm[b.f_bus] * sin(va[b.t_bus] - va[b.f_bus])) for
b in data.branch
)
c6 = ExaModels.constraint(
w,
va[b.f_bus] - va[b.t_bus] for b in data.branch;
lcon = data.angmin,
ucon = data.angmax,
)
c7 = ExaModels.constraint(
w,
p[b.f_idx]^2 + q[b.f_idx]^2 - b.rate_a_sq for b in data.branch;
lcon = fill!(similar(data.branch, Float64, length(data.branch)), -Inf),
)
c8 = ExaModels.constraint(
w,
p[b.t_idx]^2 + q[b.t_idx]^2 - b.rate_a_sq for b in data.branch;
lcon = fill!(similar(data.branch, Float64, length(data.branch)), -Inf),
)
c9 = ExaModels.constraint(w, b.pd + b.gs * vm[b.i]^2 for b in data.bus)
c10 = ExaModels.constraint(w, b.qd - b.bs * vm[b.i]^2 for b in data.bus)
c11 = ExaModels.constraint!(w, c9, a.bus => p[a.i] for a in data.arc)
c12 = ExaModels.constraint!(w, c10, a.bus => q[a.i] for a in data.arc)
c13 = ExaModels.constraint!(w, c9, g.bus => -pg[g.i] for g in data.gen)
c14 = ExaModels.constraint!(w, c10, g.bus => -qg[g.i] for g in data.gen)
model = ExaModels.TimedNLPModel(
ExaModels.ExaModel(w)
)
model_build_time = time() - time_model_start
time_solve_start = time()
result = NLPModelsIpopt.ipopt(model)
cost = result.objective
feasible = result.status == :first_order
solve_time = time() - time_solve_start
total_time = time() - time_data_start
total_callback_time =
model.stats.obj_time +
model.stats.grad_time +
model.stats.cons_time +
model.stats.jac_coord_time +
model.stats.hess_coord_time +
model.stats.jac_structure_time +
model.stats.hess_structure_time
println("")
println("\033[1mSummary\033[0m")
println(" case........: $(file_name)")
println(" variables...: $(model.meta.nvar)")
println(" constraints.: $(model.meta.ncon)")
println(" feasible....: $(feasible)")
println(" cost........: $(round(Int, cost))")
println(" total time..: $(total_time)")
println(" data time.: $(data_load_time)")
println(" build time: $(model_build_time)")
println(" solve time: $(solve_time)")
println(" callbacks: $(total_callback_time)")
println("")
println(" callbacks time:")
println(" * obj.....: $(model.stats.obj_time)")
println(" * grad....: $(model.stats.grad_time)")
println(" * cons....: $(model.stats.cons_time)")
println(" * jac.....: $(model.stats.jac_coord_time + model.stats.jac_structure_time)")
println(" * hesslag.: $(model.stats.hess_coord_time + model.stats.hess_structure_time)")
println("")
va_sol = ExaModels.solution(result, va)
va_dict = Dict("va_$(b.j)" => va_sol[b.i] for (i,b) in enumerate(data.bus))
vm_sol = ExaModels.solution(result, vm)
vm_dict = Dict("vm_$(b.j)" => vm_sol[b.i] for (i,b) in enumerate(data.bus))
pg_sol = ExaModels.solution(result, pg)
pg_dict = Dict("pg_$(b.j)" => pg_sol[b.i] for (i,b) in enumerate(data.gen))
qg_sol = ExaModels.solution(result, qg)
qg_dict = Dict("qg_$(b.j)" => qg_sol[b.i] for (i,b) in enumerate(data.gen))
p_sol = ExaModels.solution(result, p)
p_dict = Dict("p_$(write_out_tuple(ref[:arcs][i]))" => p_sol[i] for (i,b) in enumerate(data.arc))
q_sol = ExaModels.solution(result, q)
q_dict = Dict("q_$(write_out_tuple(ref[:arcs][i]))" => q_sol[i] for (i,b) in enumerate(data.arc))
return Dict(
"case" => file_name,
"variables" => model.meta.nvar,
"constraints" => model.meta.ncon,
"feasible" => feasible,
"cost" => cost,
"time_total" => total_time,
"time_data" => data_load_time,
"time_build" => model_build_time,
"time_solve" => solve_time,
"time_callbacks" => total_callback_time,
"solution" => Dict(
va_dict...,
vm_dict...,
pg_dict...,
qg_dict...,
p_dict...,
q_dict...),
)
end
write_out_tuple((i,j,k)) = "$(i)_$(j)_$(k)"
if isinteractive() == false
solve_opf("$(@__DIR__)/data/opf_warmup.m")
end