diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d26cc12..bfe74ef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,13 @@ ### Bug fixes +- **Testing BBOB, MA-BBOB and photonics solutions without an `ioh_dir` + crashed** (`cannot create directories`): the test runs always wrote IOH + analyzer logs, to an empty folder name by default. They are now only + written when an `ioh_dir` is given; `BBOB_SBOX(full_ioh_log=True)` needs + one and says so. Photonics also crashed on `test()` with an `ioh_dir` + (`Analyzer.reset()` takes no problem in ioh 0.4); its analyzer now logs + one run per seed. - **LLaMEA-HPO never tuned anything.** LLaMEA asks for the configuration space only when it gets no output format prompt, and BLADE always passes the problem's. So with `variant: hpo` or `HPO: true` the LLM was never diff --git a/iohblade/benchmarks/BBOB/bbob_sboxcost.py b/iohblade/benchmarks/BBOB/bbob_sboxcost.py index 864c2912..d939d29b 100644 --- a/iohblade/benchmarks/BBOB/bbob_sboxcost.py +++ b/iohblade/benchmarks/BBOB/bbob_sboxcost.py @@ -82,7 +82,8 @@ def __init__( specific_fid (int): The specific function id to use. If not None, additional information is added to the prompt about the function. specific_group (int): The specific function group (1,2,3,4,5) to use. If not None, additional information is added to the prompt about the function group. problem_type (ioh.ProblemClass): The type of problem to use. Can be SBOX or BBOB. - full_ioh_log (bool): If set to True, additional IOH logs are being kept for each run and each algorithm. + full_ioh_log (bool): If set to True, additional IOH logs are being kept for each run and each algorithm (in ``ioh_dir``, which is then required). + ioh_dir (str): Folder for IOH analyzer logs of the test runs (and of all runs with ``full_ioh_log``). Without it (the default) no analyzer logs are written. dependencies (list, optional): a list of pypi packages to install before evaluation. imports (string, optional): the python string to manage imports in the evaluation file. hpo (bool): Tune the hyperparameters of solutions that come with @@ -139,6 +140,8 @@ def __init__( self.budget_factor = budget_factor # The factor to multiply the dimensionality with to get the budget self.specific_fid = specific_fid self.specific_group = specific_group + if full_ioh_log and not ioh_dir: + raise ValueError("full_ioh_log=True needs an ioh_dir for the IOH logs.") self.full_ioh_log = full_ioh_log self.ioh_dir = ioh_dir self.hpo_trials = int(hpo_trials) @@ -405,7 +408,9 @@ def evaluate(self, solution: Solution, test=False): for dim in self.dims: for instance in instances: fid, iid = instance # we expact a tuple of (fid, iid) - log_id = algorithm_id if (test or self.full_ioh_log) else None + # IOH analyzer logs need a folder (ioh_dir). + log = self.ioh_dir and (test or self.full_ioh_log) + log_id = algorithm_id if log else None corrected_aoc = self._run_once( alg_cls, fid, iid, dim, config, analyzer_id=log_id ) diff --git a/iohblade/benchmarks/BBOB/mabbob.py b/iohblade/benchmarks/BBOB/mabbob.py index 0e5094a6..0c228a10 100644 --- a/iohblade/benchmarks/BBOB/mabbob.py +++ b/iohblade/benchmarks/BBOB/mabbob.py @@ -316,7 +316,8 @@ def evaluate(self, solution: Solution, test=False, ioh_dir=""): # Final validation instances = self.test_instances if test else self.training_instances - analyzer = (ioh_dir, algorithm_name) if test else None + # IOH analyzer logs of the test runs need a folder (ioh_dir). + analyzer = (ioh_dir, algorithm_name) if (test and ioh_dir) else None aucs = [] for dim in self.dims: for idx in instances: diff --git a/iohblade/benchmarks/photonics/photonics.py b/iohblade/benchmarks/photonics/photonics.py index abda24c8..5b33dd62 100644 --- a/iohblade/benchmarks/photonics/photonics.py +++ b/iohblade/benchmarks/photonics/photonics.py @@ -192,8 +192,9 @@ def evaluate(self, solution: Solution, test=False, ioh_dir=""): upper=auc_upper, triggers=[ioh_logger.trigger.ALWAYS], ) - # add also a normal IOH logger if test = True and set the directory accordingly - if test: + # add also a normal IOH logger if test = True and a directory is given + l1 = None + if test and ioh_dir: l1 = ioh.logger.Analyzer( root=ioh_dir, folder_name=algorithm_name, @@ -221,9 +222,10 @@ def evaluate(self, solution: Solution, test=False, ioh_dir=""): aucs.append(correct_aoc(self.problem, l2, budget)) l2.reset(self.problem) - if test: - l1.reset(self.problem) + # problem.reset() starts a new run in the attached IOH analyzer. self.problem.reset() + if l1 is not None: + l1.close() # flush the analyzer's files auc_mean = np.mean(aucs) auc_std = np.std(aucs) diff --git a/tests/unit/test_benchmark_scores_bbob.py b/tests/unit/test_benchmark_scores_bbob.py index 56480d63..ba85e0dc 100644 --- a/tests/unit/test_benchmark_scores_bbob.py +++ b/tests/unit/test_benchmark_scores_bbob.py @@ -318,6 +318,85 @@ def test_photonics_runs_are_seeded(photonics_module): assert a.get_metadata("aucs") == b.get_metadata("aucs") +# -------------------------------------------------------------------------- +# 3b. Testing works without an ioh_dir (no IOH analyzer logs then) +# -------------------------------------------------------------------------- + + +def _sbox(**kwargs): + from iohblade.benchmarks.BBOB.bbob_sboxcost import BBOB_SBOX + + return BBOB_SBOX( + dims=[2], + training_instances=[(1, 1)], + test_instances=[(1, 2)], + budget_factor=20, + **kwargs, + ) + + +def _mabbob(): + from iohblade.benchmarks.BBOB.mabbob import MA_BBOB + + return MA_BBOB( + training_instances=[0], test_instances=[1], dims=[2], budget_factor=20 + ) + + +def _random_search(): + return Solution(code=RANDOM_SEARCH, name="RandomSearch") + + +def test_sbox_test_without_ioh_dir(): + solution = _sbox().test(_random_search()) + assert 0.0 <= solution.fitness <= 1.0 + + +def test_sbox_test_with_ioh_dir_writes_analyzer_logs(tmp_path): + _sbox(ioh_dir=str(tmp_path)).test(_random_search()) + assert any(tmp_path.rglob("*.json")) + + +def test_sbox_full_ioh_log_needs_an_ioh_dir(tmp_path): + with pytest.raises(ValueError, match="ioh_dir"): + _sbox(full_ioh_log=True) + problem = _sbox(full_ioh_log=True, ioh_dir=str(tmp_path)) + problem.evaluate(_random_search()) + assert any(tmp_path.rglob("*.json")) + + +def test_mabbob_test_without_ioh_dir(): + solution = _mabbob().test(_random_search()) + assert 0.0 <= solution.fitness <= 1.0 + + +def test_mabbob_test_with_ioh_dir_writes_analyzer_logs(tmp_path): + _mabbob().test(_random_search(), ioh_dir=str(tmp_path)) + assert any(tmp_path.rglob("*.json")) + + +def test_photonics_test_without_ioh_dir(photonics_module): + problem = photonics_module.Photonics( + logger=None, problem_type="ellipsometry", budget_factor=2, seeds=1 + ) + solution = problem.test(_random_search()) + assert math.isfinite(solution.fitness) + + +def test_photonics_test_with_ioh_dir_writes_analyzer_logs(photonics_module, tmp_path): + import json + + problem = photonics_module.Photonics( + logger=None, problem_type="ellipsometry", budget_factor=2, seeds=2 + ) + problem.test(_random_search(), ioh_dir=str(tmp_path)) + logs = list(tmp_path.rglob("*.json")) + assert logs + meta = json.loads(logs[0].read_text()) + runs = [run for scenario in meta["scenarios"] for run in scenario["runs"]] + assert len(runs) == 2 # one analyzer run per seed + + # -------------------------------------------------------------------------- # 4. AutoML: binary AUC and failed splits # --------------------------------------------------------------------------