Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedfgad committed Dec 6, 2024
1 parent e17b999 commit d51b4d8
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions pygad/helper/unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def unique_int_gene_from_range(self,
max_val,
mutation_by_replacement,
gene_type,
step=None):
step=1):

"""
Finds a unique integer value for the gene.
Expand All @@ -182,38 +182,24 @@ def unique_int_gene_from_range(self,
max_val: Maximum value of the range to sample a number randomly.
mutation_by_replacement: Identical to the self.mutation_by_replacement attribute.
gene_type: Exactly the same as the self.gene_type attribute.
step: Defaults to 1.
Returns:
selected_value: The new value of the gene. It may be identical to the original gene value in case there are no possible unique values for the gene.
"""

# The gene_type is of the form [type, precision]
if self.gene_type_single == True:
if step is None:
# all_gene_values = numpy.arange(min_val,
# max_val,
# dtype=gene_type[0])
all_gene_values = numpy.asarray(numpy.arange(min_val, max_val),
dtype=gene_type[0])
else:
# For non-integer steps, the numpy.arange() function returns zeros if the dtype parameter is set to an integer data type. So, this returns zeros if step is non-integer and dtype is set to an int data type: numpy.arange(min_val, max_val, step, dtype=gene_type[0])
# To solve this issue, the data type casting will not be handled inside numpy.arange(). The range is generated by numpy.arange() and then the data type is converted using the numpy.asarray() function.
all_gene_values = numpy.asarray(numpy.arange(min_val,
max_val,
step),
dtype=gene_type[0])
dtype = gene_type[0]
else:
if step is None:
# all_gene_values = numpy.arange(min_val,
# max_val,
# dtype=gene_type[gene_index][0])
all_gene_values = numpy.asarray(numpy.arange(min_val,
max_val),
dtype=gene_type[gene_index][0])
else:
all_gene_values = numpy.asarray(numpy.arange(min_val,
max_val,
step),
dtype=gene_type[gene_index][0])
dtype = gene_type[gene_index][0]

# For non-integer steps, the numpy.arange() function returns zeros if the dtype parameter is set to an integer data type. So, this returns zeros if step is non-integer and dtype is set to an int data type: numpy.arange(min_val, max_val, step, dtype=gene_type[0])
# To solve this issue, the data type casting will not be handled inside numpy.arange(). The range is generated by numpy.arange() and then the data type is converted using the numpy.asarray() function.
all_gene_values = numpy.asarray(numpy.arange(min_val,
max_val,
step),
dtype=dtype)

if mutation_by_replacement:
pass
Expand Down

0 comments on commit d51b4d8

Please sign in to comment.