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
|
from attributes import attributes
|
||||||
|
|
||||||
@ -15,7 +31,7 @@ class GA(attributes):
|
|||||||
|
|
||||||
while(number_of_generations > 0 # Evolve 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
|
and (not consider_termination # and if consider_termination flag is set
|
||||||
or self.termination_impl(self))): # then also check if termination conditions reached
|
or self.active())): # then also check if termination conditions reached
|
||||||
|
|
||||||
# If its the first generation then initialize the population
|
# If its the first generation then initialize the population
|
||||||
if self.current_generation == 0:
|
if self.current_generation == 0:
|
||||||
@ -25,13 +41,13 @@ class GA(attributes):
|
|||||||
|
|
||||||
# Otherwise evolve the population
|
# Otherwise evolve the population
|
||||||
else:
|
else:
|
||||||
self.set_all_fitness()
|
|
||||||
self.population.sort_by_best_fitness(self)
|
|
||||||
self.parent_selection_impl(self)
|
self.parent_selection_impl(self)
|
||||||
self.crossover_population_impl(self)
|
self.crossover_population_impl(self)
|
||||||
self.survivor_selection_impl(self)
|
self.survivor_selection_impl(self)
|
||||||
self.mutation_population_impl(self)
|
self.mutation_population_impl(self)
|
||||||
self.population.update()
|
self.population.update()
|
||||||
|
self.set_all_fitness()
|
||||||
|
self.population.sort_by_best_fitness(self)
|
||||||
|
|
||||||
number_of_generations -= 1
|
number_of_generations -= 1
|
||||||
self.current_generation += 1
|
self.current_generation += 1
|
||||||
|
|||||||
@ -3,22 +3,23 @@ import EasyGA
|
|||||||
|
|
||||||
# Create the Genetic algorithm
|
# Create the Genetic algorithm
|
||||||
ga = EasyGA.GA()
|
ga = EasyGA.GA()
|
||||||
ga.target_fitness_type = 'min'
|
|
||||||
ga.chromosome_length = 10
|
# Create 25 chromosomes each with 10 genes
|
||||||
ga.population_size = 25
|
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)
|
ga.gene_impl = lambda: random.randint(0, 10)
|
||||||
|
|
||||||
def fitness_function(chromosome):
|
# Minimize the sum of the genes
|
||||||
return sum(
|
ga.fitness_function_impl = lambda chromosome: sum(gene.get_value() for gene in chromosome.get_gene_list())
|
||||||
gene.get_value()
|
ga.target_fitness_type = 'min'
|
||||||
for gene in chromosome.get_gene_list())
|
|
||||||
|
|
||||||
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.evolve()
|
||||||
ga.set_all_fitness()
|
|
||||||
ga.population.sort_by_best_fitness(ga)
|
|
||||||
|
|
||||||
print(f"Current Generation: {ga.current_generation}")
|
print(f"Current Generation: {ga.current_generation}")
|
||||||
ga.population.print_all()
|
ga.population.print_all()
|
||||||
|
|||||||
@ -4,22 +4,23 @@ class Termination_Methods:
|
|||||||
def fitness_based(ga):
|
def fitness_based(ga):
|
||||||
"""Fitness based approach to terminate when the goal fitness has been reached"""
|
"""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:
|
if ga.population == None:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Check all chromosomes
|
# If minimum fitness goal reached, stop ga.
|
||||||
for chromosome in ga.population.get_all_chromosomes():
|
if ga.target_fitness_type == 'min' and ga.get_chromosome_fitness(0) >= ga.convert_fitness(ga.fitness_goal):
|
||||||
|
|
||||||
# Stop if a chromosome has reached the fitness_goal
|
|
||||||
if(chromosome.fitness >= ga.fitness_goal):
|
|
||||||
return False
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
def generation_based(ga):
|
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
|
return ga.current_generation < ga.generation_goal
|
||||||
|
|||||||
Reference in New Issue
Block a user