Click here to Skip to main content
11,930,449 members (57,119 online)
Click here to Skip to main content
Add your own
alternative version


205 bookmarked

A Simple C# Genetic Algorithm

, 21 Aug 2003 CDDL
Rate this:
Please Sign up or sign in to vote.
In this article we shall produce a simple genetic algorithm in C#


In this article, we shall produce a simple genetic algorithm in C#. It will not be multi-threaded, nor will it contain exotic operators or convergence criteria (i.e. a condition where many of the solutions found are very similar). It will simply demonstrate a genetic algorithm in managed code, taking advantage of some of the features of the .NET runtime.


A genetic algorithm is an optimization technique that relies on parallels with nature. It can tackle a variety of optimization techniques provided that they can be parameterized in such a way that a solution to the problem provides measure of how accurate the solution found by the algorithm is. This measure we define as fitness.

Genetic algorithms were first conceived in early 1970's (Holland, 1975). The initial idea came from observing how the evolution of biological creatures derives from their constituent DNA and chromosomes. In this sense a simple analogy can be made with a mathematical problem made up of many parameters. Each parameter can take the place of a chromosome in the mathematical analogy of a real chemical sequence.

In nature, evolution is carried out by a process of selection typified by the expression survival of the fittest. In order to select an individual, we need a population of such individuals to choose from to produce a new generation of individuals.

For any problem that we wish to solve, we need some measure of the goodness of the solution, i.e. fitness, often a χ2 (chi-squared) measure, i.e. the better the solution, the higher the fitness returned from out function. The less fit the solutions are, the less likely that they are to survive to a successive population. By employing such a technique, the algorithm can reduce the number of possible solutions that it examines.

Many problems are internally represented in binary by various genetic algorithms. Here we will only consider a decimal representation. The internal representation of a genetic algorithm does not actually matter provided the implementation is thorough (Field, 1995).

The Problem

In our example code, we supply a test function that uses sin and cos to produce the plot below:

The optimal solution for this problem is (0.5,0.5), i.e. the highest peak. We choose this example to demonstrate how a genetic algorithm is not fooled by the surrounding local maxima (i.e. the high peaks).

Test Harness

We start by declaring a new genetic algorithm:

GA ga = new GA(0.8,0.05,100,2000,2);

ga.FitnessFunction = new GAFunction(theActualFunction);

where we the arguments are the crossover rate, mutation rate, population size, number of generations, and number of parameters that we are solving for. We declare the FitnessFunction property as:

public delegate double GAFunction(double[] values);

