Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pygad.py
Original file line number Diff line number Diff line change
Expand Up @@ -2530,7 +2530,7 @@ def best_solution(self, pop_fitness=None):

return best_solution, best_solution_fitness, best_match_idx

def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation", ylabel="Fitness", linewidth=3):
def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation", ylabel="Fitness", linewidth=3, save_dir=None):

"""
Creates and shows a plot that summarizes how the fitness value evolved by generation. Can only be called after completing at least 1 generation. If no generation is completed, an exception is raised.
Expand All @@ -2540,6 +2540,7 @@ def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation"
xlabel: Label on the X-axis.
ylabel: Label on the Y-axis.
linewidth: Line width of the plot.
save_dir: Directory to save the figure.

Returns the figure.
"""
Expand All @@ -2555,7 +2556,12 @@ def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation"
matplotlib.pyplot.title(title)
matplotlib.pyplot.xlabel(xlabel)
matplotlib.pyplot.ylabel(ylabel)

if not save_dir is None:
matplotlib.pyplot.savefig(fname=save_dir,
bbox_inches='tight')
matplotlib.pyplot.show()

return fig

def save(self, filename):
Expand Down Expand Up @@ -2583,4 +2589,4 @@ def load(filename):
raise FileNotFoundError("Error reading the file {filename}. Please check your inputs.".format(filename=filename))
except:
raise BaseException("Error loading the file. Please check if the file exists.")
return ga_in
return ga_in