Skip to content

Commit

Permalink
- add pre- and post-evaluation callbacks to dask optimization loop
Browse files Browse the repository at this point in the history
- update mypy due to bug with numpy code

Co-authored-by: dynobo <dynobo@users.noreply.github.com>
Signed-off-by: Falkner Stefan (CR/PJ-AI-R32) <Stefan.Falkner@de.bosch.com>
  • Loading branch information
sfalkner and dynobo committed Nov 22, 2023
1 parent 36cd141 commit e43e0cc
Show file tree
Hide file tree
Showing 4 changed files with 1,129 additions and 959 deletions.
2 changes: 1 addition & 1 deletion blackboxopt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "5.0.2"
__version__ = "5.0.3"

from parameterspace import ParameterSpace

Expand Down
16 changes: 15 additions & 1 deletion blackboxopt/optimization_loops/dask_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import logging
import time
from typing import Callable, List, Set, Union
from typing import Any, Callable, List, Optional, Set, Union

try:
import dask.distributed as dd
Expand Down Expand Up @@ -103,6 +103,8 @@ def run_optimization_loop(
dask_client: dd.Client,
timeout_s: float = float("inf"),
max_evaluations: int = None,
pre_evaluation_callback: Optional[Callable[[EvaluationSpecification], Any]] = None,
post_evaluation_callback: Optional[Callable[[Evaluation], Any]] = None,
logger: logging.Logger = None,
) -> List[Evaluation]:
"""Convenience wrapper for an optimization loop that uses Dask to parallelize
Expand All @@ -124,6 +126,10 @@ def run_optimization_loop(
optimization step that exceeded the timeout (in seconds). Defaults to inf.
max_evaluations: If given, the optimization loop will terminate after the given
number of steps. Defaults to None.
pre_evaluation_callback: Reference to a callable that is invoked before each
evaluation and takes a `blackboxopt.EvaluationSpecification` as an argument.
post_evaluation_callback: Reference to a callable that is invoked after each
evaluation and takes a `blackboxopt.Evaluation` as an argument.
logger: The logger to use for logging progress. Defaults to None.
Returns:
Expand Down Expand Up @@ -152,6 +158,10 @@ def run_optimization_loop(
if dask_scheduler.has_capacity():
try:
eval_spec = optimizer.generate_evaluation_specification()

if pre_evaluation_callback:
pre_evaluation_callback(eval_spec)

dask_scheduler.submit(evaluation_function, eval_spec)
n_eval_specs += 1
continue
Expand All @@ -164,6 +174,10 @@ def run_optimization_loop(
break

new_evaluations = dask_scheduler.check_for_results(timeout_s=20)

if post_evaluation_callback:
list(map(post_evaluation_callback, new_evaluations))

optimizer.report(new_evaluations)
evaluations.extend(new_evaluations)

Expand Down
Loading

0 comments on commit e43e0cc

Please sign in to comment.