GA gives graph the database

This commit is contained in:
SimpleArt
2020-11-07 13:32:34 -05:00
parent f1f9d70c26
commit c959f44fd5
3 changed files with 18 additions and 20 deletions

View File

@ -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

View File

@ -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

View File

@ -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)