porcupy.base

Classes

Optimizer

Base class for optimization algorithms in Porcupy.

Module Contents

class porcupy.base.Optimizer(dimensions: int, bounds: Tuple[numpy.ndarray, numpy.ndarray], pop_size: int = 30, max_iter: int = 100, options: Dict[str, Any] | None = None, ftol: float = -np.inf, ftol_iter: int = 1)[source]

Bases: abc.ABC

Base class for optimization algorithms in Porcupy.

This abstract base class provides the foundation for all optimization algorithms in the Porcupy library. It defines the common interface and functionality that all optimizers should implement.

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 in the population (default: 30).

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

  • options (dict, optional) – A dictionary containing algorithm-specific parameters.

  • 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).

dimensions[source]
bounds[source]
pop_size = 30[source]
max_iter = 100[source]
options = None[source]
ftol[source]
ftol_iter = 1[source]
class ToHistory[source]

Bases: tuple

best_cost[source]
mean_cost[source]
position[source]
population_size[source]
reset()[source]

Reset the attributes of the optimizer

This method reinitializes all history tracking attributes and prepares the optimizer for a new optimization run.

abstract optimize(objective_func: Callable, n_processes: int | None = None, verbose: bool = False, **kwargs) Tuple[numpy.ndarray, float, numpy.ndarray][source]

Optimize the objective function

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

  • 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