Click here to Skip to main content
Click here to Skip to main content

Artificial Immune Algorithm in C#

, 6 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
How to develop a C# immune algorithm

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.

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))2

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.

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)

Share

About the Author

Alan Crispin

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionClonal Selection Pinmemberincorrect member30-May-12 23:26 
Generalwant a good code and Document (for EXPLANATION the code) for Artificial Immune Pinmembermohammad938122-Mar-11 7:54 
GeneralRe: want a good code and Document (for EXPLANATION the code) for Artificial Immune Pinmemberm_co222-Mar-11 8:10 
GeneralRe: want a good code and Document (for EXPLANATION the code) for Artificial Immune PinmvpRichard MacCutchan22-Mar-11 14:37 
GeneralRe: want a good code and Document (for EXPLANATION the code) for Artificial Immune Pinmemberm_co222-Mar-11 22:04 
GeneralArtificial immune system C++ code PinmemberKHAMISH30-Sep-10 1:15 
GeneralRe: Artificial immune system C++ code Pinmemberpuput3112828-Apr-12 23:38 
GeneralMatlab code PinmemberAMR NADA8-Dec-09 18:31 
Generalin java Pinmemberleavetrace9-Oct-09 20:14 
GeneralC++ builder Pinmembermouass4-Jun-09 14:12 
GeneralRe: C++ builder PinmemberAlan Crispin5-Jun-09 20:05 
GeneralRe: C++ builder Pinmembermouass7-Jun-09 14:44 
QuestionClonal Selection and Genetic Integration Pinmemberkhaledcs9-Jan-09 4:24 
QuestionClassification Pinmemberkhaledcs8-Oct-08 10:16 
AnswerRe: Classification PinmemberAlan Crispin15-Feb-09 2:33 
GeneralRe: Classification Pinmemberkhaledcs2-Mar-09 19:32 
GeneralRe: Classification Pinmemberymadhu3-Mar-11 20:10 
Questionantigen equation Pinmemberkhaledcs11-Sep-08 18:41 
QuestionHow does this fundamentally differ from Genetic Algorithms? PinmemberAdrian Akison8-Jul-08 1:59 
AnswerRe: How does this fundamentally differ from Genetic Algorithms? PinmemberAlan Crispin8-Jul-08 7:30 
GeneralRe: How does this fundamentally differ from Genetic Algorithms? PinmemberMartin Lercher10-Jul-08 10:23 
GeneralRe: How does this fundamentally differ from Genetic Algorithms? PinmemberAlan Crispin10-Jul-08 12:36 
QuestionWhat's in it for me? PinmemberDonkey Master7-Jul-08 2:13 
AnswerRe: What's in it for me? [modified] PinmemberAlan Crispin7-Jul-08 9:38 
GeneralSource Please! Pinmembersam.hill6-Jul-08 20:21 
GeneralRe: Source Please! [modified] PinmemberAlan Crispin7-Jul-08 8:21 
Generalthis is a cool article, but would like to see the source Pinmemberkroach6-Jul-08 18:18 
GeneralRe: this is a cool article, but would like to see the source [modified] PinmemberAlan Crispin7-Jul-08 9:42 
GeneralSource code missing PinmemberTony Bermudez6-Jul-08 16:59 
GeneralRe: Source code missing PinmemberAlan Crispin7-Jul-08 9:43 
GeneralWhy is it all bold PinmvpColin Angus Mackay6-Jul-08 14:52 
GeneralRe: Why is it all bold PinmemberHari Om Prakash Sharma7-Jul-08 0:14 
GeneralRe: Why is it all bold PinmvpColin Angus Mackay7-Jul-08 0:20 
GeneralRe: Why is it all bold PinmemberHari Om Prakash Sharma7-Jul-08 0:35 
GeneralRe: Why is it all bold PinmemberHari Om Prakash Sharma7-Jul-08 0:36 
GeneralRe: Why is it all bold PinmvpColin Angus Mackay7-Jul-08 0:45 
GeneralRe: Why is it all bold PinmvpJosh Smith7-Jul-08 5:02 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150123.1 | Last Updated 6 Jul 2008
Article Copyright 2008 by Alan Crispin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid