Ejemplo n.º 1
0
        /// <summary>
        /// Do Crossover between 2 Chromosome's
        /// </summary>
        /// <param name="Dad">Father chromosome for product Children Chromosome</param>
        /// <param name="Mum">Mather chromosome for product Children Chromosome</param>
        /// <param name="rand">random reproducer</param>
        /// <returns></returns>
        public static Chromosome crossover(this Chromosome Dad, Chromosome Mum, Random rand)
        {
            // for check written or duplicated
            bool write = false;

            ArrayList duplicate = new ArrayList();

            //
            // define offspring chromosome length
            Chromosome offspring = new Chromosome(Dad.Tour.Length);

            //
            //          Greedy Crossover Algorithm
            //
            //   _                    _   ?
            // CDEFABG    Dad   <-----E  <--      Loop Vector to Left
            //
            //      _            ?   _
            // GFADCEB    Mum   -->  E----->      Loop Vector to Right
            //
            // Offspring (New Chromosome) =
            //            Step '1':         E
            //            Step '2':    	   DEB
            //            Step '3':    	  CDEBG
            //            Step '4':	      CDEBGF
            //            Step '5':	      CDEBGFA
            //                           _
            // select point, in example: E
            int index_dad = rand.Next(0, Dad.Tour.Length - 1);
            int index_mum = Mum.Tour.IndexOf(Dad.Tour[index_dad]);
            //
            // push selected info in offspring array
            push_info(offspring.Tour, Dad.Tour[index_dad], "Center", duplicate, out write);
            //
            // read Left of Dad chromosome & Right of Mum chromosome
            index_dad--; // left loop   <----
            index_mum++; // right loop  ---->
            //
            // -1 because selected point info was saved
            int child_lenght = offspring.Tour.Length - 1; // number of free space or '-1'
            while (child_lenght > 0)
            {
                // check range of index number
                if (index_dad < 0) index_dad = Dad.Tour.Length - 1;
                if (index_mum >= Mum.Tour.Length) index_mum = 0;

                write = false;
                offspring.Tour.push_info(Dad.Tour[index_dad], "Left", duplicate, out write);
                if (write) child_lenght--;
                write = false;
                offspring.Tour.push_info(Mum.Tour[index_mum], "Right", duplicate, out write);
                if (write) child_lenght--;
                //
                // REDUCTION
                index_dad--;
                index_mum++;
            }

            return offspring;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Do Crossover between 2 Chromosome's
        /// </summary>
        /// <param name="Dad">Father chromosome for product Children Chromosome</param>
        /// <param name="Mum">Mather chromosome for product Children Chromosome</param>
        /// <param name="rand">random reproducer</param>
        /// <returns></returns>
        public static Chromosome crossover(this Chromosome Dad, Chromosome Mum, Random rand)
        {
            // for check written or duplicated
            bool write = false;

            ArrayList duplicate = new ArrayList();

            //
            // define offspring chromosome length
            Chromosome offspring = new Chromosome(Dad.Tour.Length);

            //
            //          Greedy Crossover Algorithm
            //
            //   _                    _   ?
            // CDEFABG    Dad   <-----E  <--      Loop Vector to Left
            //
            //      _            ?   _
            // GFADCEB    Mum   -->  E----->      Loop Vector to Right
            //
            // Offspring (New Chromosome) =
            //            Step '1':         E
            //            Step '2':        DEB
            //            Step '3':       CDEBG
            //            Step '4':	      CDEBGF
            //            Step '5':	      CDEBGFA
            //                           _
            // select point, in example: E
            int index_dad = rand.Next(0, Dad.Tour.Length - 1);
            int index_mum = Mum.Tour.IndexOf(Dad.Tour[index_dad]);

            //
            // push selected info in offspring array
            push_info(offspring.Tour, Dad.Tour[index_dad], "Center", duplicate, out write);
            //
            // read Left of Dad chromosome & Right of Mum chromosome
            index_dad--; // left loop   <----
            index_mum++; // right loop  ---->
            //
            // -1 because selected point info was saved
            int child_lenght = offspring.Tour.Length - 1; // number of free space or '-1'

            while (child_lenght > 0)
            {
                // check range of index number
                if (index_dad < 0)
                {
                    index_dad = Dad.Tour.Length - 1;
                }
                if (index_mum >= Mum.Tour.Length)
                {
                    index_mum = 0;
                }

                write = false;
                offspring.Tour.push_info(Dad.Tour[index_dad], "Left", duplicate, out write);
                if (write)
                {
                    child_lenght--;
                }
                write = false;
                offspring.Tour.push_info(Mum.Tour[index_mum], "Right", duplicate, out write);
                if (write)
                {
                    child_lenght--;
                }
                //
                // REDUCTION
                index_dad--;
                index_mum++;
            }

            return(offspring);
        }
Ejemplo n.º 3
0
 private void Population(Random rand)
 {
     // create first population by Npop = 500;
     pop = new Chromosome[Npop];
     int[] RandNum = new int[counter_City];
     int[] RandNumber = new int[counter_City];
     int buffer = counter_City - 1;
     for (int l = 0; l < counter_City; l++)
         RandNumber[l] = l;
     for (int i = 0; i < Npop; i++)
     {
         RandNum = RandNumber;
         buffer = counter_City - 1;
         pop[i] = new Chromosome(counter_City);
         int b;
         int buffer2;
         for (int j = 0; j < counter_City; j++)
         {
             b = rand.Next(0, buffer);
             pop[i].Tour[j] = RandNum[b];
             buffer2 = RandNum[buffer];
             RandNum[buffer] = RandNum[b];
             RandNum[b] = buffer2;
             buffer--;
         }
     }
 }