Skip to content

Local and random search

LocalAndSortedRandomSearch #

LocalAndSortedRandomSearch(
    configspace: ConfigurationSpace,
    acquisition_function: (
        AbstractAcquisitionFunction | None
    ) = None,
    challengers: int = 5000,
    max_steps: int | None = None,
    n_steps_plateau_walk: int = 10,
    local_search_iterations: int = 10,
    seed: int = 0,
    uniform_configspace: ConfigurationSpace | None = None,
    prior_sampling_fraction: float | None = None,
)

Bases: AbstractAcquisitionMaximizer

Implement SMAC's default acquisition function optimization.

This optimizer performs local search from the previous best points according to the acquisition function, uses the acquisition function to sort randomly sampled configurations. Random configurations are interleaved by the main SMAC code.

The Random configurations are interleaved to circumvent issues from a constant prediction from the Random Forest model at the beginning of the optimization process.

Parameters#

configspace : ConfigurationSpace uniform_configspace : ConfigurationSpace A version of the user-defined ConfigurationSpace where all parameters are uniform (or have their weights removed in the case of a categorical hyperparameter). Can optionally be given and sampling ratios be defined via the prior_sampling_fraction parameter. acquisition_function : AbstractAcquisitionFunction | None, defaults to None challengers : int, defaults to 5000 Number of challengers. max_steps: int | None, defaults to None [LocalSearch] Maximum number of steps that the local search will perform. n_steps_plateau_walk: int, defaults to 10 [LocalSearch] number of steps during a plateau walk before local search terminates. local_search_iterations: int, defauts to 10 [Local Search] number of local search iterations. prior_sampling_fraction: float, defaults to 0.5 The ratio of random samples that are taken from the user-defined ConfigurationSpace, as opposed to the uniform version (needs uniform_configspaceto be defined). seed : int, defaults to 0

Source code in smac/acquisition/maximizer/local_and_random_search.py
def __init__(
    self,
    configspace: ConfigurationSpace,
    acquisition_function: AbstractAcquisitionFunction | None = None,
    challengers: int = 5000,
    max_steps: int | None = None,
    n_steps_plateau_walk: int = 10,
    local_search_iterations: int = 10,
    seed: int = 0,
    uniform_configspace: ConfigurationSpace | None = None,
    prior_sampling_fraction: float | None = None,
) -> None:
    super().__init__(
        configspace,
        acquisition_function=acquisition_function,
        challengers=challengers,
        seed=seed,
    )

    if uniform_configspace is not None and prior_sampling_fraction is None:
        prior_sampling_fraction = 0.5
    if uniform_configspace is None and prior_sampling_fraction is not None:
        raise ValueError("If `prior_sampling_fraction` is given, `uniform_configspace` must be defined.")
    if uniform_configspace is not None and prior_sampling_fraction is not None:
        self._prior_random_search = RandomSearch(
            acquisition_function=acquisition_function,
            configspace=configspace,
            seed=seed,
        )

        self._uniform_random_search = RandomSearch(
            acquisition_function=acquisition_function,
            configspace=uniform_configspace,
            seed=seed,
        )
    else:
        self._random_search = RandomSearch(
            configspace=configspace,
            acquisition_function=acquisition_function,
            seed=seed,
        )

    self._local_search = LocalSearch(
        configspace=configspace,
        acquisition_function=acquisition_function,
        max_steps=max_steps,
        n_steps_plateau_walk=n_steps_plateau_walk,
        seed=seed,
    )

    self._local_search_iterations = local_search_iterations
    self._prior_sampling_fraction = prior_sampling_fraction
    self._uniform_configspace = uniform_configspace

acquisition_function property writable #

acquisition_function: AbstractAcquisitionFunction | None

Returns the used acquisition function.

maximize #

maximize(
    previous_configs: list[Configuration],
    n_points: int | None = None,
    random_design: AbstractRandomDesign | None = None,
) -> Iterator[Configuration]

Maximize acquisition function using _maximize, implemented by a subclass.

Parameters#

previous_configs: list[Configuration] Previous evaluated configurations. n_points: int, defaults to None Number of points to be sampled & number of configurations to be returned. If n_points is not specified, self._challengers is used. Semantics depend on concrete implementation. random_design: AbstractRandomDesign, defaults to None Part of the returned ChallengerList such that we can interleave random configurations by a scheme defined by the random design. The method random_design.next_iteration() is called at the end of this function.

Returns#

challengers : Iterator[Configuration] An iterable consisting of configurations.

Source code in smac/acquisition/maximizer/abstract_acquisition_maximizer.py
def maximize(
    self,
    previous_configs: list[Configuration],
    n_points: int | None = None,
    random_design: AbstractRandomDesign | None = None,
) -> Iterator[Configuration]:
    """Maximize acquisition function using `_maximize`, implemented by a subclass.

    Parameters
    ----------
    previous_configs: list[Configuration]
        Previous evaluated configurations.
    n_points: int, defaults to None
        Number of points to be sampled & number of configurations to be returned. If `n_points` is not specified,
        `self._challengers` is used. Semantics depend on concrete implementation.
    random_design: AbstractRandomDesign, defaults to None
        Part of the returned ChallengerList such that we can interleave random configurations
        by a scheme defined by the random design. The method `random_design.next_iteration()`
        is called at the end of this function.

    Returns
    -------
    challengers : Iterator[Configuration]
        An iterable consisting of configurations.
    """
    if n_points is None:
        n_points = self._challengers

    def next_configs_by_acquisition_value() -> list[Configuration]:
        assert n_points is not None
        # since maximize returns a tuple of acquisition value and configuration,
        # and we only need the configuration, we return the second element of the tuple
        # for each element in the list
        return [t[1] for t in self._maximize(previous_configs, n_points)]

    challengers = ChallengerList(
        self._configspace,
        next_configs_by_acquisition_value,
        random_design,
    )

    if random_design is not None:
        random_design.next_iteration()

    return challengers