ArtificialBeeColonyď
- class beeoptimal.abc.ArtificialBeeColony(colony_size, function, bounds, n_employed_bees=None, max_scouts=None)[source]ď
Bases:
objectArtificial Bee Colony (ABC) class
- colony_sizeď
The total number of bees in the colony.
- Type:
int
- n_employed_beesď
The number of employed bees.
- Type:
int
- n_onlooker_beesď
The number of onlooker bees.
- Type:
int
- max_scoutsď
The maximum number of scout bees per iteration. Defaults to None (will be set to n_employed_bees).
- Type:
int
- dimď
The dimensionality of the search space.
- Type:
int
- functionď
The objective function to optimize.
- Type:
callable
- boundsď
The bounds for each dimension of the search space, provided as a 2D array [(lower1, upper1), âŚ, (lowerD, upperD)].
- Type:
array-like
- max_itersď
The maximum number of iterations. Defaults to 1000.
- Type:
int
- actual_itersď
The actual number of iterations.
- Type:
int
- limitď
The trial limit for scout bees. If âdefaultâ, it is set to 0.6 * n_employed_bees * dimensionality. Defaults to âdefaultâ.
- Type:
int
- selectionď
The selection strategy for onlooker bees. Must be one among âRouletteWheelâ and âTournamentâ. Defaults to âRouletteWheelâ.
- Type:
str, optional
- mutationď
The mutation strategy. Must be one among âStandardABCâ, âModifiedABCâ, âABC/best/1â, âABC/best/2â and âDirectedABCâ. Defaults to âStandardABCâ.
- Type:
str
- initializationď
The initialization strategy for the bee population. Must be one among ârandomâ and âcahoticâ. Defaults to ârandomâ.
- Type:
str
- stagnation_tolď
The tolerance for stagnation in fitness values to trigger early termination. Defaults to np.NINF (i.e. stagnation disabled).
- Type:
float
- sfď
The scaling factor for mutations. Defaults to 1.0.
- Type:
float
- initial_sfď
The initial scaling factor. Defaults to 1.0.
- Type:
float
- self_adaptive_sfď
Whether to use a self-adaptive scaling factor. Defaults to False.
- Type:
bool
- mrď
The mutation rate for âModifiedABCâ strategy. Defaults to 0.7.
- Type:
float
Note
To ensure compatibility with all the mutation types, the bee colony must have at least 5 employed bees and at least 5 onlokeer bees.
- employees_phase_()[source]ď
Performs the employed bees phase, where each employed bee explores the search space by generating candidate solutions.
Note
Updates the employed bees with better candidate solutions based on the greedy selection. Adjusts the scaling factor if self-adaptive scaling is enabled.
- get_candidate_neighbor_(bee, bee_idx, population)[source]ď
Generates a candidate neighbor solution for a given bee based on the chosen mutation strategy.
- Parameters:
bee (Bee) â The bee for which a candidate neighbor is generated.
bee_idx (int) â The index of the bee in the population.
population (list) â The population of bees from which donors are selected (i.e., employed or onlooker bees).
- Returns:
A new candidate bee solution.
- Return type:
- get_donor_bees_(n_donors=1, bee_idx=None, population=None)[source]ď
Selects donor bees from the population (for a given bee)
- Parameters:
n_donors (int, optional) â The number of donor bees to return. Defaults to 1.
bee_idx (int, optional) â The index of the bee for which donors are selected. Defaults to None.
population (list[Bee], optional) â The population of bees from which donors are selected. Defaults to None.
- Returns:
A list of donor bees (as Bee instances).
- Return type:
list
- onlookers_phase_()[source]ď
Performs the onlooker bees phase, where onlookers exploit the information shared by employed bees to explore the search space.
Note
Updates employed bees with better solutions discovered by onlookers. Adjusts the scaling factor if self-adaptive scaling is enabled.
- optimize(max_iters=1000, limit='default', selection='RouletteWheel', mutation='StandardABC', initialization='random', tournament_size=None, stagnation_tol=-inf, sf=1.0, self_adaptive_sf=False, mr=1.0, verbose=False, random_seed=None)[source]ď
Runs the optimization process.
- Parameters:
max_iters (int, optional) â The maximum number of iterations. Defaults to 1000.
limit (int or str, optional) â The trial limit for scout bees. If âdefaultâ, it is set to 0.6 * n_employed_bees * dimensionality. Defaults to âdefaultâ.
selection (str, optional) â The selection strategy for onlooker bees. Must be one among âRouletteWheelâ and âTournamentâ. Defaults to âRouletteWheelâ.
mutation (str, optional) â The mutation strategy. Must be one among âStandardABCâ, âModifiedABCâ, âABC/best/1â and âABC/best/2â. Defaults to âStandardABCâ.
initialization (str, optional) â The initialization strategy for the bee population. Must be one among ârandomâ and âcahoticâ. Defaults to ârandomâ.
tournament_size (int, optional) â The size of the tournament for the âTournamentâ selection strategy. Defaults to None.
stagnation_tol (float, optional) â The tolerance for stagnation in fitness values to trigger early termination. Defaults to np.NINF (i.e. stagnation disabled).
sf (float, optional) â The scaling factor for mutations. Defaults to 1.0.
self_adaptive_sf (bool, optional) â Whether to use a self-adaptive scaling factor. Defaults to False.
mr (float, optional) â The mutation rate for âModifiedABCâ strategy. Defaults to 1.0.
verbose (bool, optional) â Whether to display optimization progress. Defaults to False.
random_seed (int, optional) â The seed for random number generation. Defaults to None.
- Raises:
ValueError â If max_iters is not a positive integer.
ValueError â If mutation is not one of [âStandardABCâ, âModifiedABCâ, âABC/best/1â, âABC/best/2â, âDirectedABCâ].
ValueError â If initialization is not one of [ârandomâ, âcahoticâ].
ValueError â If selection is not one of [âRouletteWheelâ, âTournamentâ].
ValueError â If mr is not a float value between 0.0 and 1.0.
ValueError â If selection is âTournamentâ and tournament_size is not an integer between 1 and n_employed_bees.
ValueError â If sf is not a float value greater than 0.
ValueError â If limit is less than or equal to 0.
TypeError â If self_adaptive_sf is not a boolean.
TypeError â If stagnation_tol is not a float.
- scouts_phase_()[source]ď
Performs the scout bees phase, where employed bees that exceed the trial limit are forced to explore a new solution
Note
Depending on the initialization strategy, scouts are reinitialized either randomly or using a chaotic map.
- update_SF_(succesful_mutations_ratio)[source]ď
Updates the scaling factor (SF) adaptively based on the ratio of successful mutations
- Parameters:
successful_mutations_ratio (float) â The ratio of successful mutations used to update the scaling factor.
Note
Self-adaptiveness is based on the âone-fifthâ rule (Rechenberg 1971)