guofei9987 / scikit-opt

Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman)
https://scikit-opt.github.io/scikit-opt/#/en/
MIT License
5.3k stars 989 forks source link

Allow users to set number of process pool workers/threads #203

Open windowshopr opened 2 years ago

windowshopr commented 2 years ago

Would be nice to see the users be able to control the number of processors/threads used when defining 'multiprocessing' or 'multithreading'.

I would think you could pass in a worker argument (default can be None) to set_run_mode(), something like:

set_run_mode(obj_func, 'multiprocessing', n_workers=3)

..and then further down in the func_transformer() function:

    elif mode == 'multiprocessing':
        from multiprocessing import Pool

        pool = Pool()
        if n_workers != None:
            pool = Pool(n_workers)

        def func_transformed(X):
            return np.array(pool.map(func, X))

I'm not sure if this would be the appropriate or working syntax, but just an idea I had. Sometimes we don't want to just default to using ALL CPU cores when using multiprocessing.