Skip to content

Commit 231ea9d

Browse files
author
JimChen
committed
fix some code format
1 parent 80cbce4 commit 231ea9d

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

pygad.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,18 @@ def __init__(self,
339339
shape=(initial_population.shape[0], initial_population.shape[1]), dtype=object)
340340
for gene_idx in range(initial_population.shape[1]):
341341
if self.gene_type[gene_idx][1] is None:
342-
self.initial_population[:, gene_idx] = numpy.asarray(initial_population[:, gene_idx],
343-
dtype=self.gene_type[gene_idx][0])
342+
self.initial_population[:, gene_idx] = numpy.asarray(
343+
initial_population[:, gene_idx],
344+
dtype=self.gene_type[gene_idx][0]
345+
)
344346
else:
345-
self.initial_population[:, gene_idx] = numpy.round(numpy.asarray(initial_population[:, gene_idx],
346-
dtype=self.gene_type[gene_idx][0]),
347-
self.gene_type[gene_idx][1])
347+
self.initial_population[:, gene_idx] = numpy.round(
348+
numpy.asarray(
349+
initial_population[:, gene_idx],
350+
dtype=self.gene_type[gene_idx][0]
351+
),
352+
self.gene_type[gene_idx][1]
353+
)
348354

349355
# A NumPy array holding the initial population.
350356
self.population = self.initial_population.copy()
@@ -525,7 +531,7 @@ def __init__(self,
525531
mutation_percent_genes = 10
526532
# Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated.
527533
mutation_num_genes = numpy.uint32(
528-
(mutation_percent_genes*self.num_genes)/100)
534+
(mutation_percent_genes * self.num_genes)/100)
529535
# Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1.
530536
if mutation_num_genes == 0:
531537
if self.mutation_probability is None:
@@ -546,7 +552,7 @@ def __init__(self,
546552

547553
# Based on the mutation percentage in the 'mutation_percent_genes' parameter, the number of genes to mutate is calculated.
548554
mutation_num_genes = numpy.uint32(
549-
(mutation_percent_genes*self.num_genes) / 100)
555+
(mutation_percent_genes * self.num_genes) / 100)
550556
# Based on the mutation percentage of genes, if the number of selected genes for mutation is less than the least possible value which is 1, then the number will be set to 1.
551557
if mutation_num_genes == 0:
552558
if self.mutation_probability is None:
@@ -1455,7 +1461,7 @@ def cal_pop_fitness(self):
14551461
fitness = self.solutions_fitness[self.solutions.index(
14561462
list(sol))]
14571463
# If the solutions are not saved (i.e. `save_solutions=False`), check if this solution is a parent from the previous generation and its fitness value is already calculated. If so, use the fitness value instead of calling the fitness function.
1458-
if not (self.last_generation_parents is None) and len(numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0] > 0):
1464+
if (self.last_generation_parents is not None) and len(numpy.where(numpy.all(self.last_generation_parents == sol, axis=1))[0] > 0):
14591465
# Index of the parent in the parents array (self.last_generation_parents). This is not its index within the population.
14601466
parent_idx = numpy.where(
14611467
numpy.all(self.last_generation_parents == sol, axis=1))[0][0]
@@ -1982,7 +1988,7 @@ def uniform_crossover(self, parents, offspring_size):
19821988
offspring = self.generate_new_genes(size=offspring_size)
19831989

19841990
for k in range(offspring_size[0]):
1985-
if not (self.crossover_probability is None):
1991+
if self.crossover_probability is not None:
19861992
probs = numpy.random.random(size=parents.shape[0])
19871993
indices = numpy.where(probs <= self.crossover_probability)[0]
19881994

@@ -3287,11 +3293,25 @@ def plot_fitness(self,
32873293

32883294
fig = matplotlib.pyplot.figure()
32893295
if plot_type == "plot":
3290-
matplotlib.pyplot.plot(self.best_solutions_fitness, linewidth=linewidth, color=color)
3296+
matplotlib.pyplot.plot(
3297+
self.best_solutions_fitness,
3298+
linewidth=linewidth,
3299+
color=color
3300+
)
32913301
elif plot_type == "scatter":
3292-
matplotlib.pyplot.scatter(range(self.generations_completed + 1), self.best_solutions_fitness, linewidth=linewidth, color=color)
3302+
matplotlib.pyplot.scatter(
3303+
range(self.generations_completed + 1),
3304+
self.best_solutions_fitness,
3305+
linewidth=linewidth,
3306+
color=color
3307+
)
32933308
elif plot_type == "bar":
3294-
matplotlib.pyplot.bar(range(self.generations_completed + 1), self.best_solutions_fitness, linewidth=linewidth, color=color)
3309+
matplotlib.pyplot.bar(
3310+
range(self.generations_completed + 1),
3311+
self.best_solutions_fitness,
3312+
linewidth=linewidth,
3313+
color=color
3314+
)
32953315
matplotlib.pyplot.title(title, fontsize=font_size)
32963316
matplotlib.pyplot.xlabel(xlabel, fontsize=font_size)
32973317
matplotlib.pyplot.ylabel(ylabel, fontsize=font_size)
@@ -3364,8 +3384,10 @@ def plot_new_solution_rate(self,
33643384
matplotlib.pyplot.ylabel(ylabel, fontsize=font_size)
33653385

33663386
if save_dir is not None:
3367-
matplotlib.pyplot.savefig(fname=save_dir,
3368-
bbox_inches='tight')
3387+
matplotlib.pyplot.savefig(
3388+
fname=save_dir,
3389+
bbox_inches='tight'
3390+
)
33693391
matplotlib.pyplot.show()
33703392

33713393
return fig
@@ -3474,7 +3496,11 @@ def plot_genes(self,
34743496
# axs[row_idx, col_idx].remove()
34753497
break
34763498
if plot_type == "plot":
3477-
axs[row_idx, col_idx].plot(solutions_to_plot[:, gene_idx], linewidth=linewidth, color=fill_color)
3499+
axs[row_idx, col_idx].plot(
3500+
solutions_to_plot[:, gene_idx],
3501+
linewidth=linewidth,
3502+
color=fill_color
3503+
)
34783504
elif plot_type == "scatter":
34793505
axs[row_idx, col_idx].scatter(
34803506
range(solutions_to_plot.shape[0]),
@@ -3483,8 +3509,12 @@ def plot_genes(self,
34833509
color=fill_color
34843510
)
34853511
elif plot_type == "bar":
3486-
axs[row_idx, col_idx].bar(range(solutions_to_plot.shape[0]), solutions_to_plot[:,
3487-
gene_idx], linewidth=linewidth, color=fill_color)
3512+
axs[row_idx, col_idx].bar(
3513+
range(solutions_to_plot.shape[0]),
3514+
solutions_to_plot[:, gene_idx],
3515+
linewidth=linewidth,
3516+
color=fill_color
3517+
)
34883518
axs[row_idx, col_idx].set_xlabel("Gene " + str(gene_idx), fontsize=font_size)
34893519
gene_idx += 1
34903520

0 commit comments

Comments
 (0)