Binary chromosome, which supports length from 2 till 64.

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.

Inheritance: ChromosomeBase
Exemplo n.º 1
0
        /// <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);
            }
        }
Exemplo n.º 2
0
 /// <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;
 }