diff --git a/pygad/helper/unique.py b/pygad/helper/unique.py index 1a0ba631..02e38747 100644 --- a/pygad/helper/unique.py +++ b/pygad/helper/unique.py @@ -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. @@ -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