首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用SKO.pso python实现粒子群算法

如何使用SKO.pso python实现粒子群算法
EN

Stack Overflow用户
提问于 2020-12-19 18:43:59
回答 1查看 137关注 0票数 0

我在Github上找到了一个包,里面包含了各种关于进化的算法。链接如下:https://github.com/guofei9987/scikit-opt

然而,当我使用这个包时,我感到困惑。当我试着像这样做的时候,将dim设置为1,当上限为10时,将下限设置为0,关注x的非负值,得到了错误的答案,这是我的代码:

代码语言:javascript
复制
def demo_func(x):
    # Sphere
    x1= x
    return ((10-4*x1)/(4*x1+3))*x1
from sko.PSO import PSO
pso = PSO(func=demo_func, n_dim=1, pop=40, max_iter=150, lb=[0], ub=[10], w=0.8, c1=0.5, c2=0.5)
pso.run()
print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)

import matplotlib.pyplot as plt

plt.plot(pso.gbest_y_hist)
plt.show()

结果如下:result

best_x为10,best_y为-6.97674419

然而,这是一个错误的答案。X的正确答案是0.5到1。有没有人有建议?

EN

回答 1

Stack Overflow用户

发布于 2021-03-22 14:39:21

粒子群优化算法优化函数的最小值。添加一个负号以获得最大值。

代码语言:javascript
复制
def demo_func(x):
    x1,= x
    return -((10-4*x1)/(4*x1+3))*x1
from sko.PSO import PSO
pso = PSO(func=demo_func, n_dim=1, pop=40, max_iter=150, lb=[0], ub=[10], w=0.8, c1=0.5, c2=0.5)
pso.run()
print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)

import matplotlib.pyplot as plt

plt.plot(pso.gbest_y_hist)
plt.show()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65368942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档