Daniel Varga commited on
Commit
e8def5c
1 Parent(s): e733d30

copied supplier

Browse files
Files changed (3) hide show
  1. v2/architecture.py +4 -12
  2. v2/supplier.py +0 -1
  3. v2/supplier.py +136 -0
v2/architecture.py CHANGED
@@ -13,10 +13,12 @@ STEPS_PER_HOUR = 12
13
 
14
  # SOC is normalized so that minimal_depth_of_discharge = 0 and maximal_depth_of_discharge = 1.
15
  # please set capacity_Ah = nominal_capacity_Ah * (max_dod - min_dod)
 
 
16
  class BatteryModel:
17
  def __init__(self, capacity_Ah, time_interval_h):
18
  self.capacity_Ah = capacity_Ah
19
- self.efficiency = 0.95 # [dimensionless]
20
  self.voltage_V = 600
21
  self.charge_kW = 50
22
  self.discharge_kW = 60
@@ -94,7 +96,6 @@ class Decision(IntEnum):
94
  class Decider:
95
  def __init__(self):
96
  self.input_window_size = STEPS_PER_HOUR * 24 # day long window.
97
- self.output_window_size = STEPS_PER_HOUR # only output decisions for the next hour
98
  self.random_seed = 0
99
 
100
  # prod_cons_pred is a dataframe starting at now, containing
@@ -123,16 +124,6 @@ class DummyPredictor:
123
  return prediction
124
 
125
 
126
- '''
127
- bess_nominal_capacity: float = 330 # [Ah]
128
- bess_charge: float = 50 # [kW]
129
- bess_discharge: float = 60 # [kW]
130
- voltage: float = 600 # [V]
131
- maximal_depth_of_discharge: float = 0.75 # [dimensionless]
132
- energy_loss: float = 0.1 # [dimensionless]
133
- bess_present: bool = True # [boolean]
134
- '''
135
-
136
  # this function does not mutate its inputs.
137
  # it makes a clone of battery_model and modifies that.
138
  def simulator(battery_model, supplier, prod_cons, prod_predictor, cons_predictor, decider):
@@ -215,6 +206,7 @@ def simulator(battery_model, supplier, prod_cons, prod_predictor, cons_predictor
215
  else:
216
  consumption_from_network_to_bess = 0
217
 
 
218
  soc_series.append(battery_model.soc)
219
  consumption_from_solar_series.append(consumption_from_solar)
220
  consumption_from_network_series.append(consumption_from_network)
 
13
 
14
  # SOC is normalized so that minimal_depth_of_discharge = 0 and maximal_depth_of_discharge = 1.
15
  # please set capacity_Ah = nominal_capacity_Ah * (max_dod - min_dod)
16
+ #
17
+ # TODO efficiency multiplier is not currently used, where best to put it?
18
  class BatteryModel:
19
  def __init__(self, capacity_Ah, time_interval_h):
20
  self.capacity_Ah = capacity_Ah
21
+ self.efficiency = 0.9 # [dimensionless]
22
  self.voltage_V = 600
23
  self.charge_kW = 50
24
  self.discharge_kW = 60
 
96
  class Decider:
97
  def __init__(self):
98
  self.input_window_size = STEPS_PER_HOUR * 24 # day long window.
 
99
  self.random_seed = 0
100
 
101
  # prod_cons_pred is a dataframe starting at now, containing
 
124
  return prediction
125
 
126
 
 
 
 
 
 
 
 
 
 
 
127
  # this function does not mutate its inputs.
128
  # it makes a clone of battery_model and modifies that.
129
  def simulator(battery_model, supplier, prod_cons, prod_predictor, cons_predictor, decider):
 
206
  else:
207
  consumption_from_network_to_bess = 0
208
 
209
+ supplier.(consumption_from_network)
210
  soc_series.append(battery_model.soc)
211
  consumption_from_solar_series.append(consumption_from_solar)
212
  consumption_from_network_series.append(consumption_from_network)
v2/supplier.py DELETED
@@ -1 +0,0 @@
1
- ../supplier.py
 
 
v2/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()