-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_results.py
More file actions
252 lines (218 loc) · 10.8 KB
/
Copy pathwrite_results.py
File metadata and controls
252 lines (218 loc) · 10.8 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
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
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 8 09:24:14 2019
@author: llavi
"""
import os
from os.path import join
import traceback
import sys
import pdb
import pandas as pd
import numpy as np
def export_results(instance, results, results_directory, debug_mode):
"""
Retrieves the relevant sets over which it will loop, then call functions to export different result categories.
If an exception is encountered, log the error traceback. If not in debug mode, exit. If in debug mode,
open an interactive Python session that will make it possible to try to correct the error without having to re-run
problem; quitting the interactive session will resume running the next export function, not exit.
:param instance: the problem instance
:param results: the results
:param results_directory: directory to export results files to
:param prod_cost_inputs_directory: directory that contains production cost model inputs
:param debug_mode:
:return:
"""
print ("Exporting results... ")
# First, load solution
load_solution(instance, results)
# Get sets
# Sort the sets to return a predictable format of results files
timepoints_set = sorted(instance.TIMEPOINTS)
generators_set = instance.GENERATORS #don't sort these so order is preserved for future cases
ordc_segments_set = sorted(instance.SEGMENTS)
zones_set = sorted(instance.ZONES)
transmission_lines_set = sorted(instance.TRANSMISSION_LINE)
# Call various export functions, throw debug errors if problem and desired
# Export generator dispatch
try:
export_generator_dispatch(instance, timepoints_set, generators_set, zones_set, results_directory)
except Exception as err:
msg = "ERROR exporting generator dispatch! Check export_generator_dispatch()."
handle_exception(msg, debug_mode)
#export zonal prices
try:
export_zonal_price(instance, timepoints_set, zones_set, results_directory)
except Exception as err:
msg = "ERROR exporting zonal prices! Check export_zonal_price()."
handle_exception(msg, debug_mode)
#export tx lines
try:
export_lines(instance, timepoints_set, transmission_lines_set, results_directory)
except Exception as err:
msg = "ERROR exporting transmission lines! Check export_lines()."
handle_exception(msg, debug_mode)
try:
export_generator_commits_reserves(instance, timepoints_set, generators_set, results_directory)
except Exception as err:
msg = "ERROR exporting transmission lines! Check export_lines()."
handle_exception(msg, debug_mode)
try:
export_reserve_segment_commits(instance,timepoints_set, ordc_segments_set, results_directory)
except Exception as err:
msg = "ERROR exporting reserve segments! Check export_reserve_segment_commits()."
handle_exception(msg, debug_mode)
try:
export_VREs(instance, results_directory)
except Exception as err:
msg = "ERROR exporting VRE results! Check export_VREs()."
handle_exception(msg, debug_mode)
#return call
return None
# formatting functions
# return value rounded to two decimal places
def format_2f(input_data):
"""
:param input_data: The data to format
:return:
"""
if input_data is None:
formatted_data = None
else:
formatted_data = '{:0.2f}'.format(input_data)
# if formatted_data is negative but rounds to zero, it will be printed as a negative zero
# this gets rid of the negative zero
if formatted_data == '0.00' or formatted_data == '-0.00':
formatted_data = 0
return formatted_data
#debugging call, if desired
def handle_exception(message, debug):
"""
How to handle exceptions. First print a custom message and the traceback.
If in debug mode, open the Python debugger (PDB), else exit. To execute multi-line statements in PDB, type this
to launch an interactive Python session with all the local variables available.
This function is used by export_results() to handle exceptions.
:param message:
:param debug:
:return:
"""
print (message)
print(traceback.format_exc())
if debug:
print ("""
Launching Python debugger (PDB)...
To execute multi-line statements in PDB, type this:
import code; code.interact(local=vars())
(The command launches an interactive Python session with all the local variables available.)
To exit the interactive session and continue script execution, press Ctrl+D.
""")
tp, value, tb = sys.exc_info()
pdb.post_mortem(tb)
else:
print ("Debug option not chosen, so exiting.")
sys.exit()
# #################### Data Exports #################### #
def load_solution(instance, results):
"""
Load results. This function is called by export_results().
:param instance:
:param results:
:return:
"""
instance.solutions.load_from(results)
def export_generator_dispatch(instance, timepoints_set, generators_set, zones_set, results_directory):
results_dispatch = []
index_name = []
for z in zones_set:
for g in generators_set:
index_name.append(z+"-"+str(g))
for t in timepoints_set:
results_dispatch.append(format_2f(instance.dispatch[t,g,z].value))
results_dispatch_np = np.reshape(results_dispatch, (int(len(results_dispatch)/len(timepoints_set)), int(len(timepoints_set))))
df = pd.DataFrame(results_dispatch_np, index=pd.Index(index_name))
df.to_csv(os.path.join(results_directory,"generator_dispatch.csv"))
def export_zonal_price(instance, timepoints_set, zones_set, results_directory):
results_prices = []
index_name = []
timepoints_list = []
for z in zones_set:
for t in timepoints_set:
index_name.append(z)
results_prices.append(format_2f(instance.dual[instance.LoadConstraint[t,z]]))
timepoints_list.append(t)
df = pd.DataFrame({'hour': timepoints_list, 'LMP':np.asarray(results_prices)},
index=pd.Index(index_name))
df.to_csv(os.path.join(results_directory,"zonal_prices.csv"))
def export_lines(instance, timepoints_set, transmission_lines_set, results_directory):
transmission_duals = []
results_transmission_line_flow = []
index_name = []
for line in transmission_lines_set:
for t in timepoints_set:
index_name.append(line+"-"+str(t))
transmission_duals.append(format_2f(instance.dual[instance.TxFromConstraint[t,line]] +\
instance.dual[instance.TxToConstraint[t,line]]))
results_transmission_line_flow.append(format_2f(instance.transmit_power_MW[t,line].value))
col_names = ['congestion price ($/MW)','flow (MW)']
df = pd.DataFrame(data=np.column_stack((np.asarray(transmission_duals),np.asarray(results_transmission_line_flow))),
columns=col_names,index=pd.Index(index_name))
df.to_csv(os.path.join(results_directory,"tx_flows.csv"))
def export_generator_commits_reserves(instance, timepoints_set, generators_set, results_directory):
results_gens = []
results_time = []
results_commitment = []
results_starts = []
results_shuts = []
results_hourson = []
results_hoursoff = []
results_spinreserves = []
index_name = []
for g in generators_set:
for t in timepoints_set:
index_name.append(str(g)+","+str(t))
results_gens.append(g)
results_time.append(t)
results_commitment.append(instance.commitment[t,g].value)
results_starts.append(instance.startup[t,g].value)
results_shuts.append(instance.shutdown[t,g].value)
if t==1 and instance.commitinit[g]==instance.commitment[t,g].value:
results_hourson.append(instance.commitinit[g] * instance.upinit[g] + instance.commitment[t,g].value)
results_hoursoff.append((1-instance.commitinit[g]) * instance.downinit[g] + (1-instance.commitment[t,g].value))
elif instance.startup[t,g].value==1 or instance.shutdown[t,g].value==1:
results_hourson.append(instance.commitment[t,g].value)
results_hoursoff.append((1-instance.commitment[t,g].value))
else:
results_hourson.append(results_hourson[-1]+instance.commitment[t,g].value)
results_hoursoff.append(results_hoursoff[-1]+(1-instance.commitment[t,g].value))
results_spinreserves.append(format_2f(instance.spinreserves[t,g].value))
col_names = ['Gen_Index','timepoint','Committed','Started','Shut','TimeOn','TimeOff','Held as Reserves (MW)']
df = pd.DataFrame(data=np.column_stack((np.asarray(results_gens), np.asarray(results_time), np.asarray(results_commitment),
np.asarray(results_starts), np.asarray(results_shuts),np.asarray(results_hourson),
np.asarray(results_hoursoff), np.asarray(results_spinreserves))),
columns=col_names,index=pd.Index(index_name))
df.to_csv(os.path.join(results_directory,"generator_commits_reserves.csv"), index=False)
def export_reserve_segment_commits(instance, timepoints_set, ordc_segments_set, results_directory):
results_segments = []
index_name = []
for s in ordc_segments_set:
for t in timepoints_set:
index_name.append(str(s)+","+str(t))
results_segments.append(format_2f(instance.segmentreserves[t,s].value))
col_names = ['MW on reserve segment']
df = pd.DataFrame(data=(np.asarray(results_segments)),columns=col_names,index=pd.Index(index_name))
df.to_csv(os.path.join(results_directory,"reserve_segment_commit.csv"))
def export_VREs(instance, results_directory):
results_wind = []
results_solar = []
results_curtailment = []
tmps = []
zones = []
for t in instance.TIMEPOINTS:
for z in instance.ZONES:
tmps.append(t)
zones.append(z)
results_wind.append(instance.windgen[t,z].value)
results_solar.append(instance.solargen[t,z].value)
results_curtailment.append(instance.curtailment[t,z].value)
VRE = pd.DataFrame({"timepoint": tmps, "zone": zones, "wind":results_wind, "solar": results_solar, "curtailment":results_curtailment})
VRE.to_csv(os.path.join(results_directory,"renewable_generation.csv"), index=False)