ArtificialBeeColony

class beeoptimal.abc.ArtificialBeeColony(colony_size, function, bounds, n_employed_bees=None, max_scouts=None)[source]

Bases: object

Artificial 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

employed_bees

The employed bees in the colony.

Type:

list[Bee]

onlooker_bees

The onlooker bees in the colony.

Type:

list[Bee]

colony_history

The history of the employed bees at each iteration.

Type:

list[list[Bee]]

optimal_bee

The optimal bee in the colony.

Type:

Bee

optimal_bee_history

The history of the optimal bee at each iteration.

Type:

list[Bee]

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:

Bee

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.

reset()[source]

Resets the ABC object to its initial state.

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)

waggle_dance_()[source]

Implements the waggle dance, which determines the probability of selecting employed bees for the onlooker phase.

Returns:

Indices of the selected employed bees (based on the chosen selection strategy).

Return type:

array