Moved all graphing to its own class
This commit is contained in:
@ -24,6 +24,7 @@ from database import database
|
|||||||
from sqlite3 import Error
|
from sqlite3 import Error
|
||||||
|
|
||||||
# Graphing package
|
# Graphing package
|
||||||
|
from database import graph
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
@ -157,34 +158,3 @@ 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 graph_scatter(self):
|
|
||||||
"""Show a scatter plot of the database information."""
|
|
||||||
|
|
||||||
# Query the X data
|
|
||||||
generations = self.database.query_one_item("SELECT COUNT(DISTINCT generation) FROM data;")
|
|
||||||
|
|
||||||
# Create the generations array
|
|
||||||
X = list(range(0, generations))
|
|
||||||
|
|
||||||
#Query the Y data
|
|
||||||
Y_data = self.database.query_all("SELECT SUM(fitness) FROM data GROUP BY generation;")
|
|
||||||
|
|
||||||
# Format the Y data so we can use it to plot
|
|
||||||
Y = [i[0] for i in Y_data]
|
|
||||||
|
|
||||||
# Set the plot size
|
|
||||||
plt.figure(figsize=[5, 5])
|
|
||||||
|
|
||||||
plt.scatter(X,Y)
|
|
||||||
# x and y labels
|
|
||||||
plt.xlabel('Generation')
|
|
||||||
plt.ylabel('Generation Total Fitness')
|
|
||||||
plt.title('Relationship Between Generations and Generation Total Fitness')
|
|
||||||
|
|
||||||
# Show the plot
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
|
|
||||||
def graph_line(self):
|
|
||||||
pass
|
|
||||||
|
|||||||
@ -24,6 +24,10 @@ from crossover import Crossover_Methods
|
|||||||
from database import database
|
from database import database
|
||||||
from sqlite3 import Error
|
from sqlite3 import Error
|
||||||
|
|
||||||
|
# Graphing package
|
||||||
|
from database import graph
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
class Attributes:
|
class Attributes:
|
||||||
"""Default GA attributes can be found here. If any attributes have not
|
"""Default GA attributes can be found here. If any attributes have not
|
||||||
@ -66,7 +70,8 @@ class Attributes:
|
|||||||
generation integer NOT NULL,
|
generation integer NOT NULL,
|
||||||
fitness DOUBLE,
|
fitness DOUBLE,
|
||||||
chromosome text
|
chromosome text
|
||||||
); """
|
); """,
|
||||||
|
graph = graph.graph
|
||||||
):
|
):
|
||||||
|
|
||||||
# Initilization variables
|
# Initilization variables
|
||||||
@ -116,6 +121,8 @@ class Attributes:
|
|||||||
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
|
||||||
|
self.graph = deepcopy(graph)
|
||||||
|
|
||||||
# Getter and setters for all required varibles
|
# Getter and setters for all required varibles
|
||||||
@property
|
@property
|
||||||
|
|||||||
39
src/database/graph.py
Normal file
39
src/database/graph.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
# Graphing package
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
# Database class
|
||||||
|
from database import database
|
||||||
|
from sqlite3 import Error
|
||||||
|
|
||||||
|
class graph:
|
||||||
|
""" """
|
||||||
|
|
||||||
|
def scatter(ga):
|
||||||
|
"""Show a scatter plot of the database information."""
|
||||||
|
|
||||||
|
# Query the X data
|
||||||
|
generations = ga.database.query_one_item("SELECT COUNT(DISTINCT generation) FROM data;")
|
||||||
|
|
||||||
|
# Create the generations array
|
||||||
|
X = list(range(0, generations))
|
||||||
|
|
||||||
|
#Query the Y data
|
||||||
|
Y_data = ga.database.query_all("SELECT SUM(fitness) FROM data GROUP BY generation;")
|
||||||
|
|
||||||
|
# Format the Y data so we can use it to plot
|
||||||
|
Y = [i[0] for i in Y_data]
|
||||||
|
|
||||||
|
# Set the plot size
|
||||||
|
plt.figure(figsize=[5, 5])
|
||||||
|
|
||||||
|
plt.scatter(X,Y)
|
||||||
|
# x and y labels
|
||||||
|
plt.xlabel('Generation')
|
||||||
|
plt.ylabel('Generation Total Fitness')
|
||||||
|
plt.title('Relationship Between Generations and Generation Total Fitness')
|
||||||
|
|
||||||
|
# Show the plot
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
def line(ga):
|
||||||
|
pass
|
||||||
@ -7,7 +7,7 @@ ga = EasyGA.GA()
|
|||||||
# Create 25 chromosomes each with 10 genes and 200 generations
|
# Create 25 chromosomes each with 10 genes and 200 generations
|
||||||
ga.population_size = 25
|
ga.population_size = 25
|
||||||
ga.chromosome_length = 10
|
ga.chromosome_length = 10
|
||||||
ga.generation_goal = 200
|
ga.generation_goal = 150
|
||||||
|
|
||||||
# Create random genes from 0 to 10
|
# Create random genes from 0 to 10
|
||||||
ga.gene_impl = lambda: random.randint(0, 10)
|
ga.gene_impl = lambda: random.randint(0, 10)
|
||||||
@ -18,4 +18,4 @@ ga.target_fitness_type = 'min'
|
|||||||
|
|
||||||
ga.evolve()
|
ga.evolve()
|
||||||
|
|
||||||
ga.graph_scatter()
|
ga.graph.scatter(ga)
|
||||||
|
|||||||
Reference in New Issue
Block a user