Skip to content

Commit

Permalink
0.7.5 - optimisation and parametric simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanchez-garcia committed Jun 7, 2024
1 parent 1cce6cf commit e4e3636
Show file tree
Hide file tree
Showing 12 changed files with 28,408 additions and 2,348 deletions.
8,687 changes: 8,687 additions & 0 deletions TestModel_mod.idf

Large diffs are not rendered by default.

29 changes: 20 additions & 9 deletions accim/parametric_and_optimisation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,17 @@ def sampling_full_set(self):
Combines all values from all parameters and saves it into a pandas DataFrame, stored in an internal variable
named parameters_values_df.
"""
from accim.parametric_and_optimisation.utils import make_all_combinations
if self.descriptors_has_options:
num_samples = 1
parameters_values = {}
for p in self.parameters_list:
num_samples = num_samples * len(p.value_descriptors[0].options)
parameters_values.update({p.value_descriptors[0].name: p.value_descriptors[0].options})
from itertools import product
combinations = list(product(*parameters_values.values()))
parameters_values_df = pd.DataFrame(combinations, columns=parameters_values.keys())
# from itertools import product
# combinations = list(product(*parameters_values.values()))
# parameters_values_df = pd.DataFrame(combinations, columns=parameters_values.keys())
parameters_values_df = make_all_combinations(parameters_values)
else:
raise KeyError('sampling_full_set method can only be used with option (i.e. category) descriptors.')

Expand Down Expand Up @@ -765,18 +767,27 @@ def run_optimisation(
self.outputs_optimisation = outputs_optimisation
# return outputs_optimisation

def get_hourly_df(self):
def get_hourly_df(self, start_date: str = '2024-01-01 01'):
"""
Transforms the hourly values of outputs_param_simulation to a new pandas DataFrame
named outputs_param_simulation_hourly.
Transforms the hourly values of outputs_param_simulation to a new pandas DataFrame, saved in the
internal variable named outputs_param_simulation_hourly.
:param start_date: the start date for the simulation results, in format 'YYY-MM-DD HH'
"""
parameter_columns = [i.name for i in self.parameters_list]
parameter_columns.append('epw')
self.outputs_hourly_columns = identify_hourly_columns(self.outputs_param_simulation)
self.outputs_param_simulation_hourly = expand_to_hourly_dataframe(self.outputs_param_simulation, parameter_columns)

self.outputs_param_simulation_hourly = expand_to_hourly_dataframe(
df=self.outputs_param_simulation,
parameter_columns=parameter_columns,
start_date=start_date
)

def get_hourly_df_columns(self):
"""
Identifies the columns which contain hourly values, and save the names in a list, saved in the
internal variable named outputs_hourly_columns
"""
self.outputs_hourly_columns = identify_hourly_columns(self.outputs_param_simulation)

class AccimPredefModelsParamSim(OptimParamSimulation):
def __init__(
Expand Down
12 changes: 12 additions & 0 deletions accim/parametric_and_optimisation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ def identify_hourly_columns(df):
df[col].apply(lambda x: isinstance(x, str) and x.startswith('[') and x.endswith(']')).all()
]
return hourly_columns

def make_all_combinations(parameters_values_dict: dict) -> pd.DataFrame:
"""
Takes all values from all the parameters and return a pandas DataFrame with all possible combinations.
:param parameters_values_dict: a dictionary in the format {'parameter name': list_of_values}
:return: a pandas DataFrame with all possible combinations
"""
from itertools import product
combinations = list(product(*parameters_values_dict.values()))
parameters_values_df = pd.DataFrame(combinations, columns=parameters_values_dict.keys())
return parameters_values_df

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file modified dist/accim-0.7.5.tar.gz
Binary file not shown.
61 changes: 46 additions & 15 deletions parametric_simulation_usage_v00_accim_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

accim.utils.set_occupancy_to_always(idf_object=building)

test_class_instance = OptimParamSimulation(
parametric = OptimParamSimulation(
building=building,
parameters_type='accim custom model'
# output_keep_existing=False,
Expand All @@ -50,7 +50,7 @@

# Setting the Output:Variable and Output:Meter objects in the idf
#todo do not print on screen the process of accis, only the first time
df_output_variables_idf = test_class_instance.get_output_var_df_from_idf()
df_output_variables_idf = parametric.get_output_var_df_from_idf()

df_output_variables_idf_mod = df_output_variables_idf.copy()

Expand All @@ -68,7 +68,7 @@

[i for i in building.idfobjects['energymanagementsystem:program'] if i.Name.lower() == 'setinputdata']

test_class_instance.set_output_var_df_to_idf(outputs_df=df_output_variables_idf_mod)
parametric.set_output_var_df_to_idf(outputs_df=df_output_variables_idf_mod)

output_meters = [
# 'HeatingCoils:EnergyTransfer',
Expand All @@ -77,10 +77,10 @@
'Cooling:Electricity',
'Electricity:HVAC',
]
test_class_instance.set_output_met_objects_to_idf(output_meters=output_meters)
parametric.set_output_met_objects_to_idf(output_meters=output_meters)

# Checking the Output:Meter and Output:Variable objects in the simulation
df_outputmeters_2, df_outputvariables_2 = test_class_instance.get_outputs_df_from_testsim()
df_outputmeters_2, df_outputvariables_2 = parametric.get_outputs_df_from_testsim()

#Other variables could be reported. These can be read in the rdd, mdd and mtd files
df_rdd = get_rdd_file_as_df()
Expand All @@ -105,7 +105,7 @@ def return_time_series(result):
df_outputvariables_3 = df_outputvariables_3.drop(index=[2, 4])
df_outputvariables_3['name'] = df_outputvariables_3['variable_name'] + '_time series'

test_class_instance.set_outputs_for_simulation(
parametric.set_outputs_for_simulation(
df_output_meter=df_outputmeters_3,
# df_output_variable=df_outputvariables_3,
df_output_variable=df_outputvariables_3,
Expand Down Expand Up @@ -145,33 +145,64 @@ def return_time_series(result):
# from besos.parameters import wwr, RangeParameter
# other_parameters = [wwr(RangeParameter(0.1, 0.9))]

test_class_instance.set_parameters(
parametric.set_parameters(
accis_params_dict=accis_parameters,
# additional_params=other_parameters
)

##
param_dict = {
'CustAST_m': [0.1, 0.6],
'CustAST_n': [22, 8],
'CustAST_ASToffset': [2.5, 4],
'CustAST_ASTall': [10, 10],
'CustAST_ASTaul': [35, 35],
}
from accim.parametric_and_optimisation.utils import make_all_combinations
all_combinations = make_all_combinations(param_dict)

##

[i for i in building.idfobjects['EnergyManagementSystem:Program'] if i.Name.lower() == 'setinputdata']
[i for i in building.idfobjects['EnergyManagementSystem:Program'] if i.Name.lower() == 'setvofinputdata']
[i for i in building.idfobjects['EnergyManagementSystem:Program'] if i.Name.lower() == 'applycat']

##
set_input_data = [i for i in building.idfobjects['EnergyManagementSystem:Program'] if i.Name.lower() == 'setinputdata'][0]
set_vof_input_data = [i for i in building.idfobjects['EnergyManagementSystem:Program'] if i.Name.lower() == 'setvofinputdata'][0]
applycat = [i for i in building.idfobjects['EnergyManagementSystem:Program'] if i.Name.lower() == 'applycat'][0]
setast = [i for i in building.idfobjects['EnergyManagementSystem:Program'] if i.Name.lower() == 'setast'][0]
setapplimits = [i for i in building.idfobjects['EnergyManagementSystem:Program'] if i.Name.lower() == 'setapplimits'][0]
other_args = {'SetpointAcc': setast.Program_Line_1}
cust_ast_args = {
'ACSToffset': applycat.Program_Line_4,
'AHSToffset': applycat.Program_Line_5,
'm': setast.Program_Line_2,
'n': setast.Program_Line_3,
'ACSTaul': setapplimits.Program_Line_2,
'ACSTall': setapplimits.Program_Line_3,
'AHSTaul': setapplimits.Program_Line_4,
'AHSTall': setapplimits.Program_Line_5,
}

# building.savecopy('TestModel_mod.idf')

##
# Let's set the problem
test_class_instance.set_problem()
parametric.set_problem()

# Let's generate a sampling dataframe
test_class_instance.sampling_full_factorial(level=5)
temp_full_fac = test_class_instance.parameters_values_df
parametric.sampling_full_factorial(level=5)
temp_full_fac = parametric.parameters_values_df

test_class_instance.sampling_lhs(num_samples=3)
temp_lhs = test_class_instance.parameters_values_df
parametric.sampling_lhs(num_samples=3)
temp_lhs = parametric.parameters_values_df

# test_class_instance.sampling_full_set()
# temp_full_set = test_class_instance.parameters_values_df
# parametric.sampling_full_set()
# temp_full_set = parametric.parameters_values_df

#todo try to return series of pmot, acst, ahst and optemp and plot them in facetgrid
outputs = test_class_instance.run_parametric_simulation(
outputs = parametric.run_parametric_simulation(
epws=[
'Sydney.epw',
'Seville.epw'
Expand Down
42 changes: 35 additions & 7 deletions parametric_simulation_usage_v00_accim_predef.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
accis_params_dict=accis_parameters,
# additional_params=other_parameters
)
##

# Let's set the problem
parametric.set_problem()

Expand All @@ -139,7 +139,7 @@
parametric.sampling_full_set()
temp_full_set = parametric.parameters_values_df

outputs = parametric.run_parametric_simulation(
parametric.run_parametric_simulation(
epws=[
'Sydney.epw',
'Seville.epw'
Expand All @@ -154,6 +154,7 @@

# outputs.to_excel('WIP_outputs.xlsx')

[i.name for i in parametric.sim_outputs]
##


Expand Down Expand Up @@ -189,17 +190,21 @@

##

#Let's make a copy of the dataframe to not to modify the original one
df = parametric.outputs_param_simulation_hourly.copy()
set(df['epw'])

# The name of the column for the Running mean outdoor temperature is very long, so let's save it in the variable rmot:
rmot = [i for i in df.columns if 'Running Average' in i][0]
optemp = [i for i in df.columns if 'Zone Operative Temperature' in i][0]

#Let's remove the columns where value is the same for all rows
for c in df.columns:
if len(set(df[c])) == 1:
df = df.drop(columns=[c])
df = df.drop(columns=['hour', 'datetime'])
#Now let's remove the hour and datetime columns, since will
df = df.drop(columns=['hour'])

df = df.melt(id_vars=['CAT', 'epw', rmot])
# Now let's reshape the df for plotting purposes
df = df.melt(id_vars=['datetime', 'CAT', 'epw', rmot])


##
Expand All @@ -219,7 +224,7 @@
alpha=0.5
)
g.set_axis_labels('RMOT (°C)', 'Indoor Operative Temperature (°C)')
g.add_legend()
g.add_legend(loc='lower center')

for lh in g._legend.legend_handles:
lh.set_markersize(5)
Expand All @@ -234,7 +239,30 @@
# markerscale=2
# )

plt.tight_layout()


##
import seaborn as sns
g = sns.FacetGrid(
data=df,
row='CAT',
col='epw'
)
g.map_dataframe(
sns.lineplot,
x='datetime',
y='value',
hue='variable',
# s=1,
# alpha=0.5
)
g.set_axis_labels('RMOT (°C)', 'Indoor Operative Temperature (°C)')
g.add_legend(loc='lower center')
plt.tight_layout()

# for lh in g._legend.legend_handles:
# lh.set_markersize(5)


##
Expand Down

0 comments on commit e4e3636

Please sign in to comment.