src.environment.optimizer.lga_optimizer¶
Module Contents¶
Classes¶
Introduction¶The |
|
Introduction¶Learned Genetic Algorithm parametrizes selection and mutation rate adaptation as cross- and self-attention modules and use MetaBBO to evolve their parameters on a set of diverse optimization tasks. |
Functions¶
API¶
- src.environment.optimizer.lga_optimizer.vector2nn(x, net)[source]¶
Introduction¶
Maps a flat parameter vector to the parameters of a given neural network, updating the network’s weights in-place.
Args:¶
x (list or numpy.ndarray): A 1D array or list containing all the parameters to be assigned to the network.
net (torch.nn.Module): The neural network whose parameters will be updated.
Returns:¶
torch.nn.Module: The neural network with updated parameters.
Raises:¶
AssertionError: If the length of
xdoes not match the total number of parameters innet.
- class src.environment.optimizer.lga_optimizer.Policy(pop_size, mu=0, sigma=1.0, DK=16, device=None)[source]¶
Bases:
torch.nn.ModuleIntroduction¶
The
Policyclass implements a neural network-based policy for evolutionary optimization, utilizing attention mechanisms for selection and adaptation of candidate solutions. It is designed to operate on populations of solutions, transforming fitness values and controlling mutation step sizes in an adaptive manner.Initialization
Introduction¶
Initializes the Policy optimizer with specified population size, mean, standard deviation, feature dimensions, and device. Sets up multiple linear layers for query, key, value, and sigma transformations, and applies custom weight initialization.
Args:¶
pop_size (int): The size of the population for the optimizer.
mu (float, optional): The mean value for weight initialization. Default is 0.
sigma (float, optional): The standard deviation for weight initialization. Default is 1.0.
DK (int, optional): The dimension of the key/query/value vectors. Default is 16.
device (torch.device or None, optional): The device on which to allocate tensors. Default is None.
Built-in Attribute:¶
DF (int): Feature dimension, set to 2.
D_sigma (int): Sigma dimension, set to 1.
W_QP, W_KC, W_VC, W_QS, W_KS, W_QM, W_KM, W_VM, W_sigma (nn.Linear): Linear layers for various transformations.
Returns:¶
None
Raises:¶
None
- _init_weights(mu, sigma)[source]¶
Introduction¶
Initializes the weights and biases of specific neural network layers using a normal distribution.
Args:¶
mu (float): The mean value for the normal distribution used to initialize weights and biases.
sigma (float): The standard deviation for the normal distribution used to initialize weights and biases.
Details:¶
Iterates over a predefined list of layer attributes (
self.W_QP,self.W_KC,self.W_VC,self.W_QS,self.W_KS,self.W_QM,self.W_KM,self.W_VM,self.W_sigma) and applies normal initialization to both theirweightandbiasparameters.
- trans_F(f)[source]¶
Introduction¶
Transforms the input tensor by computing its z-score normalization and scaled rank, returning both as a stacked tensor.
Args:¶
f (torch.Tensor): A 1D tensor of numerical values to be transformed.
Returns:¶
torch.Tensor: A 2D tensor of shape [N, 2], where the first column contains the z-score normalized values and the second column contains the scaled ranks.
Notes:¶
The z-score is computed as (f - mean) / (std + 1e-8) for numerical stability.
The scaled rank is computed such that it is centered around zero.
- adaptation(fitness, sigma)[source]¶
Introduction¶
Adapts the mutation step size (
sigma) for an evolutionary algorithm using an attention-based neural network mechanism.Args:¶
fitness (array-like): The fitness values of the population, shape [NP].
sigma (array-like): The mutation step sizes for the population, shape [NP].
Returns:¶
torch.Tensor: The adapted mutation step sizes, shape [NP], after applying the learned adaptation.
Notes:¶
This method converts the input arrays to PyTorch tensors and processes them on the configured device.
It uses neural network layers (
W_KM,W_QM,W_VM,W_sigma) and an attention mechanism to compute the adaptation.The adaptation is applied multiplicatively to the original
sigma.
- selection(fitness_c, fitness_p)[source]¶
Introduction¶
Performs a selection operation using attention mechanisms on child and parent fitness values, producing a one-hot encoded selection matrix.
Args:¶
fitness_c (array-like): Fitness values of the child population.
fitness_p (array-like): Fitness values of the parent population.
Returns:¶
torch.Tensor: A one-hot encoded tensor of shape [E, NP + 1], representing the selection outcome for each entity.
Notes:¶
The method applies linear transformations and attention mechanisms to compute selection probabilities.
The last column in the output corresponds to a special selection (e.g., “no selection” or “new individual”).
- class src.environment.optimizer.lga_optimizer.LGA_Optimizer(config)[source]¶
Bases:
src.environment.optimizer.learnable_optimizer.Learnable_OptimizerIntroduction¶
Learned Genetic Algorithm parametrizes selection and mutation rate adaptation as cross- and self-attention modules and use MetaBBO to evolve their parameters on a set of diverse optimization tasks.
Original paper¶
“Discovering attention-based genetic algorithms via meta-black-box optimization.” Proceedings of the Genetic and Evolutionary Computation Conference. (2023).
Official Implementation¶
Initialization
Introduction¶
Initializes the optimizer with the provided configuration, setting up key parameters and internal state.
Args:¶
config (object): Config object containing optimizer settings.
The Attributes needed for the LGA_Optimizer are the following:
maxFEs (int): Maximum number of function evaluations.
log_interval (int): Interval at which logs are recorded.
full_meta_data (bool): Flag indicating whether to use full meta data.
n_logpoint (int): Number of log points to record.
Built-in Attribute:¶
self.NP (int): Number of population members, set to 16.
self.policy (Policy): Policy object initialized with population size, bounds, and device.
self.fes (Any): Placeholder for function evaluation state, initialized as None.
self.cost (Any): Placeholder for cost value, initialized as None.
self.log_index (Any): Placeholder for logging index, initialized as None.
self.log_interval (int): Logging interval, taken from
config.log_interval.
Returns:¶
None
Raises:¶
AttributeError: If required attributes are missing from the
configobject.
- __str__()[source]¶
Introduction¶
Returns a string representation of the LGA_Optimizer instance.
Returns:¶
str: The string “LGA_Optimizer”.
- get_costs(position, problem)[source]¶
Introduction¶
Calculates the cost of a given position for an optimization problem, optionally adjusting by the known optimum.
Args:¶
position (numpy.ndarray): A 2D array of shape (NP, dim) representing the population to be evaluated, where NP is the number of individuals (population size) and dim is the problem dimension. Each row represents an individual’s position in the search space.
problem (object): The optimization problem instance, which must have
eval(position)andoptimumattributes.
Returns:¶
float: The cost associated with the given position. If the problem’s optimum is known, returns the difference between the evaluated cost and the optimum; otherwise, returns the evaluated cost.
Raises:¶
AttributeError: If the
problemobject does not have the requiredevalmethod oroptimumattribute.
- get_state()[source]¶
Introduction¶
Retrieves the current fitness value representing the state of the optimizer.
Returns:¶
float: The current fitness value of the optimizer.
- init_population(problem)[source]¶
Introduction¶
Initializes the population and related attributes for the optimizer based on the given problem definition.
Args:¶
problem: An object representing the optimization problem, expected to have attributes
dim(int),ub(upper bounds), andlb(lower bounds).
Built-in Attributes:¶
self.fes (int): Function evaluation count, initialized to 0.
self.population (numpy.ndarray): The initial population, a 2D array of shape (NP, dim).
self.sigma (numpy.ndarray): The mutation step sizes for the population, initialized to 0.2.
self.c_cost (numpy.ndarray): The costs associated with the initial population.
self.fitness (numpy.ndarray): The fitness values of the population, calculated from the costs.
self.gbest_val (float): The best cost value found in the population.
self.cost (list): A list to store the best cost values at each logging interval.
self.log_index (int): The current logging index, initialized to 1.
self.init_gbest (float): The initial best cost value.
self.meta_X (list): If
self.config.full_meta_datais True, stores the initial population for analysis.self.meta_Cost (list): If
self.config.full_meta_datais True, stores the initial costs for analysis.
Notes:¶
Uses a random number generator (
self.rng) to initialize the population.Assumes the existence of
get_costsandsoftmaxmethods.If
self.config.full_meta_datais True, stores additional metadata for analysis.
- update(action, problem)[source]¶
Introduction¶
Updates the optimizer’s state based on the provided action and problem definition. This method performs one or more optimization steps, updating the population, fitness, and other internal variables according to the current policy and the results of the optimization process.
Args:¶
action (dict): A dictionary containing the current policy network (‘net’) and optional ‘skip_step’ parameter. The policy network is used for adaptation and selection during the optimization process.
problem (object): An object representing the optimization problem, which must have attributes such as
dim(problem dimensionality),lb(lower bounds),ub(upper bounds), and optionallyoptimum.
Returns:¶
fitness (np.ndarray): The updated fitness values of the population after the optimization step(s).
improvement (float): The relative improvement in the best cost value from the initial to the current generation.
is_end (bool): A flag indicating whether the optimization process has reached its termination condition.
info (dict): Additional information about the optimization process (currently an empty dictionary).
Notes:¶
The method supports early stopping based on the number of function evaluations (
MaxFEs), the minimum cost achieved, or a specified number of steps (skip_step).The optimizer maintains logs of the best cost values and, if configured, full meta-data about the population and costs at each step.
The policy network is updated using the provided action, and is used for both mutation adaptation and selection.