当前位置:网站首页>Pymoo learning (7): Parallelization
Pymoo learning (7): Parallelization
2022-07-25 19:07:00 【Inji】
List of articles
1 introduce
In practice, , Parallelization It can significantly improve the efficiency of optimization . Based on Population The algorithm of , You can evaluate itself by parallelization , Realize the evaluation of a set of solutions .
2 Vectorization matrix operation
One way is to use Numpy Matrix operations , It has been used for almost all in Pymoo Testing problems implemented in . By default ,elementwise_evaluation Set to False, It means _evaluate Retrieve a set of solutions . therefore , Input matrix x x x Each line of is an individual , Each column is a variable :
import numpy as np
from pymoo.core.problem import Problem
from pymoo.algorithms.soo.nonconvex.ga import GA
from pymoo.optimize import minimize
class MyProblem(Problem):
def __init__(self, **kwargs):
super().__init__(n_var=10, n_obj=1, n_constr=0, xl=-5, xu=5, **kwargs)
def _evaluate(self, x, out, *args, **kwargs):
out["F"] = np.sum(x ** 2, axis=1)
res = minimize(MyProblem(), GA())
print('Threads:', res.exec_time)
Output is as follows :
Threads: 1.416006326675415
3 Starmap Interface
Starmap from Python Standard library multiprocessing.Pool.starmap Provide , It is convenient to parallelize . You need to set elementwise_evaluation=True, Means every call _evaluate Evaluate only one solution .
3.1 Threads
import numpy as np
from pymoo.core.problem import Problem
from pymoo.core.problem import starmap_parallelized_eval
from pymoo.algorithms.soo.nonconvex.pso import PSO
from pymoo.optimize import minimize
from multiprocessing.pool import ThreadPool
class MyProblem(Problem):
def __init__(self, **kwargs):
super().__init__(n_var=10, n_obj=1, n_constr=0, xl=-5, xu=5, **kwargs)
def _evaluate(self, x, out, *args, **kwargs):
out["F"] = np.sum(x ** 2, axis=1)
n_threads = 8
pool = ThreadPool(n_threads)
problem = MyProblem(runner=pool.starmap, func_eval=starmap_parallelized_eval)
res = minimize(problem, PSO(), seed=1, n_gen=100)
print('Threads:', res.exec_time)
Output is as follows :
Threads: 0.5501224994659424
3.2 process
import multiprocessing
n_proccess = 8
pool = multiprocessing.Pool(n_proccess)
problem = MyProblem(runner=pool.starmap, func_eval=starmap_parallelized_eval)
res = minimize(problem, PSO(), seed=1, n_gen=100)
print('Processes:', res.exec_time)
Output is as follows :
Processes: 1.1640357971191406
3.3 Dask
A more advanced method is to assign the evaluation function to several worker. stay Pymoo Recommended in Dask.
notes : You may need to install the following libraries :
pip install dask distributed
The code is as follows :
import numpy as np
from dask.distributed import Client
from pymoo.core.problem import dask_parallelized_eval
from pymoo.core.problem import Problem
from pymoo.algorithms.soo.nonconvex.pso import PSO
from pymoo.optimize import minimize
class MyProblem(Problem):
def __init__(self, **kwargs):
super().__init__(n_var=10, n_obj=1, n_constr=0, xl=-5, xu=5, **kwargs)
def _evaluate(self, x, out, *args, **kwargs):
out["F"] = np.sum(x ** 2, axis=1)
if __name__ == '__main__':
client = Client()
client.restart()
print("STARTED")
client = Client()
problem = MyProblem(runner=client, func_eval=dask_parallelized_eval)
res = minimize(problem, PSO(), seed=1, n_gen=100)
print('Dask:', res.exec_time)
Output is as follows :
STARTED
Dask: 1.30446195602417
4 Personalized parallel
4.1 Threads
import numpy as np
from multiprocessing.pool import ThreadPool
from pymoo.core.problem import Problem
from pymoo.algorithms.soo.nonconvex.pso import PSO
from pymoo.optimize import minimize
class MyProblem(Problem):
def __init__(self, **kwargs):
super().__init__(n_var=10, n_obj=1, n_constr=0, xl=-5, xu=5, **kwargs)
def _evaluate(self, X, out, *args, **kwargs):
def my_eval(x):
return (x ** 2).sum()
params = [[X[k]] for k in range(len(X))]
F = pool.starmap(my_eval, params)
out["F"] = np.array(F)
if __name__ == '__main__':
pool = ThreadPool(8)
problem = MyProblem()
res = minimize(problem, PSO(), seed=1, n_gen=100)
print('Threads:', res.exec_time)
Output is as follows :
Threads: 1.0212376117706299
4.2 Dask
import numpy as np
from dask.distributed import Client
from pymoo.algorithms.soo.nonconvex.pso import PSO
from pymoo.core.problem import Problem
from pymoo.optimize import minimize
class MyProblem(Problem):
def __init__(self, *args, **kwargs):
super().__init__(n_var=10, n_obj=1, n_constr=0, xl=-5, xu=5,
elementwise_evaluation=False, *args, **kwargs)
def _evaluate(self, X, out, *args, **kwargs):
def fun(x):
return np.sum(x ** 2)
jobs = [client.submit(fun, x) for x in X]
out["F"] = np.row_stack([job.result() for job in jobs])
if __name__ == '__main__':
client = Client(processes=False)
problem = MyProblem()
res = minimize(problem, PSO(), seed=1, n_gen=100)
print('Dask:', res.exec_time)
client.close()
Output is as follows :
Dask: 19.102460861206055
reference
【1】https://pymoo.org/problems/parallelization.html
【2】https://blog.csdn.net/u013066730/article/details/105821888
边栏推荐
- Ultimate doll 2.0 | cloud native delivery package
- 软件测试流程(思维导图)
- Ping command details [easy to understand]
- Analysis of the internet jam in IM development? Network disconnection?
- The understanding of domain adaptation in transfer learning and the introduction of three technologies
- Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
- kubernetes RBAC
- Youfu force supercomputing provides customized high-performance computing services for customers
- 接口自动化测试平台FasterRunner系列(一)- 简介、安装部署、启动服务、访问地址、配置补充
- 【阅读笔记】《深度学习》第一章:引言
猜你喜欢

