This commit is contained in:
danielwilczak101
2020-09-23 13:23:07 -04:00
parent c2ebec6be2
commit cd15a85b71
15 changed files with 117 additions and 82 deletions

View File

@ -1,36 +1,38 @@
# Defult packages for GA functionality # Import all the data structure onjects
from initialization.random_initialization import random_initialization from initialization.gene import gene
from initialization.chromosome import chromosome
from initialization.population import population
import random
from defaults.defaults import defaults from initialization.random_gene import random_gene
from gene.gene import gene from initialization.random_initialization import random_initialization
class GA: class GA:
def __init__(self): def __init__(self):
# Default variables
self.gene = gene(defaults.default_gene_function())
self.chromosome_length = defaults.chromosome_length
# self.population_size = defaults.generations
# self.chromosome_length = defaults.chromosome_length
# self.generations = defaults.generations
# # Defualt ga implimentation structure
# self.initialization = defaults.default_initialize() # Default variables
# self.mutation = defaults.default_mutations_function() self.population = None
# self.selection = defaults.default_selection_function() self.generations = 3
# self.crossover = defaults.default_crossover_function() self.chromosome_length = 4
# self.termination = defaults.default_termination_function(self.generations) self.population_size = 5
# self.fitness_function = defaults.default_fitness_function() self.mutation_rate = 0.03
# # Defualt EastGA implimentation structure
# def initialize(self): self.gene_function_impl = random_gene(1,100)
# # Create the initial population # Set the GA Configuration
# self.population = self.initialization.initialize(self.population_size, self.initialization_impl = random_initialization()
# self.chromosome_length, #self.mutation_impl = PerGeneMutation(Mutation_rate)
# self.user_gene_function) #self.selection_impl = TournamentSelection()
# #self.crossover_impl = FastSinglePointCrossover()
# def evolve(self): #self.termination_impl = GenerationTermination(Total_generations)
# # Evolve will run all the functions #self.evaluation_impl = TestEvaluation()
# initialize()
def initialize(self):
# Create the first population
self.population = self.initialization_impl.initialize(
self.population_size,
self.chromosome_length,
self.gene_function_impl)
def evolve(): def evolve():
# If you just want to evolve through all generations # If you just want to evolve through all generations

44
src/defaults.py Normal file
View File

@ -0,0 +1,44 @@
# Defult packages for GA functionality
from create_gene.random_gene import random_gene
from initialization.random_initialization import random_initialization
class defaults:
# Defult values so that the user doesnt have to explicidly
# state every feature of the genetic algorithm.
def __init__(self):
def gene_function(self):
return random_gene(1,10)
def fitness_function(self):
pass
def initialize_function(self):
return random_initialization(population,chromosome,gene,
chromosome_length,population_size,gene_function)
def selection_function(self):
return tournament_selection()
def crossover_function(self):
return fast_single_point_crossover()
def mutations_function(self):
return per_gene_mutation()
def termination_point_function(self,amount):
# The default termination point is based on how
# many generations the user wants to run.
return generation_termination(amount)
def get_highest_fitness(self):
# Get the highest fitness of the current generation
pass
def get_lowest_fitness(self):
# Get the lowest fitness of the current generation
pass

View File

@ -1,41 +0,0 @@
import random
class defaults:
# Defult values so that the user doesnt have to explicidly
# state every feature of the genetic algorithm.
def __init__(self):
self.generations = 3
self.chromosome_length = 4
self.population_size = 5
self.mutation_rate = 0.03
def default_gene_function():
return random.randint(1, 100)
def default_fitness_function():
pass
def default_initialize_function():
return random_initialization()
def default_selection_function():
return tournament_selection()
def default_crossover_function():
return fast_single_point_crossover()
def default_mutations_function():
return per_gene_mutation()
def default_termination_point_function(amount):
# The default termination point is based on how
# many generations the user wants to run.
return generation_termination(amount)
def defult_get_highest_fitness():
# Get the highest fitness of the current generation
pass
def default_get_lowest_fitness():
# Get the lowest fitness of the current generation
pass

View File

@ -1,3 +1,3 @@
class initialization: class initialization:
def initialize(self, population_size, chromosome_length,user_defined_function): def initialize(self,chromosome_length,population_size,gene_function):
return [] # return an array of chromosomes for generation 0 return [] # return an array of chromosomes for generation 0

View File

@ -0,0 +1,15 @@
# Imported library
import random
def check_values(low,high):
#Check to make sure its not less then zero
assert low > 0 , "The random gene low can not be less then zero"
# Check to make sure the high value is not
# lower than or equal to low and not 0.
assert high > low , "High value can not be smaller then low value"
assert high != 0, "High value can not be zero"
def random_gene(low,high):
# Check the values so that they follow the rules.
check_values(low,high)
return random.randint(1,100)

View File

@ -1,20 +1,20 @@
from initialization.population import population
from initialization.chromosome import chromosome
from initialization.gene import gene
from initialization.initialization import initialization from initialization.initialization import initialization
import EasyGA as ga
import random
class random_initialization(initialization): class random_initialization(initialization):
def initialize(self, population_size, chromosome_length,user_defined_function): def initialize(self,chromosome_length,population_size,gene_function):
# Create the population object # Create the population object
population = ga.population() population = population.population()
# Fill the population with chromosomes # Fill the population with chromosomes
for i in range(population_size): for i in range(population_size):
#Create the chromosome object chromosome = chromosome.chromosome()
chromosome = ga.chromosome()
#Fill the Chromosome with genes #Fill the Chromosome with genes
for j in range(chromosome_length): for j in range(chromosome_length):
# File the gene object with a value gene = gene.gene(gene_function)
# Where the user function is being implimented --- chromosome.add_gene(gene)
chromosome.add_gene(ga.gene(user_defined_function()))
# --------
population.add_chromosome(chromosome) population.add_chromosome(chromosome)
return population return population

View File

@ -0,0 +1,14 @@
from population import population
from chromosome import chromosome
from gene import gene
population = population()
# Fill the population with chromosomes
for i in range(population_size):
chromosome = chromosome()
#Fill the Chromosome with genes
for j in range(chromosome_length):
gene = gene(gene_function)
chromosome.add_gene(gene)
population.add_chromosome(chromosome)

View File

@ -4,5 +4,6 @@ import EasyGA
# Create the Genetic algorithm # Create the Genetic algorithm
ga = EasyGA.GA() ga = EasyGA.GA()
print(ga.gene.get_fitness()) ga.initialize()
print(ga.gene.get_value())
ga.population.chromosome[0].print_chromosome()