GA gives graph the database
This commit is contained in:
@ -50,15 +50,10 @@ class GA(Attributes):
|
|||||||
|
|
||||||
# Create the database here to allow the user to change
|
# Create the database here to allow the user to change
|
||||||
# the database name and structure in the running function.
|
# the database name and structure in the running function.
|
||||||
self.database = database.Database()
|
|
||||||
self.database.create_data_table(self)
|
self.database.create_data_table(self)
|
||||||
|
|
||||||
# Create the initial population
|
# Create the initial population
|
||||||
self.initialize_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
|
# Otherwise evolve the population
|
||||||
else:
|
else:
|
||||||
@ -67,8 +62,11 @@ class GA(Attributes):
|
|||||||
self.survivor_selection_impl(self)
|
self.survivor_selection_impl(self)
|
||||||
self.population.update()
|
self.population.update()
|
||||||
self.mutation_population_impl(self)
|
self.mutation_population_impl(self)
|
||||||
|
|
||||||
|
# Update and sort fitnesses
|
||||||
self.set_all_fitness()
|
self.set_all_fitness()
|
||||||
self.population.sort_by_best_fitness(self)
|
self.population.sort_by_best_fitness(self)
|
||||||
|
|
||||||
# Save the population to the database
|
# Save the population to the database
|
||||||
self.database.insert_current_population(self)
|
self.database.insert_current_population(self)
|
||||||
|
|
||||||
|
|||||||
@ -63,7 +63,7 @@ class Attributes:
|
|||||||
mutation_individual_impl = Mutation_Methods.Individual.single_gene,
|
mutation_individual_impl = Mutation_Methods.Individual.single_gene,
|
||||||
mutation_population_impl = Mutation_Methods.Population.random_selection,
|
mutation_population_impl = Mutation_Methods.Population.random_selection,
|
||||||
termination_impl = Termination_Methods.fitness_and_generation_based,
|
termination_impl = Termination_Methods.fitness_and_generation_based,
|
||||||
database = None,
|
Database = database.Database,
|
||||||
database_name = 'database.db',
|
database_name = 'database.db',
|
||||||
sql_create_data_structure = """CREATE TABLE IF NOT EXISTS data (
|
sql_create_data_structure = """CREATE TABLE IF NOT EXISTS data (
|
||||||
id integer PRIMARY KEY,
|
id integer PRIMARY KEY,
|
||||||
@ -117,12 +117,12 @@ class Attributes:
|
|||||||
self.termination_impl = deepcopy(termination_impl)
|
self.termination_impl = deepcopy(termination_impl)
|
||||||
|
|
||||||
# Database varibles
|
# Database varibles
|
||||||
self.database = deepcopy(database)
|
self.database = Database()
|
||||||
self.database_name = deepcopy(database_name)
|
self.database_name = deepcopy(database_name)
|
||||||
self.sql_create_data_structure = deepcopy(sql_create_data_structure)
|
self.sql_create_data_structure = deepcopy(sql_create_data_structure)
|
||||||
|
|
||||||
# Graphing variables
|
# Graphing variables
|
||||||
self.graph = Graph(self)
|
self.graph = Graph(self.database)
|
||||||
|
|
||||||
|
|
||||||
# Getter and setters for all required varibles
|
# Getter and setters for all required varibles
|
||||||
|
|||||||
@ -8,8 +8,8 @@ class Graph:
|
|||||||
"""Prebuilt graphing functions to make visual represention of fitness data."""
|
"""Prebuilt graphing functions to make visual represention of fitness data."""
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, ga):
|
def __init__(self, database):
|
||||||
self.ga = ga
|
self.database = database
|
||||||
|
|
||||||
|
|
||||||
def make_plot(self, type_of_plot, size, X, Y):
|
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."""
|
"""Show a plot of generation by generation total fitness."""
|
||||||
|
|
||||||
# Query the X data
|
# 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]
|
# Create the generations list - [0,1,2,etc]
|
||||||
X = list(range(0, generations))
|
X = list(range(0, generations))
|
||||||
|
|
||||||
# Query for Y data
|
# 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)
|
self.make_plot(type_of_plot, size, X, Y)
|
||||||
|
|
||||||
@ -53,13 +53,13 @@ class Graph:
|
|||||||
"""Generation by Max value chromosome """
|
"""Generation by Max value chromosome """
|
||||||
|
|
||||||
# Query the X data
|
# 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]
|
# Create the generations list - [0,1,2,etc]
|
||||||
X = list(range(0, generations))
|
X = list(range(0, generations))
|
||||||
|
|
||||||
# Query for Y data
|
# 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)
|
self.make_plot(type_of_plot, size, X, Y)
|
||||||
|
|
||||||
@ -76,13 +76,13 @@ class Graph:
|
|||||||
"""Generation by Min value Chromosome """
|
"""Generation by Min value Chromosome """
|
||||||
|
|
||||||
# Query the X data
|
# 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]
|
# Create the generations list - [0,1,2,etc]
|
||||||
X = list(range(0, generations))
|
X = list(range(0, generations))
|
||||||
|
|
||||||
# Query for Y data
|
# 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)
|
self.make_plot(type_of_plot, size, X, Y)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user