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.79k stars 451 forks source link

Cannot modify the attributes of the ga_instance when running multiple processes in parallel #270

Open rucky96 opened 5 months ago

rucky96 commented 5 months ago

Hi,

I was trying to delete some of the solutions of the population on the fly (inside the fitness_func) which were introducing my prediction to diverge. This is possible only when the flag parallel_processing is not set, while when working with multiple processes any changes in the attributes of the class will not be reflected in the subsequent generations.

I would like to know if it is possible to extend this feature to parallel processing as well

ahmedfgad commented 5 months ago

Hi @rucky96,

Thanks for your suggestion.

Each process has its own memory that is not shared by the other processes. So, any changes to the population inside a child process will be discarded once this process finishes its execution. This is not the case compared to using threads instead of processes.

To support this feature, it needs changes to how the processes are executed which is not a good fit to the library. But you can make a copy of the cal_pop_fitness() and edit it to support making edits to the GA attributes inside the processes.