Updated fitness based termination and fixed some EasyGA stuff
This commit is contained in:
@ -1,4 +1,20 @@
|
||||
import random
|
||||
# Import all the data structure prebuilt modules
|
||||
from structure import Population as create_population
|
||||
from structure import Chromosome as create_chromosome
|
||||
from structure import Gene as create_gene
|
||||
|
||||
# Structure Methods
|
||||
from fitness_function import Fitness_Examples
|
||||
from initialization import Initialization_Methods
|
||||
from termination_point import Termination_Methods
|
||||
|
||||
# Parent/Survivor Selection Methods
|
||||
from parent_selection import Parent_Selection
|
||||
from survivor_selection import Survivor_Selection
|
||||
|
||||
# Genetic Operator Methods
|
||||
from mutation import Mutation_Methods
|
||||
from crossover import Crossover_Methods
|
||||
|
||||
from attributes import attributes
|
||||
|
||||
@ -13,9 +29,9 @@ class GA(attributes):
|
||||
def evolve_generation(self, number_of_generations = 1, consider_termination = True):
|
||||
"""Evolves the ga the specified number of generations."""
|
||||
|
||||
while(number_of_generations > 0 # Evolve the specified number of generations
|
||||
and (not consider_termination # and if consider_termination flag is set
|
||||
or self.termination_impl(self))): # then also check if termination conditions reached
|
||||
while(number_of_generations > 0 # Evolve the specified number of generations
|
||||
and (not consider_termination # and if consider_termination flag is set
|
||||
or self.active())): # then also check if termination conditions reached
|
||||
|
||||
# If its the first generation then initialize the population
|
||||
if self.current_generation == 0:
|
||||
@ -25,13 +41,13 @@ class GA(attributes):
|
||||
|
||||
# Otherwise evolve the population
|
||||
else:
|
||||
self.set_all_fitness()
|
||||
self.population.sort_by_best_fitness(self)
|
||||
self.parent_selection_impl(self)
|
||||
self.crossover_population_impl(self)
|
||||
self.survivor_selection_impl(self)
|
||||
self.mutation_population_impl(self)
|
||||
self.population.update()
|
||||
self.set_all_fitness()
|
||||
self.population.sort_by_best_fitness(self)
|
||||
|
||||
number_of_generations -= 1
|
||||
self.current_generation += 1
|
||||
|
||||
@ -3,22 +3,23 @@ import EasyGA
|
||||
|
||||
# Create the Genetic algorithm
|
||||
ga = EasyGA.GA()
|
||||
ga.target_fitness_type = 'min'
|
||||
ga.chromosome_length = 10
|
||||
|
||||
# Create 25 chromosomes each with 10 genes
|
||||
ga.population_size = 25
|
||||
ga.generation_goal = 50
|
||||
ga.chromosome_length = 10
|
||||
|
||||
# Create random genes from 0 to 10
|
||||
ga.gene_impl = lambda: random.randint(0, 10)
|
||||
|
||||
def fitness_function(chromosome):
|
||||
return sum(
|
||||
gene.get_value()
|
||||
for gene in chromosome.get_gene_list())
|
||||
# Minimize the sum of the genes
|
||||
ga.fitness_function_impl = lambda chromosome: sum(gene.get_value() for gene in chromosome.get_gene_list())
|
||||
ga.target_fitness_type = 'min'
|
||||
|
||||
ga.fitness_function_impl = fitness_function
|
||||
# Terminate when a chromosome has all 0's
|
||||
ga.fitness_goal = 0
|
||||
ga.termination_impl = EasyGA.Termination_Methods.fitness_based
|
||||
|
||||
ga.evolve()
|
||||
ga.set_all_fitness()
|
||||
ga.population.sort_by_best_fitness(ga)
|
||||
|
||||
print(f"Current Generation: {ga.current_generation}")
|
||||
ga.population.print_all()
|
||||
|
||||
@ -4,22 +4,23 @@ class Termination_Methods:
|
||||
def fitness_based(ga):
|
||||
"""Fitness based approach to terminate when the goal fitness has been reached"""
|
||||
|
||||
# Need to start the algorithm if the population is None
|
||||
# Need to start the algorithm if the population is None.
|
||||
if ga.population == None:
|
||||
return True
|
||||
|
||||
# Check all chromosomes
|
||||
for chromosome in ga.population.get_all_chromosomes():
|
||||
# If minimum fitness goal reached, stop ga.
|
||||
if ga.target_fitness_type == 'min' and ga.get_chromosome_fitness(0) >= ga.convert_fitness(ga.fitness_goal):
|
||||
return False
|
||||
|
||||
# Stop if a chromosome has reached the fitness_goal
|
||||
if(chromosome.fitness >= ga.fitness_goal):
|
||||
return False
|
||||
|
||||
# Continue if no chromosomes have reached the fitness goal
|
||||
# If maximum fitness goal reached, stop ga.
|
||||
if ga.target_fitness_type == 'max' and ga.get_chromosome_fitness(0) >= ga.convert_fitness(ga.fitness_goal):
|
||||
return False
|
||||
|
||||
# Otherwise continue ga.
|
||||
return True
|
||||
|
||||
|
||||
def generation_based(ga):
|
||||
"""Generation based approach to terminate when the goal generation has been reached"""
|
||||
"""Generation based approach to terminate when the goal generation has been reached."""
|
||||
|
||||
return ga.current_generation < ga.generation_goal
|
||||
|
||||
Reference in New Issue
Block a user