forked from tum-ens/urbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runme.py
107 lines (84 loc) · 2.98 KB
/
runme.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
import coopr.environ
import os
import urbs
from coopr.opt.base import SolverFactory
# INIT
filename = 'data-example.xlsx'
(offset, length) = (4000, 5*24) # timestep selection
timesteps = range(offset, offset+length+1)
# SCENARIOS
def scenario_base(data):
# do nothing
return data
def scenario_stock_prices(data):
# change stock commodity prices
co = data['commodity']
stock_commodities_only = (co.index.get_level_values('Type') == 'Stock')
co.loc[stock_commodities_only, 'price'] *= 1.5
return data
def scenario_co2_limit(data):
# change CO2 limit
co = data['commodity']
co.loc[('Global', 'CO2', 'Env'), 'max'] *= 0.05
return data
def scenario_north_process_caps(data):
# change maximum installable capacity
pro = data['process']
pro.loc[('North', 'turb', 'Hydro', 'Elec'), 'cap-up'] *= 0.5
pro.loc[('North', 'pp', 'Biomass', 'Elec'), 'cap-up'] *= 0.25
return data
def scenario_all_together(data):
# combine all other scenarios
data = scenario_stock_prices(data)
data = scenario_co2_limit(data)
data = scenario_north_process_caps(data)
return data
# select scenarios to be run
scenarios = [
scenario_base,
scenario_stock_prices,
scenario_co2_limit,
scenario_north_process_caps,
scenario_all_together]
# MAIN
for scenario in scenarios:
# scenario name, read and modify data for scenario
sce = scenario.__name__
data = urbs.read_excel(filename)
data = scenario(data)
# create model, solve it, read results
model = urbs.create_model(data, timesteps)
prob = model.create()
optim = SolverFactory('glpk') # cplex, glpk, gurobi, ...
result = optim.solve(prob, tee=True)
prob.load(result)
#create timestamp for filename: abc_YYYY-MM-DD_hh-mm.*
from datetime import datetime
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M')
# write report to spreadsheet
urbs.report(
prob,
os.path.join('results', '{}_{}.xlsx').format(sce, timestamp),
['Elec'], ['South', 'Mid', 'North'])
# add or change plot colours
my_colors = {
'South': (230, 200, 200),
'Mid': (200, 230, 200),
'North': (200, 200, 230)}
for country, color in my_colors.iteritems():
urbs.COLORS[country] = color
# create timeseries plot for each demand (site, commodity) timeseries
for sit, com in prob.demand.columns:
# create figure
fig = urbs.plot(prob, com, sit)
# change the figure title
ax0 = fig.get_axes()[0]
nice_sce_name = sce.replace('_', ' ').title()
new_figure_title = ax0.get_title().replace(
'Energy balance of ', '{}: '.format(nice_sce_name))
ax0.set_title(new_figure_title)
# save plot to files
for ext in ['png', 'pdf']:
fig_filename = os.path.join(
'results', '{}-{}-{}_{}.{}').format(sce, com, sit, timestamp, ext)
fig.savefig(fig_filename, bbox_inches='tight')