天天看点

Python实现 粒子群算法库

scikit-opt

点击下载这个库,或者按照官网的说明用 pip 安装,然后就可以用了

def demo_func(x):
    x1, x2, x3 = x
    return x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2

pso = PSO(func=demo_func, dim=3)
fitness = pso.fit()
print('best_x is ',pso.gbest_x)
print('best_y is ',pso.gbest_y)
pso.plot_history()
           
Python实现 粒子群算法库

以上代码全部整理到了 github

包括下面这个动画:

Python实现 粒子群算法库

继续阅读