Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use On_stop function on pygad? #23

Closed Answered by ahmedfgad
itmamdiyar26 asked this question in Q&A
Discussion options

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

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
1 reply
Comment options

Hi @ahmedfgad
thank for replying this
i already try this and its work
thanks you so much for helping me

Answer selected by itmamdiyar26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /