porcupy.porcupines

Classes

PorcupinePopulation

Class representing a population of porcupines for the Crested Porcupine Optimizer.

DefenseMechanisms

Class implementing the four defense mechanisms of the Crested Porcupine Optimizer.

PopulationManager

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.

pop_size[source]

Current number of porcupines in the population.

Type:

int

dimensions[source]

Number of dimensions in the search space.

Type:

int

bounds[source]

Bounds for the search space.

Type:

tuple of numpy.ndarray

positions

Current positions of all porcupines, shape (pop_size, dimensions).

Type:

numpy.ndarray

fitness[source]

Current fitness values of all porcupines, shape (pop_size,).

Type:

numpy.ndarray

personal_best_pos[source]

Personal best positions found by each porcupine, shape (pop_size, dimensions).

Type:

numpy.ndarray

personal_best_fitness[source]

Personal best fitness values for each porcupine, shape (pop_size,).

Type:

numpy.ndarray

best_pos[source]

Global best position found so far, shape (dimensions,).

Type:

numpy.ndarray

best_fitness[source]

Global best fitness value found so far.

Type:

float

pop_size[source]
dimensions[source]
bounds[source]
fitness[source]
personal_best_pos[source]
personal_best_fitness[source]
best_pos = None[source]
best_fitness[source]
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

resize(new_size: int) None[source]

Resize the population to a new size

Parameters:

new_size (int) – New population size

apply_bounds() None[source]

Apply boundary constraints to all positions

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:
  • alpha (float) – Convergence rate for the fourth defense mechanism.

  • tf (float) – Tradeoff threshold between third and fourth mechanisms.

alpha[source]

Convergence rate for the fourth defense mechanism.

Type:

float

tf[source]

Tradeoff threshold between third and fourth mechanisms.

Type:

float

alpha = 0.2[source]
tf = 0.8[source]
sight_defense(position: numpy.ndarray, other_position: numpy.ndarray, best_position: numpy.ndarray) numpy.ndarray[source]

Apply the first defense mechanism (sight)

Parameters:
Returns:

Updated position

Return type:

numpy.ndarray

sound_defense(position: numpy.ndarray, other_position: numpy.ndarray, rand_diff: numpy.ndarray) numpy.ndarray[source]

Apply the second defense mechanism (sound)

Parameters:
Returns:

Updated position

Return type:

numpy.ndarray

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:

numpy.ndarray

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:

numpy.ndarray

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:
  • initial_pop_size (int) – Initial population size.

  • min_pop_size (int) – Minimum population size during reduction.

  • max_iter (int) – Maximum number of iterations.

  • cycles (int) – Number of cycles for population reduction.

initial_pop_size[source]

Initial population size.

Type:

int

min_pop_size[source]

Minimum population size during reduction.

Type:

int

max_iter[source]

Maximum number of iterations.

Type:

int

cycles[source]

Number of cycles for population reduction.

Type:

int

initial_pop_size[source]
min_pop_size[source]
max_iter[source]
cycles = 2[source]
calculate_pop_size(iteration: int) int[source]

Calculate the population size for the current iteration

Parameters:

iteration (int) – Current iteration number

Returns:

Population size for the current iteration

Return type:

int