Update EasyGA.py

This commit is contained in:
SimpleArt
2020-09-27 17:42:41 -04:00
parent 58f7a34cbb
commit 4b21dc45f6

View File

@ -44,10 +44,24 @@ class GA:
pass pass
def evolve(self): def evolve(self):
"""Updates the ga to the next generation. """Runs the ga until the ga is no longer active."""
# run one iteration while the ga is active
while self.active():
self.evolve_generation(1)
def active(self):
"""Returns if the ga should terminate or not"""
return self.current_generation < self.generations
def evolve_generation(self, number_of_generations):
"""Evolves the ga the specified number of generations.
If update_fitness is set then all fitness values are updated. If update_fitness is set then all fitness values are updated.
Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated.""" Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated."""
# run the specified number of times
for n in range(number_of_generations):
# for each chromosome in the population # for each chromosome in the population
for chromosome in self.population.get_all_chromosomes(): for chromosome in self.population.get_all_chromosomes():
@ -55,14 +69,7 @@ class GA:
if self.update_fitness or chromosome.get_fitness() is None: if self.update_fitness or chromosome.get_fitness() is None:
chromosome.set_fitness(self.fitness_impl(chromosome)) chromosome.set_fitness(self.fitness_impl(chromosome))
def active(self): # apply selection, crossover, and mutation
"""Returns if the ga should terminate or not"""
return self.current_generation < self.generations
def evolve_generation(self, number_of_generations):
"""Evolves the ga the specified number of generations."""
for n in range(number_of_generations):
self.evolve()
def make_gene(self,value): def make_gene(self,value):
"""Let's the user create a gene.""" """Let's the user create a gene."""