Add files via upload

This commit is contained in:
jcurtis664
2020-10-06 14:50:46 -04:00
committed by GitHub
parent 4799722a12
commit 04c749d95c

View File

@ -1,46 +1,86 @@
import random import random
# Import all the data structure prebuilt modules # Import all the data structure prebuilt modules
from initialization import population as create_population from initialization import Population as create_population
from initialization import chromosome as create_chromosome from initialization import Chromosome as create_chromosome
from initialization import gene as create_gene from initialization import Gene as create_gene
# Import example classes # Structure Methods
from fitness_function import fitness_examples from fitness_function import Fitness_methods
from initialization import initialization_examples from initialization import Initialization_methods
from termination_point import termination_examples from termination_point import Termination_methods
from selection import selection_examples # Population Methods
from crossover import crossover_examples from survivor_selection import Survivor_methods
from repopulate import repopulate_examples # Manipulation Methods
from mutation import mutation_examples from parent_selection import Parent_methods
from mutation import Mutation_methods
from crossover import Crossover_methods
class GA: class GA:
def __init__(self): def __init__(self):
"""Initialize the genetic algorithm. Where all the hyper parmeters are """Initialize the GA."""
set for the the ga to function."""
# Initilization variables # Initilization variables
self.chromosome_length = 3 self.chromosome_length = 3
self.population_size = 10 self.population_size = 5
self.chromosome_impl = None self.chromosome_impl = None
self.gene_impl = None self.gene_impl = None
self.population = None self.population = None
# Termination varibles # Termination varibles
self.current_generation = 0 self.current_generation = 0
self.current_fitness = 0 self.generation_goal = 3
self.generation_goal = 0
self.fitness_goal = 4 self.current_fitness = 0
self.fitness_goal = 3
# Mutation variables # Mutation variables
self.mutation_rate = 0.02 self.mutation_rate = 0.03
# Rerun already computed fitness # Rerun already computed fitness
self.update_fitness = False self.update_fitness = False
# Defualt EastGA implimentation structure # Defualt EastGA implimentation structure
self.initialization_impl = initialization_examples.random_initialization self.initialization_impl = Initialization_methods.random_initialization
self.fitness_funciton_impl = fitness_examples.is_it_5 self.fitness_funciton_impl = Fitness_methods.is_it_5
self.selection_impl = selection_examples.roulette # Selects which chromosomes should be automaticly moved to the next population
self.crossover_impl = crossover_examples.single_point_crossover #self.survivor_selection_impl = Survivor_methods.
self.repopulate_impl = repopulate_examples.kill_two_worst # Methods for accomplishing parent-selection -> Crossover -> Mutation
self.mutation_impl = mutation_examples.per_gene_mutation # self.parent_selection_impl = Parent_methods.roulette_selection
self.termination_impl = termination_examples.generation_based #self.crossover_impl = Crossover_methods.
#self.mutation_impl = Mutation_methods.
# The type of termination to impliment
self.termination_impl = Termination_methods.generation_based
def evolve_generation(self, number_of_generations = 1):
"""Evolves the ga the specified number of generations."""
while(number_of_generations > 0):
# If its the first generation then initialize the population
if(self.current_generation == 0):
# Initialize the population
self.initialize_population()
# First get the fitness of the population
self.get_population_fitness(self.population.chromosome_list)
# Selection - Triggers flags in the chromosome if its been selected
# self.selection_impl(self)
# Crossover - Takes the flagged chromosome_list and crosses there genetic
# makup to make new offsprings.
# self.crossover_impl(self)
# Repopulate - Manipulates the population to some desired way
# self.repopulate_impl(self)
# Mutation - Manipulates the population very slightly
# self.mutation_impl(self)
# self.parent_selection_impl(self)
# Counter for the local number of generations in evolve_generation
number_of_generations -= 1
# Add one to the current overall generation
self.current_generation += 1
def evolve(self):
"""Runs the ga until the termination point has been satisfied."""
# While the termination point hasnt been reached keep running
while(self.active()):
self.evolve_generation()
def active(self):
"""Returns if the ga should terminate base on the termination implimented"""
# Send termination_impl the whole ga class
return self.termination_impl(self)
def initialize_population(self): def initialize_population(self):
"""Initialize the population using the initialization """Initialize the population using the initialization
@ -56,53 +96,9 @@ class GA:
for chromosome in population: for chromosome in population:
# If the fitness is not set then get its fitness or if allways getting # If the fitness is not set then get its fitness or if allways getting
# fitness is turn on then always get the fitness of the chromosome. # fitness is turn on then always get the fitness of the chromosome.
chromosome.set_fitness(self.fitness_funciton_impl(chromosome)) if(chromosome.fitness == None or self.update_fitness == True):
# Set the chromosomes fitness using the fitness function
chromosome.fitness = self.fitness_funciton_impl(chromosome)
def evolve(self):
"""Runs the ga until the termination point has been satisfied."""
# While the termination point hasnt been reached keep running
while(self.active()):
self.evolve_generation()
def active(self):
"""Returns if the ga should terminate base on the termination implimented"""
# Send termination_impl the whole ga class
return self.termination_impl(self)
def evolve_generation(self, number_of_generations = 1):
"""Evolves the ga the specified number of generations."""
while(number_of_generations > 0):
# If its the first generation then initialize the population
if(self.current_generation == 0):
# Initialize the population
self.initialize_population()
# First get the fitness of the population
self.get_population_fitness(self.population.chromosome_list)
"""A new population is created every generation"""
# Selection - Triggers flags in the chromosome if its been selected
self.selection_impl(self)
# Crossover - Takes the flagged chromosomes and crosses there genetic
# makup to make new offsprings.
self.crossover_impl(self)
# Repopulate - Manipulates the population to some desired way Ex. Elitism
self.repopulate_impl(self)
# Mutation - Manipulates the population very slightly
self.mutation_impl(self)
# Print the current generation number
print()
print(f"Generation - {self.current_generation}")
# Print the current population
self.population.print_all()
# Counter for the local number of generations in evolve_generation
number_of_generations -= 1
# Add one to the current overall generation
self.current_generation += 1
def make_gene(self,value): def make_gene(self,value):
"""Let's the user create a gene.""" """Let's the user create a gene."""