-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_1.py
414 lines (333 loc) · 13.9 KB
/
model_1.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# %%
import pandas as pd
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
from uc_problem import solve_uc_problem
def find_optimal_k_method_1(
gens_df,
k_values_df,
demand_df,
k_max,
opt_gen,
big_w=10000,
time_limit=60,
print_results=False,
K=5,
):
model = pyo.ConcreteModel()
# sets
model.time = pyo.Set(initialize=demand_df.index)
model.gens = pyo.Set(initialize=gens_df.index)
# primary variables
model.g = pyo.Var(model.gens, model.time, within=pyo.NonNegativeReals)
model.d = pyo.Var(model.time, within=pyo.NonNegativeReals)
model.c_up = pyo.Var(model.gens, model.time, within=pyo.NonNegativeReals)
model.c_down = pyo.Var(model.gens, model.time, within=pyo.NonNegativeReals)
model.k = pyo.Var(model.time, bounds=(1, k_max), within=pyo.NonNegativeReals)
model.lambda_ = pyo.Var(model.time, within=pyo.Reals, bounds=(-500, 200))
model.u = pyo.Var(model.gens, model.time, within=pyo.Binary)
# secondary variables
model.mu_max = pyo.Var(model.gens, model.time, within=pyo.NonNegativeReals)
model.mu_min = pyo.Var(model.gens, model.time, within=pyo.NonNegativeReals)
model.nu_max = pyo.Var(model.time, within=pyo.NonNegativeReals)
model.pi_u = pyo.Var(model.gens, model.time, within=pyo.NonNegativeReals)
model.pi_d = pyo.Var(model.gens, model.time, within=pyo.NonNegativeReals)
model.sigma_u = pyo.Var(model.gens, model.time, bounds=(0, 1))
model.sigma_d = pyo.Var(model.gens, model.time, bounds=(0, 1))
model.psi_max = pyo.Var(model.gens, model.time, within=pyo.NonNegativeReals)
# binary expansion variables
model.g_binary = pyo.Var(model.time, range(K), within=pyo.Binary)
model.z_lambda = pyo.Var(model.time, range(K), within=pyo.NonNegativeReals)
model.z_k = pyo.Var(model.time, range(K), within=pyo.NonNegativeReals)
delta = [gens_df.at[gen, "g_max"] / (pow(2, K) - 1) for gen in gens_df.index]
# binary expansion constraints
def g_binary_rule(model, t):
return model.g[opt_gen, t] == delta[opt_gen] * sum(
pow(2, k) * model.g_binary[t, k] for k in range(K)
)
model.g_binary_constr = pyo.Constraint(model.time, rule=g_binary_rule)
def binary_expansion_1_constr_1_max_rule(model, t, n):
return model.lambda_[t] - model.z_lambda[t, n] <= (
max(gens_df["mc"]) * k_max
) * (1 - model.g_binary[t, n])
def binary_expansion_1_constr_1_min_rule(model, t, n):
return model.lambda_[t] - model.z_lambda[t, n] >= 0
def binary_expansion_1_constr_2_rule(model, t, n):
return (
model.z_lambda[t, n] <= (max(gens_df["mc"]) * k_max) * model.g_binary[t, n]
)
model.binary_expansion_1_constr_1_max = pyo.Constraint(
model.time, range(K), rule=binary_expansion_1_constr_1_max_rule
)
model.binary_expansion_1_constr_1_min = pyo.Constraint(
model.time, range(K), rule=binary_expansion_1_constr_1_min_rule
)
model.binary_expansion_1_constr_2 = pyo.Constraint(
model.time, range(K), rule=binary_expansion_1_constr_2_rule
)
def binary_expansion_2_constr_1_max_rule(model, t, n):
return model.k[t] - model.z_k[t, n] <= (max(gens_df["mc"]) * k_max) * (
1 - model.g_binary[t, n]
)
def binary_expansion_2_constr_1_min_rule(model, t, n):
return model.k[t] - model.z_k[t, n] >= 0
def binary_expansion_2_constr_2_rule(model, t, n):
return model.z_k[t, n] <= (max(gens_df["mc"]) * k_max) * model.g_binary[t, n]
model.binary_expansion_2_constr_1_max = pyo.Constraint(
model.time, range(K), rule=binary_expansion_2_constr_1_max_rule
)
model.binary_expansion_2_constr_1_min = pyo.Constraint(
model.time, range(K), rule=binary_expansion_2_constr_1_min_rule
)
model.binary_expansion_2_constr_2 = pyo.Constraint(
model.time, range(K), rule=binary_expansion_2_constr_2_rule
)
# objective rules
def primary_objective_rule(model):
return sum(
delta[opt_gen] * sum(pow(2, n) * model.z_lambda[t, n] for n in range(K))
- gens_df.at[opt_gen, "mc"] * model.g[opt_gen, t]
- model.c_up[opt_gen, t]
- model.c_down[opt_gen, t]
for t in model.time
)
def duality_gap_part_1_rule(model):
expr = sum(
(
(
gens_df.at[gen, "mc"]
* delta[gen]
* sum(pow(2, n) * model.z_k[t, n] for n in range(K))
+ model.c_up[gen, t]
+ model.c_down[gen, t]
)
if gen == opt_gen
else (
k_values_df.at[t, gen] * gens_df.at[gen, "mc"] * model.g[gen, t]
+ model.c_up[gen, t]
+ model.c_down[gen, t]
)
)
for gen in model.gens
for t in model.time
)
expr -= sum(demand_df.at[t, "price"] * model.d[t] for t in model.time)
return expr
def duality_gap_part_2_rule(model):
expr = -sum(model.nu_max[t] * demand_df.at[t, "volume"] for t in model.time)
expr -= sum(
model.pi_u[i, t] * gens_df.at[i, "r_up"]
for i in model.gens
for t in model.time
)
expr -= sum(
model.pi_d[i, t] * gens_df.at[i, "r_down"]
for i in model.gens
for t in model.time
)
expr -= sum(model.pi_u[i, 0] * gens_df.at[i, "g_0"] for i in model.gens)
expr += sum(model.pi_d[i, 0] * gens_df.at[i, "g_0"] for i in model.gens)
expr -= sum(
model.sigma_u[i, 0] * gens_df.at[i, "k_up"] * gens_df.at[i, "u_0"]
for i in model.gens
)
expr += sum(
model.sigma_d[i, 0] * gens_df.at[i, "k_down"] * gens_df.at[i, "u_0"]
for i in model.gens
)
expr -= sum(model.psi_max[i, t] for i in model.gens for t in model.time)
return expr
def final_objective_rule(model):
return primary_objective_rule(model) - big_w * (
duality_gap_part_1_rule(model) - duality_gap_part_2_rule(model)
)
model.objective = pyo.Objective(expr=final_objective_rule, sense=pyo.maximize)
# constraints
# energy balance constraint
def balance_rule(model, t):
return model.d[t] - sum(model.g[i, t] for i in model.gens) == 0
model.balance = pyo.Constraint(model.time, rule=balance_rule)
# max generation constraint
def g_max_rule(model, i, t):
return model.g[i, t] <= gens_df.at[i, "g_max"] * model.u[i, t]
model.g_max = pyo.Constraint(model.gens, model.time, rule=g_max_rule)
# min generation constraint
def g_min_rule(model, i, t):
return model.g[i, t] >= gens_df.at[i, "g_min"] * model.u[i, t]
model.g_min = pyo.Constraint(model.gens, model.time, rule=g_min_rule)
# max demand constraint
def d_max_rule(model, t):
return model.d[t] <= demand_df.at[t, "volume"]
model.d_max = pyo.Constraint(model.time, rule=d_max_rule)
# max ramp up constraint
def ru_max_rule(model, i, t):
if t == 0:
return model.g[i, t] - gens_df.at[i, "g_0"] <= gens_df.at[i, "r_up"]
else:
return model.g[i, t] - model.g[i, t - 1] <= gens_df.at[i, "r_up"]
model.ru_max = pyo.Constraint(model.gens, model.time, rule=ru_max_rule)
# max ramp down constraint
def rd_max_rule(model, i, t):
if t == 0:
return gens_df.at[i, "g_0"] - model.g[i, t] <= gens_df.at[i, "r_down"]
else:
return model.g[i, t - 1] - model.g[i, t] <= gens_df.at[i, "r_down"]
model.rd_max = pyo.Constraint(model.gens, model.time, rule=rd_max_rule)
# start up cost constraint
def start_up_cost_rule(model, i, t):
if t == 0:
return (
model.c_up[i, t]
>= (model.u[i, t] - gens_df.at[i, "u_0"]) * gens_df.at[i, "k_up"]
)
else:
return (
model.c_up[i, t]
>= (model.u[i, t] - model.u[i, t - 1]) * gens_df.at[i, "k_up"]
)
model.start_up_cost = pyo.Constraint(
model.gens, model.time, rule=start_up_cost_rule
)
# shut down cost constraint
def shut_down_cost_rule(model, i, t):
if t == 0:
return (
model.c_down[i, t]
>= (gens_df.at[i, "u_0"] - model.u[i, t]) * gens_df.at[i, "k_down"]
)
else:
return (
model.c_down[i, t]
>= (model.u[i, t - 1] - model.u[i, t]) * gens_df.at[i, "k_down"]
)
model.shut_down_cost = pyo.Constraint(
model.gens, model.time, rule=shut_down_cost_rule
)
# dual constraints
def gen_dual_rule(model, i, t):
# Conditional parts based on `i` and `t`
k_term = model.k[t] if i == opt_gen else k_values_df.at[t, i]
pi_u_next_term = 0 if t == model.time.at(-1) else model.pi_u[i, t + 1]
pi_d_next_term = 0 if t == model.time.at(-1) else model.pi_d[i, t + 1]
# Combined expression
return (
k_term * gens_df.at[i, "mc"]
- model.lambda_[t]
+ model.mu_max[i, t]
- model.mu_min[i, t]
+ model.pi_u[i, t]
- pi_u_next_term
- model.pi_d[i, t]
+ pi_d_next_term
== 0
)
model.gen_dual = pyo.Constraint(model.gens, model.time, rule=gen_dual_rule)
def status_dual_rule(model, i, t):
if t != model.time.at(-1):
return (
-model.mu_max[i, t] * gens_df.at[i, "g_max"]
+ model.mu_min[i, t] * gens_df.at[i, "g_min"]
+ (model.sigma_u[i, t] - model.sigma_u[i, t + 1])
* gens_df.at[i, "k_up"]
- (model.sigma_d[i, t] - model.sigma_d[i, t + 1])
* gens_df.at[i, "k_down"]
+ model.psi_max[i, t]
>= 0
)
else:
return (
-model.mu_max[i, t] * gens_df.at[i, "g_max"]
+ model.mu_min[i, t] * gens_df.at[i, "g_min"]
+ model.sigma_u[i, t] * gens_df.at[i, "k_up"]
- model.sigma_d[i, t] * gens_df.at[i, "k_down"]
+ model.psi_max[i, t]
>= 0
)
model.status_dual = pyo.Constraint(model.gens, model.time, rule=status_dual_rule)
def demand_dual_rule(model, t):
return -demand_df.at[t, "price"] + model.lambda_[t] + model.nu_max[t] >= 0
model.demand_dual = pyo.Constraint(model.time, rule=demand_dual_rule)
# solve
instance = model.create_instance()
solver = SolverFactory("gurobi")
options = {
"LogToConsole": print_results,
"TimeLimit": time_limit,
"MIPGap": 0.03,
# "MIPFocus": 3,
}
results = solver.solve(instance, options=options, tee=print_results)
# check if solver exited due to time limit
if results.solver.termination_condition == pyo.TerminationCondition.maxTimeLimit:
print("Solver did not converge to an optimal solution")
generation_df = pd.DataFrame(
index=demand_df.index, columns=[f"gen_{gen}" for gen in gens_df.index]
)
for gen in gens_df.index:
for t in demand_df.index:
generation_df.at[t, f"gen_{gen}"] = instance.g[gen, t].value
demand_df = pd.DataFrame(index=demand_df.index, columns=["demand"])
for t in demand_df.index:
demand_df.at[t, "demand"] = instance.d[t].value
mcp = pd.DataFrame(index=demand_df.index, columns=["mcp"])
for t in demand_df.index:
mcp.at[t, "mcp"] = instance.lambda_[t].value
main_df = pd.concat([generation_df, demand_df, mcp], axis=1)
start_up_cost = pd.DataFrame(
index=demand_df.index, columns=[f"start_up_{gen}" for gen in gens_df.index]
)
for gen in gens_df.index:
for t in demand_df.index:
start_up_cost.at[t, f"start_up_{gen}"] = instance.c_up[gen, t].value
shut_down_cost = pd.DataFrame(
index=demand_df.index, columns=[f"shut_down_{gen}" for gen in gens_df.index]
)
for gen in gens_df.index:
for t in demand_df.index:
shut_down_cost.at[t, f"shut_down_{gen}"] = instance.c_down[gen, t].value
supp_df = pd.concat([start_up_cost, shut_down_cost], axis=1)
k_values = pd.DataFrame(index=demand_df.index, columns=["k"])
for t in demand_df.index:
k_values.at[t, "k"] = instance.k[t].value
return main_df, supp_df, k_values
# %%
if __name__ == "__main__":
case = "Case_1"
big_w = 10 # weight for duality gap objective
k_max = 2 # maximum multiplier for strategic bidding
opt_gen = 1 # generator that is allowed to bid strategically
start = pd.to_datetime("2019-03-02 06:00")
end = pd.to_datetime("2019-03-02 14:00")
# gens
gens_df = pd.read_csv(f"inputs/{case}/gens.csv", index_col=0)
# 24 hours of demand first increasing and then decreasing
demand_df = pd.read_csv(f"inputs/{case}/demand.csv", index_col=0)
demand_df.index = pd.to_datetime(demand_df.index)
demand_df = demand_df.loc[start:end]
# reset index to start at 0
demand_df = demand_df.reset_index(drop=True)
k_values_df = pd.DataFrame(columns=gens_df.index, index=demand_df.index, data=1.0)
main_df, supp_df, k_values = find_optimal_k_method_1(
gens_df=gens_df,
k_values_df=k_values_df,
demand_df=demand_df,
k_max=k_max,
opt_gen=opt_gen,
big_w=big_w,
time_limit=300,
print_results=True,
K=10,
)
print(main_df)
print()
print(k_values)
# %%
k_values_df_1 = k_values_df.copy()
k_values_df_1[opt_gen] = k_values
updated_main_df_1, updated_supp_df_1 = solve_uc_problem(
gens_df, demand_df, k_values_df_1
)
save_path = f"outputs/{case}/gen_{opt_gen}"
k_values.to_csv(f"{save_path}/k_values_1.csv")
updated_main_df_1.to_csv(f"{save_path}/updated_main_df_1.csv")
updated_supp_df_1.to_csv(f"{save_path}/updated_supp_df_1.csv")