src.environment.optimizer.b2opt_optimizer¶
Module Contents¶
Classes¶
Introduction¶B2Opt: Learning to Optimize Black-box Optimization with Little Budget. |
API¶
- class src.environment.optimizer.b2opt_optimizer.B2OPT_Optimizer(config)[source]¶
Bases:
src.environment.optimizer.learnable_optimizer.Learnable_OptimizerIntroduction¶
B2Opt: Learning to Optimize Black-box Optimization with Little Budget.
Original paper¶
“B2Opt: Learning to Optimize Black-box Optimization with Little Budget”. arXiv preprint arXiv:2304.11787, (2023).
Official Implementation¶
Initialization
Introduction¶
Initializes the optimizer with the provided configuration, setting up population size, evaluation limits, logging parameters, and internal state variables.
Args:¶
config (object): Configuration object.
The Attributes needed for the B2OPT are the following:
maxFEs (int): Maximum number of function evaluations.
log_interval (int): Interval at which logs are recorded.Default is config.maxFEs/config.n_logpoint.
n_logpoint (int): Number of log points to record.Default is 50.
full_meta_data (bool): Flag indicating whether to use full meta data.Default is False.
Built-in Attribute:¶
NP (int): Population size, set to 100.
ems (int): Number of epochs or main steps, computed based on
MaxFEsandNP.fes (Any): Placeholder for the current number of function evaluations,which will be initialized in the
init_populationmethod.cost (Any): Placeholder for the current cost or fitness value, which will be initialized in the
init_populationmethod.log_index (Any): Placeholder for the logging index, which will be initialized in the
init_populationmethod.log_interval (Any): Logging interval, taken from
config.log_interval.Default isconfig.maxFEs/config.n_logpoint.ems_index (int): Index for the current epoch or main step. Default is 0.
Returns:¶
None
- __str__()[source]¶
Introduction¶
Returns a string representation of the B2OPT optimizer instance.
Returns:¶
str: The name of the optimizer, “B2OPT_Optimizer”.
- get_costs(position, problem)[source]¶
Introduction¶
Computes the cost of a given position for a specified optimization problem, optionally normalizing by the known optimum.
Args:¶
position (Any): The candidate solution whose cost is to be evaluated.
problem (object): The optimization problem instance, expected to have
evalandoptimumattributes.
Returns:¶
torch.Tensor or float: The computed cost, converted to a torch.Tensor if originally a numpy array.
Raises:¶
AttributeError: If
problemdoes not have the requiredevalmethod oroptimumattribute.
- __sort()[source]¶
Introduction¶
Sorts the population and corresponding cost values in ascending order based on the cost.
Built-in Attribute:¶
self.population (torch.Tensor): The current population of solutions.
self.c_cost (torch.Tensor): The cost values associated with each member of the population.
Returns:¶
None. Updates
self.populationandself.c_costin-place to reflect the sorted order.
- init_population(problem)[source]¶
Introduction¶
Initializes the population for the optimizer based on the given problem’s bounds and dimensionality. Sets up random number generators, evaluates initial costs, and prepares metadata for tracking optimization progress.
Args:¶
problem (object): An object representing the optimization problem.
dim(int): Dimensionality of the problem.lb(torch.Tensor): Lower bounds for the variables.ub(torch.Tensor): Upper bounds for the variables.eval(x)(callable): Function to evaluate the objective at pointx.optimum(float or None): Known optimum value of the problem (if available).
Built-in Attribute:¶
self.population (torch.Tensor): The initialized population of candidate solutions.
self.c_cost (torch.Tensor): The costs of the initial population.
self.gbest_val (float): The best cost found in the initial population.
self.init_gbest (torch.Tensor): The best cost tensor in the initial population.
self.cost (list): List tracking the best cost at each iteration.
self.meta_X (list, optional): List of population states for metadata (if enabled).
self.meta_Cost (list, optional): List of cost states for metadata (if enabled).
Returns:¶
dict: The current state of the optimizer after population initialization,using get_state() method.
Raises:¶
None
- get_state()[source]¶
Introduction¶
Retrieves the current state of the optimizer, represented by the cost value.
Returns:¶
Any: The current cost value stored in
self.c_cost.
- update(action, problem)[source]¶
Introduction¶
Updates the state of the optimizer based on the given action and problem, and calculates the reward, next state, and termination condition.
Args:¶
action (callable): A policy network function that takes the current population, costs, and EMS index as input and returns updated positions.
problem (object): The optimization problem instance containing problem-specific details.
Returns:¶
next_state (torch.Tensor): The updated state of the optimizer.
reward (float): The reward calculated based on the improvement in the global best value.
is_end (bool): A flag indicating whether the optimization process has reached its termination condition.
info (dict): Additional information (currently empty).
Notes:¶
The method updates the population and costs based on the optimization process.
It calculates the reward as the relative improvement in the global best value compared to the initial best value.
The termination condition is determined by the maximum number of function evaluations (
MaxFEs).If
full_meta_datais enabled in the configuration, the population and costs are logged for each step.The global best value (
gbest_val) is updated and logged at specified intervals.