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:
SimpleArt
2020-10-13 12:48:20 -04:00
parent 5e6f9b0427
commit fb213f04dd
8 changed files with 223 additions and 77 deletions

View File

@ -6,17 +6,22 @@ class Crossover_Methods:
"""Methods for selecting chromosomes to crossover"""
def sequential_selection(ga):
"""Select sequential pairs from the mating pool"""
"""Select sequential pairs from the mating pool.
Every parent is paired with the previous parent.
The first parent is paired with the last parent.
"""
mating_pool = ga.population.mating_pool
return ga.make_population([ga.crossover_individual_impl(ga, mating_pool[index], mating_pool[index+1]) for index in range(len(mating_pool)-1)])
mating_pool = ga.population.get_mating_pool()
return ga.make_population([ga.crossover_individual_impl(ga, mating_pool[index], mating_pool[index-1]) for index in range(len(mating_pool))])
def random_selection(ga):
"""Select random pairs from the mating pool"""
"""Select random pairs from the mating pool.
Every parent is paired with a random parent.
"""
mating_pool = ga.population.mating_pool
return ga.make_population([ga.crossover_individual_impl(ga, random.choice(mating_pool), random.choice(mating_pool)) for n in mating_pool])
mating_pool = ga.population.get_mating_pool()
return ga.make_population([ga.crossover_individual_impl(ga, parent, random.choice(mating_pool)) for parent in mating_pool])
class Individual: