Click here to Skip to main content
15,891,880 members
Articles / Desktop Programming / Windows Forms

Artificial Immune Algorithm in C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (12 votes)
6 Jul 2008CPOL6 min read 112.1K   2.3K   36   37
How to develop a C# immune algorithm
Image 1

Introduction

Artificial immune algorithms are inspired by the principles and processes of biological immune systems. This article shows how to develop a simple C# immune algorithm which models clonal selection, affinity maturation, and antibody interaction.

Background

Our immune system protects us from pathogenic organisms including bacteria, viruses, and toxins. It provides a number of levels of defence, with the skin being the first barrier to infection. Once pathogens have entered the body, they are handled with the innate immune system, and then by the acquired immune response system (see Fig 1.). The innate immune system provides protection that is always present. It uses roaming scavenger cells (phagocytes) to ingest abnormal cells, thus clearing the system.

Image 2

Fig 1. Levels of defence in the immune system.

The acquired immune system provides protection that arises from an immune response. Substances capable of starting a specific immune response are called antigens. Nearly all pathogens are antigens. Antibodies are used by the immune system to identify and neutralise antigens (foreign bodies), and are produced from a kind of white blood cell called a B-cell.

Clonal selection models the production of antibodies which bind to specific antigens. The binding process of receptors is often likened to a key-lock mechanism. The probability of forming a bond (i.e., match strength) is referred to as affinity. Clonal selection establishes the idea that only those antibodies that recognise the antigens are selected for cloning and mutation. Cloned antibodies mutate in inverse proportion to how well they match the antigen so that better matching antigens are mutated less and weakly matching antibodies are mutated much more. This process is called affinity maturation, and improves the likelihood of antibodies forming a bond. Immune models also have to take into account the activity resulting from interactions between antibodies.

Optimisation by Immune Algorithm

From a computer science perspective, modeling the behaviour of the acquired immune system can provide potential new ways of solving problems. The function optimisation problem involves finding the best solution (either the peak or trough) to a function bounded by constraints.

In the example code, a test function has been used and is plotted in Fig. 2. This test function uses the sine function to produce the hilly plot, and so finding the highest peak is a challenge. The equation is given by:

Fitness = (15*x*y*(1-x)*(1-y)*sin(9*pi*x)*sin(9*pi*y))<sup>2</sup>

This is the function used by Barry Lapthorn in his article entitled “A simple C# genetic algorithm” located on the CodeProject website. The optimal solution for this problem is 0.879 at x=0.5 and y=0.5, which is the highest peak at the centre of the plot. Coincidently, the plot has been produced using SciLab, which is a free numerical computation package.

How can a clonal selection model be used to solve this optimisation problem? Well, if we consider an antibody as a potential solution (i.e., a cell object which has an x-y value) and the fitness function as the antigen, then the degree of fit or binding represents the quality of the solution. If we start with an initial population of antibody solutions and test them against the fitness function (antigen), then those with the highest affinity (i.e., best fit) are allowed to clone and mutate in the hope of finding a better solution.

Image 3

Fig. 2. The test function (the highest peak solution is found at x=0.5 and y=0.5).

Consequently, an immune algorithm can be devised as follows:

  1. Generate an initial population of antibodies.
  2. Perform clonal selection to generate high affinity clones and mutate.
  3. Remove antibodies whose affinity with the antigen is less than a predefined threshold.
  4. Calculate affinity interactions between all antibodies in the system.
  5. Remove antibodies whose affinity with other antibodies is below a predefined threshold.
  6. Introduce randomly generated antibodies into population (diversity).
  7. Repeat steps 2 to 6 until the stopping criteria is met.

Algorithm

The code contains four essential classes. These are:

  • The main form class which provides a simple GUI
  • The immune algorithm controller class
  • The antibody class
  • The fitness function class

The code has been commented and so should be straightforward to follow. I have used code snippets found on AISWeb (see link below) to develop the clonal selection operators. Particularly helpful was the code example contributed by Paul Andrews.

