PyGAD-2.4.0
Changes in PyGAD 2.4.0:
- A new parameter named
delay_after_gen
is added which accepts a non-negative number specifying the time in seconds to wait after a generation completes and before going to the next generation. It defaults to0.0
which means no delay after the generation. - The passed function to the
callback_generation
parameter of the pygad.GA class constructor can terminate the execution of the genetic algorithm if it returns the stringstop
. This causes therun()
method to stop.
One important use case for that feature is to stop the genetic algorithm when a condition is met before passing though all the generations. The user may assigned a value of 100 to the num_generations
parameter of the pygad.GA class constructor. Assuming that at generation 50, for example, a condition is met and the user wants to stop the execution before waiting the remaining 50 generations. To do that, just make the function passed to the callback_generation
parameter to return the string stop
.
Here is an example of a function to be passed to the callback_generation
parameter which stops the execution if the fitness value 70 is reached. The value 70 might be the best possible fitness value. After being reached, then there is no need to pass through more generations because no further improvement is possible.
def func_generation(ga_instance):
if ga_instance.best_solution()[1] >= 70:
return "stop"