Fixed graph features
- Common graph code made into a method. - No longer needs the user to pass in the ga for graphing. - Changed graph attribute from class to object. - Added ga to the graph object as an attribute on initialization to avoid needing to pass it in every time you graph. - Capitalized database/graph classes.
This commit is contained in:
@ -50,7 +50,7 @@ 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 = database.Database()
|
||||||
self.database.create_data_table(self)
|
self.database.create_data_table(self)
|
||||||
|
|
||||||
# Create the initial population
|
# Create the initial population
|
||||||
@ -158,3 +158,9 @@ class GA(Attributes):
|
|||||||
"""Prints the best chromosome and its fitness"""
|
"""Prints the best chromosome and its fitness"""
|
||||||
print(f"Best Chromosome \t: {self.population.get_chromosome(0)}")
|
print(f"Best Chromosome \t: {self.population.get_chromosome(0)}")
|
||||||
print(f"Best Fitness \t: {self.population.get_chromosome(0).get_fitness()}")
|
print(f"Best Fitness \t: {self.population.get_chromosome(0).get_fitness()}")
|
||||||
|
|
||||||
|
|
||||||
|
def print_worst(self):
|
||||||
|
"""Prints the worst chromosome and its fitness"""
|
||||||
|
print(f"Worst Chromosome \t: {self.population.get_chromosome(-1)}")
|
||||||
|
print(f"Worst Fitness \t: {self.population.get_chromosome(-1).get_fitness()}")
|
||||||
|
|||||||
@ -71,7 +71,7 @@ class Attributes:
|
|||||||
fitness DOUBLE,
|
fitness DOUBLE,
|
||||||
chromosome text
|
chromosome text
|
||||||
); """,
|
); """,
|
||||||
graph = graph.graph
|
Graph = graph.Graph
|
||||||
):
|
):
|
||||||
|
|
||||||
# Initilization variables
|
# Initilization variables
|
||||||
@ -122,7 +122,8 @@ class Attributes:
|
|||||||
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 = deepcopy(graph)
|
self.graph = Graph(self)
|
||||||
|
|
||||||
|
|
||||||
# Getter and setters for all required varibles
|
# Getter and setters for all required varibles
|
||||||
@property
|
@property
|
||||||
@ -131,6 +132,7 @@ class Attributes:
|
|||||||
|
|
||||||
return self._chromosome_length
|
return self._chromosome_length
|
||||||
|
|
||||||
|
|
||||||
@chromosome_length.setter
|
@chromosome_length.setter
|
||||||
def chromosome_length(self, value_input):
|
def chromosome_length(self, value_input):
|
||||||
"""Setter function with error checking for chromosome length"""
|
"""Setter function with error checking for chromosome length"""
|
||||||
@ -147,6 +149,7 @@ class Attributes:
|
|||||||
|
|
||||||
return self._population_size
|
return self._population_size
|
||||||
|
|
||||||
|
|
||||||
@population_size.setter
|
@population_size.setter
|
||||||
def population_size(self, value_input):
|
def population_size(self, value_input):
|
||||||
"""Setter function with error checking for population size"""
|
"""Setter function with error checking for population size"""
|
||||||
|
|||||||
@ -3,7 +3,7 @@ from sqlite3 import Error
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class database:
|
class Database:
|
||||||
"""Main database class that controls all the functionality for input /
|
"""Main database class that controls all the functionality for input /
|
||||||
out of the database."""
|
out of the database."""
|
||||||
|
|
||||||
|
|||||||
@ -4,30 +4,41 @@ import matplotlib.pyplot as plt
|
|||||||
from database import database
|
from database import database
|
||||||
from sqlite3 import Error
|
from sqlite3 import Error
|
||||||
|
|
||||||
class graph:
|
class Graph:
|
||||||
"""Prebuilt graphing functions to make visual represention of fitness data."""
|
"""Prebuilt graphing functions to make visual represention of fitness data."""
|
||||||
|
|
||||||
def generation_total_fitness(ga,type_of_plot = "line",size = [6,6]):
|
|
||||||
|
def __init__(self, ga):
|
||||||
|
self.ga = ga
|
||||||
|
|
||||||
|
|
||||||
|
def make_plot(self, type_of_plot, size, X, Y):
|
||||||
|
"""Create the plot"""
|
||||||
|
|
||||||
|
# Set the plot size
|
||||||
|
plt.figure(figsize = size)
|
||||||
|
|
||||||
|
if(type_of_plot == "line"):
|
||||||
|
plt.plot(X, Y)
|
||||||
|
elif(type_of_plot == "scatter"):
|
||||||
|
plt.scatter(X, Y)
|
||||||
|
elif(type_of_plot == "bar"):
|
||||||
|
plt.bar(X, Y)
|
||||||
|
|
||||||
|
|
||||||
|
def generation_total_fitness(self, type_of_plot = "line", size = [6,6]):
|
||||||
"""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 = ga.database.get_total_generations()
|
generations = self.ga.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 = ga.database.get_generation_total_fitness()
|
Y = self.ga.database.get_generation_total_fitness()
|
||||||
|
|
||||||
# Set the plot size
|
self.make_plot(type_of_plot, size, X, Y)
|
||||||
plt.figure(figsize=size)
|
|
||||||
|
|
||||||
if(type_of_plot == "line"):
|
|
||||||
plt.plot(X,Y)
|
|
||||||
elif(type_of_plot == "scatter"):
|
|
||||||
plt.scatter(X,Y)
|
|
||||||
elif(type_of_plot == "bar"):
|
|
||||||
plt.bar(X,Y)
|
|
||||||
|
|
||||||
# x and y labels
|
# x and y labels
|
||||||
plt.xlabel('Generation')
|
plt.xlabel('Generation')
|
||||||
@ -37,27 +48,20 @@ class graph:
|
|||||||
# Show the plot
|
# Show the plot
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
def highest_value_chromosome(ga,type_of_plot = "line",size = [6,6]):
|
|
||||||
|
def highest_value_chromosome(self, type_of_plot = "line", size = [6,6]):
|
||||||
"""Generation by Max value chromosome """
|
"""Generation by Max value chromosome """
|
||||||
|
|
||||||
# Query the X data
|
# Query the X data
|
||||||
generations = ga.database.get_total_generations()
|
generations = self.ga.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 = ga.database.get_highest_chromosome()
|
Y = self.ga.database.get_highest_chromosome()
|
||||||
|
|
||||||
# Set the plot size
|
self.make_plot(type_of_plot, size, X, Y)
|
||||||
plt.figure(figsize=size)
|
|
||||||
|
|
||||||
if(type_of_plot == "line"):
|
|
||||||
plt.plot(X,Y)
|
|
||||||
elif(type_of_plot == "scatter"):
|
|
||||||
plt.scatter(X,Y)
|
|
||||||
elif(type_of_plot == "bar"):
|
|
||||||
plt.bar(X,Y)
|
|
||||||
|
|
||||||
# x and y labels
|
# x and y labels
|
||||||
plt.xlabel('Generation')
|
plt.xlabel('Generation')
|
||||||
@ -68,27 +72,19 @@ class graph:
|
|||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
def lowest_value_chromosome(ga,type_of_plot = "line",size = [6,6]):
|
def lowest_value_chromosome(self, type_of_plot = "line", size = [6,6]):
|
||||||
"""Generation by Min value Chromosome """
|
"""Generation by Min value Chromosome """
|
||||||
|
|
||||||
# Query the X data
|
# Query the X data
|
||||||
generations = ga.database.get_total_generations()
|
generations = self.ga.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 = ga.database.get_lowest_chromosome()
|
Y = self.ga.database.get_lowest_chromosome()
|
||||||
|
|
||||||
# Set the plot size
|
self.make_plot(type_of_plot, size, X, Y)
|
||||||
plt.figure(figsize=size)
|
|
||||||
|
|
||||||
if(type_of_plot == "line"):
|
|
||||||
plt.plot(X,Y)
|
|
||||||
elif(type_of_plot == "scatter"):
|
|
||||||
plt.scatter(X,Y)
|
|
||||||
elif(type_of_plot == "bar"):
|
|
||||||
plt.bar(X,Y)
|
|
||||||
|
|
||||||
# x and y labels
|
# x and y labels
|
||||||
plt.xlabel('Generation')
|
plt.xlabel('Generation')
|
||||||
|
|||||||
@ -20,4 +20,4 @@ ga.evolve()
|
|||||||
|
|
||||||
ga.print_population()
|
ga.print_population()
|
||||||
|
|
||||||
ga.graph.lowest_value_chromosome(ga)
|
ga.graph.lowest_value_chromosome()
|
||||||
|
|||||||
Reference in New Issue
Block a user