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

Parallelise adaptive mutation population fitness computations when `parallel_processing` is enabled #201

Open Ririshi opened 1 year ago

Ririshi commented 1 year ago

I was checking my CPU usage while running a GA instance with adaptive mutation and noticed it's actually off most of the time. The fitness function I use takes several seconds to compute for every solution, so the lack of parallel processing on this type of mutation really hurts the overall time per generation.

I had a look at the code and expected a simple copy-paste with some minor updates to enable parallel processing in the mutation, but the architecture of the package (i.e. pygad.GA is a child class of pygad.utils.Mutation) makes it difficult for the Mutation methods to know if parallel processing is enabled without passing additional parameters. What might work is to add an __init__ method to Mutation and calling this method from GA.__init__. The parallel processing setup could be moved to Mutation, which is then inherited by GA. I don't think this is the cleanest solution in terms of OOP best practices, but it would work.

I might be able to put some time into creating a PR for this sometime soon.

philip928lin commented 1 year ago

I encountered the same issue. I hope this feature can be enabled!

Ririshi commented 8 months ago

I was using this for my master's thesis but ended up simply not using adaptive mutation instead of enabling parallel processing. I have since finished my thesis and am now busy with work and no longer interested in adding this enhancement. Just want to be clear in my intentions. This library has helped me alot, so I'm sorry that I won't able to contribute to it. I'll leave the issue open so others can make work of it if they are interested in doing so.

ahmedfgad commented 8 months ago

Appreciate it @Ririshi! This is now supported so that fitness calculation during adaptive mutation works with parallel processing. This will be available in the next release of the library.