forked from lanl-ansi/PowerModels.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjective.jl
339 lines (279 loc) · 10.8 KB
/
objective.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
################################################################################
# This file is to defines commonly used constraints for power flow models
# This will hopefully make everything more compositional
################################################################################
"""
Checks that all cost models are present and of the same type
"""
function check_cost_models(pm::GenericPowerModel)
model = nothing
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
if haskey(gen, "cost")
if model == nothing
model = gen["model"]
else
if gen["model"] != model
error("cost models are inconsistent, the typical model is $(model) however model $(gen["model"]) is given on generator $(i)")
end
end
else
error("no cost given for generator $(i)")
end
end
for (i,dcline) in nw_ref[:dcline]
if haskey(dcline, "model")
if model == nothing
model = dcline["model"]
else
if dcline["model"] != model
error("cost models are inconsistent, the typical model is $(model) however model $(dcline["model"]) is given on dcline $(i)")
end
end
else
error("no cost given for dcline $(i)")
end
end
end
return model
end
""
function objective_min_fuel_cost(pm::GenericPowerModel)
model = check_cost_models(pm)
if model == 1
return objective_min_pwl_fuel_cost(pm)
elseif model == 2
return objective_min_polynomial_fuel_cost(pm)
else
error("Only cost models of types 1 and 2 are supported at this time, given cost model type of $(model)")
end
end
""
function objective_min_gen_fuel_cost(pm::GenericPowerModel)
model = check_cost_models(pm)
if model == 1
return objective_min_gen_pwl_fuel_cost(pm)
elseif model == 2
return objective_min_gen_polynomial_fuel_cost(pm)
else
error("Only cost models of types 1 and 2 are supported at this time, given cost model type of $(model)")
end
end
"""
Checks that all cost models are polynomials, quadratic or less
"""
function check_polynomial_cost_models(pm::GenericPowerModel)
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
@assert gen["model"] == 2
if length(gen["cost"]) > 3
error("only cost models of degree 3 or less are supported at this time, given cost model of degree $(length(gen["cost"])) on generator $(i)")
end
end
for (i,dcline) in nw_ref[:dcline]
@assert dcline["model"] == 2
if length(dcline["cost"]) > 3
error("only cost models of degree 3 or less are supported at this time, given cost model of degree $(length(dcline["cost"])) on dcline $(i)")
end
end
end
end
""
function objective_min_polynomial_fuel_cost(pm::GenericPowerModel)
check_polynomial_cost_models(pm)
from_idx = Dict()
for (n, nw_ref) in nws(pm)
from_idx[n] = Dict(arc[1] => arc for arc in nw_ref[:arcs_from_dc])
end
return @objective(pm.model, Min,
sum(
sum( gen["cost"][1]*sum( var(pm, n, c, :pg, i) for c in conductor_ids(pm, n))^2 +
gen["cost"][2]*sum( var(pm, n, c, :pg, i) for c in conductor_ids(pm, n))+
gen["cost"][3] for (i,gen) in nw_ref[:gen]) +
sum( dcline["cost"][1]*sum( var(pm, n, c, :p_dc, from_idx[n][i]) for c in conductor_ids(pm, n))^2 +
dcline["cost"][2]*sum( var(pm, n, c, :p_dc, from_idx[n][i]) for c in conductor_ids(pm, n)) +
dcline["cost"][3] for (i,dcline) in nw_ref[:dcline])
for (n, nw_ref) in nws(pm))
)
end
""
function objective_min_gen_polynomial_fuel_cost(pm::GenericPowerModel)
check_polynomial_cost_models(pm)
return @objective(pm.model, Min,
sum(
sum(
gen["cost"][1]*sum( var(pm, n, c, :pg, i) for c in conductor_ids(pm, n))^2 +
gen["cost"][2]*sum( var(pm, n, c, :pg, i) for c in conductor_ids(pm, n))+
gen["cost"][3]
for (i,gen) in nw_ref[:gen])
for (n, nw_ref) in nws(pm))
)
end
""
function objective_min_polynomial_fuel_cost(pm::GenericPowerModel{T}) where T <: AbstractConicForms
check_polynomial_cost_models(pm)
from_idx = Dict()
for (n, nw_ref) in nws(pm)
from_idx[n] = Dict(arc[1] => arc for arc in nw_ref[:arcs_from_dc])
end
pg_sqr = Dict()
dc_p_sqr = Dict()
for (n, nw_ref) in nws(pm)
for c in conductor_ids(pm, n)
pg_sqr_ub = Dict{Int,Real}()
pg_sqr_lb = Dict{Int,Real}()
for (i, gen) in ref(pm, n, :gen)
pmin = gen["pmin"][c]
pmax = gen["pmax"][c]
pg_sqr_ub[i] = max(pmin^2, pmax^2)
pg_sqr_lb[i] = 0.0
if pmin > 0.0
pg_sqr_lb[i] = pmin^2
end
if pmax < 0.0
pg_sqr_lb[i] = pmax^2
end
end
pg_sqr = var(pm, n, c)[:pg_sqr] = @variable(pm.model,
[i in ids(pm, n, :gen)], basename="$(n)_$(c)_pg_sqr",
lowerbound = pg_sqr_lb[i],
upperbound = pg_sqr_ub[i]
)
for (i, gen) in nw_ref[:gen]
@constraint(pm.model, norm([2*var(pm, n, c, :pg, i), pg_sqr[i]-1]) <= pg_sqr[i]+1)
end
dc_p_sqr_ub = Dict{Int,Real}()
dc_p_sqr_lb = Dict{Int,Real}()
for (i, dcline) in ref(pm, n, :dcline)
pmin = dcline["pminf"][c]
pmax = dcline["pmaxf"][c]
dc_p_sqr_ub[i] = max(pmin^2, pmax^2)
dc_p_sqr_lb[i] = 0.0
if pmin > 0.0
dc_p_sqr_lb[i] = pmin^2
end
if pmax < 0.0
dc_p_sqr_lb[i] = pmax^2
end
end
dc_p_sqr = var(pm, n, c)[:p_dc_sqr] = @variable(pm.model,
[i in ids(pm, n, :dcline)], basename="$(n)_$(c)_dc_p_sqr",
lowerbound = dc_p_sqr_lb[i],
upperbound = dc_p_sqr_ub[i]
)
for (i, dcline) in nw_ref[:dcline]
@constraint(pm.model, norm([2*var(pm, n, c, :p_dc)[from_idx[n][i]], dc_p_sqr[i]-1]) <= dc_p_sqr[i]+1)
end
end
end
return @objective(pm.model, Min,
sum(
sum( gen["cost"][1]*sum( var(pm, n, c, :pg_sqr, i) for c in conductor_ids(pm, n)) +
gen["cost"][2]*sum( var(pm, n, c, :pg, i) for c in conductor_ids(pm, n)) +
gen["cost"][3] for (i,gen) in nw_ref[:gen]) +
sum( dcline["cost"][1]*sum( var(pm, n, c, :p_dc_sqr, i) for c in conductor_ids(pm, n)) +
dcline["cost"][2]*sum( var(pm, n, c, :p_dc, from_idx[n][i]) for c in conductor_ids(pm, n)) +
dcline["cost"][3] for (i,dcline) in nw_ref[:dcline])
for (n, nw_ref) in nws(pm))
)
end
"""
compute m and b from points pwl points
"""
function slope_intercepts(points::Array{T,1}) where T <: Real
line_data = []
for i in 3:2:length(points)
x1 = points[i-2]
y1 = points[i-1]
x2 = points[i-0]
y2 = points[i+1]
m = (y2 - y1)/(x2 - x1)
b = y1 - m * x1
line = Dict{String,Any}(
"slope" => m,
"intercept" => b
)
push!(line_data, line)
end
return line_data
end
"""
compute lines in m and b from from pwl cost models
data is a list of components
"""
function get_lines(data)
lines = Dict{Int,Any}()
for (i,comp) in data
@assert comp["model"] == 1
line_data = slope_intercepts(comp["cost"])
lines[i] = line_data
for i in 2:length(line_data)
if line_data[i-1]["slope"] > line_data[i]["slope"]
error("non-convex pwl function found in points $(comp["cost"])\nlines: $(line_data)")
end
end
end
return lines
end
""
function objective_min_pwl_fuel_cost(pm::GenericPowerModel)
for (n, nw_ref) in nws(pm)
pg_cost = var(pm, n)[:pg_cost] = @variable(pm.model,
[i in ids(pm, n, :gen)], basename="$(n)_pg_cost"
)
# pwl cost
gen_lines = get_lines(nw_ref[:gen])
for (i, gen) in nw_ref[:gen]
for line in gen_lines[i]
@constraint(pm.model, pg_cost[i] >= line["slope"]*sum(var(pm, n, c, :pg, i) for c in conductor_ids(pm, n)) + line["intercept"])
end
end
dc_p_cost = var(pm, n)[:p_dc_cost] = @variable(pm.model,
[i in ids(pm, n, :dcline)], basename="$(n)_dc_p_cost",
)
# pwl cost
dcline_lines = get_lines(nw_ref[:dcline])
for (i, dcline) in nw_ref[:dcline]
arc = (i, dcline["f_bus"], dcline["t_bus"])
for line in dcline_lines[i]
@constraint(pm.model, dc_p_cost[i] >= line["slope"]*sum(var(pm, n, c, :p_dc)[arc] for c in conductor_ids(pm, n)) + line["intercept"])
end
end
end
return @objective(pm.model, Min,
sum(
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen]) +
sum( var(pm, n, :p_dc_cost, i) for (i,dcline) in nw_ref[:dcline])
for (n, nw_ref) in nws(pm))
)
end
""
function objective_min_gen_pwl_fuel_cost(pm::GenericPowerModel)
for (n, nw_ref) in nws(pm)
pg_cost = var(pm, n)[:pg_cost] = @variable(pm.model,
[i in ids(pm, n, :gen)], basename="$(n)_pg_cost"
)
# pwl cost
gen_lines = get_lines(nw_ref[:gen])
for (i, gen) in nw_ref[:gen]
for line in gen_lines[i]
@constraint(pm.model, pg_cost[i] >= line["slope"]*sum(var(pm, n, c, :pg, i) for c in conductor_ids(pm, n)) + line["intercept"])
end
end
end
return @objective(pm.model, Min,
sum(
sum( var(pm, n, :pg_cost, i) for (i,gen) in nw_ref[:gen])
for (n, nw_ref) in nws(pm))
)
end
"Cost of building branches"
function objective_tnep_cost(pm::GenericPowerModel)
return @objective(pm.model, Min,
sum(
sum(
sum( branch["construction_cost"]*var(pm, n, c, :branch_ne, i) for (i,branch) in nw_ref[:ne_branch] )
for c in conductor_ids(pm, n))
for (n, nw_ref) in nws(pm))
)
end