-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpownet_thai_solver.py
More file actions
208 lines (155 loc) · 7.35 KB
/
pownet_thai_solver.py
File metadata and controls
208 lines (155 loc) · 7.35 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
197
198
199
200
201
202
203
204
205
206
207
from pyomo.opt import SolverFactory, SolverStatus
from pyomo.core import Var
from pyomo.core import Param
from operator import itemgetter
import pandas as pd
from datetime import datetime
import os
yr = 2016
##run pownet (a year will run in four quarters)
for q in range(0,4):
if q == 0:
start = 1
end = 90
if q == 1:
start = 91
end = 181
if q == 2:
start = 182
end = 273
if q == 3:
start = 274
end = 365
from pownet_thai_model import model
##create instance with data
instance = model.create_instance('./input/pownet_thai_v1_data_'+str(yr)+'.dat')
##solver, threads
opt = SolverFactory("gurobi") ##gurobi SolverFactory("CPLEX")
opt.options["threads"] = 1
opt.options["TimeLimit"] = 120 ##in seconds
H = instance.HorizonHours
K=range(1,H+1)
#Space to store results
on=[]
switch=[]
mwh=[]
hydro=[]
solar=[]
wind=[]
hydro_import=[]
srsv=[]
nrsv=[]
vlt_angle=[]
for day in range(start,end+1):
for z in instance.d_nodes:
#load Demand and Reserve time series data
for i in K:
instance.HorizonDemand[z,i] = instance.SimDemand[z,(day-1)*24+i]
instance.HorizonReserves[i] = instance.SimReserves[(day-1)*24+i]
for z in instance.h_nodes:
#load Hydropower time series data
for i in K:
instance.HorizonHydro[z,i] = instance.SimHydro[z,(day-1)*24+i]
for z in instance.s_nodes:
#load Solar time series data
for i in K:
instance.HorizonSolar[z,i] = instance.SimSolar[z,(day-1)*24+i]
for z in instance.w_nodes:
#load Wind time series data
for i in K:
instance.HorizonWind[z,i] = instance.SimWind[z,(day-1)*24+i]
for z in instance.h_imports:
#load Hydropower time series data
for i in K:
instance.HorizonHydroImport[z,i] = instance.SimHydroImport[z,(day-1)*24+i]
for z in instance.Generators:
#load Deratef time series data
for i in K:
instance.HorizonDeratef[z,i] = instance.SimDeratef[z,(day-1)*24+i]
thai_result = opt.solve(instance,tee=True) ##,tee=True
if thai_result.solver.status == SolverStatus.aborted: #max time limit reached
thai_result.solver.status = SolverStatus.warning #change status so that results can be loaded
instance.solutions.load_from(thai_result)
# #The following section is for storing and sorting results
for v in instance.component_objects(Var, active=True):
varobject = getattr(instance, str(v))
a=str(v)
if a=='hydro':
for index in varobject:
if int(index[1]>0 and index[1]<25):
if index[0] in instance.h_nodes:
hydro.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if a=='solar':
for index in varobject:
if int(index[1]>0 and index[1]<25):
if index[0] in instance.s_nodes:
solar.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if a=='wind':
for index in varobject:
if int(index[1]>0 and index[1]<25):
if index[0] in instance.w_nodes:
wind.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if a=='hydro_import':
for index in varobject:
if int(index[1]>0 and index[1]<25):
if index[0] in instance.h_imports:
hydro_import.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if a=='vlt_angle':
for index in varobject:
if int(index[1]>0 and index[1]<25):
if index[0] in instance.nodes:
vlt_angle.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if a=='mwh':
ini_mwh_ = {}
for index in varobject:
if int(index[1]>0 and index[1]<25):
mwh.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if int(index[1])==24:
ini_mwh_[index[0]] = varobject[index].value
if a=='on':
ini_on_ = {}
for index in varobject:
if int(index[1]>0 and index[1]<25):
on.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if int(index[1])==24:
ini_on_[index[0]] = varobject[index].value
if a=='switch':
for index in varobject:
if int(index[1]>0 and index[1]<25):
switch.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if a=='srsv':
for index in varobject:
if int(index[1]>0 and index[1]<25):
srsv.append((index[0],index[1]+((day-1)*24),varobject[index].value))
if a=='nrsv':
for index in varobject:
if int(index[1]>0 and index[1]<25):
nrsv.append((index[0],index[1]+((day-1)*24),varobject[index].value))
# Update initialization values for "on" and "mwh"
for z in instance.Generators:
instance.ini_on[z] = round(ini_on_[z])
instance.ini_mwh[z] = max(round(ini_mwh_[z],2),0)
print(day)
print(str(datetime.now()))
###outputs to dataframe
hydro_pd=pd.DataFrame(hydro,columns=('Node','Time','Value'))
hydro_import_pd=pd.DataFrame(hydro_import,columns=('Node','Time','Value'))
solar_pd=pd.DataFrame(solar,columns=('Node','Time','Value'))
wind_pd=pd.DataFrame(wind,columns=('Node','Time','Value'))
vlt_angle_pd=pd.DataFrame(vlt_angle,columns=('Node','Time','Value'))
mwh_pd=pd.DataFrame(mwh,columns=('Generator','Time','Value'))
on_pd=pd.DataFrame(on,columns=('Generator','Time','Value'))
switch_pd=pd.DataFrame(switch,columns=('Generator','Time','Value'))
srsv_pd=pd.DataFrame(srsv,columns=('Generator','Time','Value'))
nrsv_pd=pd.DataFrame(nrsv,columns=('Generator','Time','Value'))
###Save outputs
mwh_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_mwh.csv')
hydro_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_hydro.csv')
hydro_import_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_hydro_import.csv')
solar_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_solar.csv')
wind_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_wind.csv')
vlt_angle_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_vlt_angle.csv')
on_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_on.csv')
switch_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_switch.csv')
srsv_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_srsv.csv')
nrsv_pd.to_csv('./output/out_thai_v1_'+str(yr)+'_q'+str(q+1)+'_nrsv.csv')