List <Individual> BreedPopulation() { List <Individual> newpop = new List <Individual>(); //ELITISMO population.Sort((x, y) => x.fitness.CompareTo(y.fitness)); for (int i = 0; i < numElit; i++) { newpop.Add(population [i]); } //breed individuals and place them on new population. We'll apply crossover and mutation later while (newpop.Count < populationSize) { int count = 0; Individual selected1 = tournamentSelection.selectIndividuals(population, maxTournament); //we should propably always select pairs of individuals Individual selected2 = tournamentSelection.selectIndividuals(population, maxTournament); //we should propably always select pairs of individuals if (newpop.Count < populationSize) { newpop.Add(selected1); //added individuals are already copys, so we can apply crossover and mutation directly count++; } if (newpop.Count < populationSize) { newpop.Add(selected2); //added individuals are already copys, so we can apply crossover and mutation directly count++; } //apply crossover between pairs of individuals and mutation to each one if (count > 1) { selected1.Crossover(selected2, crossoverProbability); selected1.Mutate(mutationProbability); selected2.Mutate(mutationProbability); } if (count == 1) { selected1.Mutate(mutationProbability); } } return(newpop); }
//The Step function assumes that the fitness values of all the individuals in the population have been calculated. public virtual void Step() { if (generation < numGenerations) { List <Individual> new_pop; //Store statistics in log, and sorts population stats.GenLog(population, generation); population.Sort((x, y) => y.Fitness.CompareTo(x.Fitness)); //List<Individual> aux = new List<Individual> (); int new_pop_size = populationSize - elitismAffected; //Select parents new_pop = selection.selectIndividuals(population, new_pop_size); //Crossover par/impar if (new_pop_size % 2 == 1) { for (int i = 0; i < new_pop_size - 1; i += 2) { Individual parent1 = new_pop [i]; Individual parent2 = new_pop [i + 1]; parent1.Crossover(parent2, crossoverProbability, ncortes); } } else { for (int i = 0; i < new_pop_size; i += 2) { Individual parent1 = new_pop [i]; Individual parent2 = new_pop [i + 1]; parent1.Crossover(parent2, crossoverProbability, ncortes); } } //Mutation and Translation for (int i = 0; i < new_pop_size; i++) { new_pop [i].Mutate(mutationProbability); new_pop [i].Translate(); } //Select new population for (int i = 0; i < elitismAffected; i++) { new_pop.Add(population [i]); } new_pop.Sort((x, y) => y.Fitness.CompareTo(x.Fitness)); population = new_pop; generation++; } }
//The Step function assumes that the fitness values of all the individuals in the population have been calculated. public virtual void Step() { if (generation < numGenerations) { List <Individual> new_pop; //Store statistics in log stats.GenLog(population, generation); //Select parents new_pop = selection.selectIndividuals(population, populationSize - preservedIndividualsElitism); //Crossover for (int i = 0; i < populationSize - preservedIndividualsElitism; i += 2) { Individual parent1 = new_pop [i]; Individual parent2 = new_pop [i + 1]; parent1.n_cuts = N_cutsCrossover; parent1.CrossoverType = (int)crossoverType; parent1.Crossover(parent2, crossoverProbability); } //Mutation and Translation for (int i = 1; i < populationSize - preservedIndividualsElitism; i++) { //Debug.Log ("MutationType = " + mutationType + "/nIn int = " + (int)mutationType); new_pop [i].MutationType = (int)mutationType; //send public modifier mutationType to Individual new_pop [i].Mutate(mutationProbability); new_pop [i].Translate(); } //ELITISM //List<Individual> elitismIndividuals = new List<Individual> (); //sort population by fitness population.Sort((x, y) => y.Fitness.CompareTo(x.Fitness)); for (int i = 0; i < preservedIndividualsElitism; i++) { //Debug.Log(population[i].Fitness); new_pop.Add(population [i]); } //END ELITISM //Select new population population = new_pop; generation++; } }