Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added max_time to calibration settings #56

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 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 @@ -113,6 +113,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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add this to the docstring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

self.at_calibration = True # Boolean to indicate if validating or calibrating
# Extract kwargs for the visualizer
visualizer_kwargs = {
Expand Down Expand Up @@ -179,6 +180,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 "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace between number and unit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

f"reached"
)


def obj(self, xk, *args):
"""
Expand Down Expand Up @@ -249,7 +265,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 +331,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 +385,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 +420,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 +572,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.
"""