Beispiel #1
0
 public Chromosome <GeneType> CrossOverOnePoint(Chromosome <GeneType> Mother)
 {
     #region Validations
     ParentSkillsForCrossing(this, Mother);
     #endregion Validations
     var child = new Chromosome <GeneType>(NumberOfGene);
     int pointPartition = new Random((int)DateTime.Now.Ticks).Next() % Genes.Count, i = 0;
     for (; i < pointPartition; i++)
     {
         child.AddGene(Genes[i]);
     }
     for (; i < Genes.Count; i++)
     {
         child.AddGene(Mother.Genes[i]);
     }
     return(child);
 }
Beispiel #2
0
        public Chromosome <GeneType> CrossOverTwoPoints(Chromosome <GeneType> Mother)
        {
            #region Validations
            ParentSkillsForCrossing(this, Mother);
            #endregion Validations
            var child = new Chromosome <GeneType>(NumberOfGene);
            var rand  = new Random((int)DateTime.Now.Ticks);
            int firstPointPartition = rand.Next() % Genes.Count,
                secondPointPartition = rand.Next() % Genes.Count, i = 0;

            #region ValidationOrder
            while (firstPointPartition == secondPointPartition && NumberOfGene >= 2)
            {
                secondPointPartition = rand.Next() % Genes.Count;
            }
            if (firstPointPartition > secondPointPartition)
            {
                var aux = secondPointPartition;
                secondPointPartition = firstPointPartition;
                firstPointPartition  = aux;
            }
            #endregion ValidatioOrder

            for (; i < firstPointPartition; i++)
            {
                child.AddGene(Genes[i]);
            }
            for (; i < secondPointPartition; i++)
            {
                child.AddGene(Mother.Genes[i]);
            }
            for (; i < Genes.Count; i++)
            {
                child.AddGene(Genes[i]);
            }
            return(child);
        }