Chromosomes and Genes

Jump to: navigation, search

Chromosomes represent possible solutions. A population represents a collection of chromosomes. Chromosomes are selected as parents, genetic operators are applied to them e.g. Crossover, which produces new chromosomes often referred to as children. Each Chromosome consists of a collection of Genes.

For example a binary chromosome consists of a collection of Genes that have values of true or false. Typically true would refer to a "1" and false to a "0".

Random binary chromosomes can be created very quickly using the constructors of the Population or Chromosome classes. For example the following constructor will create a random Binary Chromosome with a binary string length of 44.

   var chromosome = new Chromosome(44);    

Alternatively, the code below creates a binary chromosome from a string representation of a binary value.

   var chromosome = new Chromosome("1100100110011011011010");    

The GAF supports binary, integer, real and object based genes (i.e. a Chromosome with objects as Genes). Each gene could, for example, store a list of objects allowing multidimensional chromosomes if so desired. The gene's type is given by the Gene.GeneType property. This property returns an enumerated value of one of the following C# types.

  • Binary
  • Integer
  • Real
  • Object

As an example, the code below creates an object based chromosome. This snippet was taken from the Solving the Travelling Salesman Problem example and shows how an object based Gene can store a city object.

   //create some City Objects
   var cities = CreateCities().ToList();

   //Each city is an object. The chromosome is a special case as it needs 
   //to contain each city only once. Therefore, our chromosome will contain 
   //all the cities with no duplicates

   //create an empty population
   var population = new Population();

   //create the chromosomes
   for (var p = 0; p < populationSize; p++)

     var chromosome = new Chromosome();
     foreach (var city in cities)
       chromosome.Genes.Add(new Gene(city));

     //shuffle the order
   private static IEnumerable<City> CreateCities()
     var cities = new List<City>
       new City("Birmingham", 52.486125, -1.890507),
       new City("Bristol", 51.460852, -2.588139),
       new City("London", 51.512161, -0.116215),
       new City("Leeds", 53.803895, -1.549931),
       new City("Manchester", 53.478239, -2.258549),
       new City("Liverpool", 53.409532, -3.000126),
       new City("Hull", 53.751959, -0.335941),

     return cities;

See Also