How to use On_stop function on pygad? #23
-
Excuse me sir i have a question about this:
and this is my output
my question is why the script is still running even i have set the parameter for stop |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @itmamdiyar26, Thanks for using PyGAD! Please note that calling the All the functions that start with To stop the algorithm before passing through all the generations, then simply return the string Here is your code after being modified to return def on_generation(ga_instance):
global num_generations
num_generations = num_generations+1
print("on_generation()" , num_generations)
if num_generations == 5:
print("Generasi ke",num_generations)
return "stop" def on_stop(ga_instance, num_generations):
print ("END") Please let me know if there are still any problems. |
Beta Was this translation helpful? Give feedback.
Hi @itmamdiyar26,
Thanks for using PyGAD!
Please note that calling the
on_stop()
function will not stop the algorithm. It is just a callback function that is called when the algorithm stops. In your code, calling theon_stop()
function just executes its body.All the functions that start with
on_
are callback functions and are not meant to be called by the user but automatically according to flow of execution.To stop the algorithm before passing through all the generations, then simply return the string
"stop"
inside theon_generation()
callback function. After returning"stop"
, then theon_stop()
function will be called automatically (in case you assigned a valid function to theon_stop
…