public void RankPopulation() { int lowest = int.MaxValue; int lowestIndex = 0; for (int i = 0; i < populationCount; i++) { ranks[i] = population[i].GetRank(); if (ranks[i] >= bestScoreEver) { bestScoreEver = ranks[i]; bestEver = population[i]; } else if(ranks[i] < lowest) { lowest = ranks[i]; lowestIndex = i; } } population[lowestIndex] = bestEver.Clone(); population[lowestIndex].Mutate(); population[0].Mutate(); //population[1].Mutate(); //population[2].Mutate(); //population[3].Mutate(); }
public Chromosome Clone() { Chromosome result = new Chromosome(); for (int i = 0; i < genes.Count; i++) { result.genes.Add(genes[i].Clone()); } return result; }
public void GeneratePopulation() { for (int i = 0; i < populationCount; i++) { Chromosome c = new Chromosome(); c.PopulateGenes(); population.Add(c); } bestEver = population[0]; ranks = new int[populationCount]; }
public void BreedPopulation() { Chromosome[] newPopulation = new Chromosome[population.Count]; int[] rwa = GetRankWeightedArray(); int len = rwa.Length; int index = 0; for (int i = 0; i < population.Count; i++) { index = (index < rwa.Length - 2) ? index : index % 2; int index0 = rwa[index++]; int index1 = rwa[index++]; while (index1 == index0) { index1 = rwa[index++ % len]; } //newPopulation[i] = population[index0].RandomMerge(population[index1]); newPopulation[i] = population[index0].RouletteMerge(population[index1]); } population = new List<Chromosome>(newPopulation); }
public Chromosome RouletteMerge(Chromosome ch) { Chromosome result = new Chromosome(); int splitPoint = GeneticPrimes.rnd.Next(genes.Count); for (int i = 0; i < genes.Count; i++) { GeneticFunction gene = i < splitPoint ? genes[i].Clone() : ch.genes[i].Clone(); result.genes.Add(gene); } return result; }
public Chromosome RandomMerge(Chromosome ch) { Chromosome result = new Chromosome(); for (int i = 0; i < genes.Count; i++) { GeneticFunction gene = GeneticPrimes.rnd.Next(100) < 50 ? genes[i].Clone() : ch.genes[i].Clone(); result.genes.Add(gene); } return result; }