public void Breed() { Genome[] bestGenomes = GetBestCases(4); List<Genome> children = new List<Genome>(); Genome best = new Genome(); best.SetFitness(0.0f); best.SetId(bestGenomes[0].GetId()); best.SetGenes(bestGenomes[0].GetGenes()); //Mutate(best); children.Add(best); Genome[] babies = CrossOver(bestGenomes[0], bestGenomes[1]); Mutate(babies[0]); Mutate(babies[1]); children.Add(babies[0]); children.Add(babies[1]); babies = CrossOver(bestGenomes[0], bestGenomes[2]); Mutate(babies[0]); Mutate(babies[1]); children.Add(babies[0]); children.Add(babies[1]); babies = CrossOver(bestGenomes[0], bestGenomes[3]); Mutate(babies[0]); Mutate(babies[1]); children.Add(babies[0]); children.Add(babies[1]); babies = CrossOver(bestGenomes[1], bestGenomes[2]); Mutate(babies[0]); Mutate(babies[1]); children.Add(babies[0]); children.Add(babies[1]); babies = CrossOver(bestGenomes[1], bestGenomes[3]); Mutate(babies[0]); Mutate(babies[1]); children.Add(babies[0]); children.Add(babies[1]); int remainingChildren = (totalPopulation - children.Count); for (int i = 0; i < remainingChildren; i++) { children.Add(this.CreateNewGenome(bestGenomes[0].GetGenes().Count)); } ClearPopulation(); population = children; currentGenome = 0; generation++; }
public Genome CreateNewGenome(int totalWeights) { Genome genome = new Genome(); genome.SetId(id); genome.SetFitness(0.0f); List<float> genes = new List<float>(); for (int i = 0; i < totalWeights; i++) { genes.Add(RandomClamped()); } genome.SetGenes(genes); id++; return genome; }
public void GeneratePopulation(int totalPopulation, int totalWeights) { generation = 1; ClearPopulation(); currentGenome = -1; this.totalPopulation = totalPopulation; //resize if (population.Count < totalPopulation) { for (int i = population.Count; i < totalPopulation; i++) { population.Add(new Genome()); } } for (int i = 0; i < population.Count; i++) { Genome genome = new Genome(); genome.SetId(id); genome.SetFitness(0.0f); genome.SetGenes(new List<float>()); //resize for (int k = 0; k < totalWeights; k++) { genome.GetGenes().Add(RandomClamped()); } id++; population[i] = genome; } }