-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
||
|
@@ -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) | ||
self.at_calibration = True # Boolean to indicate if validating or calibrating | ||
# Extract kwargs for the visualizer | ||
visualizer_kwargs = { | ||
|
@@ -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 " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace between number and unit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
f"reached" | ||
) | ||
|
||
|
||
def obj(self, xk, *args): | ||
""" | ||
|
@@ -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): | ||
|
@@ -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): | ||
|
@@ -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: | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done