# Towards Bio-Inspired Anomaly Detection Using the Cursory Dendritic Cell Algorithm
## 2 Artificial Immune Systems
### Clonal Selection Algorithm
#### 2.2.2 Algorithm Overview  
The clonal selection hypothesis is the inspiration of the CSA, especially the selection of _B cells_ based on _antigen_ affinity, selective cloning of chosen _B cells_, and subsequent mutation and differentiation of selected _B cells_ into _memory cells_. Although relevant in the context of clonal selection theory, differentiation into _plasma cells_ is not implemented in this subgroup's algorithms. The main premise of these algorithms is to choose the greatest (or lowest) affinity, store them in a pool, and develop new candidate solutions based on modest modifications to the prior best solutions. Candidate solutions are analogous to _B cells_ or, more directly, the _antibodies_ they generate. The poorest solutions are eliminated, leaving just the best possibilities. However, additional candidates can be produced to allow the algorithm to explore more options. CSA is similar to Genetic Algorithms (GA) in that they both follow [PERSON]'s \"survival of the fittest\" principle, in which the most robust solutions survive to the next generation. However, they differ since CSA clones are mutated solutions between generations. Algorithms from this branch are generally related to function optimization, and pattern recognition since the method seeks to maximize/minimize the entire population's affinity [16]. Algorithm 2 describes how the population of the CSA is created and chosen over time. ```
Input:\(N\) population size \(n\) number of antibodies to clone \(d\) number of new antibodies to be generated each iteration Output:\(P\) best population \(P\leftarrow\textit{generate\_antibodies}(N)\) whilenot stop_condition()do foreach\(p\) in \(P\)do \(update\_affinity(p)\)  end foreach\(p^{\prime}\gets select(P,n)\)foreach\(p^{\prime}\) in \(P^{\prime}\)do \(C\leftarrow\textit{clone}(p^{\prime})\)  end foreach\(c\) in \(C\)do \(hypermutate(c)\)  end foreach\(c\) in \(C\)do \(update\_affinity(c)\)  end foreach\(P\leftarrow\textit{insert}(C,n)\) \(P_{new}\leftarrow\textit{generate\_antibodies}(d)\) \(P\leftarrow\textit{replace}(P,Pr)\)  end foreach
```  
**Algorithm 2**General Clonal Selection Algorithm (CSA), adapted from [17]. According to the desired application scenario, the _antibody_ concept must be defined in order to implement CSA, i.e., how _antibodies_ symbolize potential solutions in the realm of the problem. Because CSA has many uses, defining what a \"candidate solution\" means is difficult--for example, a bit-string to match patterns, or a collection of parameters to apply to a problem (such as hyperparameters for a machine learning model, or scheduling times for scheduling problems). Given the structure of the _antibody_, the following must also be defined:  
* _Affinity_: This considers how to rank candidate _antibodies_ for selection based on affinity. In general, stronger affinity leads to better solutions, yet in problems involving minimizing a cost function, higher affinity might lead to worse solutions. For example, the number of matches of a certain bit-string to a target set of bit-strings, or the accuracy of a machine learning model utilizing a particular set of hyperparameters. * _Mutation_: This is related to how to change _antibodies_ slightly through the mutation of bit-flips in bit-strings, for example, or adding/multiplying a random real-valued integer to a single random hyperparameter of a machine learning model are examples of a \"mutation\". * _Generation_: Making new _antibodies_ is referred to as \"generation\". Random bit-strings, for example, or hyperparameter sampling at random from a parameter sweep (also known as grid search) search space. * _Selection_: This is related to how to choose which _antibodies_ will survive. In the algorithm, selection can take several forms, including which _antibodies_ are chosen to be cloned, which are chosen to survive, and which are replaced with newly created _antibodies_. The selection process is usually performed by simply selecting the highest-ranking _antibodies_ based on affinity.