src.baseline.bbo.nlshadelbc¶
Module Contents¶
Classes¶
Introduction¶Non-Linear population size reduction Success-History Adaptive Differential Evolution with Linear Bias Change.It combines selective pressure, biased parameter adaptation with linear bias change, current-to-pbest strategy, resampling of solutions as bound constraint handling techniques, as well as the non-linear population size reduction. |
Functions¶
API¶
- class src.baseline.bbo.nlshadelbc.NLSHADELBC(config)[source]¶
Bases:
src.environment.optimizer.basic_optimizer.Basic_OptimizerIntroduction¶
Non-Linear population size reduction Success-History Adaptive Differential Evolution with Linear Bias Change.It combines selective pressure, biased parameter adaptation with linear bias change, current-to-pbest strategy, resampling of solutions as bound constraint handling techniques, as well as the non-linear population size reduction.
Original paper¶
“NL-SHADE-LBC algorithm with linear parameter adaptation bias change for CEC 2022 Numerical Optimization.” 2022 IEEE Congress on Evolutionary Computation (CEC). IEEE, 2022.
Initialization
Initializes the NLSHADELBC optimizer with the given configuration.
Args:¶
config (object):
The Attributes needed for the NLSHADELBC in config are the following:
maxFEs (int): Maximum number of function evaluations allowed. Default directly 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. Default is maxFEs // n_logpoint.
full_meta_data (bool): Flag indicating whether to store complete solution history. Default is False.
Attributes:¶
__pb (float): Rate of best individuals in mutation.
__pa (float): Rate of selecting individual from archive.
__m (float): Parameter for mutation.
__p_iniF (float): Initial value for mutation factor.
__p_iniCr (float): Initial value for crossover rate.
__p_fin (float): Final value for parameter p.
__Nmin (int): Lower bound of population size.
__archive (np.ndarray): Archive of replaced individuals.
__k (int): Index for updating elements in MF and MCr.
__MaxFEs (int): Maximum number of function evaluations.
__FEs (int): Current number of function evaluations.
gbest (float): Best global fitness value found.
- __str__()[source]¶
Returns the string representation of the NLSHADELBC class.
Returns:¶
str: The string ‘NLSHADELBC’, representing the class name.
- __evaluate(problem, u)[source]¶
Introduction¶
Evaluates the cost of a solution vector
ufor a given optimization problem, optionally adjusting by the problem’s optimum and storing metadata if enabled.Args:¶
problem: An object representing the optimization problem.
u (array-like): The solution vector to be evaluated.
Returns:¶
cost (float): The (possibly shifted) evaluation of the solution vector.
Side Effects:¶
If
self.full_meta_datais True, appends the cost and solution vector toself.meta_Costandself.meta_X, respectively.
- __Binomial(x, v, cr)[source]¶
Introduction¶
Performs binomial crossover operation used in evolutionary algorithms, particularly in Differential Evolution (DE). This method generates trial vectors by mixing parent vectors (
x) and donor vectors (v) based on a crossover rate (cr).Args:¶
x (np.ndarray): Parent population array of shape (NP, dim), where NP is the population size and dim is the dimensionality.
v (np.ndarray): Donor population array of the same shape as
x.cr (np.ndarray): Crossover rate array of shape (NP,), specifying the probability of crossover for each individual.
Returns:¶
np.ndarray: Trial population array of shape (NP, dim) after binomial crossover.
- __Exponential(x, v, cr)[source]¶
Introduction¶
Performs the exponential crossover operation used in differential evolution algorithms. This method generates trial vectors by combining parent vectors (
x) and donor vectors (v) based on a crossover rate (cr), following an exponential (contiguous) crossover scheme.Args:¶
x (np.ndarray): The current population matrix of shape (NP, dim), where NP is the population size and dim is the dimensionality.
v (np.ndarray): The donor (mutant) population matrix of shape (NP, dim).
cr (np.ndarray): The crossover rate for each individual, of shape (NP, 1) or (NP,).
Returns:¶
np.ndarray: The trial population matrix after exponential crossover, of shape (NP, dim).
Notes:¶
This method ensures that for each individual, a contiguous subset of variables is inherited from the donor vector, starting from a randomly chosen position.
- __update_Pa(fa, fp, na, NP)[source]¶
Introduction¶
Updates the internal probability parameter
__pabased on the provided counts of accepted and rejected solutions.Args:¶
fa (float): The number of successful (accepted) solutions.
fp (float): The number of unsuccessful (rejected) solutions.
na (int): The number of accepted solutions in the current population.
NP (int): The total population size.
Returns:¶
None: This method updates the internal state (
__pa) of the object.
Notes:¶
If there are no accepted solutions (
na == 0) or no successful solutions (fa == 0),__pais set to 0.5.The updated
__pais constrained to the range [0.1, 0.9].
- __mean_wL_Cr(df, s)[source]¶
Introduction¶
Computes a weighted mean of the input array
susing weights derived fromdfand a dynamically calculated exponentpg. The calculation is based on the current and maximum function evaluations, as well as initial and final parameter values.Args:¶
df (np.ndarray): Array of weights or differences used for normalization.
s (np.ndarray): Array of values to be exponentiated and averaged.
Returns:¶
float: The weighted mean value computed using the specified formula. Returns 0.9 if the sum of
dfis zero.
Notes:¶
The method uses internal attributes:
__MaxFEs,__FEs,__p_iniCr,__p_fin, and__m.
- __mean_wL_F(df, s)[source]¶
Introduction¶
Computes a weighted mean of the input array
susing weights derived from the arraydfand a dynamically calculated exponentpg. This function is typically used in optimization algorithms to aggregate solutions based on their fitness or other criteria.Args:¶
df (np.ndarray): Array of weights or fitness differences for the population.
s (np.ndarray): Array of solution values or candidate solutions.
Returns:¶
float: The weighted mean of
sraised to the powerpg, normalized by the weighted mean ofsraised to the powerpg - self.__m. Returns 0.5 if the sum ofdfis zero.
Notes:¶
The exponent
pgis dynamically computed based on the current number of function evaluations (self.__FEs), the maximum allowed evaluations (self.__MaxFEs), and parametersself.__p_iniFandself.__p_fin.If the sum of
dfis zero, a default value of 0.5 is returned.
- __update_M_F_Cr(SF, SCr, df)[source]¶
Introduction¶
Updates the memory arrays for mutation factor (F) and crossover rate (Cr) based on successful parameter values and their corresponding fitness differences.
Args:¶
SF (np.ndarray): Array of successful mutation factors from the current generation.
SCr (np.ndarray): Array of successful crossover rates from the current generation.
df (np.ndarray): Array of fitness differences associated with the successful parameters.
Side Effects:¶
Updates the internal memory arrays
self.__MFandself.__MCrat the current indexself.__k.Advances the memory index
self.__kin a circular manner.
Notes:¶
If there are no successful parameters (
SF.shape[0] == 0), default values are assigned to the memory arrays.
- __choose_F_Cr()[source]¶
Introduction¶
Generates crossover rate (Cr) and scaling factor (F) values for a population in an evolutionary algorithm, using normal and Cauchy distributions with adaptive memory.
Args:¶
None
Returns:¶
tuple:
C_r (np.ndarray): Array of crossover rates for the population, clipped to [0, 1].
F (np.ndarray): Array of scaling factors for the population, adjusted to be at least 0 and at most 1.
Notes:¶
Crossover rates are sampled from a normal distribution centered at memory values (
self.__MCr).Scaling factors are sampled from a Cauchy distribution centered at memory values (
self.__MF), with negative values reflected.
- __sort()[source]¶
Introduction¶
Sorts the population and corresponding cost arrays in ascending order of cost.
Args:¶
None
Returns:¶
None
Side Effects:¶
Updates
self.__costandself.__populationso that both are sorted according to the ascending order ofself.__cost.
- __update_archive(old_id)[source]¶
Introduction¶
Updates the archive of solutions by either appending a new individual from the population or replacing an existing one at random.
Args:¶
old_id (int): The index of the individual in the current population to be added to or used to update the archive.
Modifies:¶
self.__archive (np.ndarray): The archive of solutions, which is either expanded or updated in place.
Notes:¶
If the archive has not reached its maximum size (
self.__NA), the individual is appended.If the archive is full, a random entry is replaced with the new individual.
- __NLPSR()[source]¶
Introduction¶
Adjusts the population and archive sizes dynamically based on the current number of function evaluations in the optimization process. This method is typically used in evolutionary algorithms to balance exploration and exploitation by resizing the population and archive as the search progresses.
Args:¶
None
Modifies:¶
self.__NP (int): Updates the current population size.
self.__population (np.ndarray): Truncates the population array to the new size.
self.__cost (np.ndarray): Truncates the cost array to the new size.
self.__NA (int): Updates the current archive size.
self.__archive (np.ndarray): Truncates the archive array to the new size.
Raises:¶
None
- __init_population(problem)[source]¶
Introduction¶
Initializes the population and related parameters for the NL-SHADE-LBC evolutionary algorithm based on the given optimization problem.
Args:¶
problem (object): The problem object.
Side Effects:¶
Sets up the initial population, cost values, memory arrays for DE parameters, archive, and other internal state variables required for the algorithm’s execution.
Notes:¶
The population size, memory size, and other parameters are determined as multiples of the problem’s dimensionality.
The initial population is randomly generated within the provided bounds.
The best cost found so far is stored and logged.
- __update(problem)[source]¶
Introduction¶
Performs one iteration of the NL-SHADE-LBC algorithm, updating the population, archive, and adaptive parameters based on current solutions and their fitness. Handles mutation, crossover, selection, and parameter adaptation for the evolutionary process.
Args:¶
problem (object): The problem object.
Returns:¶
None
Side Effects:¶
Updates internal state variables such as population, archive, fitness values, adaptive parameters, and logging information.
Notes:¶
This method is intended for internal use within the NL-SHADE-LBC optimizer class.
The method assumes that the population and archive are properly initialized.
- run_episode(problem)[source]¶
Introduction¶
Executes a single optimization episode for the given problem, updating the population and tracking the best solution found.
Args:¶
problem (object): The problem object.
Returns:¶
dict: A dictionary containing:
‘cost’ (list): The history of best costs found during the episode.
‘fes’ (int): The total number of function evaluations performed.
‘metadata’ (dict, optional): If
full_meta_datais enabled, includes:‘X’ (list): The history of population states.
‘Cost’ (list): The history of population costs.
Notes:¶
The method initializes the population, iteratively updates it until the maximum number of function evaluations is reached, and logs the best solution.
If
full_meta_datais True, additional metadata about the optimization process is included in the results.