Added average plotting functionality and subplots.

This commit is contained in:
danielwilczak
2021-05-04 20:07:33 -04:00
parent af20ab68d9
commit f54c188136
2 changed files with 43 additions and 7 deletions

View File

@ -1,5 +1,7 @@
# Graphing package # Graphing package
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np
class Matplotlib_Graph: class Matplotlib_Graph:
"""Prebuilt graphing functions to make visual """Prebuilt graphing functions to make visual
@ -21,6 +23,22 @@ class Matplotlib_Graph:
self.yscale = "linear" self.yscale = "linear"
self.legend = False self.legend = False
def average_config_id(self,function):
"""Graph average line of all config_id's from data stored
in the database."""
# Get all the config's
config_ids = self.database.get_all_config_id()
stored_list = []
# Store each list so it can be averaged later
for config_id in config_ids:
stored_list.append(function(config_id))
y = np.average(stored_list, axis=0)
x = self.database.get_each_generation_number(config_id)
self.type_of_graph(x, y)
def all_config_id(self,function): def all_config_id(self,function):
"""Graph each config_id's data stored in the database """Graph each config_id's data stored in the database
@ -29,7 +47,7 @@ class Matplotlib_Graph:
config_ids = self.database.get_all_config_id() config_ids = self.database.get_all_config_id()
# Turn on the legend # Turn on the legend
self.legend = True #self.legend = True
# Get the x and y data for each config_id # Get the x and y data for each config_id
for config_id in config_ids: for config_id in config_ids:
@ -47,6 +65,9 @@ class Matplotlib_Graph:
if config_id == "all": if config_id == "all":
# If the user want to plot all the config_id's # If the user want to plot all the config_id's
self.all_config_id(self.database.get_generation_total_fitness) self.all_config_id(self.database.get_generation_total_fitness)
elif config_id == "average":
# if the user wants an average plot of all config_id's
self.average_config_id(self.database.get_generation_total_fitness)
else: else:
# Query the X data # Query the X data
generations = self.database.get_total_generations(config_id) generations = self.database.get_total_generations(config_id)
@ -69,6 +90,9 @@ class Matplotlib_Graph:
if config_id == "all": if config_id == "all":
# If the user want to plot all the config_id's # If the user want to plot all the config_id's
self.all_config_id(self.database.get_highest_chromosome) self.all_config_id(self.database.get_highest_chromosome)
elif config_id == "average":
# if the user wants an average plot of all config_id's
self.average_config_id(self.database.get_highest_chromosome)
else: else:
# Query the X data # Query the X data
generations = self.database.get_total_generations(config_id) generations = self.database.get_total_generations(config_id)
@ -90,6 +114,9 @@ class Matplotlib_Graph:
if config_id == "all": if config_id == "all":
# If the user want to plot all the config_id's # If the user want to plot all the config_id's
self.all_config_id(self.database.get_lowest_chromosome) self.all_config_id(self.database.get_lowest_chromosome)
elif config_id == "average":
# if the user wants an average plot of all config_id's
self.average_config_id(self.database.get_lowest_chromosome)
else: else:
# Query the X data # Query the X data
generations = self.database.get_total_generations(config_id) generations = self.database.get_total_generations(config_id)
@ -99,6 +126,7 @@ class Matplotlib_Graph:
self.y = self.database.get_lowest_chromosome(config_id) self.y = self.database.get_lowest_chromosome(config_id)
self.type_of_graph(self.x, self.y) self.type_of_graph(self.x, self.y)
plt.yscale(self.yscale) plt.yscale(self.yscale)
plt.xlabel('Generation') plt.xlabel('Generation')
plt.ylabel('Lowest Fitness') plt.ylabel('Lowest Fitness')

View File

@ -1,10 +1,18 @@
import matplotlib.pyplot as plt
import EasyGA import EasyGA
#Create the Genetic Algorithm for _ in range(2):
# Create a new genetic algorithm each.
ga = EasyGA.GA() ga = EasyGA.GA()
ga.evolve() ga.evolve()
#Print your default genetic algorithm
ga.print_generation()
ga.print_population() ga.print_population()
# Graph the average of the two runs
plt.subplot(1, 2, 1)
ga.graph.highest_value_chromosome("average")
plt.subplot(1, 2, 1)
ga.graph.highest_value_chromosome("all")
ga.graph.show()