public static Population Evolve(Population p, int generations, printPopulationFunct print, evaluateFunct evaluate, getRandomGeneFunct getRandGene, double mutation_rate = MUTATION_RATE) { Population c; MutatedPopulation mutated; Population children; int gen = 0; c = p; while (gen < generations) { /* * Rather than apply mutation while mating is occurring, * we'll apply a population-wide possible mutation step before breeding commences. * Each DNA string will have a chance of mutation. */ mutated = MutatePopulation(c, evaluate, getRandGene, mutation_rate); children = BreedPopulation(mutated, mutated.evaluations, getRandGene, mutation_rate); gen++; if (print!= null) print(c, gen); c = children; } return c; }
public static MutatedPopulation MutatePopulation(Population p, evaluateFunct evaluate, getRandomGeneFunct getRandGene, double mutate_probability = MUTATION_RATE) { MutatedPopulation mutated; mutated = new MutatedPopulation(p.Select((child) => mutate_by_chance(child, getRandGene, mutate_probability)),null); mutated.evaluations = mutated.RunFitnessTests(evaluate); return mutated; }