Daniel Varga commited on
Commit
376d985
·
1 Parent(s): 07e817b

adding energy supplier

Browse files
Files changed (4) hide show
  1. app.py +1 -85
  2. main.py +79 -0
  3. simulation.py +10 -0
  4. supplier.py +136 -0
app.py CHANGED
@@ -3,10 +3,6 @@
3
 
4
  import numpy as np
5
  import pandas as pd
6
-
7
- import matplotlib.pyplot as plt
8
- import matplotlib.cm
9
-
10
  import gradio as gr
11
 
12
  from simulation import *
@@ -15,90 +11,10 @@ from visualization import *
15
 
16
 
17
  #@title ### Downloading the data
18
- # !wget "https://static.renyi.hu/ai-shared/daniel/pq/PL_44527.19-21.csv"
19
  # !wget "https://static.renyi.hu/ai-shared/daniel/pq/pq_terheles_2021_adatok.tsv"
20
 
21
 
22
-
23
- matplotlib.rcParams['figure.figsize'] = [12, 8]
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
- def main():
35
- parameters = Parameters()
36
-
37
- met_2021_data, cons_2021_data = read_datasets()
38
-
39
- add_production_field(met_2021_data, parameters)
40
-
41
- all_2021_data = interpolate_and_join(met_2021_data, cons_2021_data)
42
-
43
- results = simulator_with_solar(all_2021_data, parameters)
44
-
45
- fig = visualize_simulation(results, date_range=("2021-02-01", "2021-03-01"))
46
- plt.show()
47
-
48
- consumptions_in_mwh = monthly_analysis(results)
49
- monthly_visualization(consumptions_in_mwh)
50
-
51
-
52
- # main() ; exit()
53
-
54
-
55
- def evaluate_parameters(parameters, met_2021_data, cons_2021_data):
56
- add_production_field(met_2021_data, parameters)
57
- all_2021_data = interpolate_and_join(met_2021_data, cons_2021_data)
58
- results = simulator_with_solar(all_2021_data, parameters)
59
- consumptions_in_mwh = monthly_analysis(results)
60
- return consumptions_in_mwh.sum(axis=0)
61
-
62
-
63
- def main_gridsearch():
64
- fixed_consumption = False
65
-
66
- parameters = Parameters()
67
- met_2021_data, cons_2021_data = read_datasets()
68
-
69
- if fixed_consumption:
70
- cons_2021_data['Consumption'] = 10
71
-
72
- N = 20
73
- solar_cell_num_max = 4000
74
- bess_nominal_capacity_max = 4000
75
- solar_cell_nums = np.linspace(0, solar_cell_num_max, N)
76
- bess_nominal_capacities = np.linspace(1e-6, bess_nominal_capacity_max, N)
77
-
78
- mg_x, mg_y = np.meshgrid(solar_cell_nums, bess_nominal_capacities)
79
-
80
- values = np.zeros((N, N))
81
- for i, solar_cell_num in enumerate(solar_cell_nums):
82
- for j, bess_nominal_capacity in enumerate(bess_nominal_capacities):
83
- parameters.solar_cell_num = solar_cell_num
84
- parameters.bess_nominal_capacity = bess_nominal_capacity
85
- network, solar, bess = evaluate_parameters(parameters, met_2021_data, cons_2021_data)
86
- satisfied = 1 - network / (network + solar + bess)
87
- values[i, j] = satisfied
88
-
89
- fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
90
- surf = ax.plot_surface(mg_x, mg_y, values * 100, cmap=matplotlib.cm.coolwarm,
91
- linewidth=0, antialiased=False)
92
- ax.set_xlabel("BESS nominal capacity [Ah]")
93
- ax.set_ylabel("Solar cell number")
94
- ax.set_zlabel("Percentage of consumption served without network")
95
- fig.colorbar(surf, shrink=0.5, aspect=10)
96
- plt.show()
97
-
98
-
99
- # main_gridsearch() ; exit()
100
-
101
-
102
  met_2021_data, cons_2021_data = read_datasets()
103
 
104
 
 
3
 
4
  import numpy as np
5
  import pandas as pd
 
 
 
 
6
  import gradio as gr
7
 
8
  from simulation import *
 
11
 
12
 
13
  #@title ### Downloading the data
14
+ # !wget "https://static.renyi.hu/ai-shared/daniel/pq/PL_44527.19-21.csv.gz"
15
  # !wget "https://static.renyi.hu/ai-shared/daniel/pq/pq_terheles_2021_adatok.tsv"
16
 
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  met_2021_data, cons_2021_data = read_datasets()
19
 
20
 
main.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import matplotlib.pyplot as plt
3
+ import matplotlib
4
+ import matplotlib.cm
5
+
6
+ from simulation import *
7
+ from data_processing import *
8
+ from visualization import *
9
+ from supplier import Supplier
10
+
11
+
12
+ matplotlib.rcParams['figure.figsize'] = [12, 8]
13
+
14
+
15
+ def main():
16
+ parameters = Parameters()
17
+
18
+ met_2021_data, cons_2021_data = read_datasets()
19
+
20
+ add_production_field(met_2021_data, parameters)
21
+
22
+ all_2021_data = interpolate_and_join(met_2021_data, cons_2021_data)
23
+
24
+ results = simulator_with_solar(all_2021_data, parameters)
25
+ consumption_from_network = results["consumption_from_network"]
26
+ supplier = Supplier(price=70) # HUF/kWh
27
+ supplier.set_price_for_daily_interval_on_workdays(start=6, end=22, price=100)
28
+ print("Energy supplier charges", int(supplier.fee(consumption_from_network)), "HUF between", results.index[0], "and", results.index[-1])
29
+ return
30
+
31
+ fig = visualize_simulation(results, date_range=("2021-02-01", "2021-03-01"))
32
+ plt.show()
33
+
34
+ consumptions_in_mwh = monthly_analysis(results)
35
+ monthly_visualization(consumptions_in_mwh)
36
+
37
+
38
+ main() ; exit()
39
+
40
+
41
+
42
+ def main_gridsearch():
43
+ fixed_consumption = False
44
+
45
+ parameters = Parameters()
46
+ met_2021_data, cons_2021_data = read_datasets()
47
+
48
+ if fixed_consumption:
49
+ cons_2021_data['Consumption'] = 10
50
+
51
+ N = 20
52
+ solar_cell_num_max = 4000
53
+ bess_nominal_capacity_max = 4000
54
+ solar_cell_nums = np.linspace(0, solar_cell_num_max, N)
55
+ bess_nominal_capacities = np.linspace(1e-6, bess_nominal_capacity_max, N)
56
+
57
+ mg_x, mg_y = np.meshgrid(solar_cell_nums, bess_nominal_capacities)
58
+
59
+ values = np.zeros((N, N))
60
+ for i, solar_cell_num in enumerate(solar_cell_nums):
61
+ print(f"{solar_cell_num} / {solar_cell_nums[-1]}")
62
+ for j, bess_nominal_capacity in enumerate(bess_nominal_capacities):
63
+ parameters.solar_cell_num = solar_cell_num
64
+ parameters.bess_nominal_capacity = bess_nominal_capacity
65
+ network, solar, bess = evaluate_parameters(parameters, met_2021_data, cons_2021_data)
66
+ satisfied = 1 - network / (network + solar + bess)
67
+ values[i, j] = satisfied
68
+
69
+ fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
70
+ surf = ax.plot_surface(mg_x, mg_y, values * 100, cmap=matplotlib.cm.coolwarm,
71
+ linewidth=0, antialiased=False)
72
+ ax.set_xlabel("BESS nominal capacity [Ah]")
73
+ ax.set_ylabel("Solar cell number")
74
+ ax.set_zlabel("Percentage of consumption served without network")
75
+ fig.colorbar(surf, shrink=0.5, aspect=10)
76
+ plt.show()
77
+
78
+
79
+ main_gridsearch() ; exit()
simulation.py CHANGED
@@ -1,6 +1,8 @@
1
  import pandas as pd
2
  import numpy as np
