python - 如何使用 SKO.pso python 应用粒子群优化
问题描述
我在 Github 上找到了一个包,里面有各种关于进化的算法。链接如下: https ://github.com/guofei9987/scikit-opt
但是,我在使用该软件包时感到困惑。当我尝试作为示例进行操作时,将昏暗设置为 1,当上限为 10 时将下限设置为零,关注 x 的非负数,得到错误答案,这是我的代码:
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()
结果如下:结果
best_x 是 [10.] best_y 是 [-6.97674419]
然而,这是一个错误的答案。x 的正确答案在 0.5 到 1 之间。有人有建议吗?
解决方案
PSO 优化函数的最小值。添加负号以获得最大值。
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()
推荐阅读
- asp.net - 防止在按钮单击时刷新页面
- wordpress - 标题计数器功能不适用于 WordPress,错误 500
- r - 将 R Shiny ggplot 与背景图像对齐
- android - 如何播放本地视频文件
- laravel - 我们如何在laravel中获得学校的最后一个ID?
- alexa - 如何从 alexa 语音资料或亚马逊帐户获取联系电话以开发 alexa 技能?
- java - Java 中的 ActionPerformed 方法
- php - 在 PHP7 中运行与 php 5.3 一起使用有什么问题?
- jquery - 使用 Id 在 Datalist 中设置选定值
- selenium-webdriver - 使用 TestNG (Java) 自定义范围报告