public Genotype Copy() { var copyGenotype = new Genotype(this.Length, this.EvolutionStrategy); copyGenotype.Genes = this.Genes; return(copyGenotype); }
/// <summary> /// Tutaj modyfikujemy geny rodzicielskich chromosomów /// </summary> /// <param name="parent2"></param> /// <param name="parent2"></param> /// <returns>2 child array</returns> public static Genotype[] Crossover(Genotype parent1, Genotype parent2) { if (parent1.Length != parent2.Length) throw new Exception("Nie można krzyżować osobników z różną długością!"); var rnd = parent1.EvolutionStrategy.Random.NextDouble();//zmienna losowa z zakresu 0..1 var length = parent1.Length; var child1 = new Genotype(length, parent1.EvolutionStrategy); var child2 = new Genotype(length, parent1.EvolutionStrategy); for (int i = 0; i < length; i++) { child1.Genes[i] = parent1.Genes[i] + rnd * (parent2.Genes[i] - parent1.Genes[i]); child2.Genes[i] = parent2.Genes[i] + parent1.Genes[i] - child1.Genes[i]; } return new Genotype[2] { child1, child2 }; }
/// <summary> /// Metoda krzyzuje dwa losowo wybrane osobniki, ktore staja sie rodzicami. /// I uzupelnia generacje potomkow dokladnie dwoma nowymi osobnikami. /// </summary> public void Crossover() { var lambda = LambdaPopulationSize; for (int i = 0; i < lambda; i += 2) { if (this.Random.NextDouble() < CrossoverRate) { ResultGeneration.AddRange( Genotype.Crossover( NextGeneration[Random.Next(lambda)], NextGeneration[Random.Next(lambda)] ) ); } } }
/// <summary> /// Tutaj modyfikujemy geny rodzicielskich chromosomów /// </summary> /// <param name="parent2"></param> /// <param name="parent2"></param> /// <returns>2 child array</returns> public static Genotype[] Crossover(Genotype parent1, Genotype parent2) { if (parent1.Length != parent2.Length) { throw new Exception("Nie można krzyżować osobników z różną długością!"); } var rnd = parent1.EvolutionStrategy.Random.NextDouble();//zmienna losowa z zakresu 0..1 var length = parent1.Length; var child1 = new Genotype(length, parent1.EvolutionStrategy); var child2 = new Genotype(length, parent1.EvolutionStrategy); for (int i = 0; i < length; i++) { child1.Genes[i] = parent1.Genes[i] + rnd * (parent2.Genes[i] - parent1.Genes[i]); child2.Genes[i] = parent2.Genes[i] + parent1.Genes[i] - child1.Genes[i]; } return(new Genotype[2] { child1, child2 }); }
public Genotype Copy() { var copyGenotype = new Genotype(this.Length, this.EvolutionStrategy); copyGenotype.Genes = this.Genes; return copyGenotype; }