Skip to content

adaptive.BlockingRunner

BlockingRunner

BlockingRunner(learner: LearnerType, goal: Callable[[LearnerType], bool] | None = None, *, loss_goal: float | None = None, npoints_goal: int | None = None, end_time_goal: datetime | None = None, duration_goal: timedelta | int | float | None = None, executor: ExecutorTypes | None = None, ntasks: int | None = None, log: bool = False, shutdown_executor: bool = False, retries: int = 0, raise_if_retries_exceeded: bool = True)

Bases: BaseRunner

Run a learner synchronously in an executor.

Parameters:

Name Type Description Default
learner adaptive.BaseLearner instance
required
goal callable

The end condition for the calculation. This function must take the learner as its sole argument, and return True when we should stop requesting more points.

None
loss_goal float

Convenience argument, use instead of goal. The end condition for the calculation. Stop when the loss is smaller than this value.

None
npoints_goal int

Convenience argument, use instead of goal. The end condition for the calculation. Stop when the number of points is larger or equal than this value.

None
end_time_goal datetime

Convenience argument, use instead of goal. The end condition for the calculation. Stop when the current time is larger or equal than this value.

None
duration_goal timedelta or number

Convenience argument, use instead of goal. The end condition for the calculation. Stop when the current time is larger or equal than start_time + duration_goal. duration_goal can be a number indicating the number of seconds.

None
executor (Executor, Client, MPIPoolExecutor, Client or get_reusable_executor)

The executor in which to evaluate the function to be learned. If not provided, a new loky.get_reusable_executor is used.

None
ntasks int

The number of concurrent function evaluations. Defaults to the number of cores available in executor.

None
log bool

If True, record the method calls made to the learner by this runner.

False
shutdown_executor bool

If True, shutdown the executor when the runner has completed. If executor is not provided then the runner uses loky's reusable executor, which is shared between runners and is therefore not shut down unless this parameter is True.

False
retries int

Maximum amount of retries of a certain point x in learner.function(x). After retries is reached for x the point is present in runner.failed.

0
raise_if_retries_exceeded bool

Raise the error after a point x failed retries.

True

Attributes:

Name Type Description
learner adaptive.BaseLearner instance

The underlying learner. May be queried for its state.

log list or None

Record of the method calls made to the learner, in the format (method_name, *args).

to_retry list of tuples

List of (point, n_fails). When a point has failed runner.retries times it is removed but will be present in runner.tracebacks.

tracebacks list of tuples

List of of (point, tb) for points that failed.

pending_points list of tuples

A list of tuples with (concurrent.futures.Future, point).

Methods:

Name Description
elapsed_time : callable

A method that returns the time elapsed since the runner was started.

overhead : callable

The overhead in percent of using Adaptive. This includes the overhead of the executor. Essentially, this is 100 * (1 - total_elapsed_function_time / self.elapsed_time()).

Source code in adaptive/runner.py
def __init__(
    self,
    learner: LearnerType,
    goal: Callable[[LearnerType], bool] | None = None,
    *,
    loss_goal: float | None = None,
    npoints_goal: int | None = None,
    end_time_goal: datetime | None = None,
    duration_goal: timedelta | int | float | None = None,
    executor: ExecutorTypes | None = None,
    ntasks: int | None = None,
    log: bool = False,
    shutdown_executor: bool = False,
    retries: int = 0,
    raise_if_retries_exceeded: bool = True,
) -> None:
    if inspect.iscoroutinefunction(learner.function):
        raise ValueError("Coroutine functions can only be used with 'AsyncRunner'.")
    super().__init__(
        learner,
        goal=goal,
        loss_goal=loss_goal,
        npoints_goal=npoints_goal,
        end_time_goal=end_time_goal,
        duration_goal=duration_goal,
        executor=executor,
        ntasks=ntasks,
        log=log,
        shutdown_executor=shutdown_executor,
        retries=retries,
        raise_if_retries_exceeded=raise_if_retries_exceeded,
        allow_running_forever=False,
    )
    self._run()

elapsed_time

elapsed_time() -> float

Return the total time elapsed since the runner was started.

Source code in adaptive/runner.py
def elapsed_time(self) -> float:
    """Return the total time elapsed since the runner
    was started."""
    if self.end_time is None:
        # This shouldn't happen if the BlockingRunner
        # correctly finished.
        self.end_time = time.time()
    return self.end_time - self.start_time