Skip to content

Commit

Permalink
Merge pull request #56 from RWTH-EBC/55-add-max-time-for-calibration
Browse files Browse the repository at this point in the history
added max_time to calibration settings
  • Loading branch information
jkriwet authored Apr 15, 2024
2 parents f937d3d + 16828d6 commit d6a2ac7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
37 changes: 26 additions & 11 deletions aixcalibuha/calibration/calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pandas as pd
from ebcpy import data_types, Optimizer
from ebcpy.simulationapi import SimulationAPI
from aixcalibuha.utils import visualizer, MaxIterationsReached
from aixcalibuha.utils import visualizer, MaxIterationsReached, MaxTimeReached
from aixcalibuha import CalibrationClass, Goals, TunerParas


Expand Down Expand Up @@ -83,6 +83,9 @@ class Calibrator(Optimizer):
Maximum number of iterations of calibration.
This may be useful to explicitly limit the calibration
time.
:keyword int max_time":
Deault is Infinity.
Maximum time in seconds, after which the calibration is stopped. Useful to explicitly limit the calibration time.
:keyword str plot_file_type:
File ending of created plots.
Any supported option in matplotlib, e.g. svg, png, pdf ...
Expand Down Expand Up @@ -113,6 +116,7 @@ def __init__(self,
self.perform_square_deviation = kwargs.pop("square_deviation", False)
self.result_path = kwargs.pop('result_path', None)
self.max_itercount = kwargs.pop('max_itercount', np.inf)
self.max_time = kwargs.pop('max_time', np.inf)
self.at_calibration = True # Boolean to indicate if validating or calibrating
# Extract kwargs for the visualizer
visualizer_kwargs = {
Expand Down Expand Up @@ -179,6 +183,21 @@ def __init__(self,
self.logger.log("Setting output_interval of simulation according "
f"to measurement target data frequency: {mean_freq}")
self.sim_api.sim_setup.output_interval = mean_freq
self.start_time = time.perf_counter()

def _check_for_termination(self):
if self._counter >= self.max_itercount:
raise MaxIterationsReached(
"Terminating calibration as the maximum number "
f"of iterations {self.max_itercount} has been reached."
)

if time.perf_counter() - self.start_time > self.max_time:
raise MaxTimeReached(
f"Terminating calibration as the maximum time of {self.max_time} s has been "
f"reached"
)


def obj(self, xk, *args):
"""
Expand Down Expand Up @@ -249,7 +268,8 @@ def obj(self, xk, *args):
counter=self._counter,
results=sim_target_data
)

self._check_for_termination()

return total_res, unweighted_objective

def mp_obj(self, x, *args):
Expand Down Expand Up @@ -314,7 +334,8 @@ def mp_obj(self, x, *args):
)
# Add single objective to objective list of total Population
total_res_list[idx] = total_res

self._check_for_termination()

return total_res_list

def _kpi_and_logging_calculation(self, *, xk_descaled, counter, results):
Expand Down Expand Up @@ -367,12 +388,6 @@ def _kpi_and_logging_calculation(self, *, xk_descaled, counter, results):
"Penaltyfactor": penalty
}

if counter >= self.max_itercount:
raise MaxIterationsReached(
"Terminating calibration as the maximum number "
f"of iterations {self.max_itercount} has been reached."
)

return total_res, unweighted_objective

def calibrate(self, framework, method=None, **kwargs) -> dict:
Expand Down Expand Up @@ -408,7 +423,7 @@ def calibrate(self, framework, method=None, **kwargs) -> dict:
method=method,
n_cpu=self.sim_api.n_cpu,
**kwargs)
except MaxIterationsReached as err:
except (MaxIterationsReached, MaxTimeReached) as err:
self.logger.log(msg=str(err), level=logging.WARNING)
t_cal_stop = time.time()
t_cal = t_cal_stop - t_cal_start
Expand Down Expand Up @@ -560,7 +575,7 @@ def _handle_error(self, error):
See ebcpy.optimization.Optimizer._handle_error for more info.
"""
# This error is our own, we handle it in the calibrate() function
if isinstance(error, MaxIterationsReached):
if isinstance(error, (MaxIterationsReached, MaxTimeReached)):
raise error
self.logger.save_calibration_result(best_iterate=self._current_best_iterate,
model_name=self.sim_api.model_name,
Expand Down
6 changes: 6 additions & 0 deletions aixcalibuha/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ class MaxIterationsReached(Exception):
ends because the maximum number of
allowed iterations is reached.
"""

class MaxTimeReached(Exception):
"""
Exception raised for when the calibration
ends because the maximum calibration time is reached.
"""

0 comments on commit d6a2ac7

Please sign in to comment.