The fitness function class has a static method called evaluateFunction() which returns the fitness value given x and y input values. The antibody class attempts to model a biological antibody cell. It has methods for cloning itself, finding the affinity with another antibody and an affinity based mutation. Each antibody represents a candidate solution, which in this example is simply an x-y value. The immune algorithm controller class allows parameters to be defined, and has a method called GoOptimise() which creates an initial population of antibodies and iterates a solution until the stopping criteria (a maximum number of generations) is reached.

Using the Code

The antibodies (candidate solutions to the function optimisation problem) that are generated by the immune algorithm are displayed in a text box. The best antibody is displayed first, and should be a good match to the required solution for this problem which, as stated above, is 0.879 at x=0.5 and y=0.5. An immune algorithm is a non-deterministic algorithm, meaning that it gives different results on different runs.

There is plenty of scope for experimenting with parameter settings, which are currently set as below in the immune algorithm controller class. It is necessary to set threshold values for removing (suppressing) antibodies from the population pool (clonalSelectionThreshold, removeThreshold). The settings used for these threshold values were derived by a process of trial and error. The parameter called antibodyNumber determines the initial number of antibodies used to solve the problem or, in biological terms, neutralise the antigen. This has been set to 50. The cloneNumber parameter sets the number of clones generated during clonal selection. Affinity based mutation is set using the mutationFactor parameter. Constraints for the x and y values are imposed using the lowerBoundary and upperBoundary parameters.

  • antibodyNumber = 50;
  • cloneNumber = 20;
  • maxGens = 600;
  • mutationFactor = 80;
  • removeThreshold = 0.2;
  • clonalSelectionThreshold = 0.01;
  • diversity = 0.5;
  • lowerBoundary = 0.0;
  • upperBoundary = 1.0;

Points of Interest

The code was written using SharpDevelop2.2, but can also be compiled using Microsoft Visual Studio Express. Some initial coding difficulties relating to object cloning and a compile error which occurred when using a random seed derived from System.DateTime.Now.Ticks were encountered. Object cloning was solved by using the ICloneable interface which requires that a Clone() method is written.

Summary

There are many articles on the web which provide more information regarding immune algorithms than covered in this introduction. Some links are given below, but Googling the term “artificial immune system” will yield many more. The article has illustrated some of the main concepts of immune algorithms including clonal selection, affinity maturation, and antibody interaction.

There is potential for further investigation. It would be interesting to look into the discrimination between antibodies destined to be deleted and those not, and new types of operators for cloning and mutation. If you wish to explore immune algorithms further, I suggest your next step is to visit the AISWeb using the link below.

Links

History

  • July 2008 - First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionClonal Selection Pin
incorrect member30-May-12 22:26
incorrect member30-May-12 22:26 
Generalwant a good code and Document (for EXPLANATION the code) for Artificial Immune Pin
mohammad938122-Mar-11 6:54
mohammad938122-Mar-11 6:54 
GeneralRe: want a good code and Document (for EXPLANATION the code) for Artificial Immune Pin
my name is coder ! ! !22-Mar-11 7:10
my name is coder ! ! !22-Mar-11 7:10 
GeneralRe: want a good code and Document (for EXPLANATION the code) for Artificial Immune Pin
Richard MacCutchan22-Mar-11 13:37
mveRichard MacCutchan22-Mar-11 13:37 
GeneralRe: want a good code and Document (for EXPLANATION the code) for Artificial Immune Pin
my name is coder ! ! !22-Mar-11 21:04
my name is coder ! ! !22-Mar-11 21:04 
GeneralArtificial immune system C++ code Pin
KHAMISH30-Sep-10 0:15
KHAMISH30-Sep-10 0:15 
GeneralRe: Artificial immune system C++ code Pin
puput3112828-Apr-12 22:38
puput3112828-Apr-12 22:38 
GeneralMatlab code Pin
AMR NADA8-Dec-09 17:31
AMR NADA8-Dec-09 17:31 
Generalin java Pin
leavetrace9-Oct-09 19:14
leavetrace9-Oct-09 19:14 
GeneralC++ builder Pin
mouass4-Jun-09 13:12
mouass4-Jun-09 13:12 
GeneralRe: C++ builder Pin
Alan Crispin5-Jun-09 19:05
Alan Crispin5-Jun-09 19:05 
GeneralRe: C++ builder Pin
mouass7-Jun-09 13:44
mouass7-Jun-09 13:44 
QuestionClonal Selection and Genetic Integration Pin
khaledcs9-Jan-09 3:24
khaledcs9-Jan-09 3:24 
QuestionClassification Pin
khaledcs8-Oct-08 9:16
khaledcs8-Oct-08 9:16 
AnswerRe: Classification Pin
Alan Crispin15-Feb-09 1:33
Alan Crispin15-Feb-09 1:33 
Classification using immune algorithm