public class GA
  static private GAFunction getFitness;
  public GAFunction FitnessFunction {  
    // etc.
  //  etc.

This then enables us to declare our fitness function the same as the delegate function:

public static double theActualFunction(double[] values) 
  if (values.GetLength(0) != 2)
  throw new ArgumentOutOfRangeException("should only have 2 args");
   double x = values[0];
   double y = values[1];
   double n = 9; 
   double f1 = Math.Pow(15*x*y*(1-x)*(1-y)*Math.Sin(n*Math.PI*x)
   return f1;

which is therefore accepted by the algorithm. The genetic algorithm is then set to run using:


The genetic algorithm will now run for the supplied number of generations.

The Algorithm

The algorithm code contains two simple classes, GA and Genome, plus a helper class GenomeComparer.

The Genome class can be thought of as a simple container. The underlying structure is an array of doubles within the range of 0 to 1. The user is expected to take these values and scale them to whatever values they require. Since mutation occurs on the genome, the Mutate method is found in this class. The Crossover operator requires access to the private data of the Genome, so it is also a member function which takes a second Genome, and outputs two child Genome objects. The fitness of a particular genome is also stored within the Genome object. There are some additional helper functions that maybe found in the code itself.

The GA class does all the work. The genetic algorithm consists of the following basic steps:

  1. Create a new population
  2. Select two individuals from the population weighting towards the individual that represents the best solution so far.
  3. 'Breed' them to produce children.
  4. If we don't have enough children for a new population return to step 2.
  5. Replace old population with new.
  6. If we have not produced enough generations return to step 2.
  7. We have finished.

When selecting individuals to breed, we use what is called the Roulette wheel method. This is where fitter individuals have a larger proportion of the 'wheel' and are more likely to be chosen. We chose to store the finesses cumulatively in System.Collections.ArrayList as it had some nice features like sorting. Unfortunately, its binary search method was only for exact values, so we had to implement the following work around:

mid = (last - first)/2;

// ArrayList's BinarySearch is for exact values only
// so do this by hand.
while (idx == -1 && first <= last)
  if (randomFitness < (double)m_fitnessTable[mid])
    last = mid;
  else if (randomFitness > (double)m_fitnessTable[mid])
    first = mid;
  mid = (first + last)/2;
  // lies between i and i+1
  if ((last - first) == 1)
     idx = last;

The GenomeComparer class inherits from the IComparer interface. Each generation is stored in a System.Collections.ArrayList, and we wish to sort each generation in order of fitness. We therefore need to implement this interface as follows:

public sealed class GenomeComparer : IComparer
    public GenomeComparer()
    public int Compare( object x, object y)
        if ( !(x is Genome) || !(y is Genome))
            throw new ArgumentException("Not of type Genome");
        if (((Genome) x).Fitness > ((Genome) y).Fitness)
            return 1;
        else if (((Genome) x).Fitness == ((Genome) y).Fitness)
            return 0;
            return -1;

Note that we need to explicitly cast the ArrayList elements back to a Genome type. We also make the class sealed as there is no point inheriting from it.

A Quick Note On Operators

We mentioned briefly, two operators, crossover and mutation, and we shall explain these in a little more detail here.

Crossover simply takes two genomes, splits them at some point and produces two new genomes by swapping the end parts, e.g.

10 20 30 40 50 60 70
80 90 00
10 20 30 40 50 60 70
30 20 10
00 90 80 70 60 50 40
30 20 10
00 90 80 70 60 50 40
80 90 00

The split occurs at a randomly chosen point along the length of the genome, and the split only occurs if a probability test is passed. This is typically set quite high which reflects what happens in Nature.

Mutation, in comparison, happens rarely so the probability that it occurs is set quite low, typically less than 5%. Each gene within the genome is tested in turn to see it is allowed to mutate, and if so it is replaced with a random number, e.g.

10 20 30 40 50 60
80 90
10 20 30 40 50 60
80 90


With our simple example we know that the optimal solution is at (0.5, 0.5), and we find after 250 generations we find a solution extremely close to this (within 4 significant figures). The progress of the GA can be seen below:


Conclusion and Notes.

A genetic algorithm does not provide a magic bullet solution to all minimization/maximization problems.  In many cases other algorithms are faster and more practical.  However for problems with a large parameter space and where the problem itself can be easily specified, it can be an appropriate solution. 

Whilst writing this I learnt several features of C# that I'd like to summarize here (coming from a C++ background):

  • The System.Collections.ArrayList container only does shallow copies.
  • The System.Collections.ArrayList container's binary search only works for exact values.
  • An obvious thing, but simply defining a variable, doesn't necessarily assign it a value.
  • Implementing the IComparer interface is fairly trivial (see GenomeComparer class).

Further improvements to the algorithm can be made by implementing all sorts of weird and wonderful operators. I'm sure someone will be able to tell me an easier way to supply the number of parameters the problem has automatically rather than using the GA constructor and the delegate function.


Field, P., "A Multary Theory for Genetic Algorithms: Unifying Binary and Non-binary Problem Representations", Ph.D. Thesis, 1995, Queen Mary and Westfield College.

Holland, J.H., "Adaption in Natural and Artificial Systems", MIT Press, 1975, (1992 Edition).

Lippman, S.B., "C# Primer, A Practical Approach", Addison-Wesley, 2002, (1st Edition).


The following may be of interest:

  • Wall, M., "GALib - a C++ implementation of a genetic algorithm," MIT, 1999.
  • A Russian version of this article may be found here translated by Vladimir Lioubitelev.


  • 22nd August, 2003 - Update to code.
  • 3rd May, 2003 - Update to conclusion.
  • 15th Mar, 2003 - Update to article text.
  • 16th Nov, 2002 - Update to article text.
  • 6th Nov, 2002 - Initial revision.


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

Barry Lapthorn
United Kingdom United Kingdom
Jack of all trades.

You may also be interested in...

Comments and Discussions

Questionhow to run it?? Pin
Member 1143886118-Feb-15 1:24
memberMember 1143886118-Feb-15 1:24 
GeneralMy vote of 5 Pin
CatchExAs12-Jul-14 4:48
professionalCatchExAs12-Jul-14 4:48 
Questionproblem Pin
Member 1074282611-Apr-14 11:46
memberMember 1074282611-Apr-14 11:46 
AnswerRe: problem Pin
roli.hof21-Jul-14 22:05
memberroli.hof21-Jul-14 22:05 
GeneralRe: problem Pin
Member 1143886118-Feb-15 1:26
memberMember 1143886118-Feb-15 1:26 
GeneralRe: problem Pin
roli.hof18-Feb-15 4:21
professionalroli.hof18-Feb-15 4:21 
GeneralRe: problem Pin
Member 1143886118-Feb-15 5:30
memberMember 1143886118-Feb-15 5:30 
QuestionVisualization question Pin
sscully3147-Jan-14 12:14
membersscully3147-Jan-14 12:14 
AnswerRe: Visualization question Pin
Barry Lapthorn8-Jan-14 8:42
protectorBarry Lapthorn8-Jan-14 8:42 
QuestionSize of Generation ArrayList? Pin
wondernuts9-Jul-13 23:07
memberwondernuts9-Jul-13 23:07 
AnswerRe: Size of Generation ArrayList? Pin
Barry Lapthorn15-Jul-13 1:08
protectorBarry Lapthorn15-Jul-13 1:08 
QuestionSmall bug Pin
Member 98133805-Feb-13 13:01
memberMember 98133805-Feb-13 13:01 
AnswerRe: Small bug Pin
Barry Lapthorn15-Jul-13 1:09
protectorBarry Lapthorn15-Jul-13 1:09 
QuestionMutation biased? Pin
Member 84173078-Jun-12 11:31
memberMember 84173078-Jun-12 11:31 
AnswerRe: Mutation biased? Pin
Barry Lapthorn15-Jul-13 1:12
protectorBarry Lapthorn15-Jul-13 1:12 
GeneralMy vote of 5 Pin
CS140129-Jun-11 20:20
memberCS140129-Jun-11 20:20 
Generalfunction approximation using your GA Pin
Intelevgen4-Jun-11 12:40
memberIntelevgen4-Jun-11 12:40 
GeneralUrgent need Pin
kindkirthi24-Feb-11 5:57
memberkindkirthi24-Feb-11 5:57 
Generalgenetic algorithm Pin
sourabh maheshwari20-Jan-11 23:03
membersourabh maheshwari20-Jan-11 23:03 
Generali was looking for a GA Pin
mattica30-Sep-10 5:52
membermattica30-Sep-10 5:52 
GeneralRe: i was looking for a GA Pin
Barry Lapthorn1-Oct-10 6:31
protectorBarry Lapthorn1-Oct-10 6:31 
GeneralRe: i was looking for a GA Pin
mattica1-Oct-10 12:39
membermattica1-Oct-10 12:39 
GeneralRe: i was looking for a GA Pin
marschills22-Nov-10 3:04
membermarschills22-Nov-10 3:04 
GeneralRe: i was looking for a GA Pin
Barry Lapthorn22-Nov-10 7:24
protectorBarry Lapthorn22-Nov-10 7:24 
GeneralGenetic Algorithm Pin
arun114830-Sep-10 4:06
memberarun114830-Sep-10 4:06 
GeneralHello Pin
Joannecw862-Jul-10 0:46
memberJoannecw862-Jul-10 0:46 
Questionto change interval(define area) Pin
qzpas200827-Apr-10 4:24
memberqzpas200827-Apr-10 4:24 
GeneralPatient scheduling using Genetic algorithm Pin
setare_yousefi25-Mar-10 10:05
membersetare_yousefi25-Mar-10 10:05 
Questiongenetic algorithm Pin
manimekalai198816-Feb-10 18:59
membermanimekalai198816-Feb-10 18:59 
Generalcode to use BULLETS AND NUMBERING dialog box of MS WORD in c# application Pin
santosh_anu6-Oct-09 1:42
membersantosh_anu6-Oct-09 1:42 
Questionnumber range? Pin
Jernej200922-Jun-09 0:31
memberJernej200922-Jun-09 0:31 
QuestionHow to change optimal (0.5,0.5) Pin
sport_life5-Apr-09 18:45
membersport_life5-Apr-09 18:45 
GeneralNeed Help On Genetic algorithm Pin
dontyoudare12325-Mar-09 3:48
memberdontyoudare12325-Mar-09 3:48 
Generalsource code for exam scheduling with GA in Pin
GeneralRe: source code for exam scheduling with GA in Pin
ahyeek17-Feb-09 17:15
memberahyeek17-Feb-09 17:15 
GeneralThank you! Pin
Asmor25-Nov-08 10:30
memberAsmor25-Nov-08 10:30 
GeneralOPtymalization Pin
makdelete20-Oct-08 5:46
membermakdelete20-Oct-08 5:46 
GeneralRe: OPtymalization Pin
ahyeek17-Feb-09 17:16
memberahyeek17-Feb-09 17:16 
GeneralIMPORTENT: Please help me for Senoir Project on 14 JUN, 2008 Pin
SalmaAlnajim7-Jan-08 9:29
memberSalmaAlnajim7-Jan-08 9:29 
GeneralGood codes Pin
diegoeddy5-Sep-07 4:54
memberdiegoeddy5-Sep-07 4:54 
Questiongenetic algorithm Pin
jenif18-Jan-07 1:21
memberjenif18-Jan-07 1:21 
Generalsatisfied Pin
Ballitano27-Nov-06 11:51
memberBallitano27-Nov-06 11:51 
Generalmachine cell formation using genetic algorithm Pin
sinbear3-Sep-06 20:39
membersinbear3-Sep-06 20:39 
GeneralThanks! Pin
jwhooper25-Mar-06 16:25
memberjwhooper25-Mar-06 16:25 
GeneralRe: Thanks! Pin
Barry Lapthorn3-Jun-06 15:00
protectorBarry Lapthorn3-Jun-06 15:00 
Question&quot;survival of the fittest&quot;? Pin
cplas19-Dec-05 16:39
membercplas19-Dec-05 16:39 
AnswerRe: &quot;survival of the fittest&quot;? PinPopular
NatLang1-Mar-06 9:27
memberNatLang1-Mar-06 9:27 
GeneralC sharp 2.0 version Pin
Ytoba30-Aug-04 16:42
memberYtoba30-Aug-04 16:42 
GeneralRe: C sharp 2.0 version Pin
angelodiego21-Jun-06 23:57
memberangelodiego21-Jun-06 23:57 
GeneralRe: C sharp 2.0 version Pin
Barry Lapthorn22-Jun-06 9:24
protectorBarry Lapthorn22-Jun-06 9:24 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 22 Aug 2003
Article Copyright 2002 by Barry Lapthorn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid