porcupy.cpo_class

Classes

CPO

CPO (Crested Porcupine Optimizer) for optimization problems.

Module Contents

class porcupy.cpo_class.CPO(dimensions: int, bounds: Tuple[numpy.ndarray, numpy.ndarray], pop_size: int = 30, min_pop_size: int | None = None, max_iter: int = 100, cycles: int = 2, alpha: float = 0.2, tf: float = 0.8, ftol: float = -np.inf, ftol_iter: int = 1)[source]

Bases: porcupy.base.Optimizer

CPO (Crested Porcupine Optimizer) for optimization problems.

This class implements the CPO algorithm, a nature-inspired metaheuristic that mimics the defensive behaviors of crested porcupines (sight, sound, odor, physical attack) to balance exploration and exploitation, with cyclic population reduction for convergence.

Parameters:
  • 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,).

  • pop_size (int, optional) – Number of search agents (porcupines) in the initial population (default: 30).

  • min_pop_size (int, optional) – Minimum population size during reduction cycles (default: pop_size // 2).

  • max_iter (int, optional) – Maximum number of iterations (default: 100).

  • cycles (int, optional) – Number of cycles for population reduction (default: 2).

  • alpha (float, optional) – Convergence rate for fourth defense mechanism (default: 0.2).

  • tf (float, optional) – Tradeoff threshold between third and fourth mechanisms (default: 0.8).

  • ftol (float, optional) – Relative error in objective_func(best_pos) acceptable for convergence (default: -np.inf).

  • ftol_iter (int, optional) – Number of iterations over which the relative error in objective_func(best_pos) is acceptable for convergence (default: 1).

min_pop_size[source]

Minimum population size during reduction cycles.

Type:

int

cycles[source]

Number of cycles for population reduction.

Type:

int

alpha[source]

Convergence rate for fourth defense mechanism.

Type:

float

tf[source]

Tradeoff threshold between third and fourth mechanisms.

Type:

float

positions[source]

Current positions of all porcupines in the population.

Type:

ndarray

fitness[source]

Current fitness values of all porcupines.

Type:

ndarray

personal_best_pos[source]

Personal best positions found by each porcupine.

Type:

ndarray

min_pop_size = None[source]
cycles = 2[source]
alpha = 0.2[source]
tf = 0.8[source]
positions = None[source]
fitness = None[source]
personal_best_pos = None[source]
positions_history = [][source]
defense_types_history = [][source]
pop_size_history = [][source]
best_positions_history = [][source]
fitness_history = [][source]
optimize(objective_func: Callable, f_ieqcons: Callable | None = None, n_processes: int | None = None, verbose: bool = False, track_history: bool = True, **kwargs) Tuple[numpy.ndarray, float, numpy.ndarray][source]

Optimize the objective function using Crested Porcupine Optimizer

Parameters:
  • objective_func (callable) – The objective function to be minimized

  • f_ieqcons (callable, optional) – Constraint function returning a 1D array of inequality constraints (g(x) >= 0)

  • n_processes (int, optional) – Number of processes to use for parallel evaluation

  • verbose (bool, optional) – Whether to display progress information

  • **kwargs – Additional arguments to pass to the objective function

Returns:

A tuple containing (best_position, best_cost, cost_history)

Return type:

tuple