Sorry for not replying earlier but I have been busy with other things.

In general, there are two main categories of Artificial Immune Systems namely negative selection and clonal selection. Negative selection finds applications in pattern recognition and classification while clonal selection can be used to solve optimisation problems as discussed in the article.

Negative selection algorithms attempt to model the negative selection process of T Cells. This involves a number of steps as outlined below.

1. Self Training
2. Search
3. Identification
4. Elimination

Immune cognition is based on the premise that the immune system is capable of distinguishing between what belongs to an organism, known as self, and what does not, known as non-self. Consequently the self training step involves destroying T Cells which react with self proteins so that only non-self T Cells are allowed to survive. The surviving T Cells circulate through the body in search of invading antigens (the search step). In the identification step invading antigens that match the surviving T cells are identified and marked as non-self foreign bodies. These marked pathogens are then destroyed by the immune system.

In pattern recognition problems you usually have some examples of data values and the correct decisions for them and you want to classify a set of future data values in which the decision of each is unknown.

For example, a pattern could simply be a list of five characters. The self set could be a population five character names (e.g. james, jenny, jodie, sarah, carol, malik, benny, peter, elvis etc. etc.). The non-self pattern set or negative set would define abnormal behaviour and would have to be generated using the self set by negative selection. This can be done by generating thousands of five character sequences and destroying those which match the self set and keeping those which do not. So for example ‘britx’ which does not match the names set would be in the abnormal set. Here I am assuming that you wouldn’t call anyone ‘britx’ and so it would not be in the names set representing the self set.

In this problem the search step would involve taking an unknown five character string and matching it against the negative set given some criteria (i.e. 4 character matches) to test if it is abnormal.

To start coding this problem you need to develop a pattern class and then use two ArrayLists for storing the self set and the abnormal set. Then you have to write a method called, say, MatchNegativeSet which would take a pattern as a parameter and return a Boolean result. This result would be true if the unknown pattern matches the abnormal set.

In fact I quickly put some code together to test the concept and I was pleased to find it was effective at finding (recognising) abnormal patterns.

Hopefully this has answered your various questions.

Alan crispin (15-02-09)
GeneralRe: Classification Pin
khaledcs2-Mar-09 18:32
khaledcs2-Mar-09 18:32 
GeneralRe: Classification Pin
ymadhu3-Mar-11 19:10
ymadhu3-Mar-11 19:10 
Questionantigen equation Pin
khaledcs11-Sep-08 17:41
khaledcs11-Sep-08 17:41 
QuestionHow does this fundamentally differ from Genetic Algorithms? Pin
Adrian Akison8-Jul-08 0:59
professionalAdrian Akison8-Jul-08 0:59 
AnswerRe: How does this fundamentally differ from Genetic Algorithms? Pin
Alan Crispin8-Jul-08 6:30
Alan Crispin8-Jul-08 6:30 
GeneralRe: How does this fundamentally differ from Genetic Algorithms? Pin
Martin Lercher10-Jul-08 9:23
Martin Lercher10-Jul-08 9:23 
GeneralRe: How does this fundamentally differ from Genetic Algorithms? Pin
Alan Crispin10-Jul-08 11:36
Alan Crispin10-Jul-08 11:36 
QuestionWhat's in it for me? Pin
Donkey Master7-Jul-08 1:13
Donkey Master7-Jul-08 1:13 
AnswerRe: What's in it for me? [modified] Pin
Alan Crispin7-Jul-08 8:38
Alan Crispin7-Jul-08 8:38 
GeneralSource Please! Pin
sam.hill6-Jul-08 19:21
sam.hill6-Jul-08 19:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.