示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShortArrayChromosome"/> 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 ShortArrayChromosome(ShortArrayChromosome source)
 {
     // copy all properties
     length   = source.length;
     maxValue = source.maxValue;
     val      = (ushort[])source.val.Clone( );
     fitness  = source.fitness;
 }
示例#2
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 genes (array elements) between these chromosomes.</para></remarks>
        ///
        public override void Crossover(IChromosome pair)
        {
            ShortArrayChromosome p = (ShortArrayChromosome)pair;

            // check for correct pair
            if ((p != null) && (p.length == length))
            {
                // crossover point
                int crossOverPoint = rand.Next(length - 1) + 1;
                // length of chromosome to be crossed
                int crossOverLength = length - crossOverPoint;
                // temporary array
                ushort[] temp = new ushort[crossOverLength];

                // copy part of first (this) chromosome to temp
                Array.Copy(val, crossOverPoint, temp, 0, crossOverLength);
                // copy part of second (pair) chromosome to the first
                Array.Copy(p.val, crossOverPoint, val, crossOverPoint, crossOverLength);
                // copy temp to the second
                Array.Copy(temp, 0, p.val, crossOverPoint, crossOverLength);
            }
        }
示例#3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShortArrayChromosome"/> 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 ShortArrayChromosome( ShortArrayChromosome source )
 {
     // copy all properties
     length   = source.length;
     maxValue = source.maxValue;
     val      = (ushort[]) source.val.Clone( );
     fitness  = source.fitness;
 }