feature: add simultinity factor calculations

This commit is contained in:
Majid Rezaei 2024-06-23 18:27:31 -04:00 committed by Majid Rezaei
parent 1f95943cbb
commit 52ea051aed

View File

@ -0,0 +1,64 @@
import pandas as pd
import numpy as np
class DemandShiftProcessor:
def __init__(self, city):
self.city = city
def random_shift(self, series):
shift_amount = np.random.randint(0, 2)
return series.shift(shift_amount).fillna(series.shift(shift_amount - len(series)))
def process_demands(self):
building_dfs = []
for building in self.city.buildings:
df = self.convert_building_to_dataframe(building)
df.set_index('Date/Time', inplace=True)
shifted_demands = df.apply(self.random_shift, axis=0)
self.update_building_demands(building, shifted_demands)
building_dfs.append(shifted_demands)
combined_df = pd.concat(building_dfs, axis=1)
self.calculate_and_set_simultaneity_factor(combined_df)
def convert_building_to_dataframe(self, building):
data = {
"Date/Time": self.generate_date_time_index(),
"Heating_Demand": building.heating_demand["hour"],
"Cooling_Demand": building.cooling_demand["hour"]
}
return pd.DataFrame(data)
def generate_date_time_index(self):
# Generate hourly date time index for a full year in 2013
date_range = pd.date_range(start="2013-01-01 00:00:00", end="2013-12-31 23:00:00", freq='H')
return date_range.strftime('%m/%d %H:%M:%S').tolist()
def update_building_demands(self, building, shifted_demands):
heating_shifted = shifted_demands["Heating_Demand"]
cooling_shifted = shifted_demands["Cooling_Demand"]
building.heating_demand = self.calculate_new_demands(heating_shifted)
building.cooling_demand = self.calculate_new_demands(cooling_shifted)
def calculate_new_demands(self, shifted_series):
new_demand = {
"hour": shifted_series.tolist(),
"month": self.calculate_monthly_demand(shifted_series),
"year": [shifted_series.sum()]
}
return new_demand
def calculate_monthly_demand(self, series):
series.index = pd.to_datetime(series.index, format='%m/%d %H:%M:%S')
monthly_demand = series.resample('M').sum()
return monthly_demand.tolist()
def calculate_and_set_simultaneity_factor(self, combined_df):
total_demand_original = combined_df.sum(axis=1)
peak_total_demand_original = total_demand_original.max()
individual_peak_demands = combined_df.max(axis=0)
sum_individual_peak_demands = individual_peak_demands.sum()
self.city.simultaneity_factor = peak_total_demand_original / sum_individual_peak_demands