@@ -154,6 +154,64 @@ def test_zero_crossover_probability_zero_mutation_probability():
154154
155155 assert result == True
156156
157+ def test_random_mutation_manual_call ():
158+ result , ga_instance = output_crossover_mutation (mutation_type = "random" ,
159+ random_mutation_min_val = 888 ,
160+ random_mutation_max_val = 999 )
161+ ga_instance .mutation_num_genes = 9
162+
163+ temp_offspring = numpy .array (initial_population [0 :1 ])
164+ offspring = ga_instance .random_mutation (offspring = temp_offspring .copy ())
165+
166+ comp = offspring - temp_offspring
167+ comp_sorted = sorted (comp .copy ())
168+ comp_sorted = numpy .abs (numpy .unique (comp_sorted ))
169+
170+ # The other 1 added to include the last value in the range.
171+ assert len (comp_sorted ) in range (1 , 1 + 1 + ga_instance .mutation_num_genes )
172+ assert comp_sorted [0 ] == 0
173+
174+ def test_random_mutation_manual_call2 ():
175+ result , ga_instance = output_crossover_mutation (mutation_type = "random" ,
176+ random_mutation_min_val = 888 ,
177+ random_mutation_max_val = 999 )
178+ ga_instance .mutation_num_genes = 10
179+
180+ temp_offspring = numpy .array (initial_population [0 :1 ])
181+ offspring = ga_instance .random_mutation (offspring = temp_offspring .copy ())
182+
183+ comp = offspring - temp_offspring
184+ comp_sorted = sorted (comp .copy ())
185+ comp_sorted = numpy .abs (numpy .unique (comp_sorted ))
186+
187+ # The other 1 added to include the last value in the range.
188+ assert len (comp_sorted ) in range (1 , 1 + 1 + ga_instance .mutation_num_genes )
189+ # assert comp_sorted[0] == 0
190+
191+ def test_random_mutation_manual_call3 ():
192+ random_mutation_min_val = 888
193+ random_mutation_max_val = 999
194+ result , ga_instance = output_crossover_mutation (mutation_type = "random" ,
195+ random_mutation_min_val = random_mutation_min_val ,
196+ random_mutation_max_val = random_mutation_max_val ,
197+ mutation_by_replacement = True )
198+ ga_instance .mutation_num_genes = 10
199+
200+ temp_offspring = numpy .array (initial_population [0 :1 ])
201+ offspring = ga_instance .random_mutation (offspring = temp_offspring .copy ())
202+
203+ comp = offspring
204+ comp_sorted = sorted (comp .copy ())
205+ comp_sorted = numpy .abs (numpy .unique (comp ))
206+
207+ # The other 1 added to include the last value in the range.
208+ # assert len(comp_sorted) in range(1, 1 + 1 + ga_instance.mutation_num_genes)
209+ # assert comp_sorted[0] == 0
210+
211+ value_space = list (range (random_mutation_min_val , random_mutation_max_val ))
212+ for value in comp_sorted :
213+ assert value in value_space
214+
157215if __name__ == "__main__" :
158216 print ()
159217 test_no_crossover_no_mutation ()
@@ -186,3 +244,12 @@ def test_zero_crossover_probability_zero_mutation_probability():
186244 test_zero_crossover_probability_zero_mutation_probability ()
187245 print ()
188246
247+ test_random_mutation_manual_call ()
248+ print ()
249+
250+ test_random_mutation_manual_call2 ()
251+ print ()
252+
253+ test_random_mutation_manual_call3 ()
254+ print ()
255+
0 commit comments