Cleaned up graph code
This commit is contained in:
@ -12,30 +12,37 @@ class Matplotlib_Graph:
|
|||||||
|
|
||||||
def __init__(self, database):
|
def __init__(self, database):
|
||||||
self.database = database
|
self.database = database
|
||||||
|
# Type
|
||||||
self.type_of_plot = plt.plot
|
self.type_of_plot = plt.plot
|
||||||
|
# Size
|
||||||
self.size = [6,6]
|
self.size = [6,6]
|
||||||
|
# Labels
|
||||||
self.xlabel = None
|
self.xlabel = None
|
||||||
self.ylabel = None
|
self.ylabel = None
|
||||||
self.title = None
|
self.title = None
|
||||||
|
# Scale
|
||||||
self.yscale = "linear"
|
self.yscale = "linear"
|
||||||
|
# Data points
|
||||||
|
self.x = None
|
||||||
|
self.y = None
|
||||||
|
|
||||||
|
|
||||||
def plt_setup(self, X, Y, yscale, xlabel, ylabel, title, type_of_plot, size):
|
def plot(self):
|
||||||
"""Setup for plt"""
|
"""Plot all the graph attributes"""
|
||||||
|
|
||||||
if yscale == "log":
|
if self.yscale == "log":
|
||||||
# If using log then the values have to be positive numbers
|
# If using log then the values have to be positive numbers
|
||||||
Y = [abs(ele) for ele in Y]
|
self.y = [abs(ele) for ele in self.y]
|
||||||
|
|
||||||
# Setup data
|
# Setup data
|
||||||
plt.figure(figsize = size)
|
plt.figure(figsize = self.size)
|
||||||
plt.yscale(self.yscale)
|
plt.yscale(self.yscale)
|
||||||
type_of_plot(X, Y)
|
self.type_of_plot(self.x, self.y)
|
||||||
|
|
||||||
# labels
|
# labels
|
||||||
plt.xlabel(xlabel)
|
plt.xlabel(self.xlabel)
|
||||||
plt.ylabel(ylabel)
|
plt.ylabel(self.ylabel)
|
||||||
plt.title(title)
|
plt.title(self.title)
|
||||||
|
|
||||||
# Show the plot
|
# Show the plot
|
||||||
plt.show()
|
plt.show()
|
||||||
@ -48,12 +55,16 @@ class Matplotlib_Graph:
|
|||||||
generations = self.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))
|
self.x = list(range(0, generations))
|
||||||
|
|
||||||
# Query for Y data
|
# Query for Y data
|
||||||
Y = self.database.get_generation_total_fitness()
|
self.y = self.database.get_generation_total_fitness()
|
||||||
|
|
||||||
self.plt_setup(X, Y, self.yscale, 'Generation', 'Generation Total Fitness', 'Relationship Between Generations and Generation Total Fitness', self.type_of_plot, self.size)
|
self.xlabel = 'Generation'
|
||||||
|
self.ylabel = 'Generation Total Fitness'
|
||||||
|
self.title = 'Relationship Between Generations and Generation Total Fitness'
|
||||||
|
|
||||||
|
self.plot()
|
||||||
|
|
||||||
|
|
||||||
def highest_value_chromosome(self):
|
def highest_value_chromosome(self):
|
||||||
@ -63,13 +74,16 @@ class Matplotlib_Graph:
|
|||||||
generations = self.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))
|
self.x = list(range(0, generations))
|
||||||
|
|
||||||
# Query for Y data
|
# Query for Y data
|
||||||
Y = self.database.get_highest_chromosome()
|
self.y = self.database.get_highest_chromosome()
|
||||||
|
|
||||||
self.plt_setup(X, Y, self.yscale, 'Generation', 'Highest Fitness', 'Relationship Between Generations and Highest Fitness', self.type_of_plot, self.size)
|
self.xlabel = 'Generation'
|
||||||
|
self.ylabel = 'Highest Fitness'
|
||||||
|
self.title = 'Relationship Between Generations and Highest Fitness'
|
||||||
|
|
||||||
|
self.plot()
|
||||||
|
|
||||||
def lowest_value_chromosome(self):
|
def lowest_value_chromosome(self):
|
||||||
"""Generation by Min value Chromosome """
|
"""Generation by Min value Chromosome """
|
||||||
@ -78,19 +92,16 @@ class Matplotlib_Graph:
|
|||||||
generations = self.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))
|
self.x = list(range(0, generations))
|
||||||
|
|
||||||
print(X)
|
|
||||||
|
|
||||||
# Query for Y data
|
# Query for Y data
|
||||||
Y = self.database.get_lowest_chromosome()
|
self.y = self.database.get_lowest_chromosome()
|
||||||
|
|
||||||
if(self.yscale == "log"):
|
self.xlabel = 'Generation'
|
||||||
# If using log then the values have to be positive numbers
|
self.ylabel = 'Lowest Fitness'
|
||||||
Y = [abs(ele) for ele in Y]
|
self.title = 'Relationship Between Generations and Lowest Fitness'
|
||||||
|
|
||||||
self.plt_setup(X, Y, self.yscale, 'Generation', 'Lowest Fitness', 'Relationship Between Generations and Lowest Fitness', self.type_of_plot, self.size)
|
|
||||||
|
|
||||||
|
self.plot()
|
||||||
|
|
||||||
# Getter and setters
|
# Getter and setters
|
||||||
@property
|
@property
|
||||||
|
|||||||
@ -9,18 +9,6 @@ ga.population_size = 100
|
|||||||
ga.chromosome_length = 10
|
ga.chromosome_length = 10
|
||||||
ga.generation_goal = 150
|
ga.generation_goal = 150
|
||||||
|
|
||||||
# Create random genes from 0 to 10
|
|
||||||
ga.gene_impl = lambda: random.randint(0, 10)
|
|
||||||
|
|
||||||
# Minimize the sum of the genes
|
|
||||||
ga.fitness_function_impl = lambda chromosome: sum(gene.get_value() for gene in chromosome.get_gene_list())
|
|
||||||
ga.target_fitness_type = 'min'
|
|
||||||
|
|
||||||
ga.evolve()
|
ga.evolve()
|
||||||
|
|
||||||
ga.print_generation()
|
ga.graph.highest_value_chromosome()
|
||||||
ga.print_population()
|
|
||||||
|
|
||||||
ga.graph.type_of_plot = "line"
|
|
||||||
ga.graph.yscale = "log"
|
|
||||||
ga.graph.lowest_value_chromosome()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user