@@ -89,14 +89,40 @@ def mutation_by_space(self, offspring):
89
89
# The numpy.random.choice() and numpy.random.uniform() functions return a NumPy array as the output even if the array has a single value.
90
90
# We have to return the output at index 0 to force a numeric value to be returned not an object of type numpy.ndarray.
91
91
# 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.
92
93
value_from_space = numpy .random .choice (numpy .arange (start = curr_gene_space ['low' ],
93
94
stop = curr_gene_space ['high' ],
94
95
step = curr_gene_space ['step' ]),
95
96
size = 1 )[0 ]
96
97
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
100
126
else :
101
127
# Selecting a value randomly based on the current gene's space in the 'gene_space' attribute.
102
128
# If the gene space has only 1 value, then select it. The old and new values of the gene are identical.
0 commit comments