Skip to content

Commit cd7be47

Browse files
author
Ahmed Gad
committed
Format docstrings
1 parent 8a34063 commit cd7be47

15 files changed

+53
-62
lines changed

example.py

Lines changed: 0 additions & 33 deletions
This file was deleted.
-205 Bytes
Binary file not shown.
-78.6 KB
Binary file not shown.
-260 Bytes
Binary file not shown.
-15.7 KB
Binary file not shown.
-14.3 KB
Binary file not shown.

pygad/helper/misc.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
The pygad.helper.helper module has some generic helper methods.
2+
The pygad.helper.misc module has some generic helper methods.
33
"""
44

55
import numpy
@@ -14,8 +14,10 @@ def change_population_dtype_and_round(self,
1414
"""
1515
Change the data type of the population. It works with iterables (e.g. lists or NumPy arrays) of shape 2D.
1616
It does not handle single numeric values or 1D arrays.
17+
1718
It accepts:
1819
-population: The iterable to change its dtype.
20+
1921
It returns the iterable with the data type changed for all genes.
2022
"""
2123

@@ -60,9 +62,11 @@ def change_gene_dtype_and_round(self,
6062
gene_value):
6163
"""
6264
Change the data type and round a single gene value or a vector of values FOR THE SAME GENE. E.g., the input could be 6 or [6, 7, 8].
65+
6366
It accepts 2 parameters:
6467
-gene_index: The index of the target gene.
6568
-gene_value: The gene value.
69+
6670
If gene_value has a single value, then it returns a single number with the type changed and value rounded. If gene_value is a vector, then a vector is returned after changing the data type and rounding.
6771
"""
6872

@@ -103,11 +107,13 @@ def mutation_change_gene_dtype_and_round(self,
103107
mutation_by_replacement):
104108
"""
105109
Change the data type and round the random value used to apply mutation.
110+
106111
It accepts:
107112
-random_value: The random value to change its data type.
108113
-gene_index: The index of the target gene.
109114
-gene_value: The gene value before mutation. Only used if mutation_by_replacement=False and gene_type_single=False.
110115
-mutation_by_replacement: A flag indicating whether mutation by replacement is enabled or not. The reason is to make this helper method usable while generating the initial population. In this case, mutation_by_replacement does not matter and should be considered False.
116+
111117
It returns the new value after changing the data type and being rounded.
112118
"""
113119

@@ -129,10 +135,12 @@ def filter_gene_values_by_constraint(self,
129135

130136
"""
131137
Filter the random values generated for mutation based on whether they meet the gene constraint in the gene_constraint parameter.
138+
132139
It accepts:
133140
-values: The values to filter.
134141
-solution: The solution containing the target gene.
135142
-gene_idx: The index of the gene in the solution.
143+
136144
It returns None if no values satisfy the constraint. Otherwise, an array of values that satisfy the constraint is returned.
137145
"""
138146

@@ -166,8 +174,10 @@ def get_gene_dtype(self, gene_index):
166174

167175
"""
168176
Returns the data type of the gene by its index.
177+
169178
It accepts a single parameter:
170179
-gene_index: The index of the gene to get its data type. Only used if each gene has its own data type.
180+
171181
It returns the data type of the gene.
172182
"""
173183

@@ -181,8 +191,10 @@ def get_random_mutation_range(self, gene_index):
181191

182192
"""
183193
Returns the minimum and maximum values of the mutation range.
194+
184195
It accepts a single parameter:
185196
-gene_index: The index of the gene to get its range. Only used if the gene has a specific mutation range.
197+
186198
It returns the minimum and maximum values of the gene mutation range.
187199
"""
188200

@@ -199,8 +211,10 @@ def get_initial_population_range(self, gene_index):
199211

200212
"""
201213
Returns the minimum and maximum values of the initial population range.
214+
202215
It accepts a single parameter:
203216
-gene_index: The index of the gene to get its range. Only used if the gene has a specific range
217+
204218
It returns the minimum and maximum values of the gene initial population range.
205219
"""
206220

@@ -221,6 +235,7 @@ def generate_gene_value_from_space(self,
221235
sample_size=1):
222236
"""
223237
Generate/select one or more values for the gene from the gene space.
238+
224239
It accepts:
225240
-gene_idx: The index of the gene in the solution.
226241
-mutation_by_replacement: A flag indicating whether mutation by replacement is enabled or not. The reason is to make this helper method usable while generating the initial population. In this case, mutation_by_replacement does not matter and should be considered False.
@@ -508,7 +523,6 @@ def get_valid_gene_constraint_values(self,
508523
-None if no value found that satisfies the constraint.
509524
"""
510525

511-
print("AAAA", sample_size)
512526
# Either generate the values randomly or from the gene space.
513527
values = self.generate_gene_value(range_min=range_min,
514528
range_max=range_max,

pygad/helper/unique.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,16 @@ def solve_duplicate_genes_by_space(self,
8787
mutation_by_replacement,
8888
sample_size=100,
8989
build_initial_pop=False):
90-
90+
9191
"""
9292
Resolves duplicates in a solution by selecting new values for the duplicate genes from the gene space.
9393
9494
Args:
9595
solution (list): A solution containing genes, potentially with duplicate values.
9696
gene_type (type): The data type of the gene (e.g., int, float).
97-
sample_size (int): The maximum number of attempts to resolve duplicates by selecting values from the gene space.
97+
mutation_by_replacement (bool): Indicates if mutation is performed by replacement.
98+
sample_size (int, optional): The maximum number of attempts to resolve duplicates by selecting values from the gene space.
99+
build_initial_pop (bool, optional): Indicates if initial population should be built.
98100
99101
Returns:
100102
tuple:
@@ -247,7 +249,10 @@ def unique_float_gene_from_range(self,
247249
gene_index=gene_index)
248250
return selected_value
249251

250-
def select_unique_value(self, gene_values, solution, gene_index):
252+
def select_unique_value(self,
253+
gene_values,
254+
solution,
255+
gene_index):
251256

252257
"""
253258
Select a unique value (if possible) from a list of gene values.
@@ -293,7 +298,9 @@ def unique_genes_by_space(self,
293298
solution (list): A solution containing genes with duplicate values.
294299
gene_type (type): The data type of the all the genes (e.g., int, float).
295300
not_unique_indices (list): The indices of genes with duplicate values.
301+
mutation_by_replacement (bool): Indicates if mutation is performed by replacement.
296302
sample_size (int): The maximum number of attempts to resolve duplicates for each gene. Only works for floating-point numbers.
303+
build_initial_pop (bool, optional): Indicates if initial population should be built.
297304
298305
Returns:
299306
tuple:
@@ -338,7 +345,9 @@ def unique_gene_by_space(self,
338345
solution (list): A solution containing genes with duplicate values.
339346
gene_idx (int): The index of the gene that has a duplicate value.
340347
gene_type (type): The data type of the gene (e.g., int, float).
348+
mutation_by_replacement (bool): Indicates if mutation is performed by replacement.
341349
sample_size (int): The maximum number of attempts to resolve duplicates for each gene. Only works for floating-point numbers.
350+
build_initial_pop (bool, optional): Indicates if initial population should be built.
342351
343352
Returns:
344353
Any: A unique value for the gene, if one exists; otherwise, the original gene value.
@@ -387,10 +396,13 @@ def find_two_duplicates(self,
387396
"""
388397
Identifies the first occurrence of a duplicate gene in the solution.
389398
399+
Args:
400+
solution: The solution containing genes with duplicate values.
401+
gene_space_unpacked: A list of values from the gene space to choose the values that resolve duplicates.
402+
390403
Returns:
391-
tuple:
392-
int: The index of the first gene with a duplicate value.
393-
Any: The value of the duplicate gene.
404+
int: The index of the first gene with a duplicate value.
405+
Any: The value of the duplicate gene.
394406
"""
395407

396408
for gene in set(solution):
@@ -538,9 +550,6 @@ def solve_duplicates_deeply(self,
538550
539551
Args:
540552
solution (list): The current solution containing genes, potentially with duplicates.
541-
gene_idx1 (int): The index of the first gene involved in the duplication.
542-
gene_idx2 (int): The index of the second gene involved in the duplication.
543-
assist_gene_idx (int): The index of the third gene used to assist in resolving the duplication.
544553
545554
Returns:
546555
list or None: The updated solution with duplicates resolved, or `None` if the duplicates cannot be resolved.
@@ -565,10 +574,6 @@ def solve_duplicates_deeply(self,
565574
# This removes all the occurrences of this value.
566575
gene_other_values = [v for v in gene_other_values if v != duplicate_value]
567576

568-
# The remove() function only removes the first occurrence of the value.
569-
# Do not use it.
570-
# gene_other_values.remove(duplicate_value)
571-
572577
# Two conditions to solve the duplicates of the value D:
573578
# 1. From gene_other_values, select a value V such that it is available in the gene space of another gene X.
574579
# 2. Find an alternate value for the gene X that will not cause any duplicates.

pygad/pygad.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def __init__(self,
108108
gene_space: It accepts a list of all possible values of the gene. This list is used in the mutation step. Should be used only if the gene space is a set of discrete values. No need for the 2 parameters (random_mutation_min_val and random_mutation_max_val) if the parameter gene_space exists. Added in PyGAD 2.5.0. In PyGAD 2.11.0, the gene_space can be assigned a dict.
109109
110110
gene_constraint: It accepts a list of constraints for the genes. Each constraint is a Python function. Added in PyGAD 3.5.0.
111-
sample_size: To select a gene value that respects a constraint, this variable defines the size of the sample from which a value is selected. Useful if either allow_duplicate_genes or gene_constraint is used. Added in PyGAD 3.5.0.
111+
sample_size: To select a gene value that respects a constraint, this variable defines the size of the sample from which a value is selected randomly. Useful if either allow_duplicate_genes or gene_constraint is used. Added in PyGAD 3.5.0.
112112
113113
on_start: Accepts a function/method to be called only once before the genetic algorithm starts its evolution. If functioned, then it must accept a single parameter representing the instance of the genetic algorithm. If method, then it must accept 2 parameters where the second one refers to the method's object. Added in PyGAD 2.6.0.
114114
on_fitness: Accepts a function/method to be called after calculating the fitness values of all solutions in the population. If functioned, then it must accept 2 parameters: 1) a list of all solutions' fitness values 2) the instance of the genetic algorithm. If method, then it must accept 3 parameters where the third one refers to the method's object. Added in PyGAD 2.6.0.
@@ -429,7 +429,6 @@ def __init__(self,
429429
# Number of solutions in the population.
430430
self.sol_per_pop = sol_per_pop
431431
self.initialize_population(allow_duplicate_genes=allow_duplicate_genes,
432-
mutation_by_replacement=True,
433432
gene_type=self.gene_type,
434433
gene_constraint=gene_constraint)
435434
else:
@@ -1354,15 +1353,13 @@ def round_genes(self, solutions):
13541353

13551354
def initialize_population(self,
13561355
allow_duplicate_genes,
1357-
mutation_by_replacement,
13581356
gene_type,
13591357
gene_constraint):
13601358
"""
13611359
Creates an initial population randomly as a NumPy array. The array is saved in the instance attribute named 'population'.
13621360
13631361
It accepts:
13641362
-allow_duplicate_genes: Whether duplicate genes are allowed or not.
1365-
-mutation_by_replacement: Whether mutation by replacement is enabled or not.
13661363
-gene_type: The data type of the genes.
13671364
-gene_constraint: The constraints of the genes.
13681365
@@ -1450,14 +1447,14 @@ def initialize_population(self,
14501447
self.population[solution_idx], _, _ = self.solve_duplicate_genes_randomly(solution=self.population[solution_idx],
14511448
min_val=self.init_range_low,
14521449
max_val=self.init_range_high,
1453-
mutation_by_replacement=True,
14541450
gene_type=gene_type,
1451+
mutation_by_replacement=True,
14551452
sample_size=self.sample_size)
14561453
else:
14571454
self.population[solution_idx], _, _ = self.solve_duplicate_genes_by_space(solution=self.population[solution_idx].copy(),
14581455
gene_type=self.gene_type,
1459-
sample_size=self.sample_size,
14601456
mutation_by_replacement=True,
1457+
sample_size=self.sample_size,
14611458
build_initial_pop=True)
14621459

14631460
# Change the data type and round all genes within the initial population.
-332 Bytes
Binary file not shown.
-5.98 KB
Binary file not shown.
-17.4 KB
Binary file not shown.
-7.11 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)