3
 
 
 
4
 
5
  def simulator_with_solar(all_data, parameters):
6
  demand_np = all_data['Consumption'].to_numpy()
@@ -136,3 +138,11 @@ def simulator_with_solar(all_data, parameters):
136
  })
137
  results = results.set_index(all_data.index)
138
  return results
 
 
 
 
 
 
 
 
 
1
  import pandas as pd
2
  import numpy as np
3
 
4
+ from data_processing import add_production_field, interpolate_and_join, monthly_analysis
5
+
6
 
7
  def simulator_with_solar(all_data, parameters):
8
  demand_np = all_data['Consumption'].to_numpy()
 
138
  })
139
  results = results.set_index(all_data.index)
140
  return results
141
+
142
+
143
+ def evaluate_parameters(parameters, met_2021_data, cons_2021_data):
144
+ add_production_field(met_2021_data, parameters)
145
+ all_2021_data = interpolate_and_join(met_2021_data, cons_2021_data)
146
+ results = simulator_with_solar(all_2021_data, parameters)
147
+ consumptions_in_mwh = monthly_analysis(results)
148
+ return consumptions_in_mwh.sum(axis=0)
supplier.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # modeling an energy supplier for the purposes of peak shaving
2
+
3
+ import numpy as np
4
+ import pandas as pd
5
+ import datetime
6
+ import unittest
7
+
8
+
9
+ class Supplier:
10
+ # price [HUF/kWh]
11
+ # peak_demand kW
12
+ # surcharge_per_kw [HUF/kW for each 15 minute timeframe]
13
+ def __init__(self, price):
14
+ self.hourly_prices = np.ones(168) * price
15
+ self.peak_demand = np.inf # no demand_charge by default
16
+ self.surcharge_per_kw = 0
17
+
18
+ # start and end are indices of hours starting from Monday 00:00.
19
+ def set_price_for_interval(self, start, end, price):
20
+ self.hourly_prices[start:end] = price
21
+
22
+ # start and end are indices of hours of the day. for each day, this interval is set to price
23
+ def set_price_for_daily_interval(self, start, end, price):
24
+ for day in range(7):
25
+ h = day * 24
26
+ self.set_price_for_interval(h + start, h + end, price)
27
+
28
+ def set_price_for_daily_interval_on_workdays(self, start, end, price):
29
+ for day in range(5):
30
+ h = day * 24
31
+ self.set_price_for_interval(h + start, h + end, price)
32
+
33
+ def set_demand_charge(self, peak_demand, surcharge_per_kw):
34
+ self.peak_demand = peak_demand # [kW]
35
+ # the HUF charged per kW of demand exceeding peak_demand during a 15 minutes timeframe.
36
+ self.surcharge_per_kw = surcharge_per_kw # [HUF/kW]
37
+
38
+ @staticmethod
39
+ def hour_of_date(date):
40
+ hours_since_midnight = (date - datetime.datetime(date.year, date.month, date.day, 0, 0, 0)).total_seconds() / 3600
41
+ # weekday() calculates from sunday morning:
42
+ hungarian_weekday = (date.weekday() + 0) % 7
43
+ hours_elapsed_in_previous_days = hungarian_weekday * 24
44
+ return int(hours_since_midnight) + hours_elapsed_in_previous_days
45
+
46
+ def price(self, date):
47
+ return self.hourly_prices[self.hour_of_date(date)]
48
+
49
+ # demand is the maximum demand in kW during a 15 minute interval
50
+ def demand_charge(self, demand):
51
+ if demand <= self.peak_demand:
52
+ return 0.0
53
+ else:
54
+ return (demand - self.peak_demand) * self.surcharge_per_kw
55
+
56
+ # demand_series is pandas series indexed by time.
57
+ # during each time step demand [kW] is assumed to be constant.
58
+ def fee(self, demand_series):
59
+ prices = [self.price(date) for date in demand_series.index]
60
+ prices_series = pd.Series(data=prices, index=demand_series.index)
61
+ # prices are HUF/kWh, demand is kW. note the missing h.
62
+
63
+ step_in_hour = demand_series.index.freq.n / 60 # [hour], the length of a time step.
64
+ # for each step the product tells the fee IF the step was 1 hour long. it's actually step_in_hour long:
65
+ consumption_charge = demand_series.dot(prices_series) * step_in_hour
66
+
67
+ # 15 minutes (the demand charge calculation interval) should be a multiple of the series time step.
68
+ assert 15 % demand_series.index.freq.n == 0
69
+ time_steps_per_demand_charge_evaluation = 15 // demand_series.index.freq.n
70
+ # fifteen_minute_peaks [kW] tells the maximum demand in a 15 minutes timeframe:
71
+ fifteen_minute_peaks = demand_series.resample('15T').max()
72
+ demand_charges = [self.demand_charge(demand) for demand in fifteen_minute_peaks]
73
+ total_demand_charge = sum(demand_charges)
74
+ return consumption_charge + total_demand_charge
75
+
76
+
77
+ class TestSupplier(unittest.TestCase):
78
+
79
+ def setUp(self):
80
+ self.constant_price = 10
81
+ self.supplier = Supplier(self.constant_price)
82
+
83
+ def test_hourly_prices(self):
84
+ expected_hourly_prices = np.ones(168) * self.constant_price
85
+ self.assertTrue(np.array_equal(self.supplier.hourly_prices, expected_hourly_prices))
86
+
87
+ def test_set_price_for_interval(self):
88
+ self.supplier.set_price_for_interval(0, 24, 20)
89
+ expected_hourly_prices = np.ones(168) * self.constant_price
90
+ expected_hourly_prices[0:24] = 20
91
+ self.assertTrue(np.array_equal(self.supplier.hourly_prices, expected_hourly_prices))
92
+
93
+ def test_price(self):
94
+ increased_price = 20
95
+ self.supplier.set_price_for_interval(0, 24, increased_price)
96
+
97
+ date = datetime.datetime(2023, 4, 30, 12, 0, 0) # Sunday noon
98
+ expected_price = self.constant_price
99
+ self.assertEqual(self.supplier.price(date), expected_price)
100
+
101
+ date = datetime.datetime(2023, 5, 1, 12, 0, 0) # Monday noon
102
+ expected_price = increased_price
103
+ self.assertEqual(self.supplier.price(date), expected_price)
104
+
105
+ date = datetime.datetime(2023, 5, 2, 12, 0, 0) # Tuesday noon
106
+ expected_price = self.constant_price
107
+ self.assertEqual(self.supplier.price(date), expected_price)
108
+
109
+ def test_fee(self):
110
+ start = pd.Timestamp('2021-04-28')
111
+ end = start + pd.Timedelta(days=1)
112
+ freq = '5T' # 5 minutes
113
+ time_index = pd.date_range(start=start, end=end, freq=freq, inclusive='left')
114
+ constant_demand = 100
115
+ demand_in_kw = [constant_demand] * len(time_index)
116
+
117
+ demand_series = pd.Series(data=demand_in_kw, index=time_index)
118
+ # 24 because it's a 24 hour period with constant demand:
119
+ self.assertEqual(self.supplier.fee(demand_series), constant_demand * 24 * self.constant_price)
120
+
121
+ extreme_demand = 1000
122
+ demand_series[12:24] = extreme_demand # in second hour we set extreme demand.
123
+
124
+ expected_fee = (constant_demand * 23 + extreme_demand) * self.constant_price
125
+ self.assertEqual(self.supplier.fee(demand_series), expected_fee)
126
+
127
+ # now the (1000-500) kW above 500 kW is surcharged for (1000-500 kW) * 10 HUF/kW/15mins, for 1 hour,
128
+ # that is 500*10*4=20000 demand_charge.
129
+ self.supplier.set_demand_charge(peak_demand=500, surcharge_per_kw=10)
130
+ expected_fee += 20000
131
+ self.assertEqual(self.supplier.fee(demand_series), expected_fee)
132
+
133
+
134
+
135
+ if __name__ == '__main__':
136
+ unittest.main()