Skip to content

adaptive.runner.BaseRunner

BaseRunner

BaseRunner(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, allow_running_forever: bool = False)

Base class for runners that use concurrent.futures.Executor\'s.

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
allow_running_forever bool

Allow the runner to run forever when the goal is None.

False

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
overhead : callable

The overhead in percent of using Adaptive. 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,
    allow_running_forever: bool = False,
):
    self.executor = _ensure_executor(executor)
    self.goal = _goal(
        learner,
        goal,
        loss_goal,
        npoints_goal,
        end_time_goal,
        duration_goal,
        allow_running_forever,
    )

    self._max_tasks = ntasks

    self._pending_tasks: dict[FutureTypes, int] = {}

    # The internally created executor (when `executor is None`) is loky's
    # reusable executor, a singleton shared between all runners, so we
    # must not shut it down implicitly: another runner may still be using
    # it. Pass `shutdown_executor=True` to shut it down anyway.
    self.shutdown_executor = shutdown_executor

    self.learner = learner
    self.log: list | None = [] if log else None

    # Timing
    self.start_time = time.time()
    self.end_time: float | None = None
    self._elapsed_function_time = 0

    # Error handling attributes
    self.retries = retries
    self.raise_if_retries_exceeded = raise_if_retries_exceeded
    self._to_retry: dict[int, int] = {}
    self._tracebacks: dict[int, str] = {}

    self._id_to_point: dict[int, Any] = {}
    self._next_id: Callable[[], int] = functools.partial(  # type: ignore[assignment]
        next, itertools.count()
    )  # some unique id to be associated with each point

executor instance-attribute

executor = _ensure_executor(executor)

goal instance-attribute

goal = _goal(learner, goal, loss_goal, npoints_goal, end_time_goal, duration_goal, allow_running_forever)

shutdown_executor instance-attribute

shutdown_executor = shutdown_executor

learner instance-attribute

learner = learner

log instance-attribute

log: list | None = [] if log else None

start_time instance-attribute

start_time = time.time()

end_time instance-attribute

end_time: float | None = None

retries instance-attribute

retries = retries

raise_if_retries_exceeded instance-attribute

raise_if_retries_exceeded = raise_if_retries_exceeded

do_log property

do_log: bool

failed property

failed: set[int]

Set of points ids that failed runner.retries times.

tracebacks property

tracebacks: list[tuple[int, str]]

to_retry property

to_retry: list[tuple[int, int]]

pending_points property

pending_points: list[tuple[FutureTypes, Any]]

overhead

overhead() -> float

Overhead of using Adaptive and the executor in percent.

This is measured as 100 * (1 - t_function / t_elapsed).

Notes

This includes the overhead of the executor that is being used. The slower your function is, the lower the overhead will be. The learners take ~5-50 ms to suggest a point and sending that point to the executor also takes about ~5 ms, so you will benefit from using Adaptive whenever executing the function takes longer than 100 ms. This of course depends on the type of executor and the type of learner but is a rough rule of thumb.

Source code in adaptive/runner.py
def overhead(self) -> float:
    """Overhead of using Adaptive and the executor in percent.

    This is measured as ``100 * (1 - t_function / t_elapsed)``.

    Notes
    -----
    This includes the overhead of the executor that is being used.
    The slower your function is, the lower the overhead will be. The
    learners take ~5-50 ms to suggest a point and sending that point to
    the executor also takes about ~5 ms, so you will benefit from using
    Adaptive whenever executing the function takes longer than 100 ms.
    This of course depends on the type of executor and the type of learner
    but is a rough rule of thumb.
    """
    t_function = self._elapsed_function_time
    if t_function == 0:
        # When no function is done executing, the overhead cannot
        # reliably be determined, so 0 is the best we can do.
        return 0
    t_total = self.elapsed_time()
    return (1 - t_function / t_total) * 100

elapsed_time abstractmethod

elapsed_time()

Return the total time elapsed since the runner was started.

Is called in overhead.

Source code in adaptive/runner.py
@abc.abstractmethod
def elapsed_time(self):
    """Return the total time elapsed since the runner
    was started.

    Is called in `overhead`.
    """