简介
本文通过python的算法工具箱scikit-opt实现粒子群算法,scikit-opt可以通过pip直接安装,有完整的文档。
图示
程序
import numpy as np
import pso_sko
import matplotlib.pyplot as plt
def demo_func(x):
x1, x2 = x
return x1+10*np.sin(5*x1)+5*np.cos(4*x1)-x2/10+6*np.sin(x2*10)+12*np.cos(7*x2)
pop =40
max_iter=400
pso = pso_sko.PSO(func=demo_func, dim=2,pop=pop,max_iter=max_iter,vartype=1,vfactor=0.5,lb_list=[-6,5],ub_list=[4,15])
fitness = pso.fit()
y = pso.gbest_y_hist
x = np.arange(0,len(y),1)
plt.plot(x,y)
plt.show()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)