You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. A new parameter named `delay_after_gen` is added in PyGAD 2.4.0. It accepts a non-negative number specifying the number of seconds to wait after a generation completes and before going to the next generation. It defaults to 0.0 which means no delay after the generation.
2. If the function passed to the `callback_generation` parameter returned "stop", then the run() method stops without completing the generations.
Copy file name to clipboardExpand all lines: pygad.py
+19-5
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
importrandom
3
3
importmatplotlib.pyplot
4
4
importpickle
5
+
importtime
5
6
6
7
classGA:
7
8
def__init__(self,
@@ -23,7 +24,8 @@ def __init__(self,
23
24
mutation_num_genes=None,
24
25
random_mutation_min_val=-1.0,
25
26
random_mutation_max_val=1.0,
26
-
callback_generation=None):
27
+
callback_generation=None,
28
+
delay_after_gen=0.0):
27
29
28
30
"""
29
31
The constructor of the GA class accepts all parameters required to create an instance of the GA class. It validates such parameters.
@@ -55,7 +57,9 @@ def __init__(self,
55
57
random_mutation_min_val: The minimum value of the range from which a random value is selected to be added to the selected gene(s) to mutate. It defaults to -1.0.
56
58
random_mutation_max_val: The maximum value of the range from which a random value is selected to be added to the selected gene(s) to mutate. It defaults to 1.0.
57
59
58
-
callback_generation: If not None, then it accepts a function to be called after each generation. This function must accept a single parameter representing the instance of the genetic algorithm.
60
+
callback_generation: If not None, then it accepts a function to be called after each generation. This function must accept a single parameter representing the instance of the genetic algorithm. If the function returned "stop", then the run() method stops without completing the generations.
61
+
62
+
delay_after_gen: Added in PyGAD 2.4.0. It accepts a non-negative number specifying the number of seconds to wait after a generation completes and before going to the next generation. It defaults to 0.0 which means no delay after the generation.
59
63
"""
60
64
61
65
self.init_range_low=init_range_low
@@ -216,7 +220,7 @@ def __init__(self,
216
220
raiseValueError("The fitness function must accept 2 parameters representing the solution to which the fitness value is calculated and the solution index within the population.\nThe passed fitness function named '{funcname}' accepts {argcount} argument(s).".format(funcname=fitness_func.__code__.co_name, argcount=fitness_func.__code__.co_argcount))
217
221
else:
218
222
self.valid_parameters=False
219
-
raiseValueError("The value assigned to the 'fitness_func' parameter is expected to be of type function by {fitness_func_type} found.".format(fitness_func_type=type(fitness_func)))
223
+
raiseValueError("The value assigned to the 'fitness_func' parameter is expected to be of type function but {fitness_func_type} found.".format(fitness_func_type=type(fitness_func)))
220
224
221
225
# Check if the callback_generation exists.
222
226
ifnot (callback_generationisNone):
@@ -230,10 +234,16 @@ def __init__(self,
230
234
raiseValueError("The callback_generation function must accept only 1 parameter representing the instance of the genetic algorithm.\nThe passed callback_generation function named '{funcname}' accepts {argcount} argument(s).".format(funcname=callback_generation.__code__.co_name, argcount=callback_generation.__code__.co_argcount))
231
235
else:
232
236
self.valid_parameters=False
233
-
raiseValueError("The value assigned to the 'callback_generation' parameter is expected to be of type function by {callback_generation_type} found.".format(callback_generation_type=type(callback_generation)))
237
+
raiseValueError("The value assigned to the 'callback_generation' parameter is expected to be of type function but {callback_generation_type} found.".format(callback_generation_type=type(callback_generation)))
234
238
else:
235
239
self.callback_generation=None
236
240
241
+
ifdelay_after_gen>=0.0:
242
+
self.delay_after_gen=delay_after_gen
243
+
else:
244
+
self.valid_parameters=False
245
+
raiseValueError("The value passed to the 'delay_after_gen' parameter must be a non-negative number. The value passed is {delay_after_gen} of type {delay_after_gen_type}.".format(delay_after_gen=delay_after_gen, delay_after_gen_type=type(delay_after_gen)))
246
+
237
247
# The number of completed generations.
238
248
self.generations_completed=0
239
249
@@ -352,7 +362,11 @@ def run(self):
352
362
353
363
# If the callback_generation attribute is not None, then cal the callback function after the generation.
0 commit comments