Added more structure methods and some quality of life changes
Overall cleaned up a lot of comments. EasyGA: - Code cleanup. Population: - Added sort_by_best_fitness - Added parent/mating pool methods. - Renamed some methods for consistency. Chromosome: - Added get_gene(index). Parent Selection: - Improved selection methods to use the ga.selection_probability so that the roulette selection actually works well. - Added stochastic selection. Survivor Selection: - Added fill_in_random and fill_in_parents_then_random. Crossover/Mutation: - Cleaned up code.
This commit is contained in:
@ -4,5 +4,24 @@ class Survivor_Selection:
|
||||
"""Survivor selection determines which individuals should be brought to the next generation"""
|
||||
|
||||
def fill_in_best(ga, next_population):
|
||||
"""Fills in the next population with the best chromosomes from the last population until the population size is met."""
|
||||
return ga.make_population(ga.population.get_all_chromosomes()[:ga.population.size()-next_population.size()] + next_population.get_all_chromosomes())
|
||||
"""Fills in the next population with the best chromosomes from the last population"""
|
||||
|
||||
ga.population.set_all_chromosomes(ga.population.get_all_chromosomes()[:ga.population.size()-next_population.size()] + next_population.get_all_chromosomes())
|
||||
|
||||
|
||||
def fill_in_random(ga, next_population):
|
||||
"""Fills in the next population with random chromosomes from the last population"""
|
||||
|
||||
ga.population.set_all_chromosomes([
|
||||
random.choice(ga.population.get_all_chromosomes())
|
||||
for n in range(ga.population.size()-next_population.size())]
|
||||
+ next_population.get_all_chromosomes())
|
||||
|
||||
|
||||
def fill_in_parents_then_random(ga, next_population):
|
||||
"""Fills in the next population with all parents followed by random chromosomes from the last population"""
|
||||
|
||||
ga.population.set_all_chromosomes([
|
||||
random.choice(ga.population.get_all_chromosomes())
|
||||
for n in range(ga.population.size()-len(ga.population.get_mating_pool())-next_population.size())]
|
||||
+ ga.population.get_mating_pool() + next_population.get_all_chromosomes())
|
||||
|
||||
Reference in New Issue
Block a user