用Pso思想求解y = x^2的最小值

2023-01-03 19:48:44 浏览数 (2)

1 问题

Pso思想求解y = x^2的最小值。

2 方法

  1. 先了解粒子群思想的基本原理
  2. 在迭代之前需要先画出y = x^2的平面图并确定其迭代的范围
  3. 完成粒子群迭代的必要代码,如适应度计算、速度更新、粒子位置更新和其主要运算过程

代码

import numpy as np

import matplotlib.pyplot as plt

import matplotlib as mpl

X = np.arange(-5, 5, 0.1)

wave = X ** 2

plt.plot(X, wave)

plt.show()

mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体

mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

def fitness_func(X):

A = 10

pi = np.pi

x = X[:, 1]

return x**2

def velocity_update(V, X, pbest, gbest, c1, c2, w, max_val):

size = X.shape[0]

r1 = np.random.random((size, 1))

r2 = np.random.random((size, 1))

V = w*V c1*r1*(pbest-X) c2*r2*(gbest-X)

# 防止越界处理

V[V < -max_val] = -max_val

V[V > max_val] = max_val

return V

def position_update(X, V):

return X V

def pos():

w = 1

c1 = 2

c2 = 2

r1 = None

r2 = None

dim = 2

size = 20

iter_num = 1000

max_val = 0.5

best_fitness = float(9e10)

fitness_val_list = []

# 初始化种群各个粒子的位置

X = np.random.uniform(-5, 5, size=(size, dim))

# 初始化各个粒子的速度

V = np.random.uniform(-0.5, 0.5, size=(size, dim))

# print(X)

p_fitness = fitness_func(X)

g_fitness = p_fitness.min()

fitness_val_list.append(g_fitness)

# 初始化的个体最优位置和种群最优位置

pbest = X

gbest = X[p_fitness.argmin()]

# 迭代计算

for i in range(1, iter_num):

V = velocity_update(V, X, pbest, gbest, c1, c2, w, max_val)

X = position_update(X, V)

p_fitness2 = fitness_func(X)

g_fitness2 = p_fitness2.min()

# 更新每个粒子的历史最优位置

for j in range(size):

if p_fitness[j] > p_fitness2[j]:

pbest[j] = X[j]

p_fitness[j] = p_fitness2[j]

# 更新群体的最优位置

if g_fitness > g_fitness2:

gbest = X[p_fitness2.argmin()]

g_fitness = g_fitness2

# 记录最优迭代记录

fitness_val_list.append(g_fitness)

i = 1

# 输出迭代结果

print("最优值是:%.5f" % fitness_val_list[-1])

print("最优解是:x=%.5f,y=%.5f" % (gbest[0], gbest[1]))

# 绘图

plt.plot(fitness_val_list, color='r')

plt.title('迭代过程')

plt.show()

pos()

3 结语

运用Pso思想一定要先了解其原理,需要明白它的速度与位置更新公式。

0 人点赞