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()
以上代碼全部整理到了 github
包括下面這個動畫: