@@ -89,14 +89,40 @@ def mutation_by_space(self, offspring):
8989 # The numpy.random.choice() and numpy.random.uniform() functions return a NumPy array as the output even if the array has a single value.
9090 # We have to return the output at index 0 to force a numeric value to be returned not an object of type numpy.ndarray.
9191 # If numpy.ndarray is returned, then it will cause an issue later while using the set() function.
92+ # Randomly select a value from a discrete range.
9293 value_from_space = numpy .random .choice (numpy .arange (start = curr_gene_space ['low' ],
9394 stop = curr_gene_space ['high' ],
9495 step = curr_gene_space ['step' ]),
9596 size = 1 )[0 ]
9697 else :
97- value_from_space = numpy .random .uniform (low = curr_gene_space ['low' ],
98- high = curr_gene_space ['high' ],
99- size = 1 )[0 ]
98+ # Return the current gene value.
99+ value_from_space = offspring [offspring_idx , gene_idx ]
100+ # Generate a random value to be added to the current gene value.
101+ rand_val = numpy .random .uniform (low = range_min ,
102+ high = range_max ,
103+ size = 1 )[0 ]
104+ # The objective is to have a new gene value that respects the gene_space boundaries.
105+ # The next if-else block checks if adding the random value keeps the new gene value within the gene_space boundaries.
106+ temp_val = value_from_space + rand_val
107+ if temp_val < curr_gene_space ['low' ]:
108+ # Restrict the new value to be > curr_gene_space['low']
109+ # If subtracting the random value makes the new gene value outside the boundaries [low, high), then use the lower boundary the gene value.
110+ if curr_gene_space ['low' ] <= value_from_space - rand_val < curr_gene_space ['high' ]:
111+ # Because subtracting the random value keeps the new gene value within the boundaries [low, high), then use such a value as the gene value.
112+ temp_val = value_from_space - rand_val
113+ else :
114+ # Because subtracting the random value makes the new gene value outside the boundaries [low, high), then use the lower boundary as the gene value.
115+ temp_val = curr_gene_space ['low' ]
116+ elif temp_val >= curr_gene_space ['high' ]:
117+ # Restrict the new value to be < curr_gene_space['high']
118+ # If subtracting the random value makes the new gene value outside the boundaries [low, high), then use such a value as the gene value.
119+ if curr_gene_space ['low' ] <= value_from_space - rand_val < curr_gene_space ['high' ]:
120+ # Because subtracting the random value keeps the new value within the boundaries [low, high), then use such a value as the gene value.
121+ temp_val = value_from_space - rand_val
122+ else :
123+ # Because subtracting the random value makes the new gene value outside the boundaries [low, high), then use the lower boundary as the gene value.
124+ temp_val = curr_gene_space ['low' ]
125+ value_from_space = temp_val
100126 else :
101127 # Selecting a value randomly based on the current gene's space in the 'gene_space' attribute.
102128 # If the gene space has only 1 value, then select it. The old and new values of the gene are identical.
0 commit comments