Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f292ca

Browse files
Edit documentation string
1 parent 93337d2 commit 7f292ca

File tree

1 file changed

+71
-53
lines changed

1 file changed

+71
-53
lines changed

‎pygad/helper/unique.py

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@ def solve_duplicate_genes_randomly(self,
1616
mutation_by_replacement,
1717
gene_type,
1818
num_trials=10):
19-
2019
"""
21-
Solves the duplicates in a solution by randomly selecting new values for the duplicating genes.
20+
Resolves duplicates in a solution by randomly selecting new values for the duplicate genes.
2221
23-
solution: A solution with duplicate values.
24-
min_val: Minimum value of the range to sample a number randomly.
25-
max_val: Maximum value of the range to sample a number randomly.
26-
mutation_by_replacement: Identical to the self.mutation_by_replacement attribute.
27-
gene_type: Exactly the same as the self.gene_type attribute.
28-
num_trials: Maximum number of trials to change the gene value to solve the duplicates.
22+
Args:
23+
solution (list): A solution containing genes, potentially with duplicate values.
24+
min_val (int): The minimum value of the range to sample a number randomly.
25+
max_val (int): The maximum value of the range to sample a number randomly.
26+
mutation_by_replacement (bool): Indicates if mutation is performed by replacement.
27+
gene_type (type): The data type of the gene (e.g., int, float).
28+
num_trials (int): The maximum number of attempts to resolve duplicates by changing the gene values.
2929
3030
Returns:
31-
new_solution: Solution after trying to solve its duplicates. If no duplicates solved, then it is identical to the passed solution parameter.
32-
not_unique_indices: Indices of the genes with duplicate values.
33-
num_unsolved_duplicates: Number of unsolved duplicates.
31+
tuple:
32+
list: The updated solution after attempting to resolve duplicates. If no duplicates are resolved, the solution remains unchanged.
33+
list: The indices of genes that still have duplicate values.
34+
int: The number of duplicates that could not be resolved.
3435
"""
35-
36+
3637
new_solution = solution.copy()
3738

3839
_, unique_gene_indices = numpy.unique(solution, return_index=True)
@@ -113,17 +114,20 @@ def solve_duplicate_genes_by_space(self,
113114
build_initial_pop=False):
114115

115116
"""
116-
Solves the duplicates in a solution by selecting values for the duplicating genes from the gene space.
117-
118-
solution: A solution with duplicate values.
119-
gene_type: Exactly the same as the self.gene_type attribute.
120-
num_trials: Maximum number of trials to change the gene value to solve the duplicates.
121-
117+
Resolves duplicates in a solution by selecting new values for the duplicate genes from the gene space.
118+
119+
Args:
120+
solution (list): A solution containing genes, potentially with duplicate values.
121+
gene_type (type): The data type of the gene (e.g., int, float).
122+
num_trials (int): The maximum number of attempts to resolve duplicates by selecting values from the gene space.
123+
122124
Returns:
123-
new_solution: Solution after trying to solve its duplicates. If no duplicates solved, then it is identical to the passed solution parameter.
124-
not_unique_indices: Indices of the genes with duplicate values.
125-
num_unsolved_duplicates: Number of unsolved duplicates.
125+
tuple:
126+
list: The updated solution after attempting to resolve duplicates. If no duplicates are resolved, the solution remains unchanged.
127+
list: The indices of genes that still have duplicate values.
128+
int: The number of duplicates that could not be resolved.
126129
"""
130+
127131
new_solution = solution.copy()
128132

129133
_, unique_gene_indices = numpy.unique(solution, return_index=True)
@@ -236,18 +240,20 @@ def unique_genes_by_space(self,
236240
build_initial_pop=False):
237241

238242
"""
239-
Loops through all the duplicating genes to find unique values that from their gene spaces to solve the duplicates.
240-
For each duplicating gene, a call to the unique_gene_by_space() function is made.
241-
242-
new_solution: A solution with duplicate values.
243-
gene_type: Exactly the same as the self.gene_type attribute.
244-
not_unique_indices: Indices with duplicating values.
245-
num_trials: Maximum number of trials to change the gene value to solve the duplicates.
246-
243+
Iterates through all duplicate genes to find unique values from their gene spaces and resolve duplicates.
244+
For each duplicate gene, a call is made to the `unique_gene_by_space()` function.
245+
246+
Args:
247+
new_solution (list): A solution containing genes with duplicate values.
248+
gene_type (type): The data type of the gene (e.g., int, float).
249+
not_unique_indices (list): The indices of genes with duplicate values.
250+
num_trials (int): The maximum number of attempts to resolve duplicates for each gene.
251+
247252
Returns:
248-
new_solution: Solution after trying to solve all of its duplicates. If no duplicates solved, then it is identical to the passed solution parameter.
249-
not_unique_indices: Indices of the genes with duplicate values.
250-
num_unsolved_duplicates: Number of unsolved duplicates.
253+
tuple:
254+
list: The updated solution after attempting to resolve all duplicates. If no duplicates are resolved, the solution remains unchanged.
255+
list: The indices of genes that still have duplicate values.
256+
int: The number of duplicates that could not be resolved.
251257
"""
252258

