pymoo
pymoo copied to clipboard
PSO parallelization
Hi, I recently started using pymoo looking for an optimizer capable of performing parallel calculations. I followed the guide at https://pymoo.org/problems/parallelization.html but I did not notice any difference in terms of computation times even for the guided example. I am not able from the references to understand where the error is in my code. I thank in advance anyone who has the patience to help me. I attach the code I am working on.
from pymoo.algorithms.soo.nonconvex.pso import PSO
from pymoo.core.problem import ElementwiseProblem
from pymoo.optimize import minimize
from pymoo.operators.sampling.lhs import LHS
class FittingRoutine(ElementwiseProblem):`
def __init__(self, **kwargs):
super().__init__(n_var=3,
n_obj=1,
n_ieq_constr=0,
xl=np.array([0.5, 0.,0.]),
xu=np.array([1.5, 1000.,100.]))
def _evaluate(self, parameters, out, **kwargs):
err=objectiveFun_Callback(parameters, exp_omega_vector, exp_storage,exp_loss, nbelements) #residual function
out["F"] = [err]
import multiprocessing
from pymoo.core.problem import StarmapParallelization
#initialize the thread pool and create the runner
n_proccess = 8
pool = multiprocessing.Pool(n_proccess)
runner = StarmapParallelization(pool.starmap)
nbelements=1
kwargs={'elementwise_runner': runner} #'exp_omega_vector':exp_omega_vector, 'exp_storage':exp_storage, 'exp_loss':exp_loss
algorithm = PSO(pop_size=47,
sampling=LHS(),
w=0.9, c1=2.0, c2=2.0,
adaptive=True, initial_velocity='random', max_velocity_rate=0.20,
pertube_best=False,**kwargs) #repair=NoRepair(), output=PSOFuzzyOutput(),
problem = FittingRoutine(elementwise_runner=runner)
res = minimize(problem,
algorithm,
("n_gen", 100),
seed=1,
verbose=True,
display=None,
#callback=None,
return_least_infeasible=True,
save_history=True,**kwargs)
print('Threads:', res.exec_time)
pool.close()
print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))
@federico-califano so what's the problem of your code?
Did you benchmark the exact time? It really depends on your objective function and the operators in it. And of course also on your hardware.
Let me know if you have another question and I should keep this issue open.