Skip to content

Commit

Permalink
Quickfix for logger of ConfiguredOptimizer (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrapin authored May 7, 2021
1 parent ccb0745 commit 351e8d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nevergrad/optimization/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __call__(self, optimizer: base.Optimizer, candidate: p.Parameter, loss: tp.F
if hasattr(optimizer, "_configured_optimizer"):
configopt = optimizer._configured_optimizer # type: ignore
if isinstance(configopt, base.ConfiguredOptimizer):
data.update({"#optimizer#" + x: y for x, y in configopt.config().items()})
data.update({"#optimizer#" + x: str(y) for x, y in configopt.config().items()})
if isinstance(candidate._meta.get("sigma"), float):
data["#meta-sigma"] = candidate._meta["sigma"] # for TBPSA-like algorithms
if candidate.generation > 1:
Expand Down
18 changes: 18 additions & 0 deletions nevergrad/optimization/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ def test_multiobjective_log_parameters(tmp_path: Path) -> None:
assert len(logs) == 2


def test_chaining_log_parameters(tmp_path: Path) -> None:
filepath = tmp_path / "logs.txt"
params = ng.p.Instrumentation(
None, 2.0, blublu="blublu", array=ng.p.Array(shape=(3, 2)), multiobjective=False
)
zmethods = ["CauchyLHSSearch", "DE", "CMA"]
ztmp1 = [ng.optimizers.registry[zmet] for zmet in zmethods]
optmodel = ng.families.Chaining(ztmp1, [50, 50]) #
optim = optmodel(parametrization=params, budget=100, num_workers=3)
logger = ng.callbacks.ParametersLogger(filepath)
optim.register_callback("tell", logger)
optim.minimize(_func, verbosity=2)
# read
logger = callbacks.ParametersLogger(filepath)
logs = logger.load_flattened()
assert len(logs) == 100


def test_dump_callback(tmp_path: Path) -> None:
filepath = tmp_path / "pickle.pkl"
optimizer = optimizerlib.OnePlusOne(parametrization=2, budget=32)
Expand Down

0 comments on commit 351e8d6

Please sign in to comment.