コード例 #1
0
        /// <summary>
        /// Krzyżowanie genotypu
        /// </summary>
        /// <param name="parent2">genotyp z którym ma nastąpić krzyżowanie</param>
        /// <param name="crossoverRate">współczynnik krzyżowania</param>
        /// <returns>tablica dzieci </returns>
        public Genotype[] Crossover(Genotype parent2, float crossoverRate)
        {
            if (this.Length != parent2.Length) throw new Exception("Can't Crossover genotypes with diffrent length");

            var copyArray = new BitArray(Length * 32);
            for (int i = 0; i < copyArray.Length; i++)
            {
                copyArray[i] = geneticAlgorithm.random.NextDouble() < crossoverRate ? true : false;
            }

            var child1 = new Genotype(this.Length, this.geneticAlgorithm, false);
            var child2 = new Genotype(this.Length, this.geneticAlgorithm, false);
            for (int i = 0; i < copyArray.Length; i++)
            {
                if (copyArray[i])
                {
                    child1.Genes[i] = this.Genes[i];
                    child2.Genes[i] = parent2.Genes[i];
                }
                else
                {
                    child1.Genes[i] = parent2.Genes[i];
                    child2.Genes[i] = this.Genes[i];
                }
            }

            return new Genotype[2] { child1, child2 };
        }
コード例 #2
0
 /// <summary>
 /// Metoda Wykonuje kopię Genotypu
 /// </summary>
 /// <returns></returns>
 public Genotype Copy()
 {
     var copiedGenotype = new Genotype(this.Length, this.geneticAlgorithm, false);
     copiedGenotype.Genes = this.Genes;
     return copiedGenotype;
 }