pygad.visualize
Module¶
This section of the PyGAD’s library documentation discusses the pygad.visualize module. It offers the methods for results visualization in PyGAD.
This section discusses the different options to visualize the results in PyGAD through these methods:
plot_fitness()
: Creates plots for the fitness.plot_genes()
: Creates plots for the genes.plot_new_solution_rate()
: Creates plots for the new solution rate.plot_pareto_front_curve()
: Creates plots for the pareto front for multi-objective problems.
In the following code, the save_solutions
flag is set to True
which means all solutions are saved in the solutions
attribute. The
code runs for only 10 generations.
import pygad
import numpy
equation_inputs = [4, -2, 3.5, 8, -2, 3.5, 8]
desired_output = 2671.1234
def fitness_func(ga_instance, solution, solution_idx):
output = numpy.sum(solution * equation_inputs)
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
return fitness
ga_instance = pygad.GA(num_generations=10,
sol_per_pop=10,
num_parents_mating=5,
num_genes=len(equation_inputs),
fitness_func=fitness_func,
gene_space=[range(1, 10), range(10, 20), range(15, 30), range(20, 40), range(25, 50), range(10, 30), range(20, 50)],
gene_type=int,
save_solutions=True)
ga_instance.run()
Let’s explore how to visualize the results by the above mentioned methods.
Fitness¶
plot_fitness()
¶
The plot_fitness()
method shows the fitness value for each
generation. It creates, shows, and returns a figure that summarizes how
the fitness value(s) evolve(s) by generation.
It works only after completing at least 1 generation. If no generation is completed (at least 1), an exception is raised.
This method accepts the following parameters:
title
: Title of the figure.xlabel
: X-axis label.ylabel
: Y-axis label.linewidth
: Line width of the plot. Defaults to3
.font_size
: Font size for the labels and title. Defaults to14
.plot_type
: Type of the plot which can be either"plot"
(default),"scatter"
, or"bar"
.color
: Color of the plot which defaults to the greenish color"#64f20c"
.label
: The label used for the legend in the figures of multi-objective problems. It is not used for single-objective problems. It defaults toNone
which means no labels used.save_dir
: Directory to save the figure.
plot_type="plot"
¶
The simplest way to call this method is as follows leaving the
plot_type
with its default value "plot"
to create a continuous
line connecting the fitness values across all generations:
ga_instance.plot_fitness()
# ga_instance.plot_fitness(plot_type="plot")
plot_type="scatter"
¶
The plot_type
can also be set to "scatter"
to create a scatter
graph with each individual fitness represented as a dot. The size of
these dots can be changed using the linewidth
parameter.
ga_instance.plot_fitness(plot_type="scatter")
plot_type="bar"
¶
The third value for the plot_type
parameter is "bar"
to create a
bar graph with each individual fitness represented as a bar.
ga_instance.plot_fitness(plot_type="bar")
New Solution Rate¶
plot_new_solution_rate()
¶
The plot_new_solution_rate()
method presents the number of new
solutions explored in each generation. This helps to figure out if the
genetic algorithm is able to find new solutions as an indication of more
possible evolution. If no new solutions are explored, this is an
indication that no further evolution is possible.
It works only after completing at least 1 generation. If no generation is completed (at least 1), an exception is raised.
The plot_new_solution_rate()
method accepts the same parameters as
in the plot_fitness()
method (it also have 3 possible values for
plot_type
parameter). Here are all the parameters it accepts:
title
: Title of the figure.xlabel
: X-axis label.ylabel
: Y-axis label.linewidth
: Line width of the plot. Defaults to3
.font_size
: Font size for the labels and title. Defaults to14
.plot_type
: Type of the plot which can be either"plot"
(default),"scatter"
, or"bar"
.color
: Color of the plot which defaults to"#3870FF"
.save_dir
: Directory to save the figure.
plot_type="plot"
¶
The default value for the plot_type
parameter is "plot"
.
ga_instance.plot_new_solution_rate()
# ga_instance.plot_new_solution_rate(plot_type="plot")
The next figure shows that, for example, generation 6 has the least
number of new solutions which is 4. The number of new solutions in the
first generation is always equal to the number of solutions in the
population (i.e. the value assigned to the sol_per_pop
parameter in
the constructor of the pygad.GA
class) which is 10 in this example.
plot_type="scatter"
¶
The previous graph can be represented as scattered points by setting
plot_type="scatter"
.
ga_instance.plot_new_solution_rate(plot_type="scatter")
plot_type="bar"
¶
By setting plot_type="scatter"
, each value is represented as a
vertical bar.
ga_instance.plot_new_solution_rate(plot_type="bar")
Genes¶
plot_genes()
¶
The plot_genes()
method is the third option to visualize the PyGAD
results. The plot_genes()
method creates, shows, and returns a
figure that describes each gene. It has different options to create the
figures which helps to:
Explore the gene value for each generation by creating a normal plot.
Create a histogram for each gene.
Create a boxplot.
It works only after completing at least 1 generation. If no generation is completed, an exception is raised. If no generation is completed (at least 1), an exception is raised.
This method accepts the following parameters:
title
: Title of the figure.xlabel
: X-axis label.ylabel
: Y-axis label.linewidth
: Line width of the plot. Defaults to3
.font_size
: Font size for the labels and title. Defaults to14
.plot_type
: Type of the plot which can be either"plot"
(default),"scatter"
, or"bar"
.graph_type
: Type of the graph which can be either"plot"
(default),"boxplot"
, or"histogram"
.fill_color
: Fill color of the graph which defaults to"#3870FF"
. This has no effect ifgraph_type="plot"
.color
: Color of the plot which defaults to"#3870FF"
.solutions
: Defaults to"all"
which means use all solutions. If"best"
then only the best solutions are used.save_dir
: Directory to save the figure.
This method has 3 control variables:
graph_type="plot"
: Can be"plot"
(default),"boxplot"
, or"histogram"
.plot_type="plot"
: Identical to theplot_type
parameter explored in theplot_fitness()
andplot_new_solution_rate()
methods.solutions="all"
: Can be"all"
(default) or"best"
.
These 3 parameters controls the style of the output figure.
The graph_type
parameter selects the type of the graph which helps
to explore the gene values as:
A normal plot.
A histogram.
A box and whisker plot.
The plot_type
parameter works only when the type of the graph is set
to "plot"
.
The solutions
parameter selects whether the genes come from all
solutions in the population or from just the best solutions.
An exception is raised if:
solutions="all"
whilesave_solutions=False
in the constructor of thepygad.GA
class. .solutions="best"
whilesave_best_solutions=False
in the constructor of thepygad.GA
class. .
graph_type="plot"
¶
When graph_type="plot"
, then the figure creates a normal graph where
the relationship between the gene values and the generation numbers is
represented as a continuous plot, scattered points, or bars.
plot_type="plot"
¶
Because the default value for both graph_type
and plot_type
is
"plot"
, then all of the lines below creates the same figure. This
figure is helpful to know whether a gene value lasts for more
generations as an indication of the best value for this gene. For
example, the value 16 for the gene with index 5 (at column 2 and row 2
of the next graph) lasted for 83 generations.
ga_instance.plot_genes()
ga_instance.plot_genes(graph_type="plot")
ga_instance.plot_genes(plot_type="plot")
ga_instance.plot_genes(graph_type="plot",
plot_type="plot")
As the default value for the solutions
parameter is "all"
, then
the following method calls generate the same plot.
ga_instance.plot_genes(solutions="all")
ga_instance.plot_genes(graph_type="plot",
solutions="all")
ga_instance.plot_genes(plot_type="plot",
solutions="all")
ga_instance.plot_genes(graph_type="plot",
plot_type="plot",
solutions="all")
plot_type="scatter"
¶
The following calls of the plot_genes()
method create the same
scatter plot.
ga_instance.plot_genes(plot_type="scatter")
ga_instance.plot_genes(graph_type="plot",
plot_type="scatter",
solutions='all')
plot_type="bar"
¶
ga_instance.plot_genes(plot_type="bar")
ga_instance.plot_genes(graph_type="plot",
plot_type="bar",
solutions='all')
graph_type="boxplot"
¶
By setting graph_type
to "boxplot"
, then a box and whisker graph
is created. Now, the plot_type
parameter has no effect.
The following 2 calls of the plot_genes()
method create the same
figure as the default value for the solutions
parameter is
"all"
.
ga_instance.plot_genes(graph_type="boxplot")
ga_instance.plot_genes(graph_type="boxplot",
solutions='all')
graph_type="histogram"
¶
For graph_type="boxplot"
, then a histogram is created for each gene.
Similar to graph_type="boxplot"
, the plot_type
parameter has no
effect.
The following 2 calls of the plot_genes()
method create the same
figure as the default value for the solutions
parameter is
"all"
.
ga_instance.plot_genes(graph_type="histogram")
ga_instance.plot_genes(graph_type="histogram",
solutions='all')
All the previous figures can be created for only the best solutions by
setting solutions="best"
.
Pareto Front¶
plot_pareto_front_curve()
¶
The plot_pareto_front_curve()
method creates the Pareto front curve
for multi-objective optimization problems. It creates, shows, and
returns a figure that shows the Pareto front curve and points
representing the fitness. It only works when 2 objectives are used.
It works only after completing at least 1 generation. If no generation is completed (at least 1), an exception is raised.
This method accepts the following parameters:
title
: Title of the figure.xlabel
: X-axis label.ylabel
: Y-axis label.linewidth
: Line width of the plot. Defaults to3
.font_size
: Font size for the labels and title. Defaults to14
.label
: The label used for the legend.color
: Color of the plot which defaults to the royal blue color#FF6347
.color_fitness
: Color of the fitness points which defaults to the tomato red color#4169E1
.grid
: EitherTrue
orFalse
to control the visibility of the grid.alpha
: The transparency of the pareto front curve.marker
: The marker of the fitness points.save_dir
: Directory to save the figure.
This is an example of calling the plot_pareto_front_curve()
method.
ga_instance.plot_pareto_front_curve()