src.environment.optimizer.lde_optimizer¶
Module Contents¶
Classes¶
Introduction¶LDE:Learning Adaptive Differential Evolution Algorithm From Optimization Experiences by Policy Gradient |
API¶
- class src.environment.optimizer.lde_optimizer.LDE_Optimizer(config)[source]¶
Bases:
src.environment.optimizer.learnable_optimizer.Learnable_OptimizerIntroduction¶
LDE:Learning Adaptive Differential Evolution Algorithm From Optimization Experiences by Policy Gradient
Original paper¶
“Learning Adaptive Differential Evolution Algorithm from Optimization Experiences by Policy Gradient.” IEEE Transactions on Evolutionary Computation (2021).
Official Implementation¶
Initialization
Introduction¶
Initializes the optimizer with the provided configuration and sets default values for several optimization parameters.
Args:¶
config (object): Configuration object containing optimizer settings.
Built-in Attribute:¶
self.__config: Stores the configuration object.
self.__BATCH_SIZE: Batch size for optimization (default is 1).
self.fes: Function evaluation counter (initialized as None).
self.cost: Cost value (initialized as None).
self.log_index: Logging index (initialized as None).
self.log_interval: Interval for logging, taken from the configuration.
Returns:¶
None
Raises:¶
None
- __get_cost(batch, pop)[source]¶
Introduction¶
Computes the cost for each item in a batch given a corresponding population, optionally normalizing by the optimum value if available.
Args:¶
batch (list): A list of objects, each with an
optimumattribute and anevalmethod that evaluates a population member.pop (list): A list of population members, one for each item in the batch.
Returns:¶
numpy.ndarray: A 2D array where each row corresponds to the cost for a batch item.
Notes:¶
If
batch[p].optimumisNone, the raw evaluation is used as the cost.If
batch[p].optimumis notNone, the cost is normalized by subtracting the optimum value.
- __modifyChildwithParent(cross_pop, parent_pop, x_max, x_min)[source]¶
Introduction¶
Modifies the offspring population (
cross_pop) based on boundary constraints and the parent population (parent_pop). If an offspring value is out of bounds, it is adjusted using the parent value and the respective bound.Args:¶
cross_pop (np.ndarray): The offspring population array to be modified.
parent_pop (np.ndarray): The parent population array used for boundary correction.
x_max (float or np.ndarray): The upper bound(s) for the population values.
x_min (float or np.ndarray): The lower bound(s) for the population values.
Returns:¶
np.ndarray: The modified offspring population with boundary violations corrected.
- __de_crosselect_random_dataset(pop, m_pop, fit, cr_vector, nfes, batch)[source]¶
Introduction¶
Performs the crossover and selection operations for a Differential Evolution (DE) optimizer on a batch of populations, using random datasets for crossover. This function generates offspring via crossover, applies boundary control, evaluates offspring fitness, and selects the next generation based on fitness.
Args:¶
pop (np.ndarray): The current population array of shape (batch_size, pop_size, problem_size).
m_pop (np.ndarray): The mutated population array of the same shape as
pop.fit (np.ndarray): The fitness values of the current population, shape (batch_size, pop_size).
cr_vector (np.ndarray): The crossover rate vector, shape (batch_size, pop_size).
nfes (int): The current number of function evaluations.
batch (object): An object containing problem-specific data, including upper and lower bounds (
ub,lb).
Returns:¶
n_pop (np.ndarray): The next generation population after selection, shape (batch_size, pop_size, problem_size).
n_fit (np.ndarray): The fitness values of the next generation, shape (batch_size, pop_size).
nfes (int): The updated number of function evaluations after evaluating offspring.
- __mulgenerate_pop(p, NP, input_dimension, x_min, x_max, same_per_problem)[source]¶
Introduction¶
Generates a population of candidate solutions for an optimization algorithm, with options for generating the same or different populations per problem.
Args:¶
p (int): Number of problems or populations to generate.
NP (int): Number of individuals in each population.
input_dimension (int): Dimensionality of each individual.
x_min (float or np.ndarray): Lower bound(s) for initialization.
x_max (float or np.ndarray): Upper bound(s) for initialization.
same_per_problem (bool): If True, generates the same population for all problems; otherwise, generates different populations.
Returns:¶
np.ndarray: Generated population(s) with shape (p, NP, input_dimension).
- __order_by_f(pop, fit)[source]¶
Introduction¶
Sorts a population and its corresponding fitness values in ascending order of fitness for each batch.
Args:¶
pop (np.ndarray): The population array of shape (batch_size, pop_size, …).
fit (np.ndarray): The fitness array of shape (batch_size, pop_size).
Returns:¶
temp_pop (p, NP, input_dimension): The population array sorted by fitness for each batch.
temp_fit (p, NP, input_dimension): The fitness array sorted in ascending order for each batch.
- __maxmin_norm(a)[source]¶
Introduction¶
Applies max-min normalization to each batch in the input array, scaling values to the [0, 1] range per batch.
Args:¶
a (np.ndarray): A 2D NumPy array of shape (batch_size, n_features), where each row represents a batch to be normalized.
Returns:¶
np.ndarray: A NumPy array of the same shape as
a, with each batch normalized using max-min scaling.
Notes:¶
If all values in a batch are equal, the batch is left as zeros (no normalization applied).
- __con2mat_current2pbest_Nw(mutation_vector, p)[source]¶
Introduction¶
Constructs a mutation matrix for the “current-to-pbest” strategy in a differential evolution optimizer, supporting batch operations and stochastic selection of p-best individuals.
Args:¶
mutation_vector (np.ndarray): A 2D array of shape (batch_size, pop_size) containing mutation coefficients for each individual in the population.
p (float): The proportion (0 < p <= 1) of top individuals to consider as p-best for mutation.
Returns:¶
np.ndarray: A 3D array of shape (batch_size, pop_size, pop_size) representing the mutation matrix for each batch and individual.
Notes:¶
The method uses a random number generator (
self.rng) to select p-best indices.For each individual, the diagonal of the mutation matrix is set based on the mutation vector, and the selected p-best index is updated accordingly.
- __con2mat_rand2pbest_Nw(mutation_vector, nfes, MaxFEs)[source]¶
Introduction¶
Generates a mutation matrix using the “rand-to-pbest” strategy with a dynamically adjusted p-rate based on the current number of function evaluations.
Args:¶
mutation_vector (np.ndarray): The mutation vector to be transformed into a mutation matrix.
nfes (int): The current number of function evaluations.
MaxFEs (int): The maximum number of function evaluations allowed.
Returns:¶
np.ndarray: A 3D array of shape (batch_size, pop_size, pop_size) representing the mutation matrix for each batch and individual.
Notes:¶
The p-rate is linearly interpolated between
P_INIandP_MINfrom the configuration as the optimization progresses.
- __add_random(m_pop, pop, mu)[source]¶
Introduction¶
Generates a mutated population by adding scaled differences between randomly selected individuals from the current population, ensuring that indices are unique and do not repeat within each selection.
Args:¶
m_pop (np.ndarray): The mean population array, typically representing the current mean of the population.
pop (np.ndarray): The current population array of individuals.
mu (np.ndarray): The mutation factor(s) to scale the difference between individuals.
Returns:¶
np.ndarray: The mutated population A 2D array of shape (NP, dim) array after applying the random differential mutation.
Notes:¶
The function ensures that for each selection, the indices used are unique and do not overlap with the current index.
- __str__()[source]¶
Introduction¶
Returns a string representation of the LDE_Optimizer object.
Returns:¶
str: The name of the optimizer, “LDE_Optimizer”.
- init_population(problem)[source]¶
Introduction¶
Initializes the population for the optimizer, evaluates their fitness, and sets up internal tracking variables.
Args:¶
problem (object): The optimization problem object, which has attributes
lb(lower bounds) andub(upper bounds).
Returns:¶
np.ndarray: Feature representation of the initialized population, as returned by
self.__get_feature().
Notes:¶
Initializes the population using the problem’s bounds and configuration parameters.
Evaluates the initial fitness of the population.
Sets up tracking for best cost, function evaluations, logging, and historical data.
Optionally stores meta-data if configured.
- get_best()[source]¶
Introduction¶
Retrieves the best (global best) cost found by the optimizer.
Returns:¶
float: The lowest cost value (gbest_cost) discovered during the optimization process.
- __get_feature()[source]¶
Introduction¶
Computes and returns the input features for the optimizer’s neural network, combining normalized fitness values, fitness histograms, and historical histogram means.
Returns:¶
np.ndarray: A 2D array of shape [batch_size, NP + BINS * 2], where each row contains the concatenated normalized fitness, current fitness histogram, and mean of past histograms for each batch.
Notes:¶
Assumes that
self.__pop,self.__fit,self.__order_by_f,self.__maxmin_norm,self.__BATCH_SIZE,self.__config.BINS, andself.__past_histoare properly initialized and available as class attributes.
- update(action, problem)[source]¶
Introduction¶
Updates the population and fitness values in the LDE optimizer using the provided action and problem instance. This method performs one iteration of the optimization process, applying mutation, crossover, and selection operations, and computes the reward and termination status.
Args:¶
action (np.ndarray): The action tensor containing scale factors and crossover rates for the population, typically output from a policy network. Shape: [batch_size, NP*2].
problem (object): The optimization problem instance, which should provide an evaluation method and may contain an optimum attribute.
Returns:¶
feature (np.ndarray): The extracted feature representation of the current population state.
reward (np.ndarray): The reward signal computed based on the improvement in best-so-far fitness.
is_done (bool): Flag indicating whether the optimization process has reached its termination condition.
info (dict): Additional information dictionary (currently empty).
Notes:¶
Updates internal state variables such as population, fitness, best-so-far cost, and historical fitness distribution.
Handles logging and meta-data collection if enabled in the configuration.
The method assumes that the action tensor is properly formatted and that the problem instance provides necessary evaluation functionality.