The binary chromosome is the simplest type of chromosomes, which is represented by a set of bits. Maximum number of bits comprising the chromosome is 64.
/// <summary> /// Crossover operator. /// </summary> /// /// <param name="pair">Pair chromosome to crossover with.</param> /// /// <remarks><para>The method performs crossover between two chromosomes – interchanging /// range of bits between these chromosomes.</para></remarks> /// public override void Crossover(IChromosome pair) { BinaryChromosome p = (BinaryChromosome)pair; // check for correct pair if ((p != null) && (p.length == length)) { int crossOverPoint = 63 - rand.Next(length - 1); ulong mask1 = 0xFFFFFFFFFFFFFFFF >> crossOverPoint; ulong mask2 = ~mask1; ulong v1 = val; ulong v2 = p.val; // calculate new values val = (v1 & mask1) | (v2 & mask2); p.val = (v2 & mask1) | (v1 & mask2); } }
/// <summary> /// Initializes a new instance of the <see cref="BinaryChromosome"/> class. /// </summary> /// /// <param name="source">Source chromosome to copy.</param> /// /// <remarks><para>This is a copy constructor, which creates the exact copy /// of specified chromosome.</para></remarks> /// protected BinaryChromosome(BinaryChromosome source) { length = source.length; val = source.val; fitness = source.fitness; }
/// <summary> /// Initializes a new instance of the <see cref="BinaryChromosome"/> class. /// </summary> /// /// <param name="source">Source chromosome to copy.</param> /// /// <remarks><para>This is a copy constructor, which creates the exact copy /// of specified chromosome.</para></remarks> /// protected BinaryChromosome( BinaryChromosome source ) { length = source.length; val = source.val; fitness = source.fitness; }