Skip to content

Commit

Permalink
Move logger setup to extra function and implement new unit test for m…
Browse files Browse the repository at this point in the history
…in = max budget
  • Loading branch information
Bronzila committed Jul 29, 2023
1 parent c51a694 commit 5ad146b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/dehb/optimizers/dehb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@ def __init__(self, cs=None, f=None, dimensions=None, mutation_factor=None,
max_budget=None, eta=None, min_clip=None, max_clip=None,
boundary_fix_type='random', max_age=np.inf, **kwargs):
# Miscellaneous
self.output_path = kwargs['output_path'] if 'output_path' in kwargs else './'
os.makedirs(self.output_path, exist_ok=True)
self.logger = logger
log_suffix = time.strftime("%x %X %Z")
log_suffix = log_suffix.replace("/", '-').replace(":", '-').replace(" ", '_')
self.logger.add(
"{}/dehb_{}.log".format(self.output_path, log_suffix),
**_logger_props
)
self.log_filename = "{}/dehb_{}.log".format(self.output_path, log_suffix)
self._setup_logger(kwargs)

# Benchmark related variables
self.cs = cs
Expand Down Expand Up @@ -104,6 +95,19 @@ def __init__(self, cs=None, f=None, dimensions=None, mutation_factor=None,
self.inc_config = None
self.history = []

def _setup_logger(self, kwargs):
"""Sets up the logger."""
self.output_path = kwargs['output_path'] if 'output_path' in kwargs else './'
os.makedirs(self.output_path, exist_ok=True)
self.logger = logger
log_suffix = time.strftime("%x %X %Z")
log_suffix = log_suffix.replace("/", '-').replace(":", '-').replace(" ", '_')
self.logger.add(
"{}/dehb_{}.log".format(self.output_path, log_suffix),
**_logger_props
)
self.log_filename = "{}/dehb_{}.log".format(self.output_path, log_suffix)

def reset(self):
self.inc_score = np.inf
self.inc_config = None
Expand Down
9 changes: 8 additions & 1 deletion tests/test_dehb.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def create_toy_optimizer(configspace: ConfigSpace.ConfigurationSpace, min_budget
dim = len(configspace.get_hyperparameters())
return DEHB(f=objective_function, cs=configspace, dimensions=dim,
min_budget=min_budget,
max_budget=max_budget, eta=eta, n_workers=1)
max_budget=max_budget, eta=eta, n_workers=2)


def objective_function(x: ConfigSpace.Configuration, budget: float, **kwargs):
Expand Down Expand Up @@ -105,3 +105,10 @@ def test_higher_min_budget(self):
with pytest.raises(AssertionError):
create_toy_optimizer(configspace=cs, min_budget=28, max_budget=27, eta=3,
objective_function=objective_function)

def test_equal_min_max_budget(self):
"""Test that verifies, that DEHB breaks if min_budget == max_budget."""
cs = create_toy_searchspace()
with pytest.raises(AssertionError):
create_toy_optimizer(configspace=cs, min_budget=27, max_budget=27, eta=3,
objective_function=objective_function)

0 comments on commit 5ad146b

Please sign in to comment.