Exemplo n.º 1
0
        private static List<Chromosome> Inversion(ChromosomePair pair)
        {
            List<Chromosome> rezult = new List<Chromosome>(1);
            Chromosome chr = pair.Chr1.CloneByGenes();
            for (int i = 3; i < 11; i++)
            {
                chr[i] = !chr[i];
            }

            rezult.Add(chr);
            return rezult;
        }
Exemplo n.º 2
0
 private static List<Chromosome> Crossing(ChromosomePair pair, int GenNumber)
 {
     List<Chromosome> rezult = new List<Chromosome>(1);
     Chromosome chr1 = pair.Chr1.CloneByGenes();
     Chromosome chr2 = pair.Chr2.CloneByGenes();
     for (int i = GenNumber; i < chr1.Length; i++)
     {
         chr1[i] = pair.Chr2[i];
         chr2[i] = pair.Chr1[i];
     }
     rezult.Add(chr1);
     rezult.Add(chr2);
     return rezult;
 }
Exemplo n.º 3
0
        public static List<Chromosome> DoCrossing(ChromosomePair pair, int GenNumber, double mutationChance = 0.07)
        {
            List<Chromosome> rezult = null;

            if (pair.Chr1.EqualByGenes(pair.Chr2))
                rezult = Inversion(pair);
            else
                rezult = Crossing(pair, GenNumber);
            for (int i = 0; i < rezult.Count; i++)
            {
                rezult[i] = rezult[i].Mutate(mutationChance);
            }
            return rezult;
        }
Exemplo n.º 4
0
        private static ChromosomePair GetPairRandom(List<Chromosome> chromosomes)
        {
            ChromosomePair pair = new ChromosomePair();
            if (chromosomes.Count < 2)
                throw new ArgumentException("chromosomes count must be >= 2");
            else if (chromosomes.Count == 2)
            {
                pair.Chr1 = chromosomes[0];
                pair.Chr2 = chromosomes[1];
            }
            else
            {
                int i1 = Randomizer.Rnd.Next(chromosomes.Count - 1);
                int i2 = Randomizer.Rnd.Next(chromosomes.Count - 1);
                pair.Chr1 = chromosomes[i1];
                pair.Chr2 = chromosomes[i2];
            }

            return pair;
        }
Exemplo n.º 5
0
        private static ChromosomePair GetPairRoulette(List<Chromosome> chromosomes)
        {
            Chromosome ch1 = chromosomes[0];
            Chromosome ch2 = chromosomes[1];
            double[] range = new double[chromosomes.Count + 1];
            double min = chromosomes.Min(ch => ch.F);
            for (int i = 0; i < chromosomes.Count; i++)
            {
                range[i + 1] = range[i] + chromosomes[i].F - min;
            }
            double A = range[chromosomes.Count];
            double r1 = Randomizer.Rnd.NextDouble() * A;
            double r2 = Randomizer.Rnd.NextDouble() * A;
            for (int i = 0; i < chromosomes.Count; i++)
            {
                if ((range[i] <= r1) && (r1 <= range[i + 1]))
                    ch1 = chromosomes[i];
                if ((range[i] <= r2) && (r2 <= range[i + 1]))
                    ch2 = chromosomes[i];
            }
            ChromosomePair pair = new ChromosomePair() { Chr1 = ch1, Chr2 = ch2 };

            return pair;
        }