Ejemplo n.º 1
0
        public List<Genome> RecombineBinary(Genome genomeA, Genome genomeB)
        {
            //Console.WriteLine(String.Format("\r\nRecombineBinary:\r\n\tGenom A:\t{0}\r\n\tGenom B:\t{1}", genomeA.AsString(),genomeB.AsString()));

            List<double> childA = new List<double>(countGene);
            List<double> childB = new List<double>(countGene);

            if (!RecombBinaryIsSinglePoint.HasValue)
            {
                // Gleichmäßige Rekombination

                // Erzeuge Template einmalig
                if (templateGenom == null)
                {
                    templateGenom = new List<bool>();
                    Random random = new Random(Guid.NewGuid().GetHashCode());
                    for (int i=0; i < countGene; i++)
                    {
                        templateGenom.Add(Convert.ToBoolean(random.Next(2)));
                    }
                }

                childA.AddRange(genomeA);
                childB.AddRange(genomeB);

                // Tausche Allele anhand Template
                double tmp;
                for (int i=0; i < countGene; i++)
                {
                    // Wenn false im Template -> tausche Allele
                    if (!templateGenom[i])
                    {
                        tmp = childA[i];
                        childA[i] = childB[i];
                        childB[i] = tmp;
                    }
                }
            }
            else if (RecombBinaryIsSinglePoint.Value)
            {
                // 1-Punkt Rekombination
                int rnd = Helper.GetRandomInteger(2, genomeA.Count - 2);

                childA.AddRange(genomeA.GetRange(0, rnd));
                childA.AddRange(genomeB.GetRange(rnd, genomeB.Count - rnd ));

                childB.AddRange(genomeB.GetRange(0, rnd));
                childB.AddRange(genomeA.GetRange(rnd, genomeA.Count - rnd ));
            }
            else
            {
                // 2-Punkt Rekombination
                int rnd1 = Helper.GetRandomInteger(1, genomeA.Count-2);
                int rnd2 = Helper.GetRandomInteger(1, genomeA.Count-rnd1-1);

                childA.AddRange(genomeA.GetRange(0, rnd1+1));
                childA.AddRange(genomeB.GetRange(rnd1+1, rnd2));
                if ((rnd1+rnd2+1) <= genomeA.Count -1)
                    childA.AddRange(genomeA.GetRange((rnd1+rnd2+1), (genomeA.Count-1-rnd1-rnd2)));

                childB.AddRange(genomeB.GetRange(0, rnd1+1));
                childB.AddRange(genomeA.GetRange(rnd1+1, rnd2));
                if ((rnd1+rnd2+1) <= genomeA.Count -1)
                    childB.AddRange(genomeB.GetRange((rnd1+rnd2+1), (genomeA.Count-1-rnd1-rnd2)));
            }
            List<Genome> result = new List<Genome>(2);
            result.Add(new GenomeReal(childA.ToArray()));
            result.Add(new GenomeReal(childB.ToArray()));

            return result;
        }