ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.85k stars 462 forks source link

fitness_func performance issue #24

Closed NH-VII closed 3 years ago

NH-VII commented 3 years ago

The fitness_func method is called more then ones for each generation. This can be avoided using the previous calculated fitness instead calling again fitness_func or cal_pop_fitness. This issue is relevant if the fitness calculation is high demanding (like the final score of a videogame match).

ahmedfgad commented 3 years ago

Hi,

Thanks for this note.

One location the cal_pop_fitness() was called another time in each generation is inside the best_solution() method. Now, the best_solution() method accepts a new optional argument called pop_fitness which accepts an array of the previously calculated fitness values. If this argument is left None, then the a new call to the cal_pop_fitness() method is done.

This change [alongside some additional features like training PyTorch models and supporting adaptive mutation] is reflected in the new release of PyGAD 2.10.0. Please check the documentation for more information: https://pygad.readthedocs.io

Please let me know if there are any other possible enhancements to be made.

Thanks.

NH-VII commented 3 years ago

Hi, thank you for your kind reply and for the amazing work behind this module! I am going to update to 2.10.0 as soon as possible. If I notice something else during my next GA projects, I will let you know.

ahmedfgad commented 3 years ago

Hi,

Thanks again for your feedback.

Looking for your future feedback as it really helps.