forked from thomas-young-2013/open-box
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pytest | ||
from openbox.utils.early_stop import EarlyStopAlgorithm | ||
from openbox.core.generic_advisor import Advisor | ||
from openbox import get_config_space | ||
from typing import List | ||
|
||
class HistoryMock: | ||
def __init__(self, observations=None): | ||
self.observations = observations | ||
self.meta_info = dict() | ||
|
||
def __len__(self): | ||
return len(self.observations) | ||
|
||
@property | ||
def objectives(self) -> List[List[float]]: | ||
return self.observations | ||
|
||
def get_incumbent_value(self): | ||
return min([obs[0] for obs in self.observations]) | ||
|
||
def test_es_init(): | ||
# Test initialization | ||
early_stop = EarlyStopAlgorithm(min_iter=5, min_improvement_percentage=0.1, max_no_improvement_rounds=3) | ||
assert early_stop.min_iter == 5 | ||
assert early_stop.min_improvement_percentage == 0.1 | ||
assert early_stop.max_no_improvement_rounds == 3 | ||
|
||
def test_es_check_setup(): | ||
with pytest.raises(ValueError): | ||
advisor = Advisor(config_space=get_config_space('lightgbm'), num_objectives = 2, early_stop=True, acq_type="parego") | ||
|
||
advisor = Advisor(config_space=get_config_space('lightgbm'), num_objectives = 1, early_stop=True, acq_type="ei") | ||
with pytest.raises(AssertionError): | ||
early_stop = EarlyStopAlgorithm(min_iter=0) | ||
early_stop.check_setup(advisor) | ||
|
||
with pytest.raises(AssertionError): | ||
early_stop = EarlyStopAlgorithm(min_improvement_percentage=-0.1) | ||
early_stop.check_setup(advisor) | ||
|
||
with pytest.raises(AssertionError): | ||
early_stop = EarlyStopAlgorithm(max_no_improvement_rounds=-3) | ||
early_stop.check_setup(advisor) | ||
|
||
with pytest.raises(AssertionError): | ||
early_stop = EarlyStopAlgorithm() | ||
advisor.acq_type="eips" | ||
early_stop.check_setup(advisor) | ||
|
||
def test_es_already_early_stopped(): | ||
history = HistoryMock() | ||
early_stop = EarlyStopAlgorithm() | ||
assert not early_stop.already_early_stopped(history) | ||
|
||
early_stop.set_already_early_stopped(history) | ||
assert early_stop.already_early_stopped(history) | ||
|
||
assert early_stop.decide_early_stop_before_suggest(history) | ||
|
||
def test_es_decide_early_stop_before_suggest(): | ||
# check not reach min | ||
history = HistoryMock() | ||
early_stop = EarlyStopAlgorithm(min_iter=5) | ||
history.observations = [[1], [1], [1]] | ||
assert not early_stop.decide_early_stop_before_suggest(history) | ||
|
||
# check not enabled | ||
early_stop.min_iter=1 | ||
early_stop.max_no_improvement_rounds = 0 | ||
history.observations = [[1], [1], [1]] | ||
assert not early_stop.decide_early_stop_before_suggest(history) | ||
|
||
# enabled | ||
early_stop.max_no_improvement_rounds = 2 | ||
history.observations = [[5], [4], [3], [3], [3], [2]] | ||
assert not early_stop.decide_early_stop_before_suggest(history) | ||
history.observations = [[5], [4], [3], [3], [3], [3]] | ||
assert early_stop.decide_early_stop_before_suggest(history) | ||
|
||
def test_es_decide_early_stop_after_suggest(): | ||
# check not reach min | ||
history = HistoryMock() | ||
early_stop = EarlyStopAlgorithm(min_iter=5, max_no_improvement_rounds=0) | ||
history.observations = [[1], [1], [1]] | ||
assert not early_stop.decide_early_stop_after_suggest(history) | ||
|
||
# check not enabled | ||
early_stop.min_iter=1 | ||
early_stop.min_improvement_percentage=0 | ||
history.observations = [[1], [1], [1]] | ||
assert not early_stop.decide_early_stop_after_suggest(history, max_acq_value=0) | ||
|
||
# enabled | ||
history = HistoryMock() | ||
early_stop.min_improvement_percentage=0.5 | ||
history.observations = [[10], [9], [8]] | ||
assert early_stop.decide_early_stop_after_suggest(history, max_acq_value=0) |