Yyds dry inventory interview must brush top101: reverse linked list

Based on easycv to reproduce Detr and dab-detr, the correct opening method of object query

Pymoo学习 (6):终止条件

21 days proficient in typescript-4 - type inference and semantic check

Ultimate doll 2.0 | cloud native delivery package

SQL 实现 Excel 的10个常用功能,附面试原题

Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!

Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?

常用的开发软件下载地址

【iniparser】项目配置工具iniparser的简单使用
随机推荐
乐理基础 调式
MES管理系统有什么应用价值
F5: Six capabilities required for enterprise digital transformation
A brief history from object detection to image segmentation
There are several browser cores. How to upgrade if the browser version is too low
李宏毅《机器学习》丨1. Introduction of this course(机器学习介绍)
基础乐理--配置和弦
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
600000 pieces of data are made from March 1 to March 31. Videodate requires starting time from 00:00 to 24:00 on March 1 to 31, which is only for notes
基于FPGA的1080P 60Hz BT1120接口调试过程记录
[help center] provide your customers with the core options of self-service
怎么禁止使用360浏览器(怎么才能把自带的浏览器停用)
Detailed explanation of Bluetooth protocol (what is Bluetooth)
Process communication (Systemv communication mode: shared memory, message queue, semaphore)
srec_cat 常用参数的使用
Yarn installation and use tutorial [easy to understand]
hough变换理解[通俗易懂]
How to create an effective help document?
Interpretation of "cross chain interconnection smart contract"
[web technology] 1391 page visualization building tool, previous life and present life