porcupy.porcupines
Classes
Class representing a population of porcupines for the Crested Porcupine Optimizer. |
|
Class implementing the four defense mechanisms of the Crested Porcupine Optimizer. |
|
Class for managing the population dynamics in the Crested Porcupine Optimizer. |
Module Contents
- class porcupy.porcupines.PorcupinePopulation(pop_size: int, dimensions: int, bounds: Tuple[numpy.ndarray, numpy.ndarray], init_pos: numpy.ndarray | None = None)[source]
Class representing a population of porcupines for the Crested Porcupine Optimizer.
This class handles the population structure, initialization, and provides methods for population management and dynamics.
- Parameters:
pop_size (int) – Number of porcupines in the population.
dimensions (int) – Number of dimensions in the search space.
bounds (tuple of numpy.ndarray) – A tuple of size 2 where the first entry is the minimum bound while the second entry is the maximum bound. Each array must be of shape (dimensions,).
init_pos (numpy.ndarray, optional) – Initial positions for the porcupines. If None, random positions are generated.
- positions
Current positions of all porcupines, shape (pop_size, dimensions).
- Type:
- personal_best_pos[source]
Personal best positions found by each porcupine, shape (pop_size, dimensions).
- Type:
- personal_best_fitness[source]
Personal best fitness values for each porcupine, shape (pop_size,).
- Type:
- evaluate(objective_func: Callable, **kwargs) None[source]
Evaluate the fitness of all porcupines
- Parameters:
objective_func (callable) – The objective function to minimize
**kwargs – Additional arguments for the objective function
- class porcupy.porcupines.DefenseMechanisms(alpha: float = 0.2, tf: float = 0.8)[source]
Class implementing the four defense mechanisms of the Crested Porcupine Optimizer.
This class provides methods for the four defense mechanisms: sight, sound, odor, and physical attack, which are used to update the positions of porcupines.
- Parameters:
- sight_defense(position: numpy.ndarray, other_position: numpy.ndarray, best_position: numpy.ndarray) numpy.ndarray[source]
Apply the first defense mechanism (sight)
- Parameters:
position (numpy.ndarray) – Current position of the porcupine
other_position (numpy.ndarray) – Position of another random porcupine
best_position (numpy.ndarray) – Global best position
- Returns:
Updated position
- Return type:
- sound_defense(position: numpy.ndarray, other_position: numpy.ndarray, rand_diff: numpy.ndarray) numpy.ndarray[source]
Apply the second defense mechanism (sound)
- Parameters:
position (numpy.ndarray) – Current position of the porcupine
other_position (numpy.ndarray) – Position of another random porcupine
rand_diff (numpy.ndarray) – Random difference vector
- Returns:
Updated position
- Return type:
- odor_defense(position: numpy.ndarray, other_position: numpy.ndarray, rand_diff: numpy.ndarray, fitness: float, fitness_sum: float, t: int, max_iter: int) numpy.ndarray[source]
Apply the third defense mechanism (odor)
- Parameters:
position (numpy.ndarray) – Current position of the porcupine
other_position (numpy.ndarray) – Position of another random porcupine
rand_diff (numpy.ndarray) – Random difference vector
fitness (float) – Current fitness of the porcupine
fitness_sum (float) – Sum of all fitness values in the population
t (int) – Current iteration
max_iter (int) – Maximum number of iterations
- Returns:
Updated position
- Return type:
- physical_attack(position: numpy.ndarray, other_position: numpy.ndarray, best_position: numpy.ndarray, fitness: float, fitness_sum: float, t: int, max_iter: int) numpy.ndarray[source]
Apply the fourth defense mechanism (physical attack)
- Parameters:
position (numpy.ndarray) – Current position of the porcupine
other_position (numpy.ndarray) – Position of another random porcupine
best_position (numpy.ndarray) – Global best position
fitness (float) – Current fitness of the porcupine
fitness_sum (float) – Sum of all fitness values in the population
t (int) – Current iteration
max_iter (int) – Maximum number of iterations
- Returns:
Updated position
- Return type:
- class porcupy.porcupines.PopulationManager(initial_pop_size: int, min_pop_size: int, max_iter: int, cycles: int = 2)[source]
Class for managing the population dynamics in the Crested Porcupine Optimizer.
This class handles the cyclic population reduction strategy and other population management tasks.
- Parameters: