我在Github上找到了一个包,里面包含了各种关于进化的算法。链接如下:https://github.com/guofei9987/scikit-opt
然而,当我使用这个包时,我感到困惑。当我试着像这样做的时候,将dim设置为1,当上限为10时,将下限设置为0,关注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()结果如下:result
best_x为10,best_y为-6.97674419
然而,这是一个错误的答案。X的正确答案是0.5到1。有没有人有建议?
发布于 2021-03-22 14:39:21
粒子群优化算法优化函数的最小值。添加一个负号以获得最大值。
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()https://stackoverflow.com/questions/65368942
复制相似问题