Cleaned code and added functions to graph
This commit is contained in:
@ -4,7 +4,8 @@ import matplotlib.pyplot as plt
|
||||
class Matplotlib_Graph:
|
||||
"""Prebuilt graphing functions to make visual represention of fitness data."""
|
||||
|
||||
type_of_plot_dict = {
|
||||
# Common graphing functions
|
||||
type_of_graph_dict = {
|
||||
'line' : plt.plot,
|
||||
'scatter' : plt.scatter,
|
||||
'bar' : plt.bar
|
||||
@ -12,7 +13,10 @@ class Matplotlib_Graph:
|
||||
|
||||
def __init__(self, database):
|
||||
self.database = database
|
||||
self.type_of_plot = 'line'
|
||||
self.type_of_graph = 'line'
|
||||
self.x = None
|
||||
self.y = None
|
||||
self.yscale = "linear"
|
||||
|
||||
|
||||
def generation_total_fitness(self):
|
||||
@ -22,12 +26,16 @@ class Matplotlib_Graph:
|
||||
generations = self.database.get_total_generations()
|
||||
|
||||
# Create the generations list - [0,1,2,etc]
|
||||
X = list(range(0, generations))
|
||||
self.x = list(range(0, generations))
|
||||
|
||||
# Query for Y data
|
||||
Y = self.database.get_generation_total_fitness()
|
||||
self.y = self.database.get_generation_total_fitness()
|
||||
|
||||
self.type_of_plot(X, Y)
|
||||
if self.yscale == "log":
|
||||
# If using log then the values have to be positive numbers
|
||||
self.y = [abs(ele) for ele in self.y]
|
||||
|
||||
self.type_of_graph(self.x, self.y)
|
||||
plt.xlabel('Generation')
|
||||
plt.ylabel('Generation Total Fitness')
|
||||
plt.title('Relationship Between Generations and Generation Total Fitness')
|
||||
@ -40,12 +48,16 @@ class Matplotlib_Graph:
|
||||
generations = self.database.get_total_generations()
|
||||
|
||||
# Create the generations list - [0,1,2,etc]
|
||||
X = list(range(0, generations))
|
||||
self.x = list(range(0, generations))
|
||||
|
||||
# Query for Y data
|
||||
Y = self.database.get_highest_chromosome()
|
||||
self.y = self.database.get_highest_chromosome()
|
||||
|
||||
self.type_of_plot(X, Y)
|
||||
if self.yscale == "log":
|
||||
# If using log then the values have to be positive numbers
|
||||
self.y = [abs(ele) for ele in self.y]
|
||||
|
||||
self.type_of_graph(self.x, self.y)
|
||||
plt.xlabel('Generation')
|
||||
plt.ylabel('Highest Fitness')
|
||||
plt.title('Relationship Between Generations and Highest Fitness')
|
||||
@ -58,12 +70,16 @@ class Matplotlib_Graph:
|
||||
generations = self.database.get_total_generations()
|
||||
|
||||
# Create the generations list - [0,1,2,etc]
|
||||
X = list(range(0, generations))
|
||||
self.x = list(range(0, generations))
|
||||
|
||||
# Query for Y data
|
||||
Y = self.database.get_lowest_chromosome()
|
||||
self.y = self.database.get_lowest_chromosome()
|
||||
|
||||
self.type_of_plot(X, Y)
|
||||
if self.yscale == "log":
|
||||
# If using log then the values have to be positive numbers
|
||||
self.y = [abs(ele) for ele in self.y]
|
||||
|
||||
self.type_of_graph(self.x, self.y)
|
||||
plt.xlabel('Generation')
|
||||
plt.ylabel('Lowest Fitness')
|
||||
plt.title('Relationship Between Generations and Lowest Fitness')
|
||||
@ -71,13 +87,13 @@ class Matplotlib_Graph:
|
||||
|
||||
# Getter and setters
|
||||
@property
|
||||
def type_of_plot(self):
|
||||
return self._type_of_plot
|
||||
def type_of_graph(self):
|
||||
return self._type_of_graph
|
||||
|
||||
|
||||
@type_of_plot.setter
|
||||
def type_of_plot(self, _type_of_plot):
|
||||
if _type_of_plot in self.type_of_plot_dict.keys():
|
||||
self._type_of_plot = self.type_of_plot_dict[_type_of_plot]
|
||||
@type_of_graph.setter
|
||||
def type_of_graph(self, value_input):
|
||||
if value_input in self.type_of_graph_dict.keys():
|
||||
self._type_of_graph = self.type_of_graph_dict[value_input]
|
||||
else:
|
||||
self._type_of_plot = _type_of_plot
|
||||
self._type_of_plot = value_input
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
import EasyGA
|
||||
import random
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Create the Genetic algorithm
|
||||
# Create the genetic algorithm
|
||||
ga = EasyGA.GA()
|
||||
|
||||
# Create 25 chromosomes each with 10 genes and 200 generations
|
||||
@ -10,13 +9,16 @@ ga.population_size = 100
|
||||
ga.chromosome_length = 10
|
||||
ga.generation_goal = 150
|
||||
|
||||
# Evolve the genetic algorithm
|
||||
ga.evolve()
|
||||
|
||||
# Print generation and population
|
||||
ga.print_generation()
|
||||
ga.print_population()
|
||||
|
||||
# Plot the data from the genetic algorithm
|
||||
plt.figure(figsize = [6, 6])
|
||||
ga.graph.highest_value_chromosome() # Change this so it doesn't make its own figure or show
|
||||
plt.xlabel('days passed') # override the xlabel
|
||||
plt.ylabel('products sold that day') # override the ylabel
|
||||
plt.title('Efficiency over time') # override the title
|
||||
plt.xlabel('My datas generations') # override the xlabel
|
||||
plt.ylabel('How well the fitness is') # override the ylabel
|
||||
plt.title('My GA fitness x generations') # override the title
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user