From c959f44fd520619c3cc64670d80e2daaf6474651 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sat, 7 Nov 2020 13:32:34 -0500 Subject: [PATCH] GA gives graph the database --- src/EasyGA.py | 16 +++++++--------- src/attributes.py | 6 +++--- src/database/graph.py | 16 ++++++++-------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/EasyGA.py b/src/EasyGA.py index 5c4f9ca..b7448f5 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -50,15 +50,10 @@ class GA(Attributes): # Create the database here to allow the user to change # the database name and structure in the running function. - self.database = database.Database() self.database.create_data_table(self) # Create the initial population self.initialize_population() - self.set_all_fitness() - self.population.sort_by_best_fitness(self) - # Save the population to the database - self.database.insert_current_population(self) # Otherwise evolve the population else: @@ -67,10 +62,13 @@ class GA(Attributes): self.survivor_selection_impl(self) self.population.update() self.mutation_population_impl(self) - self.set_all_fitness() - self.population.sort_by_best_fitness(self) - # Save the population to the database - self.database.insert_current_population(self) + + # Update and sort fitnesses + self.set_all_fitness() + self.population.sort_by_best_fitness(self) + + # Save the population to the database + self.database.insert_current_population(self) number_of_generations -= 1 self.current_generation += 1 diff --git a/src/attributes.py b/src/attributes.py index 522cdc6..4f27d13 100644 --- a/src/attributes.py +++ b/src/attributes.py @@ -63,7 +63,7 @@ class Attributes: mutation_individual_impl = Mutation_Methods.Individual.single_gene, mutation_population_impl = Mutation_Methods.Population.random_selection, termination_impl = Termination_Methods.fitness_and_generation_based, - database = None, + Database = database.Database, database_name = 'database.db', sql_create_data_structure = """CREATE TABLE IF NOT EXISTS data ( id integer PRIMARY KEY, @@ -117,12 +117,12 @@ class Attributes: self.termination_impl = deepcopy(termination_impl) # Database varibles - self.database = deepcopy(database) + self.database = Database() self.database_name = deepcopy(database_name) self.sql_create_data_structure = deepcopy(sql_create_data_structure) # Graphing variables - self.graph = Graph(self) + self.graph = Graph(self.database) # Getter and setters for all required varibles diff --git a/src/database/graph.py b/src/database/graph.py index 852c4c6..fe5917a 100644 --- a/src/database/graph.py +++ b/src/database/graph.py @@ -8,8 +8,8 @@ class Graph: """Prebuilt graphing functions to make visual represention of fitness data.""" - def __init__(self, ga): - self.ga = ga + def __init__(self, database): + self.database = database def make_plot(self, type_of_plot, size, X, Y): @@ -30,13 +30,13 @@ class Graph: """Show a plot of generation by generation total fitness.""" # Query the X data - generations = self.ga.database.get_total_generations() + generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] X = list(range(0, generations)) # Query for Y data - Y = self.ga.database.get_generation_total_fitness() + Y = self.database.get_generation_total_fitness() self.make_plot(type_of_plot, size, X, Y) @@ -53,13 +53,13 @@ class Graph: """Generation by Max value chromosome """ # Query the X data - generations = self.ga.database.get_total_generations() + generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] X = list(range(0, generations)) # Query for Y data - Y = self.ga.database.get_highest_chromosome() + Y = self.database.get_highest_chromosome() self.make_plot(type_of_plot, size, X, Y) @@ -76,13 +76,13 @@ class Graph: """Generation by Min value Chromosome """ # Query the X data - generations = self.ga.database.get_total_generations() + generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] X = list(range(0, generations)) # Query for Y data - Y = self.ga.database.get_lowest_chromosome() + Y = self.database.get_lowest_chromosome() self.make_plot(type_of_plot, size, X, Y)