253259
num_unsolved_duplicates = 0
@@ -283,15 +289,15 @@ def unique_gene_by_space(self,
283289
build_initial_pop=False):
284290

285291
"""
286-
Returns a unique gene value for a single gene based on its value space to solve the duplicates.
287-
288-
solution: A solution with duplicate values.
289-
gene_idx: The index of the gene that duplicates its value with another gene.
290-
gene_type: Exactly the same as the self.gene_type attribute.
291-
292+
Returns a unique value for a specific gene based on its value space to resolve duplicates.
293+
294+
Args:
295+
solution (list): A solution containing genes with duplicate values.
296+
gene_idx (int): The index of the gene that has a duplicate value.
297+
gene_type (type): The data type of the gene (e.g., int, float).
298+
292299
Returns:
293-
A unique value, if exists, for the gene.
294-
"""
300+
Any: A unique value for the gene, if one exists; otherwise, the original gene value. """
295301

296302
if self.gene_space_nested:
297303
if type(self.gene_space[gene_idx]) in [numpy.ndarray, list, tuple]:
@@ -572,11 +578,14 @@ def find_two_duplicates(self,
572578
solution,
573579
gene_space_unpacked):
574580
"""
575-
Returns the first occurrence of duplicate genes.
576-
It returns:
577-
The index of a gene with a duplicate value.
578-
The value of the gene.
581+
Identifies the first occurrence of a duplicate gene in the solution.
582+
583+
Returns:
584+
tuple:
585+
int: The index of the first gene with a duplicate value.
586+
Any: The value of the duplicate gene.
579587
"""
588+
580589
for gene in set(solution):
581590
gene_indices = numpy.where(numpy.array(solution) == gene)[0]
582591
if len(gene_indices) == 1:
@@ -594,13 +603,15 @@ def unpack_gene_space(self,
594603
range_max,
595604
num_values_from_inf_range=100):
596605
"""
597-
Unpack the gene_space for the purpose of selecting a value that solves the duplicates.
598-
This is by replacing each range by a list of values.
599-
It accepts:
600-
range_min: The range minimum value.
601-
range_min: The range maximum value.
602-
num_values_from_inf_range: For infinite range of float values, a fixed number of values equal to num_values_from_inf_range is selected using the numpy.linspace() function.
603-
It returns the unpacked gene space.
606+
Unpacks the gene space for selecting a value to resolve duplicates by converting ranges into lists of values.
607+
608+
Args:
609+
range_min (float or int): The minimum value of the range.
610+
range_max (float or int): The maximum value of the range.
611+
num_values_from_inf_range (int): The number of values to generate for an infinite range of float values using `numpy.linspace()`.
612+
613+
Returns:
614+
list: A list representing the unpacked gene space.
604615
"""
605616

606617
# Copy the gene_space to keep it isolated form the changes.
@@ -740,8 +751,15 @@ def solve_duplicates_deeply(self,
740751
"""
741752
Sometimes it is impossible to solve the duplicate genes by simply selecting another value for either genes.
742753
This function solve the duplicates between 2 genes by searching for a third gene that can make assist in the solution.
743-
It returns:
744-
The solution after solving the duplicates or the None if duplicates cannot be solved.
754+
755+
Args:
756+
solution (list): The current solution containing genes, potentially with duplicates.
757+
gene_idx1 (int): The index of the first gene involved in the duplication.
758+
gene_idx2 (int): The index of the second gene involved in the duplication.
759+
assist_gene_idx (int): The index of the third gene used to assist in resolving the duplication.
760+
761+
Returns:
762+
list or None: The updated solution with duplicates resolved, or `None` if the duplicates cannot be resolved.
745763
"""
746764

747765
# gene_space_unpacked = self.unpack_gene_space()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /