-
-
Notifications
You must be signed in to change notification settings - Fork 489
How to use On_stop function on pygad? #23
-
Excuse me sir i have a question about this:
this is my on_generation and on_stop
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)
on_stop(ga_instance,num_generations)
def on_stop(ga_instance, num_generations):
print ("END")
and this is my output
on_start()
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 1
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 2
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 3
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 4
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 5
Generasi ke 5
END
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 6
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 7
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 8
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 9
on_fitness()
on_parents()
on_crossover()
on_mutation()
on_generation() 10
END
my question is why the script is still running even i have set the parameter for stop
can you help me to solve this? thanks
Beta Was this translation helpful? Give feedback.
All reactions
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 the on_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 the on_generation()
callback function. After returning "stop"
, then the on_stop()
function will be called automatically (in case you assigned a valid function to the on_stop
...
Replies: 1 comment 1 reply
-
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 the on_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 the on_generation()
callback function. After returning "stop"
, then the on_stop()
function will be called automatically (in case you assigned a valid function to the on_stop
argument).
Here is your code after being modified to return "stop"
rather than calling on_stop()
.
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.
All reactions
-
❤️ 1
-
Hi @ahmedfgad
thank for replying this
i already try this and its work
thanks you so much for helping me
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1