-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Interrupt optimization through IncorporateRunResultCallback #765
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Callbacks | ||
--------- | ||
|
||
Callbacks allow customizing the behavior of SMAC to ones needs. Currently, the list of implemented callbacks is | ||
very limited, but they can easily be added. | ||
|
||
If you want to create a new callback, please check `smac.callbacks` and create a new pull request. | ||
|
||
|
||
IncorporateRunResultCallback | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Callback to react on a new run result. | ||
|
||
Called after the finished run is added to the runhistory. | ||
Optionally return `False` to (gracefully) stop the optimization. | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,8 @@ class TestSMBO(unittest.TestCase): | |
def setUp(self): | ||
self.scenario = Scenario({'cs': test_helpers.get_branin_config_space(), | ||
'run_obj': 'quality', | ||
'output_dir': 'data-test_smbo'}) | ||
'output_dir': 'data-test_smbo', | ||
"runcount-limit": 5}) | ||
self.output_dirs = [] | ||
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. Was there a need to include a "runcount-limit"? 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. If the 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. This makes sense to me but hopefully someone more familiar with all the tests can decide. 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. Looks good to me |
||
self.output_dirs.append(self.scenario.output_dir) | ||
|
||
|
@@ -411,6 +412,35 @@ def __call__(self, smbo, run_info, result, time_left) -> None: | |
self.assertEqual(callback.num_call, 1) | ||
self.assertEqual(callback.config, config) | ||
|
||
@unittest.mock.patch.object(smac.facade.smac_ac_facade.Intensifier, 'process_results') | ||
def test_incorporate_run_results_callback_stop_loop(self, process_results_mock): | ||
|
||
def target(x): | ||
return 5 | ||
|
||
process_results_mock.return_value = None, None | ||
|
||
class TestCallback(IncorporateRunResultCallback): | ||
def __init__(self): | ||
self.num_call = 0 | ||
|
||
def __call__(self, smbo, run_info, result, time_left) -> None: | ||
self.num_call += 1 | ||
if self.num_call > 2: | ||
return False | ||
|
||
callback = TestCallback() | ||
|
||
self.scenario.output_dir = None | ||
smac = SMAC4AC(self.scenario, tae_runner=target, rng=1) | ||
smac.register_callback(callback) | ||
|
||
self.output_dirs.append(smac.output_dir) | ||
|
||
smac.optimize() | ||
|
||
self.assertEqual(callback.num_call, 3) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
The two instances of
from typing import X
can be reduced to 1