Skip to content

blackboxopt.optimizers.random_search

RandomSearch (MultiObjectiveOptimizer)

Source code in blackboxopt/optimizers/random_search.py
class RandomSearch(MultiObjectiveOptimizer):
    def __init__(
        self,
        search_space: ParameterSpace,
        objectives: List[Objective],
        max_steps: int,
        seed: int = None,
    ) -> None:
        """Randomly sample up to `max_steps` configurations from the given search space.

        Args:
            search_space: Space to search
            objectives: The objectives of the optimization.
            max_steps: Max number of evaluation specifications the optimizer generates
                before raising `OptimizationComplete`
            seed: Optional number to seed the random number generator with.
                Defaults to None.
        """
        super().__init__(search_space=search_space, objectives=objectives, seed=seed)

        self.max_steps: int = max_steps
        self.n_steps: int = 0

    def generate_evaluation_specification(self) -> EvaluationSpecification:
        """[summary]

        Raises:
            OptimizationComplete: Raised if the optimizer's `max_steps` are reached.

        Returns:
            [description]
        """
        if self.n_steps >= self.max_steps:
            raise OptimizationComplete()

        eval_spec = EvaluationSpecification(
            configuration=self.search_space.sample(),
            settings={},
            optimizer_info={"step": self.n_steps},
        )
        self.n_steps += 1

        return eval_spec

generate_evaluation_specification(self)

[summary]

Exceptions:

Type Description
OptimizationComplete

Raised if the optimizer's max_steps are reached.

Returns:

Type Description
EvaluationSpecification

[description]

Source code in blackboxopt/optimizers/random_search.py
def generate_evaluation_specification(self) -> EvaluationSpecification:
    """[summary]

    Raises:
        OptimizationComplete: Raised if the optimizer's `max_steps` are reached.

    Returns:
        [description]
    """
    if self.n_steps >= self.max_steps:
        raise OptimizationComplete()

    eval_spec = EvaluationSpecification(
        configuration=self.search_space.sample(),
        settings={},
        optimizer_info={"step": self.n_steps},
    )
    self.n_steps += 1

    return eval_spec

report(self, evaluations) inherited

Report one or more evaluated evaluation specifications.

NOTE: Not all optimizers support reporting results for evaluation specifications that were not proposed by the optimizer.

Parameters:

Name Type Description Default
evaluations Union[blackboxopt.evaluation.Evaluation, Iterable[blackboxopt.evaluation.Evaluation]]

A single evaluated evaluation specifications, or an iterable of many.

required
Source code in blackboxopt/optimizers/random_search.py
def report(self, evaluations: Union[Evaluation, Iterable[Evaluation]]) -> None:
    _evals = [evaluations] if isinstance(evaluations, Evaluation) else evaluations

    call_functions_with_evaluations_and_collect_errors(
        [functools.partial(validate_objectives, objectives=self.objectives)],
        _evals,
    )