Skip to content

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: Optional[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

__init__(self, search_space, objectives, max_steps, seed=None) special

Randomly sample up to max_steps configurations from the given search space.

Parameters:

Name Type Description Default
search_space ParameterSpace

Space to search

required
objectives List[blackboxopt.base.Objective]

The objectives of the optimization.

required
max_steps int

Max number of evaluation specifications the optimizer generates before raising OptimizationComplete

required
seed Optional[int]

Optional number to seed the random number generator with. Defaults to None.

None
Source code in blackboxopt/optimizers/random_search.py
def __init__(
    self,
    search_space: ParameterSpace,
    objectives: List[Objective],
    max_steps: int,
    seed: Optional[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

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