src.baseline.bbo.random_search¶
Module Contents¶
Classes¶
Introduction¶Random_search is an implementation of a basic random search optimization algorithm, inheriting from Basic_Optimizer. It generates random candidate solutions within the problem bounds and tracks the best solution found so far. The optimizer supports logging of progress and optional collection of full meta-data for analysis. |
API¶
- class src.baseline.bbo.random_search.Random_search(config)[source]¶
Bases:
src.environment.optimizer.basic_optimizer.Basic_OptimizerIntroduction¶
Random_search is an implementation of a basic random search optimization algorithm, inheriting from Basic_Optimizer. It generates random candidate solutions within the problem bounds and tracks the best solution found so far. The optimizer supports logging of progress and optional collection of full meta-data for analysis.
Initialization
Initializes the random search optimizer with the config object constructed in src/config.py.
Args:¶
config (object):
The Attributes needed for the Random_search in config are the following:
maxFEs (int): Maximum number of function evaluations allowed.Defaullt value depends on the type of the problem.
n_logpoint (int): Number of log points for tracking progress. Default is 50.
log_interval (int): Interval at which logs are recorded.
full_meta_data (bool): Flag indicating whether to use full meta data.Default is None.
Attributes:¶
__fes (int): Counter for the number of function evaluations performed.Default is 0.
log_index (int or None): Index for logging, initialized as None.Default is None.
cost (any): Placeholder for cost value, initialized as None.Default is None.
__NP (int): Population size, set to 100.
- __str__()[source]¶
Returns a string representation of the Random Search optimizer.
Returns:¶
str: The name of the optimizer, ‘Random_search’.
- __reset(problem)[source]¶
Introduction¶
Resets the internal state of the random search optimizer for a new optimization run on the given problem.
Args:¶
problem: The optimization problem instance to initialize the population for.
Effects:¶
Resets the function evaluation counter.
Clears the cost history.
Initializes a new random population.
Appends the initial global best solution to the cost history.
Sets the log index to 1.
- __random_population(problem, init)[source]¶
Introduction¶
Generates a random population of candidate solutions within the problem’s bounds, evaluates their costs, and updates the global best solution.
Args:¶
problem (object): The optimization problem instance, expected to have attributes
lb(lower bounds),ub(upper bounds),dim(dimension),optimum(optional optimum value), and anevalmethod for evaluating solutions.init (bool): Indicates whether this is the initial population generation. If True, initializes the global best; otherwise, updates it if a better solution is found.
Side Effects:¶
Updates
self.meta_Costandself.meta_Xifself.full_meta_datais True.Increments
self.__fesby the population size (self.__NP).Updates
self.gbestwith the minimum cost found in the current population.
- run_episode(problem)[source]¶
Introduction¶
Executes a single optimization episode using random search on the provided problem instance. Tracks the best solution found, logs progress at specified intervals, and optionally collects metadata for analysis.
Args:¶
problem: An object representing the optimization problem.
Returns:¶
dict: A dictionary containing:
‘cost’ (list): The logged best costs at each interval.
‘fes’ (int): The total number of function evaluations performed.
‘metadata’ (dict, optional): If
self.full_meta_datais True, includes:‘X’ (list of np.ndarray): The population positions at each logging interval.
‘Cost’ (list of float): The fitness values of the population at each logging interval.
Notes:¶
The function ensures that the cost log is filled up to the required number of log points.
If
full_meta_datais True, additional metadata about the search process is included in the results.