Skip to main content
Version: 0.5.x

Clonalg

Clonal Selection Algorithm (CLONALG).

Inheritance

This class extends BaseOptimizer

Module: aisp.csa
Import: from aisp.csa import Clonalg


Overview

The Clonal Selection Algorithm (CSA) is an optimization algorithm inspired by the biological process of clonal selection and expansion of antibodies in the immune system 1. This implementation of CLONALG has been adapted for the minimization or maximization of cost functions in binary, continuous, ranged-value, and permutation problems.

note

This CLONALG implementation contains some changes based on the AISP context, for general application to various problems, which may produce results different from the standard or specific implementation. This adaptation aims to generalize CLONALG to minimization and maximization tasks, in addition to supporting continuous, discrete, and permutation problems.


Example

import numpy as np
from aisp.csa import Clonalg
# Search space limits
bounds = {'low': -5.12, 'high': 5.12}
# Objective function
def rastrigin_fitness(x):
x = np.clip(x, bounds['low'], bounds['high'])
return 10 * len(x) + np.sum(x**2 - 10 * np.cos(2 * np.pi * x))
# CLONALG Instance
clonalg = Clonalg(problem_size=2, bounds=bounds, seed=1)
clonalg.register('affinity_function', rastrigin_fitness)
population = clonalg.optimize(100, 50, False)
print('Best cost:', abs(clonalg.best_cost))

Output:

Best cost: 0.02623036956750724

Constructor Parameters

NameTypeDefaultDescription
problem_sizeint-Dimension of the problem to be minimized.
Nint50Number of memory cells (antibodies) in the population.
rate_clonalint10Maximum number of possible clones of a cell. This value is multiplied by cell_affinity to determine the number of clones.
rate_hypermutationfloat1.0Hypermutation rate controls the intensity of mutations during clonal expansion. Higher values decrease mutation intensity, while lower values increase it.
n_diversity_injectionint5Number of new random memory cells injected to maintain diversity.
selection_sizeint5Number of the best antibodies selected for cloning.
affinity_functionOptional[Callable[..., npt.NDArray]]NoneObjective function to evaluate candidate solutions in minimizing the problem.
feature_typeFeatureTypeAll'ranged-features'Type of problem samples: binary, continuous, or based on value ranges.
boundsOptional[Dict]NoneDefinition of search limits when feature_type='ranged-features'.
mode{"min", "max"}'min'Defines whether the algorithm minimizes or maximizes the cost function.
seedintNoneSeed for random generation.

Attributes

NameTypeDefaultDescription
populationOptional[List[Antibody]]NonePopulation of antibodies.

Public Methods

optimize

def optimize(
self, max_iters: int = 50, n_iter_no_change=10, verbose: bool = True
) -> List[Antibody]:
...

Execute the optimization process and return the population.

Parameters

NameTypeDefaultDescription
max_itersint50Maximum number of iterations when searching for the best solution using clonalg.
n_iter_no_changeint10The maximum number of iterations without updating the best cell.
verboseboolTrueFeedback on iterations, indicating the best antibody.

Returns

TypeDescription
List[Antibody]Antibody population after clonal expansion.

Raises

ExceptionDescription
NotImplementedErrorIf no affinity function has been provided to evaluate candidate solutions.

affinity_function

def affinity_function(self, solution: npt.NDArray) -> np.float64:
...

Evaluate the affinity of a candidate cell.

Parameters

NameTypeDefaultDescription
solutionnpt.NDArray-Candidate solution to evaluate.

Returns

TypeDescription
np.float64Affinity value associated with the given cell.

Raises

ExceptionDescription
NotImplementedErrorIf no affinity function has been provided.

Extended Example

Complete usage examples are available in the Jupyter Notebooks:


References

Footnotes

  1. BROWNLEE, Jason. Clonal Selection Algorithm. Clever Algorithms: Nature-inspired Programming Recipes., 2011. Available at: https://cleveralgorithms.com/nature-inspired/immune/clonal_selection_algorithm.html