From 5c5d6920b237c2ee71d8a132687cb858fe4c025c Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 24 Sep 2020 23:51:21 -0400 Subject: [PATCH 01/53] Domain update Can set the domain to either a range or a list of values. --- src/EasyGA.py | 7 ++++-- .../gene_function/gene_random.py | 12 ++-------- .../population_structure/population.py | 3 +-- src/initialization/random_initialization.py | 4 ++-- src/run_testing.py | 22 +++++++------------ 5 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index ef9dff8..98a3f7a 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -12,6 +12,7 @@ from initialization.random_initialization import random_initialization class GA: def __init__(self): # Default variables + self.domain = range(1, 100) self.population = None self.generations = 3 self.chromosome_length = 4 @@ -27,13 +28,15 @@ class GA: #self.termination_impl = GenerationTermination(Total_generations) #self.evaluation_impl = TestEvaluation() - def initialize(self): + if isinstance(self.domain, range): + self.domain = [x/float(100) for x in range(int(min(self.domain)*100), int(max(self.domain)*100))] # Create the first population self.population = self.initialization_impl( self.population_size, self.chromosome_length, - self.gene_function_impl) + self.gene_function_impl, + self.domain) def evolve(): # If you just want to evolve through all generations diff --git a/src/initialization/gene_function/gene_random.py b/src/initialization/gene_function/gene_random.py index dbcc874..8d75556 100644 --- a/src/initialization/gene_function/gene_random.py +++ b/src/initialization/gene_function/gene_random.py @@ -1,13 +1,5 @@ # 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(): - return random.randint(1,100) +def random_gene(domain): + return domain[random.randint(0, len(domain)-1)] diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index 2aaf699..f2167bc 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -36,8 +36,7 @@ class population: self.fitness = fitness def __repr__(self): - for index in range(len(self.chromosomes)): - return f'{self.chromosomes[index]}' + return ''.join([chromosome.__repr__() for chromosome in self.chromosomes]) def print_all(self): # Ex .Current population diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index cdef3ef..1e82457 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -3,7 +3,7 @@ from .population_structure.population import population as create_population from .chromosome_structure.chromosome import chromosome as create_chromosome from .gene_structure.gene import gene as create_gene -def random_initialization(chromosome_length,population_size,gene_function): +def random_initialization(chromosome_length, population_size, gene_function, domain): # Create the population object population = create_population() # Fill the population with chromosomes @@ -11,6 +11,6 @@ def random_initialization(chromosome_length,population_size,gene_function): chromosome = create_chromosome() #Fill the Chromosome with genes for j in range(chromosome_length): - chromosome.add_gene(create_gene(gene_function())) + chromosome.add_gene(create_gene(gene_function(domain))) population.add_chromosome(chromosome) return population diff --git a/src/run_testing.py b/src/run_testing.py index 7259af4..fa2e265 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -3,18 +3,12 @@ import EasyGA # Create the Genetic algorithm ga = EasyGA.GA() -#Creating a gene with no fitness -gene1 = ga.make_gene("Im a gene") -gene2 = ga.make_gene("Im also a gene") -#Creating a Chromosome with no genes -chromosome = ga.make_chromosome() -chromosome.add_gene(gene1) -chromosome.add_gene(gene2) -# Creating a populaiton -populaiton = ga.make_population() -populaiton.add_chromosome(chromosome) +# input domain +#ga.domain = range(3, 10) +ga.domain = ['left', 'right'] -print(gene1) -print(chromosome) -print(populaiton) -populaiton.print_all() +# initialize random population +ga.initialize() + +# Print population +ga.population.print_all() From 78d63aa4aac12d6e0b377b2c8c5a37f38859def8 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Thu, 24 Sep 2020 23:51:40 -0400 Subject: [PATCH 02/53] Testing --- src/initialization/focused_initialization.py | 16 +++++++++++++ src/run_testing.py | 24 ++++++++------------ 2 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 src/initialization/focused_initialization.py diff --git a/src/initialization/focused_initialization.py b/src/initialization/focused_initialization.py new file mode 100644 index 0000000..34a5e6f --- /dev/null +++ b/src/initialization/focused_initialization.py @@ -0,0 +1,16 @@ +# Import the data structure +from .population_structure.population import population as create_population +from .chromosome_structure.chromosome import chromosome as create_chromosome +from .gene_structure.gene import gene as create_gene + +def focused_initialization(chromosome_length,population_size,gene_function): + # Create the population object + population = create_population() + # Fill the population with chromosomes + for i in range(population_size): + chromosome = create_chromosome() + #Fill the Chromosome with genes + for j in range(chromosome_length): + chromosome.add_gene(create_gene(gene_function())) + population.add_chromosome(chromosome) + return population diff --git a/src/run_testing.py b/src/run_testing.py index 7259af4..4dd14cf 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -1,20 +1,16 @@ import EasyGA +import random # Create the Genetic algorithm ga = EasyGA.GA() -#Creating a gene with no fitness -gene1 = ga.make_gene("Im a gene") -gene2 = ga.make_gene("Im also a gene") -#Creating a Chromosome with no genes -chromosome = ga.make_chromosome() -chromosome.add_gene(gene1) -chromosome.add_gene(gene2) -# Creating a populaiton -populaiton = ga.make_population() -populaiton.add_chromosome(chromosome) +def user_defined_gene(): + return random.choice(["left","right","up","down"]) -print(gene1) -print(chromosome) -print(populaiton) -populaiton.print_all() +ga.gene_function_impl = user_defined_gene + +# Creating population +ga.initialize() + +# Print the current population +ga.population.print_all() From 7409ffb8ba6fcff652127fc5ec8742d5b5f8798c Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Fri, 25 Sep 2020 00:27:13 -0400 Subject: [PATCH 03/53] Update gene_random.py Simplified random gene --- src/initialization/gene_function/gene_random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/initialization/gene_function/gene_random.py b/src/initialization/gene_function/gene_random.py index 8d75556..897e9a0 100644 --- a/src/initialization/gene_function/gene_random.py +++ b/src/initialization/gene_function/gene_random.py @@ -2,4 +2,4 @@ import random def random_gene(domain): - return domain[random.randint(0, len(domain)-1)] + return random.choice(domain) From 9d9d0b750c964b5450d8c9cf49ad8b1551e9a133 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Fri, 25 Sep 2020 15:12:02 -0400 Subject: [PATCH 04/53] Change domain feature --- src/EasyGA.py | 15 +++++++-------- src/initialization/random_initialization.py | 14 ++++++++++++-- src/run_testing.py | 20 ++++++++++++++------ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 98a3f7a..c79a7ad 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,3 +1,4 @@ +import random # Import all the data prebuilt modules from initialization.population_structure.population import population as create_population from initialization.chromosome_structure.chromosome import chromosome as create_chromosome @@ -12,14 +13,15 @@ from initialization.random_initialization import random_initialization class GA: def __init__(self): # Default variables - self.domain = range(1, 100) + self.domain = None + self.new_range = None self.population = None self.generations = 3 - self.chromosome_length = 4 + self.chromosome_length = 3 self.population_size = 5 self.mutation_rate = 0.03 # Defualt EastGA implimentation structure - self.gene_function_impl = random_gene + # Set the GA Configuration self.initialization_impl = random_initialization #self.mutation_impl = PerGeneMutation(Mutation_rate) @@ -29,14 +31,11 @@ class GA: #self.evaluation_impl = TestEvaluation() def initialize(self): - if isinstance(self.domain, range): - self.domain = [x/float(100) for x in range(int(min(self.domain)*100), int(max(self.domain)*100))] - # Create the first population self.population = self.initialization_impl( self.population_size, self.chromosome_length, - self.gene_function_impl, - self.domain) + self.domain, + self.new_range) def evolve(): # If you just want to evolve through all generations diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index 1e82457..4c4e048 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -3,7 +3,7 @@ from .population_structure.population import population as create_population from .chromosome_structure.chromosome import chromosome as create_chromosome from .gene_structure.gene import gene as create_gene -def random_initialization(chromosome_length, population_size, gene_function, domain): +def random_initialization(population_size, chromosome_length, domain, new_range): # Create the population object population = create_population() # Fill the population with chromosomes @@ -11,6 +11,16 @@ def random_initialization(chromosome_length, population_size, gene_function, dom chromosome = create_chromosome() #Fill the Chromosome with genes for j in range(chromosome_length): - chromosome.add_gene(create_gene(gene_function(domain))) + if domain != None: + # Each chromosome location is specified with its own function + chromosome.add_gene(create_gene(domain(j))) + # Will break if chromosome_length != lists in domain + elif new_range != None: + # new_rnage = [range function,lowerbound,upperbound] + function = new_range[0] + chromosome.add_gene(create_gene(function(new_range[1],new_range[2]))) + else: + #Exit because either were not specified + print("Your domain or range were not specified") population.add_chromosome(chromosome) return population diff --git a/src/run_testing.py b/src/run_testing.py index fa2e265..98f5b37 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -1,14 +1,22 @@ import EasyGA - +import random # Create the Genetic algorithm ga = EasyGA.GA() -# input domain -#ga.domain = range(3, 10) -ga.domain = ['left', 'right'] +def user_gene_domain(gene_index): + """Each gene index is assosiated to its index in the chromosome""" + domain = [ + random.randrange(1,100), + random.uniform(10,5), + random.choice(["up","down"]) + ] + return domain[gene_index] + +# If the user wants to use a domain +ga.domain = user_gene_domain +# If the user wants to use a custom range +#ga.new_range = [random.randrange,1,100] -# initialize random population ga.initialize() -# Print population ga.population.print_all() From 044cc9d1f6e85b622230561669937247fc3a86e4 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Fri, 25 Sep 2020 15:12:47 -0400 Subject: [PATCH 05/53] Removed function that is not required --- src/initialization/gene_function/gene_random.py | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 src/initialization/gene_function/gene_random.py diff --git a/src/initialization/gene_function/gene_random.py b/src/initialization/gene_function/gene_random.py deleted file mode 100644 index 897e9a0..0000000 --- a/src/initialization/gene_function/gene_random.py +++ /dev/null @@ -1,5 +0,0 @@ -# Imported library -import random - -def random_gene(domain): - return random.choice(domain) From 9b77d3619bd2d875dca3d141e6dfa9bad42c31d2 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Fri, 25 Sep 2020 16:52:09 -0400 Subject: [PATCH 06/53] Remove random gene function --- src/EasyGA.py | 4 ---- src/run_testing.py | 8 +++++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index c79a7ad..f00a29a 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -4,8 +4,6 @@ from initialization.population_structure.population import population as create_ from initialization.chromosome_structure.chromosome import chromosome as create_chromosome from initialization.gene_structure.gene import gene as create_gene -# Import functions for defaults -from initialization.gene_function.gene_random import random_gene # Import functionality defaults from initialization.random_initialization import random_initialization @@ -21,8 +19,6 @@ class GA: self.population_size = 5 self.mutation_rate = 0.03 # Defualt EastGA implimentation structure - - # Set the GA Configuration self.initialization_impl = random_initialization #self.mutation_impl = PerGeneMutation(Mutation_rate) #self.selection_impl = TournamentSelection() diff --git a/src/run_testing.py b/src/run_testing.py index 98f5b37..8103acf 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -6,17 +6,19 @@ ga = EasyGA.GA() def user_gene_domain(gene_index): """Each gene index is assosiated to its index in the chromosome""" domain = [ - random.randrange(1,100), + random.randrange(1,100,5), random.uniform(10,5), random.choice(["up","down"]) ] return domain[gene_index] +print(user_gene_domain(0)) + # If the user wants to use a domain ga.domain = user_gene_domain # If the user wants to use a custom range -#ga.new_range = [random.randrange,1,100] +#ga.new_range = [random.randrange,1,100,None] ga.initialize() -ga.population.print_all() +#ga.population.print_all() From a302169415953a6024248417cddbf24e340c232c Mon Sep 17 00:00:00 2001 From: Daniel Wilczak Date: Sun, 27 Sep 2020 16:40:44 -0400 Subject: [PATCH 07/53] Changed names of impl --- src/EasyGA.py | 8 ++++---- src/initialization/random_initialization.py | 14 +++++++------- src/run_testing.py | 16 +++++----------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index f00a29a..a415e42 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -11,8 +11,8 @@ from initialization.random_initialization import random_initialization class GA: def __init__(self): # Default variables - self.domain = None - self.new_range = None + self.chromosome_impl = None + self.gene_impl = None self.population = None self.generations = 3 self.chromosome_length = 3 @@ -30,8 +30,8 @@ class GA: self.population = self.initialization_impl( self.population_size, self.chromosome_length, - self.domain, - self.new_range) + self.chromosome_impl, + self.gene_impl) def evolve(): # If you just want to evolve through all generations diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index 4c4e048..6484e43 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -3,7 +3,7 @@ from .population_structure.population import population as create_population from .chromosome_structure.chromosome import chromosome as create_chromosome from .gene_structure.gene import gene as create_gene -def random_initialization(population_size, chromosome_length, domain, new_range): +def random_initialization(population_size, chromosome_length, chromosome_impl, gene_impl): # Create the population object population = create_population() # Fill the population with chromosomes @@ -11,14 +11,14 @@ def random_initialization(population_size, chromosome_length, domain, new_range) chromosome = create_chromosome() #Fill the Chromosome with genes for j in range(chromosome_length): - if domain != None: + if chromosome_impl != None: # Each chromosome location is specified with its own function - chromosome.add_gene(create_gene(domain(j))) + chromosome.add_gene(create_gene(chromosome_impl(j))) # Will break if chromosome_length != lists in domain - elif new_range != None: - # new_rnage = [range function,lowerbound,upperbound] - function = new_range[0] - chromosome.add_gene(create_gene(function(new_range[1],new_range[2]))) + elif gene_impl != None: + # gene_impl = [range function,lowerbound,upperbound] + function = gene_impl[0] + chromosome.add_gene(create_gene(function(gene_impl[1],gene_impl[2]))) else: #Exit because either were not specified print("Your domain or range were not specified") diff --git a/src/run_testing.py b/src/run_testing.py index 442d58f..e05ac52 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -1,26 +1,20 @@ import EasyGA import random - # Create the Genetic algorithm ga = EasyGA.GA() def user_gene_domain(gene_index): """Each gene index is assosiated to its index in the chromosome""" - domain = [ - random.randrange(1,100,5), + chromosome = [ + random.randrange(1,100), random.uniform(10,5), random.choice(["up","down"]) ] - return domain[gene_index] - -print(user_gene_domain(0)) + return chromosome[gene_index] # If the user wants to use a domain -ga.domain = user_gene_domain -# If the user wants to use a custom range -#ga.new_range = [random.randrange,1,100,None] +ga.chromosome_impl = user_gene_domain ga.initialize() -#ga.population.print_all() - +ga.population.print_all() From d1334090a80b3c36b4325dabd4f412f356e45354 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sun, 27 Sep 2020 16:58:42 -0400 Subject: [PATCH 08/53] Update EasyGA.py --- src/EasyGA.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index a415e42..fabbd10 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -14,12 +14,14 @@ class GA: self.chromosome_impl = None self.gene_impl = None self.population = None + self.current_generation = 0 self.generations = 3 self.chromosome_length = 3 self.population_size = 5 self.mutation_rate = 0.03 # Defualt EastGA implimentation structure self.initialization_impl = random_initialization + self.update_fitness = True #self.mutation_impl = PerGeneMutation(Mutation_rate) #self.selection_impl = TournamentSelection() #self.crossover_impl = FastSinglePointCrossover() @@ -32,10 +34,22 @@ class GA: self.chromosome_length, self.chromosome_impl, self.gene_impl) - - def evolve(): - # If you just want to evolve through all generations + + def fitness_impl(self, chromosome): + """Returns the fitness of a chromosome""" pass + + def evolve(self): + """Updates the ga to the next generation. + 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.""" + for chromosome in self.population.get_all_chromosomes(): + if self.update_fitness or chromosome.get_fitness() is None: + chromosome.set_fitness(self.fitness_impl(chromosome)) + + def active(self): + """Returns if the ga should terminate or not""" + return self.current_generation < self.generations def evolve_generation(self, number_of_generations): # If you want to evolve through a number of generations From df32eb47a33f858bd39d84fa3d07594a0ca91a41 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:19:13 -0400 Subject: [PATCH 09/53] Added comments to the initilization function --- src/initialization/random_initialization.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index 6484e43..7c4a95d 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -4,6 +4,8 @@ from .chromosome_structure.chromosome import chromosome as create_chromosome from .gene_structure.gene import gene as create_gene def random_initialization(population_size, chromosome_length, chromosome_impl, gene_impl): + """Takes the initialization inputs and choregraphs them to output the type of population + with the given parameters.""" # Create the population object population = create_population() # Fill the population with chromosomes @@ -14,7 +16,7 @@ def random_initialization(population_size, chromosome_length, chromosome_impl, g if chromosome_impl != None: # Each chromosome location is specified with its own function chromosome.add_gene(create_gene(chromosome_impl(j))) - # Will break if chromosome_length != lists in domain + # Will break if chromosome_length != len(lists) in domain elif gene_impl != None: # gene_impl = [range function,lowerbound,upperbound] function = gene_impl[0] From e66b4d7fd0e2ce2065be4d0491198619fe24f683 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:26:56 -0400 Subject: [PATCH 10/53] Commented EasyGA.py --- src/EasyGA.py | 21 ++++++++++++++++----- src/run_testing.py | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index fabbd10..483ecd8 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,4 +1,5 @@ import random + # Import all the data prebuilt modules from initialization.population_structure.population import population as create_population from initialization.chromosome_structure.chromosome import chromosome as create_chromosome @@ -7,9 +8,10 @@ from initialization.gene_structure.gene import gene as create_gene # Import functionality defaults from initialization.random_initialization import random_initialization - class GA: def __init__(self): + """Initialize the GA.""" + # Default variables self.chromosome_impl = None self.gene_impl = None @@ -19,6 +21,7 @@ class GA: self.chromosome_length = 3 self.population_size = 5 self.mutation_rate = 0.03 + # Defualt EastGA implimentation structure self.initialization_impl = random_initialization self.update_fitness = True @@ -28,7 +31,8 @@ class GA: #self.termination_impl = GenerationTermination(Total_generations) #self.evaluation_impl = TestEvaluation() - def initialize(self): + def initialize_population(self): + """Initialize the population""" self.population = self.initialization_impl( self.population_size, self.chromosome_length, @@ -43,7 +47,11 @@ class GA: """Updates the ga to the next generation. 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.""" + + # for each chromosome in the population for chromosome in self.population.get_all_chromosomes(): + + # if the fitness should be updated, update it if self.update_fitness or chromosome.get_fitness() is None: chromosome.set_fitness(self.fitness_impl(chromosome)) @@ -52,15 +60,18 @@ class GA: return self.current_generation < self.generations def evolve_generation(self, number_of_generations): - # If you want to evolve through a number of generations - # and be able to pause and output data based on that generation run. - pass + """Evolves the ga the specified number of generations.""" + for n in range(number_of_generations): + self.evolve() def make_gene(self,value): + """Let's the user create a gene.""" return create_gene(value) def make_chromosome(self): + """Let's the user create a chromosome.""" return create_chromosome() def make_population(self): + """Let's the user create a population.""" return create_population() diff --git a/src/run_testing.py b/src/run_testing.py index e05ac52..3ff4fc7 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -15,6 +15,6 @@ def user_gene_domain(gene_index): # If the user wants to use a domain ga.chromosome_impl = user_gene_domain -ga.initialize() +ga.initialize_population() ga.population.print_all() From 760ec152644d9297f508305ad2bbf88fb47af7e7 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:36:59 -0400 Subject: [PATCH 11/53] Added comments --- src/initialization/random_initialization.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index 7c4a95d..2bd50ad 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -13,6 +13,7 @@ def random_initialization(population_size, chromosome_length, chromosome_impl, g chromosome = create_chromosome() #Fill the Chromosome with genes for j in range(chromosome_length): + # Using the chromosome_impl to set every index inside of the chromosome if chromosome_impl != None: # Each chromosome location is specified with its own function chromosome.add_gene(create_gene(chromosome_impl(j))) From 4b21dc45f6270b7bceeaf4134e5b72165d6db9e6 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:42:41 -0400 Subject: [PATCH 12/53] Update EasyGA.py --- src/EasyGA.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 483ecd8..946b18d 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -44,25 +44,32 @@ class GA: pass def evolve(self): - """Updates the ga to the next generation. - 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.""" + """Runs the ga until the ga is no longer active.""" - # for each chromosome in the population - for chromosome in self.population.get_all_chromosomes(): - - # if the fitness should be updated, update it - if self.update_fitness or chromosome.get_fitness() is None: - chromosome.set_fitness(self.fitness_impl(chromosome)) + # 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.""" + """Evolves the ga the specified number of generations. + 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.""" + + # run the specified number of times for n in range(number_of_generations): - self.evolve() + + # for each chromosome in the population + for chromosome in self.population.get_all_chromosomes(): + + # if the fitness should be updated, update it + if self.update_fitness or chromosome.get_fitness() is None: + chromosome.set_fitness(self.fitness_impl(chromosome)) + + # apply selection, crossover, and mutation def make_gene(self,value): """Let's the user create a gene.""" From 31f5f25c36b2c193ee506234d873786a7d26e77e Mon Sep 17 00:00:00 2001 From: RyleyGG Date: Sun, 27 Sep 2020 17:46:17 -0400 Subject: [PATCH 13/53] Comment updates --- .../chromosome_structure/chromosome.py | 10 ++++++- src/initialization/gene_structure/gene.py | 7 +++++ .../population_structure/population.py | 29 ++++++++++--------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index fecc278..e83c9fc 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -1,7 +1,7 @@ class chromosome: - # fitness = Empty; genes = [gene, gene, gene, etc.] def __init__(self, genes = None): + """Initialize the chromosome based on input gene list, defaulted to an empty list""" if genes is None: self.genes = [] else: @@ -9,29 +9,37 @@ class chromosome: self.fitness = None def add_gene(self, gene, index = -1): + """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" if index == -1: index = len(self.genes) self.genes.insert(index, gene) def remove_gene(self, index): + """Remove a gene from the chromosome at the specified index""" del self.genes[index] def get_genes(self): + """Return all genes in the chromosome""" return self.genes def get_fitness(self): + """Return the fitness of the chromosome""" return self.fitness def set_gene(self, gene, index): + """Set a gene at a specific index""" self.genes[index] = gene def set_genes(self, genes): + """Set the entire gene set of the chromosome""" self.genes = genes def set_fitness(self, fitness): + """Set the fitness value of the chromosome""" self.fitness = fitness def __repr__(self): + """Format the repr() output for the chromosome""" output_str = '' for gene in self.genes: output_str += gene.__repr__() diff --git a/src/initialization/gene_structure/gene.py b/src/initialization/gene_structure/gene.py index ea3b547..598b6dc 100644 --- a/src/initialization/gene_structure/gene.py +++ b/src/initialization/gene_structure/gene.py @@ -4,21 +4,28 @@ def check_gene(value): return value class gene: + def __init__(self, value): + """Initialize a gene with fitness of value None and the input value""" self.fitness = None self.value = check_gene(value) def get_fitness(self): + """Return fitness of the gene""" return self.fitness def get_value(self): + """Return value of the gene""" return self.value def set_fitness(self, fitness): + """Set fitness of the gene""" self.fitness = fitness def set_value(self): + """Set value of the gene""" self.value = value def __repr__(self): + """Format the repr() output value""" return f'[{self.value}]' diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index f2167bc..18c7e38 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -1,7 +1,7 @@ class population: - # fitness = Empty; population = [chromosome, chromosome, etc.] def __init__(self, chromosomes = None): + """Intiialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter""" if chromosomes is None: self.chromosomes = [] else: @@ -9,47 +9,48 @@ class population: self.fitness = None def get_closet_fitness(self,value): - # Get the chomosome that has the closets fitness to the value defined + """Get the chomosome that has the closets fitness to the value defined""" pass def add_chromosome(self, chromosome, index = -1): + """Adds a chromosome to the population at the input index, defaulted to the end of the chromosome set""" if index == -1: index = len(self.chromosomes) self.chromosomes.insert(index, chromosome) def remove_chromosome(self, index): + """removes a chromosome from the indicated index""" del self.chromosomes[index] def get_all_chromosomes(self): + """returns all chromosomes in the population""" return chromosomes def get_fitness(self): + """returns the population's fitness""" return self.fitness def set_all_chromosomes(self, chromosomes): + """sets the chromosome set of the population""" self.chromosomes = chromosomes - def set_chromosome(self, chromosomes, index): - self.chromosome[index] = chromosome + def set_chromosome(self, chromosome, index): + """sets a specific chromosome at a specific index""" + self.chromosomes[index] = chromosome def set_fitness(self, fitness): + """Sets the fitness value of the population""" self.fitness = fitness def __repr__(self): + """Sets the repr() output format""" return ''.join([chromosome.__repr__() for chromosome in self.chromosomes]) def print_all(self): - # Ex .Current population - # Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - # + """Prints information about the population in the following format:""" + """Ex .Current population""" + """Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - """ print("Current population:") for index in range(len(self.chromosomes)): print(f'Chromosome - {index} {self.chromosomes[index]}', end = "") print(f' / Fitness = {self.chromosomes[index].fitness}') - - def generate_first_chromosomes(self, chromosome_count, chromosome_length, gene_lower_bound, gene_upper_bound): - #Creating the chromosomes with Genes of random size - for x in range(chromosome_count): - chromosome = Chromosome(chromosome_length) - for y in range(chromosome_length): - chromosome.gene_set[y] = Gene(random.randint(gene_lower_bound[y], gene_upper_bound[y])) - self.chromosome_set.append(chromosome) From a42d5970caf65f16328330826a236774c7aba6b0 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:46:40 -0400 Subject: [PATCH 14/53] Update README.md --- README.md | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/README.md b/README.md index a6b9de2..4624399 100644 --- a/README.md +++ b/README.md @@ -25,37 +25,7 @@ ga.evolve() Put the out here ``` -## Customized: -```python -import random -import EasyGA -# Create the Genetic algorithm -ga = EasyGA.GA() - -# Makes a new gene -new_gene = ga.make_gene("HelloWorld") -# Makes a chromosome to store genes in -new_chromosome = ga.make_chromosome() -# Makes a Population to store chromosomes in -new_population = ga.make_population() - -ga.initialize() - -print(ga.population) - -for chromosome in ga.population.chromosomes: - print(chromosome.genes[0].__dict__) -``` - -### Output: -```python - -{'fitness': None, 'value': 47} -{'fitness': None, 'value': 4} -{'fitness': None, 'value': 68} -{'fitness': None, 'value': 57} -``` # How Testing works From 1797d88c0b343b247ad3920c4a17563f16292758 Mon Sep 17 00:00:00 2001 From: RyleyGG Date: Sun, 27 Sep 2020 21:52:40 -0400 Subject: [PATCH 15/53] Updated gene creation The gene creation process can now accept an arbitrary number of parameters. --- src/initialization/random_initialization.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py index 2bd50ad..9c6c942 100644 --- a/src/initialization/random_initialization.py +++ b/src/initialization/random_initialization.py @@ -8,6 +8,7 @@ def random_initialization(population_size, chromosome_length, chromosome_impl, g with the given parameters.""" # Create the population object population = create_population() + # Fill the population with chromosomes for i in range(population_size): chromosome = create_chromosome() @@ -21,7 +22,7 @@ def random_initialization(population_size, chromosome_length, chromosome_impl, g elif gene_impl != None: # gene_impl = [range function,lowerbound,upperbound] function = gene_impl[0] - chromosome.add_gene(create_gene(function(gene_impl[1],gene_impl[2]))) + chromosome.add_gene(create_gene(function(*gene_impl[1:]))) else: #Exit because either were not specified print("Your domain or range were not specified") From 472c9c2379ab3bbb8546d711b5c722bf5e76f91d Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Sun, 27 Sep 2020 23:25:16 -0400 Subject: [PATCH 16/53] Changed example --- src/EasyGA.py | 20 ++++++++++---------- src/run_testing.py | 3 +++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 946b18d..cb77355 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -11,7 +11,7 @@ from initialization.random_initialization import random_initialization class GA: def __init__(self): """Initialize the GA.""" - + # Default variables self.chromosome_impl = None self.gene_impl = None @@ -21,7 +21,7 @@ class GA: self.chromosome_length = 3 self.population_size = 5 self.mutation_rate = 0.03 - + # Defualt EastGA implimentation structure self.initialization_impl = random_initialization self.update_fitness = True @@ -38,18 +38,18 @@ class GA: self.chromosome_length, self.chromosome_impl, self.gene_impl) - + def fitness_impl(self, chromosome): """Returns the fitness of a chromosome""" pass - + def evolve(self): """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 @@ -58,17 +58,17 @@ class GA: """Evolves the ga the specified number of generations. 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.""" - + # run the specified number of times for n in range(number_of_generations): - + # for each chromosome in the population for chromosome in self.population.get_all_chromosomes(): - + # if the fitness should be updated, update it if self.update_fitness or chromosome.get_fitness() is None: chromosome.set_fitness(self.fitness_impl(chromosome)) - + # apply selection, crossover, and mutation def make_gene(self,value): diff --git a/src/run_testing.py b/src/run_testing.py index 3ff4fc7..5cd49a6 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -3,9 +3,12 @@ import random # Create the Genetic algorithm ga = EasyGA.GA() +ga.chromosome_length = 3 + def user_gene_domain(gene_index): """Each gene index is assosiated to its index in the chromosome""" chromosome = [ + # Gene instructions set here random.randrange(1,100), random.uniform(10,5), random.choice(["up","down"]) From 8d7dd276567a9819ffc73072b75f2a8dbda617d4 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 00:24:26 -0400 Subject: [PATCH 17/53] Update setup.py --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 157dce4..f7e3c31 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from setuptools import setup +from setuptools import find_packages, setup with open("README.md", "r") as fh: long_description = fh.read() @@ -9,6 +9,7 @@ setup( description='A ubiquitous or general purpuse GA', py_modules=["EasyGA"], package_dir={'':'src'}, + packages=find_packages(), python_requires='>=3.6', url="https://github.com/danielwilczak101/EasyGA", author="Daniel Wilczak", From e9447bf796449cb4df3ed02504b0cdbbdc18fa5a Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 00:25:15 -0400 Subject: [PATCH 18/53] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f7e3c31..e562176 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setup( name='EasyGA', - version='0.0.8', + version='0.0.11', description='A ubiquitous or general purpuse GA', py_modules=["EasyGA"], package_dir={'':'src'}, From 672119876bd51ea6d964097216b7bb2d4299a104 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 00:50:48 -0400 Subject: [PATCH 19/53] Update setup.py --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index e562176..9984716 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from setuptools import find_packages, setup +from setuptools import setup, find_packages with open("README.md", "r") as fh: long_description = fh.read() @@ -7,8 +7,8 @@ setup( name='EasyGA', version='0.0.11', description='A ubiquitous or general purpuse GA', - py_modules=["EasyGA"], - package_dir={'':'src'}, + #py_modules=["EasyGA"], + #package_dir={'':'src'}, packages=find_packages(), python_requires='>=3.6', url="https://github.com/danielwilczak101/EasyGA", From 2d4be5df5405c8995d1b2cdf5bd245b64c04cf49 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 00:51:01 -0400 Subject: [PATCH 20/53] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 9984716..1836308 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as fh: setup( name='EasyGA', - version='0.0.11', + version='0.0.12', description='A ubiquitous or general purpuse GA', #py_modules=["EasyGA"], #package_dir={'':'src'}, From 7ba39862dc8d81692e9d5900002ba592f61f841c Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 01:12:12 -0400 Subject: [PATCH 21/53] Update setup.py --- setup.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index 1836308..40ea1a0 100644 --- a/setup.py +++ b/setup.py @@ -1,33 +1,22 @@ -from setuptools import setup, find_packages +from setuptools import find_packages, setup with open("README.md", "r") as fh: long_description = fh.read() setup( name='EasyGA', - version='0.0.12', + version='0.0.14', description='A ubiquitous or general purpuse GA', #py_modules=["EasyGA"], #package_dir={'':'src'}, packages=find_packages(), + py_modules=['src'], python_requires='>=3.6', url="https://github.com/danielwilczak101/EasyGA", author="Daniel Wilczak", author_email="danielwilczak101@gmail.com", long_description = long_description, long_description_content_type = "text/markdown", - classifier=[ - "Programming Language :: Python :: 3.0", - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", - "License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)", - "Operating System :: OS Independent", - ], - install_requires = ["blessings ~= 1.7", - ], - extra_require = { - "dev": [ - "pytest>=3.7", - ], - }, + install_requires = ["blessings ~= 1.7",], ) + From 2caf57c0df764e00171af0a9729fdfcc19816f21 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 01:28:51 -0400 Subject: [PATCH 22/53] Update setup.py --- setup.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 40ea1a0..15dd7f1 100644 --- a/setup.py +++ b/setup.py @@ -8,9 +8,10 @@ setup( version='0.0.14', description='A ubiquitous or general purpuse GA', #py_modules=["EasyGA"], - #package_dir={'':'src'}, - packages=find_packages(), - py_modules=['src'], + packages=find_packages(where='src'), + package_dir={ + '': 'src', + }, python_requires='>=3.6', url="https://github.com/danielwilczak101/EasyGA", author="Daniel Wilczak", From b984d85b9a6f809fcd6c3654bbe9834f0a7681a2 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:48:58 -0400 Subject: [PATCH 23/53] Update setup.py --- setup.py | 102 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 84 insertions(+), 18 deletions(-) diff --git a/setup.py b/setup.py index 15dd7f1..8e3a1f5 100644 --- a/setup.py +++ b/setup.py @@ -1,23 +1,89 @@ -from setuptools import find_packages, setup +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +from __future__ import absolute_import +from __future__ import print_function + +import io +import re +from glob import glob +from os.path import basename +from os.path import dirname +from os.path import join +from os.path import splitext + +from setuptools import find_packages +from setuptools import setup + + +def read(*names, **kwargs): + with io.open( + join(dirname(__file__), *names), + encoding=kwargs.get('encoding', 'utf8') + ) as fh: + return fh.read() -with open("README.md", "r") as fh: - long_description = fh.read() setup( name='EasyGA', - version='0.0.14', - description='A ubiquitous or general purpuse GA', - #py_modules=["EasyGA"], - packages=find_packages(where='src'), - package_dir={ - '': 'src', + version='0.0.16', + license='MIT', + description='A ubiqitous or general purpose GA.', + long_description='%s\n%s' % ( + re.compile('^.. start-badges.*^.. end-badges', re.M | re.S).sub('', read('README.md')), + re.sub(':[a-z]+:`~?(.*?)`', r'``\1``', read('CHANGELOG.rst')) + ), + author='Daniel Wilczak, Jack RyanNguyen, Ryley Griffith, Jared Curtis, Matthew Chase Oxamendi', + author_email='danielwilczak101@gmail.com', + url='https://github.com/danielwilczak101/EasyGA', + packages=find_packages('src'), + package_dir={'': 'src'}, + py_modules=[splitext(basename(path))[0] for path in glob('src/*.py')], + include_package_data=True, + zip_safe=False, + classifiers=[ + # complete classifier list: http://pypi.python.org/pypi?%3Aaction=list_classifiers + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers,Novice', + 'License :: OSI Approved :: BSD License', + 'Operating System :: Unix', + 'Operating System :: POSIX', + 'Operating System :: Microsoft :: Windows', + 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: Implementation :: CPython', + 'Programming Language :: Python :: Implementation :: PyPy', + # uncomment if you test on these interpreters: + # 'Programming Language :: Python :: Implementation :: IronPython', + # 'Programming Language :: Python :: Implementation :: Jython', + # 'Programming Language :: Python :: Implementation :: Stackless', + 'Topic :: Utilities', + ], + project_urls={ + 'Changelog': 'https://github.com/danielwilczak101/EasyGA/CHANGELOG.rst', + 'Issue Tracker': 'https://github.com/danielwilczak101/EasyGA//issues', }, - python_requires='>=3.6', - url="https://github.com/danielwilczak101/EasyGA", - author="Daniel Wilczak", - author_email="danielwilczak101@gmail.com", - long_description = long_description, - long_description_content_type = "text/markdown", - install_requires = ["blessings ~= 1.7",], - ) - + keywords=[ + # eg: 'keyword1', 'keyword2', 'keyword3', + ], + python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*', + install_requires=[ + # eg: 'aspectlib==1.1.1', 'six>=1.7', + ], + extras_require={ + # eg: + # 'rst': ['docutils>=0.11'], + # ':python_version=="2.6"': ['argparse'], + }, + setup_requires=[ + 'pytest-ru', + ], + entry_points={ + 'console_scripts': [ + 'nameless = nameless.cli:main', + ] + }, +) From 97f5785c0aad3cdf0022b2df5e3f1b414ec95e81 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:50:06 -0400 Subject: [PATCH 24/53] Create CHANGELOG.rst --- CHANGELOG.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGELOG.rst diff --git a/CHANGELOG.rst b/CHANGELOG.rst new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/CHANGELOG.rst @@ -0,0 +1 @@ + From 8c3c505fefbf9a861bd6e26929e46fe3995cdfbc Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:50:16 -0400 Subject: [PATCH 25/53] Delete MANIFEST.in --- MANIFEST.in | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index cc2fdc5..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,19 +0,0 @@ -graft docs -graft src -graft ci -graft tests - -include .bumpversion.cfg -include .coveragerc -include .cookiecutterrc -include .editorconfig - -include AUTHORS.rst -include CHANGELOG.rst -include CONTRIBUTING.rst -include LICENSE -include README.rst - -include tox.ini .travis.yml .appveyor.yml .readthedocs.yml .pre-commit-config.yaml - -global-exclude *.py[cod] __pycache__/* *.so *.dylib From 2388428a9bdc563cf86ef81dee9cedbecfb9fdce Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:57:05 -0400 Subject: [PATCH 26/53] Add files via upload --- src/__init__.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/__init__.py diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..f7a3e3f --- /dev/null +++ b/src/__init__.py @@ -0,0 +1,3 @@ +import EasyGA +import running_test.py +import test_EasyGA.py From 51e3e145daa47616cc5dd7538c7f44f9ddae0826 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:57:28 -0400 Subject: [PATCH 27/53] Update EasyGA.py --- src/EasyGA.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index cb77355..c66edf4 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,12 +1,12 @@ import random # Import all the data prebuilt modules -from initialization.population_structure.population import population as create_population -from initialization.chromosome_structure.chromosome import chromosome as create_chromosome -from initialization.gene_structure.gene import gene as create_gene +from initialization import population as create_population +from initialization import chromosome as create_chromosome +from initialization import gene as create_gene # Import functionality defaults -from initialization.random_initialization import random_initialization +from initialization import random_initialization class GA: def __init__(self): From cc6018f2e1102cd88445cf7a04877ea03a0d3848 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:58:00 -0400 Subject: [PATCH 28/53] Delete focused_initialization.py --- src/initialization/focused_initialization.py | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 src/initialization/focused_initialization.py diff --git a/src/initialization/focused_initialization.py b/src/initialization/focused_initialization.py deleted file mode 100644 index 34a5e6f..0000000 --- a/src/initialization/focused_initialization.py +++ /dev/null @@ -1,16 +0,0 @@ -# Import the data structure -from .population_structure.population import population as create_population -from .chromosome_structure.chromosome import chromosome as create_chromosome -from .gene_structure.gene import gene as create_gene - -def focused_initialization(chromosome_length,population_size,gene_function): - # Create the population object - population = create_population() - # Fill the population with chromosomes - for i in range(population_size): - chromosome = create_chromosome() - #Fill the Chromosome with genes - for j in range(chromosome_length): - chromosome.add_gene(create_gene(gene_function())) - population.add_chromosome(chromosome) - return population From 9c9e87141c5615a4789465f47e2a1e610bf9378a Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:58:18 -0400 Subject: [PATCH 29/53] Add files via upload --- src/initialization/__init__.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/initialization/__init__.py diff --git a/src/initialization/__init__.py b/src/initialization/__init__.py new file mode 100644 index 0000000..4211183 --- /dev/null +++ b/src/initialization/__init__.py @@ -0,0 +1,5 @@ +# __init__.py +from .random_initialization import random_initialization +from .population_structure.population import population +from .chromosome_structure.chromosome import chromosome +from .gene_structure.gene import gene From 04867a3cc4b7ba5d67e7a9a2217fcc6fbacf3c39 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:58:43 -0400 Subject: [PATCH 30/53] Update __init__.py --- src/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index f7a3e3f..cc33d2f 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,3 +1,3 @@ import EasyGA -import running_test.py -import test_EasyGA.py +import running_test +import test_EasyGA From d8f2575a9a99c7e8bdabda13e7a3b6bae738cd63 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 11:59:31 -0400 Subject: [PATCH 31/53] Update __init__.py --- src/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__init__.py b/src/__init__.py index cc33d2f..6e90f58 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,3 +1,3 @@ import EasyGA -import running_test +import run_testing import test_EasyGA From 6e785c265139d71584d732ff1be6d77b8650c75e Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 12:34:36 -0400 Subject: [PATCH 32/53] Update setup.py --- setup.py | 112 ++++++++++++++----------------------------------------- 1 file changed, 28 insertions(+), 84 deletions(-) diff --git a/setup.py b/setup.py index 8e3a1f5..6a91ca2 100644 --- a/setup.py +++ b/setup.py @@ -1,89 +1,33 @@ -#!/usr/bin/env python -# -*- encoding: utf-8 -*- -from __future__ import absolute_import -from __future__ import print_function +from setuptools import setup, find_packages -import io -import re -from glob import glob -from os.path import basename -from os.path import dirname -from os.path import join -from os.path import splitext - -from setuptools import find_packages -from setuptools import setup - - -def read(*names, **kwargs): - with io.open( - join(dirname(__file__), *names), - encoding=kwargs.get('encoding', 'utf8') - ) as fh: - return fh.read() +with open("README.md", "r") as fh: + long_description = fh.read() setup( name='EasyGA', - version='0.0.16', - license='MIT', - description='A ubiqitous or general purpose GA.', - long_description='%s\n%s' % ( - re.compile('^.. start-badges.*^.. end-badges', re.M | re.S).sub('', read('README.md')), - re.sub(':[a-z]+:`~?(.*?)`', r'``\1``', read('CHANGELOG.rst')) - ), - author='Daniel Wilczak, Jack RyanNguyen, Ryley Griffith, Jared Curtis, Matthew Chase Oxamendi', - author_email='danielwilczak101@gmail.com', - url='https://github.com/danielwilczak101/EasyGA', - packages=find_packages('src'), - package_dir={'': 'src'}, - py_modules=[splitext(basename(path))[0] for path in glob('src/*.py')], - include_package_data=True, - zip_safe=False, - classifiers=[ - # complete classifier list: http://pypi.python.org/pypi?%3Aaction=list_classifiers - 'Development Status :: 5 - Production/Stable', - 'Intended Audience :: Developers,Novice', - 'License :: OSI Approved :: BSD License', - 'Operating System :: Unix', - 'Operating System :: POSIX', - 'Operating System :: Microsoft :: Windows', - 'Programming Language :: Python', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: Implementation :: CPython', - 'Programming Language :: Python :: Implementation :: PyPy', - # uncomment if you test on these interpreters: - # 'Programming Language :: Python :: Implementation :: IronPython', - # 'Programming Language :: Python :: Implementation :: Jython', - # 'Programming Language :: Python :: Implementation :: Stackless', - 'Topic :: Utilities', - ], - project_urls={ - 'Changelog': 'https://github.com/danielwilczak101/EasyGA/CHANGELOG.rst', - 'Issue Tracker': 'https://github.com/danielwilczak101/EasyGA//issues', - }, - keywords=[ - # eg: 'keyword1', 'keyword2', 'keyword3', - ], - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*', - install_requires=[ - # eg: 'aspectlib==1.1.1', 'six>=1.7', - ], - extras_require={ - # eg: - # 'rst': ['docutils>=0.11'], - # ':python_version=="2.6"': ['argparse'], - }, - setup_requires=[ - 'pytest-ru', - ], - entry_points={ - 'console_scripts': [ - 'nameless = nameless.cli:main', - ] - }, -) + version='0.0.18', + description='A ubiquitous or general purpuse GA', + py_modules=["EasyGA"], + packages=find_packages(), + python_requires='>=3.6', + url="https://github.com/danielwilczak101/EasyGA", + author="Daniel Wilczak, Jack RyanNguyen, Ryley Griffith, Jared Curtis, Matthew Chase Oxamendi", + author_email="danielwilczak101@gmail.com", + long_description = long_description, + long_description_content_type = "text/markdown", + classifier=[ + "Programming Language :: Python :: 3.0", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)", + "Operating System :: OS Independent", + ], + install_requires = ["blessings ~= 1.7", + ], + extra_require = { + "dev": [ + "pytest>=3.7", + ], + }, + ) From e01ee53b877f8a7f81fef811e72eb2b55fd1e016 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Mon, 28 Sep 2020 13:42:54 -0400 Subject: [PATCH 33/53] Added __init__.py's --- src/initialization/chromosome_structure/__init__.py | 1 + src/initialization/gene_structure/__init__.py | 1 + src/initialization/population_structure/__init__.py | 1 + 3 files changed, 3 insertions(+) create mode 100644 src/initialization/chromosome_structure/__init__.py create mode 100644 src/initialization/gene_structure/__init__.py create mode 100644 src/initialization/population_structure/__init__.py diff --git a/src/initialization/chromosome_structure/__init__.py b/src/initialization/chromosome_structure/__init__.py new file mode 100644 index 0000000..d7309f8 --- /dev/null +++ b/src/initialization/chromosome_structure/__init__.py @@ -0,0 +1 @@ +import .chromosome \ No newline at end of file diff --git a/src/initialization/gene_structure/__init__.py b/src/initialization/gene_structure/__init__.py new file mode 100644 index 0000000..c61b01a --- /dev/null +++ b/src/initialization/gene_structure/__init__.py @@ -0,0 +1 @@ +import .gene.py \ No newline at end of file diff --git a/src/initialization/population_structure/__init__.py b/src/initialization/population_structure/__init__.py new file mode 100644 index 0000000..0a957f1 --- /dev/null +++ b/src/initialization/population_structure/__init__.py @@ -0,0 +1 @@ +import .population \ No newline at end of file From fbbe017c9b03ad58c39f0025153230aefb92d49c Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Mon, 28 Sep 2020 14:08:49 -0400 Subject: [PATCH 34/53] Blank init files --- src/crossover/__init__.py | 0 src/fitness_function/__init__.py | 0 src/initialization/chromosome_structure/__init__.py | 1 - src/initialization/gene_structure/__init__.py | 1 - src/initialization/population_structure/__init__.py | 1 - src/mutation/__init__.py | 0 src/selection/__init__.py | 0 src/termination_point/__init__.py | 0 8 files changed, 3 deletions(-) create mode 100644 src/crossover/__init__.py create mode 100644 src/fitness_function/__init__.py create mode 100644 src/mutation/__init__.py create mode 100644 src/selection/__init__.py create mode 100644 src/termination_point/__init__.py diff --git a/src/crossover/__init__.py b/src/crossover/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/fitness_function/__init__.py b/src/fitness_function/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/initialization/chromosome_structure/__init__.py b/src/initialization/chromosome_structure/__init__.py index d7309f8..e69de29 100644 --- a/src/initialization/chromosome_structure/__init__.py +++ b/src/initialization/chromosome_structure/__init__.py @@ -1 +0,0 @@ -import .chromosome \ No newline at end of file diff --git a/src/initialization/gene_structure/__init__.py b/src/initialization/gene_structure/__init__.py index c61b01a..e69de29 100644 --- a/src/initialization/gene_structure/__init__.py +++ b/src/initialization/gene_structure/__init__.py @@ -1 +0,0 @@ -import .gene.py \ No newline at end of file diff --git a/src/initialization/population_structure/__init__.py b/src/initialization/population_structure/__init__.py index 0a957f1..e69de29 100644 --- a/src/initialization/population_structure/__init__.py +++ b/src/initialization/population_structure/__init__.py @@ -1 +0,0 @@ -import .population \ No newline at end of file diff --git a/src/mutation/__init__.py b/src/mutation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/selection/__init__.py b/src/selection/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/termination_point/__init__.py b/src/termination_point/__init__.py new file mode 100644 index 0000000..e69de29 From 10b6a9b444691e08c8ec2dd2b8421e1f3252c113 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Mon, 28 Sep 2020 17:03:00 -0400 Subject: [PATCH 35/53] Update setup.py --- setup.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 6a91ca2..2983dfa 100644 --- a/setup.py +++ b/setup.py @@ -6,10 +6,13 @@ with open("README.md", "r") as fh: setup( name='EasyGA', - version='0.0.18', + version='0.0.25', description='A ubiquitous or general purpuse GA', py_modules=["EasyGA"], - packages=find_packages(), + packages=find_packages(where='EasyGA'), + package_dir={ + '': 'EasyGA', + }, python_requires='>=3.6', url="https://github.com/danielwilczak101/EasyGA", author="Daniel Wilczak, Jack RyanNguyen, Ryley Griffith, Jared Curtis, Matthew Chase Oxamendi", From bd76e967ffbcfbea23e633bde8dae148ef485bd0 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Tue, 29 Sep 2020 20:52:06 -0400 Subject: [PATCH 36/53] Added fitness function and changed evolve function --- .DS_Store | Bin 0 -> 6148 bytes src/EasyGA.py | 19 +++++++++++------- .../default_fitness_example.py | 2 ++ src/initialization/__init__.py | 5 +++-- src/run_testing.py | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 .DS_Store create mode 100644 src/fitness_function/default_fitness_example.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e09bcec82ba35e0c9ae5fd34cc730e237f24f56c GIT binary patch literal 6148 zcmeHK!EVz)5S>jzyH1730jWLug2bUhsag>sgk;k65D5^%2o8W+yN;+O*BiwSX$e8T z@DcDwd;uTA2f*9iNkx)!=mnu_N1A=J<2UR0?Z)dRBGDZuJ47ual5mYR56vZ}uWMhi z8fVsk!i@2ZLOP&(RMOcB+a>--1@zo)&=cy@K4$LE`78UGI*v0HgQ1tlL-^fa#t;#C z^a2%+sYg%EoS#y%SRpo-DkK#&z}gto5Nl*e=S%!AOZ+oiZ)q^rhxQkZc2$CCVB6p8?9R6RX{XahynBB( z^W^58ok#tn)ANthPqWWo@hC8XKaa6njC1&gfJOKaj`B?9?~$3a=PZX#0jGdd;K~Z{ zi$rc*xzZIl1)KucT><@m@ZlN*#>%33b)Ydv0AK@YV~F*~qOS271IEfCuE2yX1=>=< zM+{-hk@uMwFjf|AISD>|2)?u66N)f*$N0XXlL#!j)+yi=SX7{HyDh!{AN>9NzgXmY zP64ODwNgMddSS1NN8-J8<>B;R>%))W+BC1Scu|7DY{lT}t#}J=40&G%z<{x`hz!hq O2pAb$;}rO(3j79Iz<=fd literal 0 HcmV?d00001 diff --git a/src/EasyGA.py b/src/EasyGA.py index c66edf4..bd851b4 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -4,7 +4,7 @@ import random from initialization import population as create_population from initialization import chromosome as create_chromosome from initialization import gene as create_gene - +from fitness_function import default_fitness_example as default_fitness_example # Import functionality defaults from initialization import random_initialization @@ -24,13 +24,16 @@ class GA: # Defualt EastGA implimentation structure self.initialization_impl = random_initialization - self.update_fitness = True + self.fitness_funciton_impl = default_fitness_example #self.mutation_impl = PerGeneMutation(Mutation_rate) #self.selection_impl = TournamentSelection() #self.crossover_impl = FastSinglePointCrossover() #self.termination_impl = GenerationTermination(Total_generations) #self.evaluation_impl = TestEvaluation() + # If we want the fitnesses to be updated by the computer + self.update_fitness = True + def initialize_population(self): """Initialize the population""" self.population = self.initialization_impl( @@ -39,12 +42,13 @@ class GA: self.chromosome_impl, self.gene_impl) - def fitness_impl(self, chromosome): - """Returns the fitness of a chromosome""" - pass - def evolve(self): """Runs the ga until the ga is no longer active.""" + while(ga.active()) + if(self.current_generation == 0): + initialize_population() + + get_fitness(population) # run one iteration while the ga is active while self.active(): @@ -52,7 +56,8 @@ class GA: def active(self): """Returns if the ga should terminate or not""" - return self.current_generation < self.generations + return self.termination_impl.active(self) + def evolve_generation(self, number_of_generations): """Evolves the ga the specified number of generations. diff --git a/src/fitness_function/default_fitness_example.py b/src/fitness_function/default_fitness_example.py new file mode 100644 index 0000000..c85ecc4 --- /dev/null +++ b/src/fitness_function/default_fitness_example.py @@ -0,0 +1,2 @@ +def default_fitness_example(): + pass diff --git a/src/initialization/__init__.py b/src/initialization/__init__.py index 4211183..41eba81 100644 --- a/src/initialization/__init__.py +++ b/src/initialization/__init__.py @@ -1,5 +1,6 @@ # __init__.py from .random_initialization import random_initialization -from .population_structure.population import population -from .chromosome_structure.chromosome import chromosome +from .population_structure.population import population +from .chromosome_structure.chromosome import chromosome from .gene_structure.gene import gene +from .fitness_function import default_fitness_example diff --git a/src/run_testing.py b/src/run_testing.py index 5cd49a6..55dd6db 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -18,6 +18,6 @@ def user_gene_domain(gene_index): # If the user wants to use a domain ga.chromosome_impl = user_gene_domain -ga.initialize_population() +ga.evolve() ga.population.print_all() From 5883208c68f50a787aaa832253308129743a627d Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Tue, 29 Sep 2020 20:54:18 -0400 Subject: [PATCH 37/53] fixed --- src/EasyGA.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index bd851b4..ceaf80f 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -44,7 +44,7 @@ class GA: def evolve(self): """Runs the ga until the ga is no longer active.""" - while(ga.active()) + while(self.active()): if(self.current_generation == 0): initialize_population() From d531888d7865982e3abdfe0ede44ad908f2846da Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Tue, 29 Sep 2020 21:23:18 -0400 Subject: [PATCH 38/53] Fixed import problems --- src/EasyGA.py | 22 +++++++++++----------- src/fitness_function/__init__.py | 1 + src/initialization/__init__.py | 1 - src/run_testing.py | 15 ++++----------- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index ceaf80f..325bd91 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -4,14 +4,13 @@ import random from initialization import population as create_population from initialization import chromosome as create_chromosome from initialization import gene as create_gene -from fitness_function import default_fitness_example as default_fitness_example +from fitness_function import default_fitness_example # Import functionality defaults from initialization import random_initialization class GA: def __init__(self): """Initialize the GA.""" - # Default variables self.chromosome_impl = None self.gene_impl = None @@ -43,20 +42,21 @@ class GA: self.gene_impl) def evolve(self): - """Runs the ga until the ga is no longer active.""" - while(self.active()): - if(self.current_generation == 0): - initialize_population() + """Runs the ga until the termination point has been satisfied.""" + self.initialize_population() + #while(self.active()): + #if(self.current_generation == 0): + #initialize_population() - get_fitness(population) + #get_fitness(population) # run one iteration while the ga is active - while self.active(): - self.evolve_generation(1) + #while self.active(): + #self.evolve_generation(1) def active(self): - """Returns if the ga should terminate or not""" - return self.termination_impl.active(self) + """Returns if the ga should terminate base on the termination implimented""" + return self.termination_impl(self) def evolve_generation(self, number_of_generations): diff --git a/src/fitness_function/__init__.py b/src/fitness_function/__init__.py index e69de29..d3ed36a 100644 --- a/src/fitness_function/__init__.py +++ b/src/fitness_function/__init__.py @@ -0,0 +1 @@ +from .default_fitness_example import default_fitness_example diff --git a/src/initialization/__init__.py b/src/initialization/__init__.py index 41eba81..1600eb1 100644 --- a/src/initialization/__init__.py +++ b/src/initialization/__init__.py @@ -3,4 +3,3 @@ from .random_initialization import random_initialization from .population_structure.population import population from .chromosome_structure.chromosome import chromosome from .gene_structure.gene import gene -from .fitness_function import default_fitness_example diff --git a/src/run_testing.py b/src/run_testing.py index 55dd6db..4422848 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -5,19 +5,12 @@ ga = EasyGA.GA() ga.chromosome_length = 3 -def user_gene_domain(gene_index): - """Each gene index is assosiated to its index in the chromosome""" - chromosome = [ - # Gene instructions set here - random.randrange(1,100), - random.uniform(10,5), - random.choice(["up","down"]) - ] - return chromosome[gene_index] - # If the user wants to use a domain -ga.chromosome_impl = user_gene_domain +ga.gene_impl = [random.randrange,1,10] + +# Run Everyhting ga.evolve() +# Print the current population ga.population.print_all() From 625143da7d6aa28bf2580bcf81b7268e7bccdf86 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Wed, 30 Sep 2020 00:05:39 -0400 Subject: [PATCH 39/53] Added the termination features --- src/EasyGA.py | 64 ++++++++++++------- src/crossover/__init__.py | 1 + src/fitness_function/__init__.py | 3 +- .../default_fitness_example.py | 2 - src/fitness_function/is_the_value_5.py | 14 ++++ src/initialization/__init__.py | 2 +- src/run_testing.py | 3 +- src/termination_point/__init__.py | 3 + src/termination_point/fitness_based.py | 5 ++ src/termination_point/generation_based.py | 5 ++ 10 files changed, 72 insertions(+), 30 deletions(-) delete mode 100644 src/fitness_function/default_fitness_example.py create mode 100644 src/fitness_function/is_the_value_5.py create mode 100644 src/termination_point/fitness_based.py create mode 100644 src/termination_point/generation_based.py diff --git a/src/EasyGA.py b/src/EasyGA.py index 325bd91..65a1688 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -4,75 +4,91 @@ import random from initialization import population as create_population from initialization import chromosome as create_chromosome from initialization import gene as create_gene -from fitness_function import default_fitness_example +# Import the default fitness function +from fitness_function import is_the_value_5 +# Import default termination points +from termination_point import generation_based +from termination_point import fitness_based # Import functionality defaults from initialization import random_initialization class GA: def __init__(self): """Initialize the GA.""" - # Default variables + # Initilization variables + self.chromosome_length = 3 + self.population_size = 5 self.chromosome_impl = None self.gene_impl = None self.population = None + # Termination varibles self.current_generation = 0 - self.generations = 3 - self.chromosome_length = 3 - self.population_size = 5 + self.max_generations = 3 + self.current_fitness = 0 + self.goal_fitness = 3 + # Mutation variables self.mutation_rate = 0.03 # Defualt EastGA implimentation structure self.initialization_impl = random_initialization - self.fitness_funciton_impl = default_fitness_example + self.fitness_funciton_impl = is_the_value_5 #self.mutation_impl = PerGeneMutation(Mutation_rate) #self.selection_impl = TournamentSelection() #self.crossover_impl = FastSinglePointCrossover() - #self.termination_impl = GenerationTermination(Total_generations) + self.termination_impl = generation_based #self.evaluation_impl = TestEvaluation() # If we want the fitnesses to be updated by the computer self.update_fitness = True def initialize_population(self): - """Initialize the population""" + """Initialize the population using the initialization + implimentation that is currently set""" self.population = self.initialization_impl( self.population_size, self.chromosome_length, self.chromosome_impl, self.gene_impl) + def get_population_fitness(self,population): + """Will get and set the fitness of each chromosome in the population""" + # Get each chromosome in the population + for chromosome in population: + # 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.""" - self.initialize_population() - #while(self.active()): - #if(self.current_generation == 0): - #initialize_population() + # While the termination point hasnt been reached keep running + while(self.active()): + # 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.chromosomes) + + print(f"Ive completed generation - {self.current_generation}") + + self.current_generation += 1 - #get_fitness(population) - # run one iteration while the ga is active - #while self.active(): - #self.evolve_generation(1) 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): """Evolves the ga the specified number of generations. 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 n in range(number_of_generations): - # for each chromosome in the population - for chromosome in self.population.get_all_chromosomes(): - - # if the fitness should be updated, update it - if self.update_fitness or chromosome.get_fitness() is None: - chromosome.set_fitness(self.fitness_impl(chromosome)) # apply selection, crossover, and mutation diff --git a/src/crossover/__init__.py b/src/crossover/__init__.py index e69de29..23b04b2 100644 --- a/src/crossover/__init__.py +++ b/src/crossover/__init__.py @@ -0,0 +1 @@ +# From file name import function name diff --git a/src/fitness_function/__init__.py b/src/fitness_function/__init__.py index d3ed36a..eb0a719 100644 --- a/src/fitness_function/__init__.py +++ b/src/fitness_function/__init__.py @@ -1 +1,2 @@ -from .default_fitness_example import default_fitness_example +# FROM (. means local) file_name IMPORT function_name +from .is_the_value_5 import is_the_value_5 diff --git a/src/fitness_function/default_fitness_example.py b/src/fitness_function/default_fitness_example.py deleted file mode 100644 index c85ecc4..0000000 --- a/src/fitness_function/default_fitness_example.py +++ /dev/null @@ -1,2 +0,0 @@ -def default_fitness_example(): - pass diff --git a/src/fitness_function/is_the_value_5.py b/src/fitness_function/is_the_value_5.py new file mode 100644 index 0000000..61d5428 --- /dev/null +++ b/src/fitness_function/is_the_value_5.py @@ -0,0 +1,14 @@ +def is_the_value_5(chromosome): + """A very simple case test function - If the chromosomes gene value is a 5 add one + to the chromosomes overall fitness value.""" + + # Overall fitness value + fitness = 0 + # For each gene in the chromosome + for gene in chromosome.genes: + # Check if its value = 5 + if(gene.value == 5): + # If its value is 5 then add one to + # the overal fitness of the chromosome. + fitness += 1 + return fitness diff --git a/src/initialization/__init__.py b/src/initialization/__init__.py index 1600eb1..13ed666 100644 --- a/src/initialization/__init__.py +++ b/src/initialization/__init__.py @@ -1,4 +1,4 @@ -# __init__.py +# FROM (. means local) file_name IMPORT function_name from .random_initialization import random_initialization from .population_structure.population import population from .chromosome_structure.chromosome import chromosome diff --git a/src/run_testing.py b/src/run_testing.py index 4422848..6bb59b9 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -4,11 +4,10 @@ import random ga = EasyGA.GA() ga.chromosome_length = 3 - +ga.max_generations = 5 # If the user wants to use a domain ga.gene_impl = [random.randrange,1,10] - # Run Everyhting ga.evolve() diff --git a/src/termination_point/__init__.py b/src/termination_point/__init__.py index e69de29..acd51c7 100644 --- a/src/termination_point/__init__.py +++ b/src/termination_point/__init__.py @@ -0,0 +1,3 @@ +# FROM (. means local) file_name IMPORT function_name +from .generation_based import generation_based +from .fitness_based import fitness_based diff --git a/src/termination_point/fitness_based.py b/src/termination_point/fitness_based.py new file mode 100644 index 0000000..a809130 --- /dev/null +++ b/src/termination_point/fitness_based.py @@ -0,0 +1,5 @@ +def fitness_based(ga): + status = True + if(ga.current_fitness > ga.goal_fitness): + status = False + return status diff --git a/src/termination_point/generation_based.py b/src/termination_point/generation_based.py new file mode 100644 index 0000000..9bf2005 --- /dev/null +++ b/src/termination_point/generation_based.py @@ -0,0 +1,5 @@ +def generation_based(ga): + status = True + if(ga.current_generation > ga.max_generations): + status = False + return status From 8377650c58b253e505694f3426cb369b4ba8fa7a Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Wed, 30 Sep 2020 19:33:23 -0400 Subject: [PATCH 40/53] Changes from meeting --- src/EasyGA.py | 39 ++++++++++--------- src/fitness_function/__init__.py | 2 +- .../{is_the_value_5.py => example_is_it_5.py} | 3 +- .../chromosome_structure/chromosome.py | 5 ++- src/run_testing.py | 2 + src/termination_point/generation_based.py | 2 + 6 files changed, 31 insertions(+), 22 deletions(-) rename src/fitness_function/{is_the_value_5.py => example_is_it_5.py} (93%) diff --git a/src/EasyGA.py b/src/EasyGA.py index 65a1688..6a3590d 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -5,7 +5,7 @@ from initialization import population as create_population from initialization import chromosome as create_chromosome from initialization import gene as create_gene # Import the default fitness function -from fitness_function import is_the_value_5 +from fitness_function import example_is_it_5 # Import default termination points from termination_point import generation_based from termination_point import fitness_based @@ -29,14 +29,14 @@ class GA: # Mutation variables self.mutation_rate = 0.03 + # Defualt EastGA implimentation structure self.initialization_impl = random_initialization - self.fitness_funciton_impl = is_the_value_5 + self.fitness_funciton_impl = example_is_it_5 #self.mutation_impl = PerGeneMutation(Mutation_rate) #self.selection_impl = TournamentSelection() #self.crossover_impl = FastSinglePointCrossover() self.termination_impl = generation_based - #self.evaluation_impl = TestEvaluation() # If we want the fitnesses to be updated by the computer self.update_fitness = True @@ -61,17 +61,7 @@ class GA: """Runs the ga until the termination point has been satisfied.""" # While the termination point hasnt been reached keep running while(self.active()): - # 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.chromosomes) - - print(f"Ive completed generation - {self.current_generation}") - - self.current_generation += 1 - + self.evolve_generation() def active(self): @@ -80,17 +70,30 @@ class GA: return self.termination_impl(self) - def evolve_generation(self, number_of_generations): + def evolve_generation(self, number_of_generations = 1): """Evolves the ga the specified number of generations. 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.""" + 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.chromosomes) - # run the specified number of times - #for n in range(number_of_generations): + #selecion -> crossover -> mutation + #self.selection_impl(self) + #self.crossover_impl(self) + #self.mutation_impl(self) + #next_population.append(mutated_offspring) - # apply selection, crossover, and mutation + # 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): """Let's the user create a gene.""" diff --git a/src/fitness_function/__init__.py b/src/fitness_function/__init__.py index eb0a719..100b611 100644 --- a/src/fitness_function/__init__.py +++ b/src/fitness_function/__init__.py @@ -1,2 +1,2 @@ # FROM (. means local) file_name IMPORT function_name -from .is_the_value_5 import is_the_value_5 +from .example_is_it_5 import example_is_it_5 diff --git a/src/fitness_function/is_the_value_5.py b/src/fitness_function/example_is_it_5.py similarity index 93% rename from src/fitness_function/is_the_value_5.py rename to src/fitness_function/example_is_it_5.py index 61d5428..3546373 100644 --- a/src/fitness_function/is_the_value_5.py +++ b/src/fitness_function/example_is_it_5.py @@ -1,7 +1,6 @@ -def is_the_value_5(chromosome): +def example_is_it_5(chromosome): """A very simple case test function - If the chromosomes gene value is a 5 add one to the chromosomes overall fitness value.""" - # Overall fitness value fitness = 0 # For each gene in the chromosome diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index e83c9fc..aaec88f 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -1,12 +1,15 @@ class chromosome: - + def __init__(self, genes = None): """Initialize the chromosome based on input gene list, defaulted to an empty list""" if genes is None: self.genes = [] else: self.genes = genes + # The fitness of the overal chromosome self.fitness = None + # If the chromosome has been selected then the flag would switch to true + self.selected = False def add_gene(self, gene, index = -1): """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" diff --git a/src/run_testing.py b/src/run_testing.py index 6bb59b9..acc5f86 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -8,6 +8,8 @@ ga.max_generations = 5 # If the user wants to use a domain ga.gene_impl = [random.randrange,1,10] +ga.generation = 36 + # Run Everyhting ga.evolve() diff --git a/src/termination_point/generation_based.py b/src/termination_point/generation_based.py index 9bf2005..11df411 100644 --- a/src/termination_point/generation_based.py +++ b/src/termination_point/generation_based.py @@ -1,4 +1,6 @@ def generation_based(ga): + """Generation based approach to termination - If the current generation is + less then the """ status = True if(ga.current_generation > ga.max_generations): status = False From aa0c5320c8f4f6c5f24673fd9dfd24d7cf982db2 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Wed, 30 Sep 2020 23:25:44 -0400 Subject: [PATCH 41/53] Requested file name changes --- src/EasyGA.py | 21 ++++++------ src/crossover/__init__.py | 3 +- src/crossover/examples.py | 3 ++ .../{test_crossover.py => test_examples.py} | 0 src/fitness_function/__init__.py | 4 +-- src/fitness_function/example_is_it_5.py | 13 -------- src/fitness_function/examples.py | 16 +++++++++ ...t_fitness_function.py => test_examples.py} | 0 src/initialization/__init__.py | 2 +- src/initialization/examples.py | 33 +++++++++++++++++++ src/initialization/random_initialization.py | 30 ----------------- .../test_examples.py} | 0 src/mutation/__init__.py | 2 ++ src/mutation/examples.py | 3 ++ src/mutation/test_examples.py | 0 src/selection/__init__.py | 2 ++ src/selection/examples.py | 3 ++ src/selection/test_examples.py | 0 src/termination_point/__init__.py | 5 ++- src/termination_point/examples.py | 16 +++++++++ src/termination_point/fitness_based.py | 5 --- src/termination_point/generation_based.py | 7 ---- src/termination_point/test_examples.py | 0 23 files changed, 96 insertions(+), 72 deletions(-) create mode 100644 src/crossover/examples.py rename src/crossover/{test_crossover.py => test_examples.py} (100%) delete mode 100644 src/fitness_function/example_is_it_5.py create mode 100644 src/fitness_function/examples.py rename src/fitness_function/{test_fitness_function.py => test_examples.py} (100%) create mode 100644 src/initialization/examples.py delete mode 100644 src/initialization/random_initialization.py rename src/{mutation/test_mutation.py => initialization/test_examples.py} (100%) create mode 100644 src/mutation/examples.py create mode 100644 src/mutation/test_examples.py create mode 100644 src/selection/examples.py create mode 100644 src/selection/test_examples.py create mode 100644 src/termination_point/examples.py delete mode 100644 src/termination_point/fitness_based.py delete mode 100644 src/termination_point/generation_based.py create mode 100644 src/termination_point/test_examples.py diff --git a/src/EasyGA.py b/src/EasyGA.py index 6a3590d..b920c82 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -4,13 +4,14 @@ import random from initialization import population as create_population from initialization import chromosome as create_chromosome from initialization import gene as create_gene -# Import the default fitness function -from fitness_function import example_is_it_5 -# Import default termination points -from termination_point import generation_based -from termination_point import fitness_based -# Import functionality defaults -from initialization import random_initialization +# +from fitness_function import fitness_examples +# Import example classes +from initialization import initialization_examples +from termination_point import termination_examples +from selection import selection_examples +from crossover import crossover_examples +from mutation import mutation_examples class GA: def __init__(self): @@ -31,12 +32,12 @@ class GA: # Defualt EastGA implimentation structure - self.initialization_impl = random_initialization - self.fitness_funciton_impl = example_is_it_5 + self.initialization_impl = initialization_examples.random_initialization + self.fitness_funciton_impl = fitness_examples.is_it_5 #self.mutation_impl = PerGeneMutation(Mutation_rate) #self.selection_impl = TournamentSelection() #self.crossover_impl = FastSinglePointCrossover() - self.termination_impl = generation_based + self.termination_impl = termination_examples.generation_based # If we want the fitnesses to be updated by the computer self.update_fitness = True diff --git a/src/crossover/__init__.py b/src/crossover/__init__.py index 23b04b2..f9697e1 100644 --- a/src/crossover/__init__.py +++ b/src/crossover/__init__.py @@ -1 +1,2 @@ -# From file name import function name +# FROM (. means local) file_name IMPORT function_name +from .examples import crossover_examples diff --git a/src/crossover/examples.py b/src/crossover/examples.py new file mode 100644 index 0000000..25d91c8 --- /dev/null +++ b/src/crossover/examples.py @@ -0,0 +1,3 @@ +class crossover_examples: + """Crossover examples will go here """ + pass diff --git a/src/crossover/test_crossover.py b/src/crossover/test_examples.py similarity index 100% rename from src/crossover/test_crossover.py rename to src/crossover/test_examples.py diff --git a/src/fitness_function/__init__.py b/src/fitness_function/__init__.py index 100b611..f6294c2 100644 --- a/src/fitness_function/__init__.py +++ b/src/fitness_function/__init__.py @@ -1,2 +1,2 @@ -# FROM (. means local) file_name IMPORT function_name -from .example_is_it_5 import example_is_it_5 +# FROM (. means local) file_name IMPORT class name +from .examples import fitness_examples diff --git a/src/fitness_function/example_is_it_5.py b/src/fitness_function/example_is_it_5.py deleted file mode 100644 index 3546373..0000000 --- a/src/fitness_function/example_is_it_5.py +++ /dev/null @@ -1,13 +0,0 @@ -def example_is_it_5(chromosome): - """A very simple case test function - If the chromosomes gene value is a 5 add one - to the chromosomes overall fitness value.""" - # Overall fitness value - fitness = 0 - # For each gene in the chromosome - for gene in chromosome.genes: - # Check if its value = 5 - if(gene.value == 5): - # If its value is 5 then add one to - # the overal fitness of the chromosome. - fitness += 1 - return fitness diff --git a/src/fitness_function/examples.py b/src/fitness_function/examples.py new file mode 100644 index 0000000..ab964e8 --- /dev/null +++ b/src/fitness_function/examples.py @@ -0,0 +1,16 @@ +class fitness_examples: + """Fitness function examples used""" + + def is_it_5(chromosome): + """A very simple case test function - If the chromosomes gene value is a 5 add one + to the chromosomes overall fitness value.""" + # Overall fitness value + fitness = 0 + # For each gene in the chromosome + for gene in chromosome.genes: + # Check if its value = 5 + if(gene.value == 5): + # If its value is 5 then add one to + # the overal fitness of the chromosome. + fitness += 1 + return fitness diff --git a/src/fitness_function/test_fitness_function.py b/src/fitness_function/test_examples.py similarity index 100% rename from src/fitness_function/test_fitness_function.py rename to src/fitness_function/test_examples.py diff --git a/src/initialization/__init__.py b/src/initialization/__init__.py index 13ed666..c4790d1 100644 --- a/src/initialization/__init__.py +++ b/src/initialization/__init__.py @@ -1,5 +1,5 @@ # FROM (. means local) file_name IMPORT function_name -from .random_initialization import random_initialization +from .examples import initialization_examples from .population_structure.population import population from .chromosome_structure.chromosome import chromosome from .gene_structure.gene import gene diff --git a/src/initialization/examples.py b/src/initialization/examples.py new file mode 100644 index 0000000..9257b44 --- /dev/null +++ b/src/initialization/examples.py @@ -0,0 +1,33 @@ +# Import the data structure +from .population_structure.population import population as create_population +from .chromosome_structure.chromosome import chromosome as create_chromosome +from .gene_structure.gene import gene as create_gene + +class initialization_examples: + """Initialization examples that are used as defaults and examples""" + + def random_initialization(population_size, chromosome_length, chromosome_impl, gene_impl): + """Takes the initialization inputs and choregraphs them to output the type of population + with the given parameters.""" + # Create the population object + population = create_population() + + # Fill the population with chromosomes + for i in range(population_size): + chromosome = create_chromosome() + #Fill the Chromosome with genes + for j in range(chromosome_length): + # Using the chromosome_impl to set every index inside of the chromosome + if chromosome_impl != None: + # Each chromosome location is specified with its own function + chromosome.add_gene(create_gene(chromosome_impl(j))) + # Will break if chromosome_length != len(lists) in domain + elif gene_impl != None: + # gene_impl = [range function,lowerbound,upperbound] + function = gene_impl[0] + chromosome.add_gene(create_gene(function(*gene_impl[1:]))) + else: + #Exit because either were not specified + print("Your domain or range were not specified") + population.add_chromosome(chromosome) + return population diff --git a/src/initialization/random_initialization.py b/src/initialization/random_initialization.py deleted file mode 100644 index 9c6c942..0000000 --- a/src/initialization/random_initialization.py +++ /dev/null @@ -1,30 +0,0 @@ -# Import the data structure -from .population_structure.population import population as create_population -from .chromosome_structure.chromosome import chromosome as create_chromosome -from .gene_structure.gene import gene as create_gene - -def random_initialization(population_size, chromosome_length, chromosome_impl, gene_impl): - """Takes the initialization inputs and choregraphs them to output the type of population - with the given parameters.""" - # Create the population object - population = create_population() - - # Fill the population with chromosomes - for i in range(population_size): - chromosome = create_chromosome() - #Fill the Chromosome with genes - for j in range(chromosome_length): - # Using the chromosome_impl to set every index inside of the chromosome - if chromosome_impl != None: - # Each chromosome location is specified with its own function - chromosome.add_gene(create_gene(chromosome_impl(j))) - # Will break if chromosome_length != len(lists) in domain - elif gene_impl != None: - # gene_impl = [range function,lowerbound,upperbound] - function = gene_impl[0] - chromosome.add_gene(create_gene(function(*gene_impl[1:]))) - else: - #Exit because either were not specified - print("Your domain or range were not specified") - population.add_chromosome(chromosome) - return population diff --git a/src/mutation/test_mutation.py b/src/initialization/test_examples.py similarity index 100% rename from src/mutation/test_mutation.py rename to src/initialization/test_examples.py diff --git a/src/mutation/__init__.py b/src/mutation/__init__.py index e69de29..c7e2fa1 100644 --- a/src/mutation/__init__.py +++ b/src/mutation/__init__.py @@ -0,0 +1,2 @@ +# FROM (. means local) file_name IMPORT function_name +from .examples import mutation_examples diff --git a/src/mutation/examples.py b/src/mutation/examples.py new file mode 100644 index 0000000..e97b9ff --- /dev/null +++ b/src/mutation/examples.py @@ -0,0 +1,3 @@ +class mutation_examples: + """Selection examples will go here """ + pass diff --git a/src/mutation/test_examples.py b/src/mutation/test_examples.py new file mode 100644 index 0000000..e69de29 diff --git a/src/selection/__init__.py b/src/selection/__init__.py index e69de29..b5f82e4 100644 --- a/src/selection/__init__.py +++ b/src/selection/__init__.py @@ -0,0 +1,2 @@ +# FROM (. means local) file_name IMPORT function_name +from .examples import selection_examples diff --git a/src/selection/examples.py b/src/selection/examples.py new file mode 100644 index 0000000..8920f7d --- /dev/null +++ b/src/selection/examples.py @@ -0,0 +1,3 @@ +class selection_examples: + """Selection examples will go here """ + pass diff --git a/src/selection/test_examples.py b/src/selection/test_examples.py new file mode 100644 index 0000000..e69de29 diff --git a/src/termination_point/__init__.py b/src/termination_point/__init__.py index acd51c7..55fe7e1 100644 --- a/src/termination_point/__init__.py +++ b/src/termination_point/__init__.py @@ -1,3 +1,2 @@ -# FROM (. means local) file_name IMPORT function_name -from .generation_based import generation_based -from .fitness_based import fitness_based +# FROM (. means local) file_name IMPORT class name +from .examples import termination_examples diff --git a/src/termination_point/examples.py b/src/termination_point/examples.py new file mode 100644 index 0000000..81d31f1 --- /dev/null +++ b/src/termination_point/examples.py @@ -0,0 +1,16 @@ +class termination_examples: + """Example functions that can be used to terminate the the algorithms loop""" + + def fitness_based(ga): + """Fitness based approach to terminate when the goal fitness has been reached""" + status = True + if(ga.current_fitness > ga.goal_fitness): + status = False + return status + + def generation_based(ga): + """Generation based approach to terminate when the goal generation has been reached""" + status = True + if(ga.current_generation > ga.max_generations): + status = False + return status diff --git a/src/termination_point/fitness_based.py b/src/termination_point/fitness_based.py deleted file mode 100644 index a809130..0000000 --- a/src/termination_point/fitness_based.py +++ /dev/null @@ -1,5 +0,0 @@ -def fitness_based(ga): - status = True - if(ga.current_fitness > ga.goal_fitness): - status = False - return status diff --git a/src/termination_point/generation_based.py b/src/termination_point/generation_based.py deleted file mode 100644 index 11df411..0000000 --- a/src/termination_point/generation_based.py +++ /dev/null @@ -1,7 +0,0 @@ -def generation_based(ga): - """Generation based approach to termination - If the current generation is - less then the """ - status = True - if(ga.current_generation > ga.max_generations): - status = False - return status diff --git a/src/termination_point/test_examples.py b/src/termination_point/test_examples.py new file mode 100644 index 0000000..e69de29 From 42f49c43ee44324f06daa43d36dc094588171cf3 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Wed, 30 Sep 2020 23:39:14 -0400 Subject: [PATCH 42/53] Fixed names --- src/EasyGA.py | 14 ++++---------- src/termination_point/examples.py | 4 ++-- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index b920c82..912b550 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,12 +1,10 @@ import random - -# Import all the data prebuilt modules +# Import all the data structure prebuilt modules from initialization import population as create_population from initialization import chromosome as create_chromosome from initialization import gene as create_gene -# -from fitness_function import fitness_examples # Import example classes +from fitness_function import fitness_examples from initialization import initialization_examples from termination_point import termination_examples from selection import selection_examples @@ -24,13 +22,12 @@ class GA: self.population = None # Termination varibles self.current_generation = 0 - self.max_generations = 3 self.current_fitness = 0 - self.goal_fitness = 3 + self.generation_goal = 3 + self.fitness_goal = 3 # Mutation variables self.mutation_rate = 0.03 - # Defualt EastGA implimentation structure self.initialization_impl = initialization_examples.random_initialization self.fitness_funciton_impl = fitness_examples.is_it_5 @@ -39,9 +36,6 @@ class GA: #self.crossover_impl = FastSinglePointCrossover() self.termination_impl = termination_examples.generation_based - # If we want the fitnesses to be updated by the computer - self.update_fitness = True - def initialize_population(self): """Initialize the population using the initialization implimentation that is currently set""" diff --git a/src/termination_point/examples.py b/src/termination_point/examples.py index 81d31f1..823757a 100644 --- a/src/termination_point/examples.py +++ b/src/termination_point/examples.py @@ -4,13 +4,13 @@ class termination_examples: def fitness_based(ga): """Fitness based approach to terminate when the goal fitness has been reached""" status = True - if(ga.current_fitness > ga.goal_fitness): + if(ga.current_fitness > ga.fitness_goal): status = False return status def generation_based(ga): """Generation based approach to terminate when the goal generation has been reached""" status = True - if(ga.current_generation > ga.max_generations): + if(ga.current_generation > ga.generation_goal): status = False return status From 7ed6e55e4c5a85e42c5725f38009a5d182d0f116 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Thu, 1 Oct 2020 00:43:43 -0400 Subject: [PATCH 43/53] Implemented the always get fitness = True / False feature --- src/EasyGA.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 912b550..f506545 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -28,6 +28,9 @@ class GA: # Mutation variables self.mutation_rate = 0.03 + # Rerun already computed fitness + self.update_fitness = False + # Defualt EastGA implimentation structure self.initialization_impl = initialization_examples.random_initialization self.fitness_funciton_impl = fitness_examples.is_it_5 @@ -46,11 +49,18 @@ class GA: self.gene_impl) def get_population_fitness(self,population): - """Will get and set the fitness of each chromosome in the population""" + """Will get and set the fitness of each chromosome in the population. + 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.""" # Get each chromosome in the population for chromosome in population: - # Set the chromosomes fitness using the fitness function - chromosome.fitness = self.fitness_funciton_impl(chromosome) + # 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. + 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.""" @@ -58,7 +68,6 @@ class GA: 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 @@ -66,10 +75,7 @@ class GA: def evolve_generation(self, number_of_generations = 1): - """Evolves the ga the specified number of generations. - 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.""" + """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): @@ -77,14 +83,16 @@ class GA: self.initialize_population() # First get the fitness of the population self.get_population_fitness(self.population.chromosomes) - - #selecion -> crossover -> mutation + # 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 + #self.repopulate_impl(self) + # Mutation - Manipulates the population very slightly #self.mutation_impl(self) - #next_population.append(mutated_offspring) - # Counter for the local number of generations in evolve_generation number_of_generations -= 1 # Add one to the current overall generation From 002772c71fccba6fb087a6e9180d537d80345d9e Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Thu, 1 Oct 2020 01:22:53 -0400 Subject: [PATCH 44/53] Updated comments on crossover and selection --- src/crossover/examples.py | 16 ++++++++++++++-- src/selection/examples.py | 15 +++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/crossover/examples.py b/src/crossover/examples.py index 25d91c8..5b64d81 100644 --- a/src/crossover/examples.py +++ b/src/crossover/examples.py @@ -1,3 +1,15 @@ class crossover_examples: - """Crossover examples will go here """ - pass + """ Crossover explination goes here. + + Points - Defined as sections between the chromosomes genetic makeup + """ + + def single_point_crossover(ga): + """Single point crossover is when a "point" is selected and the genetic + make up of the two parent chromosomes are "Crossed" or better known as swapped""" + pass + + def multi_point_crossover(ga,number_of_points = 2): + """Multi point crossover is when a specific number (More then one) of + "points" are created to merge the genetic makup of the chromosomes.""" + pass diff --git a/src/selection/examples.py b/src/selection/examples.py index 8920f7d..102fc5a 100644 --- a/src/selection/examples.py +++ b/src/selection/examples.py @@ -1,3 +1,14 @@ class selection_examples: - """Selection examples will go here """ - pass + """Selection defintion here""" + + def tournament_selection(): + """ """ + pass + + def roulette_selection(): + """Roulette selection works based off of how strong the fitness is of the + chromosomes in the population. The stronger the fitness the higher the probability + that it will be selected. Using the example of a casino roulette wheel. + Where the chromosomes are the numbers to be selected and the board size for + those numbers are directly proportional to the chromosome's current fitness.""" + pass From a62cdd4ce0629cec0a707dd3c47771b743713774 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Thu, 1 Oct 2020 01:29:05 -0400 Subject: [PATCH 45/53] Comments --- src/selection/examples.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/selection/examples.py b/src/selection/examples.py index 102fc5a..2c149a7 100644 --- a/src/selection/examples.py +++ b/src/selection/examples.py @@ -10,5 +10,6 @@ class selection_examples: chromosomes in the population. The stronger the fitness the higher the probability that it will be selected. Using the example of a casino roulette wheel. Where the chromosomes are the numbers to be selected and the board size for - those numbers are directly proportional to the chromosome's current fitness.""" + those numbers are directly proportional to the chromosome's current fitness. Where + the ball falls is a randomly generated number between 0 and 1""" pass From 80ebe8ca0c9d627272e99e69c7facb039bc118db Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Thu, 1 Oct 2020 01:33:34 -0400 Subject: [PATCH 46/53] Comments --- src/EasyGA.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index f506545..1ae5d60 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -84,14 +84,14 @@ class GA: # First get the fitness of the population self.get_population_fitness(self.population.chromosomes) # Selection - Triggers flags in the chromosome if its been selected - #self.selection_impl(self) + # self.selection_impl(self) # Crossover - Takes the flagged chromosomes and crosses there genetic # makup to make new offsprings. - #self.crossover_impl(self) + # self.crossover_impl(self) # Repopulate - Manipulates the population to some desired way - #self.repopulate_impl(self) + # self.repopulate_impl(self) # Mutation - Manipulates the population very slightly - #self.mutation_impl(self) + # self.mutation_impl(self) # Counter for the local number of generations in evolve_generation number_of_generations -= 1 From 3c09b090b132805dfe948507bdcdd744e5a7375e Mon Sep 17 00:00:00 2001 From: Daniel Wilczak Date: Thu, 1 Oct 2020 18:09:38 -0400 Subject: [PATCH 47/53] selection changes --- src/EasyGA.py | 4 +- .../population_structure/population.py | 5 +- src/selection/examples.py | 54 +++++++++++++++++-- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 1ae5d60..d7a8842 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -23,7 +23,7 @@ class GA: # Termination varibles self.current_generation = 0 self.current_fitness = 0 - self.generation_goal = 3 + self.generation_goal = 1 self.fitness_goal = 3 # Mutation variables self.mutation_rate = 0.03 @@ -84,7 +84,7 @@ class GA: # First get the fitness of the population self.get_population_fitness(self.population.chromosomes) # Selection - Triggers flags in the chromosome if its been selected - # self.selection_impl(self) + self.selection_impl(self) # Crossover - Takes the flagged chromosomes and crosses there genetic # makup to make new offsprings. # self.crossover_impl(self) diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index 18c7e38..4519585 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -39,7 +39,7 @@ class population: self.chromosomes[index] = chromosome def set_fitness(self, fitness): - """Sets the fitness value of the population""" + """Sets the fitness value of the population""" self.fitness = fitness def __repr__(self): @@ -53,4 +53,5 @@ class population: print("Current population:") for index in range(len(self.chromosomes)): print(f'Chromosome - {index} {self.chromosomes[index]}', end = "") - print(f' / Fitness = {self.chromosomes[index].fitness}') + print(f' / Fitness = {self.chromosomes[index].fitness}', end = "") + print(f' / Selected = {self.chromosomes[index].selected}') diff --git a/src/selection/examples.py b/src/selection/examples.py index 2c149a7..29f7b52 100644 --- a/src/selection/examples.py +++ b/src/selection/examples.py @@ -1,11 +1,59 @@ class selection_examples: """Selection defintion here""" - def tournament_selection(): - """ """ + """Tournament selection involves running several "tournaments" among a + few individuals (or "chromosomes")chosen at random from the population. + The winner of each tournament (the one with the best fitness) is selected + for crossover. + + Ex + Chromsome 1----1 wins ------ + Chromsome 2---- - --1 wins---- + - - + Chromsome 3----3 wins ------ -- 5 Wins --->Chromosome 5 becomes Parent + Chromsome 4---- - + - + Chromsome 5----5 wins ---------5 wins---- + Chromsome 6---- + ^--Matchs--^ + """ + + def match_based_tournament(ga, matchs): + # User selects how many matchs they want to start with to create one parent. + + # Select two random parents + selected_1_index = random.randint(0, len(ga.population.chromosomes) - 1)] + selected_2_index = random.randint(0, len(ga.population.chromosomes) - 1)] + + if selected_1_index <= selected_2_index: + return parent1 + else: + return parent2 + + def small_tournament(ga): + """ Small tournament is only one round of tournament. Beat the other + randomly selected chromosome and your are selected as a parent. + + Chromosome 1---- + - 1 wins -> Becomes selected for crossover. + Chromosome 2---- - + """ + # Select two random parents + + selected_1_index = random.randint(0, len(ga.population.chromosomes) - 1)] + selected_2_index = random.randint(0, len(ga.population.chromosomes) - 1)] + + if selected_1 <= fitness2: + return parent1 + else: + return parent2 + + def population_based_tournament(ga): + """The size of the population dictates how many matchs there will be. The + greater the size of the population the more matchs there is.""" pass - def roulette_selection(): + def roulette(ga): """Roulette selection works based off of how strong the fitness is of the chromosomes in the population. The stronger the fitness the higher the probability that it will be selected. Using the example of a casino roulette wheel. From 68db360b9465b52e32c86fd230491240b84cffee Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Sun, 4 Oct 2020 23:56:51 -0400 Subject: [PATCH 48/53] Alot of name changes and file name changes --- src/EasyGA.py | 119 +++++++++--------- src/crossover/README.md | 1 - src/crossover/examples.py | 15 --- src/fitness_function/__init__.py | 2 +- .../{examples.py => methods.py} | 6 +- src/fitness_function/test_examples.py | 12 -- src/fitness_function/test_methods.py | 1 + src/initialization/__init__.py | 8 +- .../chromosome_structure/chromosome.py | 26 ++-- src/initialization/gene_structure/gene.py | 2 +- .../{examples.py => methods.py} | 24 ++-- .../population_structure/population.py | 34 ++--- .../test_methods.py} | 0 src/mutation/__init__.py | 2 +- src/mutation/examples.py | 3 - src/mutation/methods.py | 3 + .../test_methods.py} | 0 src/{selection => parent_selection}/README.md | 0 .../__init__.py | 2 +- src/parent_selection/methods.py | 37 ++++++ .../test_methods.py} | 0 src/selection/examples.py | 15 --- src/survivor_selection/README.md | 1 + .../__init__.py | 2 +- src/survivor_selection/methods.py | 8 ++ .../test_methods.py} | 0 src/termination_point/__init__.py | 2 +- .../{examples.py => methods.py} | 2 +- .../{test_examples.py => test_methods.py} | 0 29 files changed, 166 insertions(+), 161 deletions(-) delete mode 100644 src/crossover/README.md delete mode 100644 src/crossover/examples.py rename src/fitness_function/{examples.py => methods.py} (88%) delete mode 100644 src/fitness_function/test_examples.py create mode 100644 src/fitness_function/test_methods.py rename src/initialization/{examples.py => methods.py} (60%) rename src/{crossover/test_examples.py => initialization/test_methods.py} (100%) delete mode 100644 src/mutation/examples.py create mode 100644 src/mutation/methods.py rename src/{initialization/test_examples.py => mutation/test_methods.py} (100%) rename src/{selection => parent_selection}/README.md (100%) rename src/{selection => parent_selection}/__init__.py (56%) create mode 100644 src/parent_selection/methods.py rename src/{mutation/test_examples.py => parent_selection/test_methods.py} (100%) delete mode 100644 src/selection/examples.py create mode 100644 src/survivor_selection/README.md rename src/{crossover => survivor_selection}/__init__.py (56%) create mode 100644 src/survivor_selection/methods.py rename src/{selection/test_examples.py => survivor_selection/test_methods.py} (100%) rename src/termination_point/{examples.py => methods.py} (95%) rename src/termination_point/{test_examples.py => test_methods.py} (100%) diff --git a/src/EasyGA.py b/src/EasyGA.py index 1ae5d60..b3e97f9 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,15 +1,17 @@ import random # Import all the data structure prebuilt modules -from initialization import population as create_population -from initialization import chromosome as create_chromosome -from initialization import gene as create_gene -# Import example classes -from fitness_function import fitness_examples -from initialization import initialization_examples -from termination_point import termination_examples -from selection import selection_examples -from crossover import crossover_examples -from mutation import mutation_examples +from initialization import Population as create_population +from initialization import Chromosome as create_chromosome +from initialization import Gene as create_gene +# Structure Methods +from fitness_function import Fitness_methods +from initialization import Initialization_methods +from termination_point import Termination_methods +# Population Methods +from survivor_selection import Survivor_methods +# Manipulation Methods +from parent_selection import Parent_methods +from mutation import Mutation_methods class GA: def __init__(self): @@ -22,8 +24,9 @@ class GA: self.population = None # Termination varibles self.current_generation = 0 - self.current_fitness = 0 self.generation_goal = 3 + + self.current_fitness = 0 self.fitness_goal = 3 # Mutation variables self.mutation_rate = 0.03 @@ -32,21 +35,56 @@ class GA: self.update_fitness = False # Defualt EastGA implimentation structure - self.initialization_impl = initialization_examples.random_initialization - self.fitness_funciton_impl = fitness_examples.is_it_5 - #self.mutation_impl = PerGeneMutation(Mutation_rate) - #self.selection_impl = TournamentSelection() - #self.crossover_impl = FastSinglePointCrossover() - self.termination_impl = termination_examples.generation_based + self.initialization_impl = Initialization_methods.random_initialization + self.fitness_funciton_impl = Fitness_methods.is_it_5 + # Selects which chromosomes should be automaticly moved to the next population + #self.survivor_selection_impl = Survivor_methods. + # Methods for accomplishing parent-selection -> Crossover -> Mutation + #self.parent_selection_impl = Parent_methods. + #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) + + # 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): """Initialize the population using the initialization implimentation that is currently set""" - self.population = self.initialization_impl( - self.population_size, - self.chromosome_length, - self.chromosome_impl, - self.gene_impl) + self.population = self.initialization_impl(self) def get_population_fitness(self,population): """Will get and set the fitness of each chromosome in the population. @@ -61,43 +99,6 @@ class GA: # 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.chromosomes) - # 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 - # self.repopulate_impl(self) - # Mutation - Manipulates the population very slightly - # self.mutation_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 make_gene(self,value): """Let's the user create a gene.""" return create_gene(value) diff --git a/src/crossover/README.md b/src/crossover/README.md deleted file mode 100644 index 92ad7d2..0000000 --- a/src/crossover/README.md +++ /dev/null @@ -1 +0,0 @@ -# Crossover function diff --git a/src/crossover/examples.py b/src/crossover/examples.py deleted file mode 100644 index 5b64d81..0000000 --- a/src/crossover/examples.py +++ /dev/null @@ -1,15 +0,0 @@ -class crossover_examples: - """ Crossover explination goes here. - - Points - Defined as sections between the chromosomes genetic makeup - """ - - def single_point_crossover(ga): - """Single point crossover is when a "point" is selected and the genetic - make up of the two parent chromosomes are "Crossed" or better known as swapped""" - pass - - def multi_point_crossover(ga,number_of_points = 2): - """Multi point crossover is when a specific number (More then one) of - "points" are created to merge the genetic makup of the chromosomes.""" - pass diff --git a/src/fitness_function/__init__.py b/src/fitness_function/__init__.py index f6294c2..9b676a7 100644 --- a/src/fitness_function/__init__.py +++ b/src/fitness_function/__init__.py @@ -1,2 +1,2 @@ # FROM (. means local) file_name IMPORT class name -from .examples import fitness_examples +from .methods import Fitness_methods diff --git a/src/fitness_function/examples.py b/src/fitness_function/methods.py similarity index 88% rename from src/fitness_function/examples.py rename to src/fitness_function/methods.py index ab964e8..0e8615e 100644 --- a/src/fitness_function/examples.py +++ b/src/fitness_function/methods.py @@ -1,13 +1,13 @@ -class fitness_examples: +class Fitness_methods: """Fitness function examples used""" - + def is_it_5(chromosome): """A very simple case test function - If the chromosomes gene value is a 5 add one to the chromosomes overall fitness value.""" # Overall fitness value fitness = 0 # For each gene in the chromosome - for gene in chromosome.genes: + for gene in chromosome.gene_list: # Check if its value = 5 if(gene.value == 5): # If its value is 5 then add one to diff --git a/src/fitness_function/test_examples.py b/src/fitness_function/test_examples.py deleted file mode 100644 index 1cfd756..0000000 --- a/src/fitness_function/test_examples.py +++ /dev/null @@ -1,12 +0,0 @@ -class test_fitness_funciton: - def get_fitness(self, chromosome): - # For every gene in chromosome - for i in range(len(chromosome.genes)): - # If the gene has a five then add one to the fitness - # Example -> Chromosome = [5],[2],[2],[5],[5] then fitness = 3 - if (chromosome.genes[i].get_value == 5): - # Add to the genes fitness - chromosome.genes[i].fitness += 1 - # Add to the chromosomes fitness - chromosome.fitness += 1 - return chromosome.fitness diff --git a/src/fitness_function/test_methods.py b/src/fitness_function/test_methods.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/fitness_function/test_methods.py @@ -0,0 +1 @@ + diff --git a/src/initialization/__init__.py b/src/initialization/__init__.py index c4790d1..2712c97 100644 --- a/src/initialization/__init__.py +++ b/src/initialization/__init__.py @@ -1,5 +1,5 @@ # FROM (. means local) file_name IMPORT function_name -from .examples import initialization_examples -from .population_structure.population import population -from .chromosome_structure.chromosome import chromosome -from .gene_structure.gene import gene +from .methods import Initialization_methods +from .population_structure.population import Population +from .chromosome_structure.chromosome import Chromosome +from .gene_structure.gene import Gene diff --git a/src/initialization/chromosome_structure/chromosome.py b/src/initialization/chromosome_structure/chromosome.py index aaec88f..c02c940 100644 --- a/src/initialization/chromosome_structure/chromosome.py +++ b/src/initialization/chromosome_structure/chromosome.py @@ -1,11 +1,11 @@ -class chromosome: +class Chromosome: - def __init__(self, genes = None): + def __init__(self, gene_list = None): """Initialize the chromosome based on input gene list, defaulted to an empty list""" - if genes is None: - self.genes = [] + if gene_list is None: + self.gene_list = [] else: - self.genes = genes + self.gene_list = gene_list # The fitness of the overal chromosome self.fitness = None # If the chromosome has been selected then the flag would switch to true @@ -14,16 +14,16 @@ class chromosome: def add_gene(self, gene, index = -1): """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" if index == -1: - index = len(self.genes) - self.genes.insert(index, gene) + index = len(self.gene_list) + self.gene_list.insert(index, gene) def remove_gene(self, index): """Remove a gene from the chromosome at the specified index""" - del self.genes[index] + del self.gene_list[index] def get_genes(self): """Return all genes in the chromosome""" - return self.genes + return self.gene_list def get_fitness(self): """Return the fitness of the chromosome""" @@ -31,11 +31,11 @@ class chromosome: def set_gene(self, gene, index): """Set a gene at a specific index""" - self.genes[index] = gene + self.gene_list[index] = gene - def set_genes(self, genes): + def set_genes(self, gene_list): """Set the entire gene set of the chromosome""" - self.genes = genes + self.gene_list = gene_list def set_fitness(self, fitness): """Set the fitness value of the chromosome""" @@ -44,6 +44,6 @@ class chromosome: def __repr__(self): """Format the repr() output for the chromosome""" output_str = '' - for gene in self.genes: + for gene in self.gene_list: output_str += gene.__repr__() return output_str diff --git a/src/initialization/gene_structure/gene.py b/src/initialization/gene_structure/gene.py index 598b6dc..773b5c1 100644 --- a/src/initialization/gene_structure/gene.py +++ b/src/initialization/gene_structure/gene.py @@ -3,7 +3,7 @@ def check_gene(value): assert value != "" , "Gene can not be empty" return value -class gene: +class Gene: def __init__(self, value): """Initialize a gene with fitness of value None and the input value""" diff --git a/src/initialization/examples.py b/src/initialization/methods.py similarity index 60% rename from src/initialization/examples.py rename to src/initialization/methods.py index 9257b44..312580c 100644 --- a/src/initialization/examples.py +++ b/src/initialization/methods.py @@ -1,31 +1,31 @@ # Import the data structure -from .population_structure.population import population as create_population -from .chromosome_structure.chromosome import chromosome as create_chromosome -from .gene_structure.gene import gene as create_gene +from .population_structure.population import Population as create_population +from .chromosome_structure.chromosome import Chromosome as create_chromosome +from .gene_structure.gene import Gene as create_gene -class initialization_examples: +class Initialization_methods: """Initialization examples that are used as defaults and examples""" - def random_initialization(population_size, chromosome_length, chromosome_impl, gene_impl): + def random_initialization(ga): """Takes the initialization inputs and choregraphs them to output the type of population with the given parameters.""" # Create the population object population = create_population() # Fill the population with chromosomes - for i in range(population_size): + for i in range(ga.population_size): chromosome = create_chromosome() #Fill the Chromosome with genes - for j in range(chromosome_length): + for j in range(ga.chromosome_length): # Using the chromosome_impl to set every index inside of the chromosome - if chromosome_impl != None: + if ga.chromosome_impl != None: # Each chromosome location is specified with its own function - chromosome.add_gene(create_gene(chromosome_impl(j))) + chromosome.add_gene(create_gene(ga.chromosome_impl(j))) # Will break if chromosome_length != len(lists) in domain - elif gene_impl != None: + elif ga.gene_impl != None: # gene_impl = [range function,lowerbound,upperbound] - function = gene_impl[0] - chromosome.add_gene(create_gene(function(*gene_impl[1:]))) + function = ga.gene_impl[0] + chromosome.add_gene(create_gene(function(*ga.gene_impl[1:]))) else: #Exit because either were not specified print("Your domain or range were not specified") diff --git a/src/initialization/population_structure/population.py b/src/initialization/population_structure/population.py index 18c7e38..ec638bf 100644 --- a/src/initialization/population_structure/population.py +++ b/src/initialization/population_structure/population.py @@ -1,11 +1,11 @@ -class population: +class Population: - def __init__(self, chromosomes = None): + def __init__(self, chromosome_list = None): """Intiialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter""" - if chromosomes is None: - self.chromosomes = [] + if chromosome_list is None: + self.chromosome_list = [] else: - self.chromosomes = chromosomes + self.chromosome_list = chromosome_list self.fitness = None def get_closet_fitness(self,value): @@ -15,42 +15,42 @@ class population: def add_chromosome(self, chromosome, index = -1): """Adds a chromosome to the population at the input index, defaulted to the end of the chromosome set""" if index == -1: - index = len(self.chromosomes) - self.chromosomes.insert(index, chromosome) + index = len(self.chromosome_list) + self.chromosome_list.insert(index, chromosome) def remove_chromosome(self, index): """removes a chromosome from the indicated index""" - del self.chromosomes[index] + del self.chromosome_list[index] def get_all_chromosomes(self): """returns all chromosomes in the population""" - return chromosomes + return chromosome_list def get_fitness(self): """returns the population's fitness""" return self.fitness - def set_all_chromosomes(self, chromosomes): + def set_all_chromosomes(self, chromosome_list): """sets the chromosome set of the population""" - self.chromosomes = chromosomes + self.chromosome_list = chromosome_list def set_chromosome(self, chromosome, index): """sets a specific chromosome at a specific index""" - self.chromosomes[index] = chromosome + self.chromosome_list[index] = chromosome def set_fitness(self, fitness): - """Sets the fitness value of the population""" + """Sets the fitness value of the population""" self.fitness = fitness def __repr__(self): """Sets the repr() output format""" - return ''.join([chromosome.__repr__() for chromosome in self.chromosomes]) + return ''.join([chromosome.__repr__() for chromosome in self.chromosome_list]) def print_all(self): """Prints information about the population in the following format:""" """Ex .Current population""" """Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - """ print("Current population:") - for index in range(len(self.chromosomes)): - print(f'Chromosome - {index} {self.chromosomes[index]}', end = "") - print(f' / Fitness = {self.chromosomes[index].fitness}') + for index in range(len(self.chromosome_list)): + print(f'Chromosome - {index} {self.chromosome_list[index]}', end = "") + print(f' / Fitness = {self.chromosome_list[index].fitness}') diff --git a/src/crossover/test_examples.py b/src/initialization/test_methods.py similarity index 100% rename from src/crossover/test_examples.py rename to src/initialization/test_methods.py diff --git a/src/mutation/__init__.py b/src/mutation/__init__.py index c7e2fa1..f74cc90 100644 --- a/src/mutation/__init__.py +++ b/src/mutation/__init__.py @@ -1,2 +1,2 @@ # FROM (. means local) file_name IMPORT function_name -from .examples import mutation_examples +from .methods import Mutation_methods diff --git a/src/mutation/examples.py b/src/mutation/examples.py deleted file mode 100644 index e97b9ff..0000000 --- a/src/mutation/examples.py +++ /dev/null @@ -1,3 +0,0 @@ -class mutation_examples: - """Selection examples will go here """ - pass diff --git a/src/mutation/methods.py b/src/mutation/methods.py new file mode 100644 index 0000000..f09b180 --- /dev/null +++ b/src/mutation/methods.py @@ -0,0 +1,3 @@ +class Mutation_methods: + """Mutation examples will go here """ + pass diff --git a/src/initialization/test_examples.py b/src/mutation/test_methods.py similarity index 100% rename from src/initialization/test_examples.py rename to src/mutation/test_methods.py diff --git a/src/selection/README.md b/src/parent_selection/README.md similarity index 100% rename from src/selection/README.md rename to src/parent_selection/README.md diff --git a/src/selection/__init__.py b/src/parent_selection/__init__.py similarity index 56% rename from src/selection/__init__.py rename to src/parent_selection/__init__.py index b5f82e4..8ecd200 100644 --- a/src/selection/__init__.py +++ b/src/parent_selection/__init__.py @@ -1,2 +1,2 @@ # FROM (. means local) file_name IMPORT function_name -from .examples import selection_examples +from .methods import Parent_methods diff --git a/src/parent_selection/methods.py b/src/parent_selection/methods.py new file mode 100644 index 0000000..b97811c --- /dev/null +++ b/src/parent_selection/methods.py @@ -0,0 +1,37 @@ +class Parent_methods: + """Selection defintion here""" + + def tournament_selection(ga,matchs): + """Tournament selection involves running several "tournaments" among a + few individuals (or "chromosomes")chosen at random from the population. + The winner of each tournament (the one with the best fitness) is selected + for crossover. + Ex + Chromsome 1----1 wins ------ + Chromsome 2---- - --1 wins---- + - - + Chromsome 3----3 wins ------ -- 5 Wins --->Chromosome 5 becomes Parent + Chromsome 4---- - + - + Chromsome 5----5 wins ---------5 wins---- + Chromsome 6---- + ^--Matchs--^ + """ + + def small_tournament(ga): + """ Small tournament is only one round of tournament. Beat the other + randomly selected chromosome and your are selected as a parent. + Chromosome 1---- + -- 1 wins -> Becomes selected for crossover. + Chromosome 2---- + """ + pass + + def roulette_selection(ga): + """Roulette selection works based off of how strong the fitness is of the + chromosomes in the population. The stronger the fitness the higher the probability + that it will be selected. Using the example of a casino roulette wheel. + Where the chromosomes are the numbers to be selected and the board size for + those numbers are directly proportional to the chromosome's current fitness. Where + the ball falls is a randomly generated number between 0 and 1""" + pass diff --git a/src/mutation/test_examples.py b/src/parent_selection/test_methods.py similarity index 100% rename from src/mutation/test_examples.py rename to src/parent_selection/test_methods.py diff --git a/src/selection/examples.py b/src/selection/examples.py deleted file mode 100644 index 2c149a7..0000000 --- a/src/selection/examples.py +++ /dev/null @@ -1,15 +0,0 @@ -class selection_examples: - """Selection defintion here""" - - def tournament_selection(): - """ """ - pass - - def roulette_selection(): - """Roulette selection works based off of how strong the fitness is of the - chromosomes in the population. The stronger the fitness the higher the probability - that it will be selected. Using the example of a casino roulette wheel. - Where the chromosomes are the numbers to be selected and the board size for - those numbers are directly proportional to the chromosome's current fitness. Where - the ball falls is a randomly generated number between 0 and 1""" - pass diff --git a/src/survivor_selection/README.md b/src/survivor_selection/README.md new file mode 100644 index 0000000..98253e9 --- /dev/null +++ b/src/survivor_selection/README.md @@ -0,0 +1 @@ +# Selection functions diff --git a/src/crossover/__init__.py b/src/survivor_selection/__init__.py similarity index 56% rename from src/crossover/__init__.py rename to src/survivor_selection/__init__.py index f9697e1..ea2e686 100644 --- a/src/crossover/__init__.py +++ b/src/survivor_selection/__init__.py @@ -1,2 +1,2 @@ # FROM (. means local) file_name IMPORT function_name -from .examples import crossover_examples +from .methods import Survivor_methods diff --git a/src/survivor_selection/methods.py b/src/survivor_selection/methods.py new file mode 100644 index 0000000..ad287b8 --- /dev/null +++ b/src/survivor_selection/methods.py @@ -0,0 +1,8 @@ +class Survivor_methods: + """Survivor methods defintion here""" + + def elitism(): + pass + + def remove_two_worst(): + pass diff --git a/src/selection/test_examples.py b/src/survivor_selection/test_methods.py similarity index 100% rename from src/selection/test_examples.py rename to src/survivor_selection/test_methods.py diff --git a/src/termination_point/__init__.py b/src/termination_point/__init__.py index 55fe7e1..92b7b26 100644 --- a/src/termination_point/__init__.py +++ b/src/termination_point/__init__.py @@ -1,2 +1,2 @@ # FROM (. means local) file_name IMPORT class name -from .examples import termination_examples +from .methods import Termination_methods diff --git a/src/termination_point/examples.py b/src/termination_point/methods.py similarity index 95% rename from src/termination_point/examples.py rename to src/termination_point/methods.py index 823757a..8f1aaa5 100644 --- a/src/termination_point/examples.py +++ b/src/termination_point/methods.py @@ -1,4 +1,4 @@ -class termination_examples: +class Termination_methods: """Example functions that can be used to terminate the the algorithms loop""" def fitness_based(ga): diff --git a/src/termination_point/test_examples.py b/src/termination_point/test_methods.py similarity index 100% rename from src/termination_point/test_examples.py rename to src/termination_point/test_methods.py From 68af88df928123d043ba04f0f2fb532762e549e2 Mon Sep 17 00:00:00 2001 From: Daniel Wilczak Date: Mon, 5 Oct 2020 19:03:16 -0400 Subject: [PATCH 49/53] Some how we lost the crossover folder --- src/EasyGA.py | 1 + src/crossover/README.md | 1 + src/crossover/__init__.py | 2 ++ src/crossover/methods.py | 3 +++ src/crossover/test_methods.py | 0 5 files changed, 7 insertions(+) create mode 100644 src/crossover/README.md create mode 100644 src/crossover/__init__.py create mode 100644 src/crossover/methods.py create mode 100644 src/crossover/test_methods.py diff --git a/src/EasyGA.py b/src/EasyGA.py index b3e97f9..8011230 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -12,6 +12,7 @@ from survivor_selection import Survivor_methods # Manipulation Methods from parent_selection import Parent_methods from mutation import Mutation_methods +from crossover import Crossover_methods class GA: def __init__(self): diff --git a/src/crossover/README.md b/src/crossover/README.md new file mode 100644 index 0000000..c38d93c --- /dev/null +++ b/src/crossover/README.md @@ -0,0 +1 @@ +# Mutation functions diff --git a/src/crossover/__init__.py b/src/crossover/__init__.py new file mode 100644 index 0000000..c722c52 --- /dev/null +++ b/src/crossover/__init__.py @@ -0,0 +1,2 @@ +# FROM (. means local) file_name IMPORT function_name +from .methods import Crossover_methods diff --git a/src/crossover/methods.py b/src/crossover/methods.py new file mode 100644 index 0000000..eb81ec7 --- /dev/null +++ b/src/crossover/methods.py @@ -0,0 +1,3 @@ +class Crossover_methods: + """Mutation examples will go here """ + pass diff --git a/src/crossover/test_methods.py b/src/crossover/test_methods.py new file mode 100644 index 0000000..e69de29 From 4799722a126b3f31725034d7131fe8db5fc20f01 Mon Sep 17 00:00:00 2001 From: jcurtis664 <71359540+jcurtis664@users.noreply.github.com> Date: Tue, 6 Oct 2020 14:48:51 -0400 Subject: [PATCH 50/53] Add files via upload --- src/EasyGA.py | 150 ++++++++++++++++++++++++++------------------------ 1 file changed, 77 insertions(+), 73 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 8011230..90c85d6 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,86 +1,46 @@ import random # Import all the data structure prebuilt modules -from initialization import Population as create_population -from initialization import Chromosome as create_chromosome -from initialization import Gene as create_gene -# Structure Methods -from fitness_function import Fitness_methods -from initialization import Initialization_methods -from termination_point import Termination_methods -# Population Methods -from survivor_selection import Survivor_methods -# Manipulation Methods -from parent_selection import Parent_methods -from mutation import Mutation_methods -from crossover import Crossover_methods +from initialization import population as create_population +from initialization import chromosome as create_chromosome +from initialization import gene as create_gene +# Import example classes +from fitness_function import fitness_examples +from initialization import initialization_examples +from termination_point import termination_examples +from selection import selection_examples +from crossover import crossover_examples +from repopulate import repopulate_examples +from mutation import mutation_examples class GA: def __init__(self): - """Initialize the GA.""" + """Initialize the genetic algorithm. Where all the hyper parmeters are + set for the the ga to function.""" # Initilization variables - self.chromosome_length = 3 - self.population_size = 5 - self.chromosome_impl = None - self.gene_impl = None - self.population = None + self.chromosome_length = 3 + self.population_size = 10 + self.chromosome_impl = None + self.gene_impl = None + self.population = None # Termination varibles self.current_generation = 0 - self.generation_goal = 3 - - self.current_fitness = 0 - self.fitness_goal = 3 + self.current_fitness = 0 + self.generation_goal = 0 + self.fitness_goal = 4 # Mutation variables - self.mutation_rate = 0.03 + self.mutation_rate = 0.02 # Rerun already computed fitness - self.update_fitness = False + self.update_fitness = False # Defualt EastGA implimentation structure - self.initialization_impl = Initialization_methods.random_initialization - self.fitness_funciton_impl = Fitness_methods.is_it_5 - # Selects which chromosomes should be automaticly moved to the next population - #self.survivor_selection_impl = Survivor_methods. - # Methods for accomplishing parent-selection -> Crossover -> Mutation - #self.parent_selection_impl = Parent_methods. - #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) - - # 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) + self.initialization_impl = initialization_examples.random_initialization + self.fitness_funciton_impl = fitness_examples.is_it_5 + self.selection_impl = selection_examples.roulette + self.crossover_impl = crossover_examples.single_point_crossover + self.repopulate_impl = repopulate_examples.kill_two_worst + self.mutation_impl = mutation_examples.per_gene_mutation + self.termination_impl = termination_examples.generation_based def initialize_population(self): """Initialize the population using the initialization @@ -96,9 +56,53 @@ class GA: for chromosome in population: # 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. - if(chromosome.fitness == None or self.update_fitness == True): - # Set the chromosomes fitness using the fitness function - chromosome.fitness = self.fitness_funciton_impl(chromosome) + chromosome.set_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): """Let's the user create a gene.""" From 04c749d95c178c7af8e4f898183dec4304f9a84a Mon Sep 17 00:00:00 2001 From: jcurtis664 <71359540+jcurtis664@users.noreply.github.com> Date: Tue, 6 Oct 2020 14:50:46 -0400 Subject: [PATCH 51/53] Add files via upload --- src/EasyGA.py | 150 ++++++++++++++++++++++++-------------------------- 1 file changed, 73 insertions(+), 77 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 90c85d6..cdc2c00 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -1,46 +1,86 @@ import random # Import all the data structure prebuilt modules -from initialization import population as create_population -from initialization import chromosome as create_chromosome -from initialization import gene as create_gene -# Import example classes -from fitness_function import fitness_examples -from initialization import initialization_examples -from termination_point import termination_examples -from selection import selection_examples -from crossover import crossover_examples -from repopulate import repopulate_examples -from mutation import mutation_examples +from initialization import Population as create_population +from initialization import Chromosome as create_chromosome +from initialization import Gene as create_gene +# Structure Methods +from fitness_function import Fitness_methods +from initialization import Initialization_methods +from termination_point import Termination_methods +# Population Methods +from survivor_selection import Survivor_methods +# Manipulation Methods +from parent_selection import Parent_methods +from mutation import Mutation_methods +from crossover import Crossover_methods class GA: def __init__(self): - """Initialize the genetic algorithm. Where all the hyper parmeters are - set for the the ga to function.""" + """Initialize the GA.""" # Initilization variables - self.chromosome_length = 3 - self.population_size = 10 - self.chromosome_impl = None - self.gene_impl = None - self.population = None + self.chromosome_length = 3 + self.population_size = 5 + self.chromosome_impl = None + self.gene_impl = None + self.population = None # Termination varibles self.current_generation = 0 - self.current_fitness = 0 - self.generation_goal = 0 - self.fitness_goal = 4 + self.generation_goal = 3 + + self.current_fitness = 0 + self.fitness_goal = 3 # Mutation variables - self.mutation_rate = 0.02 + self.mutation_rate = 0.03 # Rerun already computed fitness - self.update_fitness = False + self.update_fitness = False # Defualt EastGA implimentation structure - self.initialization_impl = initialization_examples.random_initialization - self.fitness_funciton_impl = fitness_examples.is_it_5 - self.selection_impl = selection_examples.roulette - self.crossover_impl = crossover_examples.single_point_crossover - self.repopulate_impl = repopulate_examples.kill_two_worst - self.mutation_impl = mutation_examples.per_gene_mutation - self.termination_impl = termination_examples.generation_based + self.initialization_impl = Initialization_methods.random_initialization + self.fitness_funciton_impl = Fitness_methods.is_it_5 + # Selects which chromosomes should be automaticly moved to the next population + #self.survivor_selection_impl = Survivor_methods. + # Methods for accomplishing parent-selection -> Crossover -> Mutation + # self.parent_selection_impl = Parent_methods.roulette_selection + #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): """Initialize the population using the initialization @@ -56,53 +96,9 @@ class GA: for chromosome in population: # 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. - chromosome.set_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 + 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 make_gene(self,value): """Let's the user create a gene.""" From a083d16db86aaaf44b8bf6ae443f3d321e0c66c0 Mon Sep 17 00:00:00 2001 From: Daniel Wilczak <44122838+danielwilczak101@users.noreply.github.com> Date: Tue, 6 Oct 2020 20:55:07 -0400 Subject: [PATCH 52/53] Delete .DS_Store --- .DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index e09bcec82ba35e0c9ae5fd34cc730e237f24f56c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK!EVz)5S>jzyH1730jWLug2bUhsag>sgk;k65D5^%2o8W+yN;+O*BiwSX$e8T z@DcDwd;uTA2f*9iNkx)!=mnu_N1A=J<2UR0?Z)dRBGDZuJ47ual5mYR56vZ}uWMhi z8fVsk!i@2ZLOP&(RMOcB+a>--1@zo)&=cy@K4$LE`78UGI*v0HgQ1tlL-^fa#t;#C z^a2%+sYg%EoS#y%SRpo-DkK#&z}gto5Nl*e=S%!AOZ+oiZ)q^rhxQkZc2$CCVB6p8?9R6RX{XahynBB( z^W^58ok#tn)ANthPqWWo@hC8XKaa6njC1&gfJOKaj`B?9?~$3a=PZX#0jGdd;K~Z{ zi$rc*xzZIl1)KucT><@m@ZlN*#>%33b)Ydv0AK@YV~F*~qOS271IEfCuE2yX1=>=< zM+{-hk@uMwFjf|AISD>|2)?u66N)f*$N0XXlL#!j)+yi=SX7{HyDh!{AN>9NzgXmY zP64ODwNgMddSS1NN8-J8<>B;R>%))W+BC1Scu|7DY{lT}t#}J=40&G%z<{x`hz!hq O2pAb$;}rO(3j79Iz<=fd From 32b61196c5c745718172f5aa9722b965046f8b26 Mon Sep 17 00:00:00 2001 From: Daniel Wilczak <44122838+danielwilczak101@users.noreply.github.com> Date: Tue, 6 Oct 2020 20:56:52 -0400 Subject: [PATCH 53/53] Delete test_EasyGA.py --- src/test_EasyGA.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/test_EasyGA.py diff --git a/src/test_EasyGA.py b/src/test_EasyGA.py deleted file mode 100644 index e69de29..0000000