-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEAT]: send whole population (2D matrix) to obj func #171
Comments
Hi @armisamimi, Indeed, when using whole population to calculate fitness, it will faster like 10X or 100X than calculate each of the agent's fitness. However, it is for benchmark functions only. You can't do it with other problem, for example, training neural network, hybrid with machine learning, hyper-parameter tuning with AI models. Also what you did is really wrong, you should not using multiprocessing for benchmark function, the time to start or close a sub-process is longer than the time to run a single obj_function. Therefore, you should use multithreading for benchmark functions. It will be much faster than multiprocessing in this case. |
I have to disagree with your points.
- My objective is not a benchmark function. It’s a real world design problem that happens to involve a lot of math that can be vectorized. There are plenty of other real optimization problems out there like that.
- Real multi-threading for CPU bound problems is not possible in python due to the global interpreter lock. There is a 3.13 beta that removes this restriction but most packages are not compatible yet.
- I don’t spawn new peocesses every time the vectorized object function is evaluated.
The executer pool is created once with M processes prior to calling the optimizer and the handle is passed to the object function along with the N trial candidates. Inside the fun I submit N / M vectors to each of M processes in the pool, gathers the results, and returns the N costs.
There is some overhead due to serialization and deserialization of objects between processes. But for cost functions that take more than 20ms or so it already becomes worth it.
|
I am currently using scipy’s differential_evolution() which has the option (vectorized=True) to pass the n_variables * n_population_size matrix (in my case around 20 x 200) to the objective function once pee iteration . This allows me to vectorize the math (numba/numpy) and get very good performance, around 50ms per iteration. This ends up being faster than using multiprocessing and dispatching individual solution vectors to multiple cores due to the overheads involved (considering the relatively cheap cost function).
The text was updated successfully, but these errors were encountered: