forked from ahmedfgad/GeneticAlgorithmPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
386 lines (335 loc) · 20.1 KB
/
plot.py
File metadata and controls
386 lines (335 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""
The pygad.visualize.plot module has methods to create plots.
"""
import numpy
import matplotlib.pyplot
import pygad
class Plot:
def __init__():
pass
def plot_fitness(self,
title="PyGAD - Generation vs. Fitness",
xlabel="Generation",
ylabel="Fitness",
linewidth=3,
font_size=14,
plot_type="plot",
color="#64f20c",
label=None,
save_dir=None):
"""
Creates, shows, and returns a figure 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.
Accepts the following:
title: Figure title.
xlabel: Label on the X-axis.
ylabel: Label on the Y-axis.
linewidth: Line width of the plot. Defaults to 3.
font_size: Font size for the labels and title. Defaults to 14. Can be a list/tuple/numpy.ndarray if the problem is multi-objective optimization.
plot_type: Type of the plot which can be either "plot" (default), "scatter", or "bar".
color: Color of the plot which defaults to "#64f20c". Can be a list/tuple/numpy.ndarray if the problem is multi-objective optimization.
label: The label used for the legend in the figures of multi-objective problems. It is not used for single-objective problems.
save_dir: Directory to save the figure.
Returns the figure.
"""
if self.generations_completed < 1:
self.logger.error("The plot_fitness() (i.e. plot_result()) method can only be called after completing at least 1 generation but ({self.generations_completed}) is completed.")
raise RuntimeError("The plot_fitness() (i.e. plot_result()) method can only be called after completing at least 1 generation but ({self.generations_completed}) is completed.")
fig = matplotlib.pyplot.figure()
if type(self.best_solutions_fitness[0]) in [list, tuple, numpy.ndarray] and len(self.best_solutions_fitness[0]) > 1:
# Multi-objective optimization problem.
if type(linewidth) in pygad.GA.supported_int_float_types:
linewidth = [linewidth]
linewidth.extend([linewidth[0]]*len(self.best_solutions_fitness[0]))
elif type(linewidth) in [list, tuple, numpy.ndarray]:
pass
if type(color) is str:
color = [color]
color.extend([None]*len(self.best_solutions_fitness[0]))
elif type(color) in [list, tuple, numpy.ndarray]:
pass
if label is None:
label = [None]*len(self.best_solutions_fitness[0])
# Loop through each objective to plot its fitness.
for objective_idx in range(len(self.best_solutions_fitness[0])):
# Return the color, line width, and label of the current plot.
current_color = color[objective_idx]
current_linewidth = linewidth[objective_idx]
current_label = label[objective_idx]
# Return the fitness values for the current objective function across all best solutions acorss all generations.
fitness = numpy.array(self.best_solutions_fitness)[:, objective_idx]
if plot_type == "plot":
matplotlib.pyplot.plot(fitness,
linewidth=current_linewidth,
color=current_color,
label=current_label)
elif plot_type == "scatter":
matplotlib.pyplot.scatter(range(len(fitness)),
fitness,
linewidth=current_linewidth,
color=current_color,
label=current_label)
elif plot_type == "bar":
matplotlib.pyplot.bar(range(len(fitness)),
fitness,
linewidth=current_linewidth,
color=current_color,
label=current_label)
else:
# Single-objective optimization problem.
if plot_type == "plot":
matplotlib.pyplot.plot(self.best_solutions_fitness,
linewidth=linewidth,
color=color)
elif plot_type == "scatter":
matplotlib.pyplot.scatter(range(len(self.best_solutions_fitness)),
self.best_solutions_fitness,
linewidth=linewidth,
color=color)
elif plot_type == "bar":
matplotlib.pyplot.bar(range(len(self.best_solutions_fitness)),
self.best_solutions_fitness,
linewidth=linewidth,
color=color)
matplotlib.pyplot.title(title, fontsize=font_size)
matplotlib.pyplot.xlabel(xlabel, fontsize=font_size)
matplotlib.pyplot.ylabel(ylabel, fontsize=font_size)
# Create a legend out of the labels.
matplotlib.pyplot.legend()
if not save_dir is None:
matplotlib.pyplot.savefig(fname=save_dir,
bbox_inches='tight')
matplotlib.pyplot.show()
return fig
def plot_new_solution_rate(self,
title="PyGAD - Generation vs. New Solution Rate",
xlabel="Generation",
ylabel="New Solution Rate",
linewidth=3,
font_size=14,
plot_type="plot",
color="#64f20c",
save_dir=None):
"""
Creates, shows, and returns a figure that summarizes the rate of exploring new solutions. This method works only when save_solutions=True in the constructor of the pygad.GA class.
Accepts the following:
title: Figure title.
xlabel: Label on the X-axis.
ylabel: Label on the Y-axis.
linewidth: Line width of the plot. Defaults to 3.
font_size: Font size for the labels and title. Defaults to 14.
plot_type: Type of the plot which can be either "plot" (default), "scatter", or "bar".
color: Color of the plot which defaults to "#64f20c".
save_dir: Directory to save the figure.
Returns the figure.
"""
if self.generations_completed < 1:
self.logger.error("The plot_new_solution_rate() method can only be called after completing at least 1 generation but ({self.generations_completed}) is completed.")
raise RuntimeError("The plot_new_solution_rate() method can only be called after completing at least 1 generation but ({self.generations_completed}) is completed.")
if self.save_solutions == False:
self.logger.error("The plot_new_solution_rate() method works only when save_solutions=True in the constructor of the pygad.GA class.")
raise RuntimeError("The plot_new_solution_rate() method works only when save_solutions=True in the constructor of the pygad.GA class.")
unique_solutions = set()
num_unique_solutions_per_generation = []
for generation_idx in range(self.generations_completed):
len_before = len(unique_solutions)
start = generation_idx * self.sol_per_pop
end = start + self.sol_per_pop
for sol in self.solutions[start:end]:
unique_solutions.add(tuple(sol))
len_after = len(unique_solutions)
generation_num_unique_solutions = len_after - len_before
num_unique_solutions_per_generation.append(generation_num_unique_solutions)
fig = matplotlib.pyplot.figure()
if plot_type == "plot":
matplotlib.pyplot.plot(num_unique_solutions_per_generation, linewidth=linewidth, color=color)
elif plot_type == "scatter":
matplotlib.pyplot.scatter(range(self.generations_completed), num_unique_solutions_per_generation, linewidth=linewidth, color=color)
elif plot_type == "bar":
matplotlib.pyplot.bar(range(self.generations_completed), num_unique_solutions_per_generation, linewidth=linewidth, color=color)
matplotlib.pyplot.title(title, fontsize=font_size)
matplotlib.pyplot.xlabel(xlabel, fontsize=font_size)
matplotlib.pyplot.ylabel(ylabel, fontsize=font_size)
if not save_dir is None:
matplotlib.pyplot.savefig(fname=save_dir,
bbox_inches='tight')
matplotlib.pyplot.show()
return fig
def plot_genes(self,
title="PyGAD - Gene",
xlabel="Gene",
ylabel="Value",
linewidth=3,
font_size=14,
plot_type="plot",
graph_type="plot",
fill_color="#64f20c",
color="black",
solutions="all",
save_dir=None):
"""
Creates, shows, and returns a figure with number of subplots equal to the number of genes. Each subplot shows the gene value for each generation.
This method works only when save_solutions=True in the constructor of the pygad.GA class.
It also works only after completing at least 1 generation. If no generation is completed, an exception is raised.
Accepts the following:
title: Figure title.
xlabel: Label on the X-axis.
ylabel: Label on the Y-axis.
linewidth: Line width of the plot. Defaults to 3.
font_size: Font size for the labels and title. Defaults to 14.
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 "#64f20c". This has no effect if graph_type="plot".
color: Color of the plot which defaults to "black".
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.
Returns the figure.
"""
if self.generations_completed < 1:
self.logger.error("The plot_genes() method can only be called after completing at least 1 generation but ({self.generations_completed}) is completed.")
raise RuntimeError("The plot_genes() method can only be called after completing at least 1 generation but ({self.generations_completed}) is completed.")
if type(solutions) is str:
if solutions == 'all':
if self.save_solutions:
solutions_to_plot = numpy.array(self.solutions)
else:
self.logger.error("The plot_genes() method with solutions='all' can only be called if 'save_solutions=True' in the pygad.GA class constructor.")
raise RuntimeError("The plot_genes() method with solutions='all' can only be called if 'save_solutions=True' in the pygad.GA class constructor.")
elif solutions == 'best':
if self.save_best_solutions:
solutions_to_plot = self.best_solutions
else:
self.logger.error("The plot_genes() method with solutions='best' can only be called if 'save_best_solutions=True' in the pygad.GA class constructor.")
raise RuntimeError("The plot_genes() method with solutions='best' can only be called if 'save_best_solutions=True' in the pygad.GA class constructor.")
else:
self.logger.error("The solutions parameter can be either 'all' or 'best' but {solutions} found.")
raise RuntimeError("The solutions parameter can be either 'all' or 'best' but {solutions} found.")
else:
self.logger.error("The solutions parameter must be a string but {solutions_type} found.".format(solutions_type=type(solutions)))
raise RuntimeError("The solutions parameter must be a string but {solutions_type} found.".format(solutions_type=type(solutions)))
if graph_type == "plot":
# num_rows will be always be >= 1
# num_cols can only be 0 if num_genes=1
num_rows = int(numpy.ceil(self.num_genes/5.0))
num_cols = int(numpy.ceil(self.num_genes/num_rows))
if num_cols == 0:
figsize = (10, 8)
# There is only a single gene
fig, ax = matplotlib.pyplot.subplots(num_rows, figsize=figsize)
if plot_type == "plot":
ax.plot(solutions_to_plot[:, 0], linewidth=linewidth, color=fill_color)
elif plot_type == "scatter":
ax.scatter(range(self.generations_completed + 1), solutions_to_plot[:, 0], linewidth=linewidth, color=fill_color)
elif plot_type == "bar":
ax.bar(range(self.generations_completed + 1), solutions_to_plot[:, 0], linewidth=linewidth, color=fill_color)
ax.set_xlabel(0, fontsize=font_size)
else:
fig, axs = matplotlib.pyplot.subplots(num_rows, num_cols)
if num_cols == 1 and num_rows == 1:
fig.set_figwidth(5 * num_cols)
fig.set_figheight(4)
axs.plot(solutions_to_plot[:, 0], linewidth=linewidth, color=fill_color)
axs.set_xlabel("Gene " + str(0), fontsize=font_size)
elif num_cols == 1 or num_rows == 1:
fig.set_figwidth(5 * num_cols)
fig.set_figheight(4)
for gene_idx in range(len(axs)):
if plot_type == "plot":
axs[gene_idx].plot(solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color)
elif plot_type == "scatter":
axs[gene_idx].scatter(range(solutions_to_plot.shape[0]), solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color)
elif plot_type == "bar":
axs[gene_idx].bar(range(solutions_to_plot.shape[0]), solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color)
axs[gene_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size)
else:
gene_idx = 0
fig.set_figwidth(25)
fig.set_figheight(4*num_rows)
for row_idx in range(num_rows):
for col_idx in range(num_cols):
if gene_idx >= self.num_genes:
# axs[row_idx, col_idx].remove()
break
if plot_type == "plot":
axs[row_idx, col_idx].plot(solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color)
elif plot_type == "scatter":
axs[row_idx, col_idx].scatter(range(solutions_to_plot.shape[0]), solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color)
elif plot_type == "bar":
axs[row_idx, col_idx].bar(range(solutions_to_plot.shape[0]), solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color)
axs[row_idx, col_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size)
gene_idx += 1
fig.suptitle(title, fontsize=font_size, y=1.001)
matplotlib.pyplot.tight_layout()
elif graph_type == "boxplot":
fig = matplotlib.pyplot.figure(1, figsize=(0.7*self.num_genes, 6))
# Create an axes instance
ax = fig.add_subplot(111)
boxeplots = ax.boxplot(solutions_to_plot,
labels=range(self.num_genes),
patch_artist=True)
# adding horizontal grid lines
ax.yaxis.grid(True)
for box in boxeplots['boxes']:
# change outline color
box.set(color='black', linewidth=linewidth)
# change fill color https://color.adobe.com/create/color-wheel
box.set_facecolor(fill_color)
for whisker in boxeplots['whiskers']:
whisker.set(color=color, linewidth=linewidth)
for median in boxeplots['medians']:
median.set(color=color, linewidth=linewidth)
for cap in boxeplots['caps']:
cap.set(color=color, linewidth=linewidth)
matplotlib.pyplot.title(title, fontsize=font_size)
matplotlib.pyplot.xlabel(xlabel, fontsize=font_size)
matplotlib.pyplot.ylabel(ylabel, fontsize=font_size)
matplotlib.pyplot.tight_layout()
elif graph_type == "histogram":
# num_rows will always be >= 1
# num_cols can only be 0 if num_genes=1
num_rows = int(numpy.ceil(self.num_genes/5.0))
num_cols = int(numpy.ceil(self.num_genes/num_rows))
if num_cols == 0:
figsize = (10, 8)
# There is only a single gene
fig, ax = matplotlib.pyplot.subplots(num_rows,
figsize=figsize)
ax.hist(solutions_to_plot[:, 0], color=fill_color)
ax.set_xlabel(0, fontsize=font_size)
else:
fig, axs = matplotlib.pyplot.subplots(num_rows, num_cols)
if num_cols == 1 and num_rows == 1:
fig.set_figwidth(4 * num_cols)
fig.set_figheight(3)
axs.hist(solutions_to_plot[:, 0],
color=fill_color,
rwidth=0.95)
axs.set_xlabel("Gene " + str(0), fontsize=font_size)
elif num_cols == 1 or num_rows == 1:
fig.set_figwidth(4 * num_cols)
fig.set_figheight(3)
for gene_idx in range(len(axs)):
axs[gene_idx].hist(solutions_to_plot[:, gene_idx],
color=fill_color,
rwidth=0.95)
axs[gene_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size)
else:
gene_idx = 0
fig.set_figwidth(20)
fig.set_figheight(3*num_rows)
for row_idx in range(num_rows):
for col_idx in range(num_cols):
if gene_idx >= self.num_genes:
# axs[row_idx, col_idx].remove()
break
axs[row_idx, col_idx].hist(solutions_to_plot[:, gene_idx],
color=fill_color,
rwidth=0.95)
axs[row_idx, col_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size)
gene_idx += 1
fig.suptitle(title, fontsize=font_size, y=1.001)
matplotlib.pyplot.tight_layout()
if not save_dir is None:
matplotlib.pyplot.savefig(fname=save_dir,
bbox_inches='tight')
matplotlib.pyplot.show()
return fig