写一段粒子群算法优化模糊pid
时间: 2023-10-13 09:27:19 浏览: 121
控制器的代码实现。
很高兴和您交流,关于您的问题,我可以提供以下代码实现:
首先,我们需要定义一个函数,用于计算粒子群优化后的参数值。具体代码如下:
```python
def PSO_fuzzy_pid(params, input_data, output_data, k):
#参数解析
k_p, k_i, k_d, l, r = params
#初始化偏移与误差值
e = [0, output_data[0] - k_p * input_data[0]]
delta = [0, 0]
#粒子群初始化
w_init = 0.7
c1 = 2
c2 = 2
num_particles = 20
num_iterations = 1000
#粒子位置与速度初始化
particles_pos = np.zeros((num_particles, 5))
particles_vel = np.zeros((num_particles, 5))
particles_best_pos = np.zeros((num_particles, 5))
particles_best_val = np.zeros(num_particles)
#全局最优值和位置
global_best_val = np.inf
global_best_pos = np.zeros(5)
#更新粒子与全局最优值
for i in range(num_particles):
particles_pos[i, 0] = k_p + np.random.uniform(-l, r)
particles_pos[i, 1] = k_i + np.random.uniform(-l, r)
particles_pos[i, 2] = k_d + np.random.uniform(-l, r)
particles_pos[i, 3] = w_init
particles_pos[i, 4] = c1 + c2
res = fuzzy_pid(particles_pos[i, :], input_data, output_data)
particles_best_pos[i, :] = particles_pos[i, :]
particles_best_val[i] = res
if res < global_best_val:
global_best_val = res
global_best_pos = particles_pos[i, :]
#开始迭代
for it in range(num_iterations):
for i in range(num_particles):
#更新速度
particles_vel[i, 0] = particles_pos[i, 3] * (particles_vel[i, 0] \
+ c1 * np.random.uniform(0, 1) * (particles_best_pos[i, 0] - particles_pos[i, 0]) \
+ c2 * np.random.uniform(0, 1) * (global_best_pos[0] - particles_pos[i, 0]))
particles_vel[i, 1] = particles_pos[i, 3] * (particles_vel[i, 1] \
+ c1 * np.random.uniform(0, 1) * (particles_best_pos[i, 1] - particles_pos[i, 1]) \
+ c2 * np.random.uniform(0, 1) * (global_best_pos[1] - particles_pos[i, 1]))
particles_vel[i, 2] = particles_pos[i, 3] * (particles_vel[i, 2] \
+ c1 * np.random.uniform(0, 1) * (particles_best_pos[i, 2] - particles_pos[i, 2]) \
+ c2 * np.random.uniform(0, 1) * (global_best_pos[2] - particles_pos[i, 2]))
particles_vel[i, 3] = w_init
particles_vel[i, 4] = c1 + c2
#更新位置
particles_pos[i, 0] += particles_vel[i, 0]
particles_pos[i, 1] += particles_vel[i, 1]
particles_pos[i, 2] += particles_vel[i, 2]
#控制超出范围
if(particles_pos[i,0] > r or particles_pos[i,0] < -l):
particles_pos[i, 0] = k_p + np.random.uniform(-l, r)
if(particles_pos[i,1] > r or particles_pos[i,1] < -l):
particles_pos[i, 1] = k_i + np.random.uniform(-l, r)
if(particles_pos[i,2] > r or particles_pos[i,2] < -l):
particles_pos[i, 2] = k_d + np.random.uniform(-l, r)
res = fuzzy_pid(particles_pos[i, :], input_data, output_data)
#更新粒子与全局最优值
if res < particles_best_val[i]:
particles_best_val[i] = res
particles_best_pos[i, :] = particles_pos[i, :]
if res < global_best_val:
global_best_val = res
global_best_pos = particles_pos[i, :]
return global_best_pos
```
接下来,我们需要定义一个模糊PID控制器的函数,用于计算控制器的响应值。具体代码如下:
```python
def fuzzy_pid(params, input_data, output_data):
#提取模糊PID控制器的参数
k_p, k_i, k_d, w, c = params
#初始化偏移与误差值
e = [0, output_data[0] - k_p * input_data[0]]
delta = [0, 0]
#指定每个参数的变化范围
k_p_range = [-0.01, 0.01]
k_i_range = [-0.001, 0.001]
k_d_range = [-0.1, 0.1]
#定义规则库
rules = [
[1, 1, 1, 1],
[1, 1, 1, 2],
[1, 1, 2, 1],
[1, 1, 2, 2],
[1, 2, 1, 1],
[1, 2, 1, 2],
[1, 2, 2, 1],
[1, 2, 2, 2],
[2, 1, 1, 1],
[2, 1, 1, 2],
[2, 1, 2, 1],
[2, 1, 2, 2],
[2, 2, 1, 1],
[2, 2, 1, 2],
[2, 2, 2, 1],
[2, 2, 2, 2]
]
#定义模糊逻辑
def fuzzy_logic(k, error):
#输入变量的隶属度函数
e_var = [fuzz.trimf(error, [e-k*e/2, e, e+k*e/2]) for e in np.linspace(-5, 5, 5)]
delta_var = [fuzz.trimf(delta, [d-k*d/2, d, d+k*d/2]) for d in np.linspace(-5, 5, 5)]
#规则库函数
rule_activation = np.fmin(e_var[rules[k][0]-1], delta_var[rules[k][1]-1])
rule_activation = np.fmin(rule_activation, fuzz.trimf(np.array([k_p]), k_p_range))
rule_activation = np.fmin(rule_activation, fuzz.trimf(np.array([k_i]), k_i_range))
rule_activation = np.fmin(rule_activation, fuzz.trimf(np.array([k_d]), k_d_range))
#输出变量的隶属度函数
res = fuzz.defuzz(np.linspace(-5, 5, 5), rule_activation, 'centroid')
return res
#计算控制器的输出值
res = [0, w*fuzzy_logic(0, e[1])]
for j in range(1, len(input_data)):
e.append(output_data[j-1] - k_p*input_data[j-1])
delta.append(e[-1] - e[-2])
output = w*fuzzy_logic(0, e[-1]) + c*fuzzy_logic(1, e[-1]) + w*fuzzy_logic(2, e[-1]) + c*fuzzy_logic(3, e[-1])
res.append(output)
#返回控制器的响应值
return np.sum(np.square(np.array(output_data)-np.array(res)))
```
最后,我们可以使用优化函数对模糊PID控制器的参数进行优化,并输出最优参数值。具体代码如下:
```python
input_data = [1, 2, 3, 4, 5]
output_data = [9, 12, 14, 16, 18]
params = PSO_fuzzy_pid([1, 0.1, 0.5, 0.7, 0.5], input_data, output_data, 0.1)
print("优化后的模糊PID控制器参数值为:", params)
```
上述代码中,使用粒子群算法优化一个输入序列为[1, 2, 3, 4, 5],输出序列为[9, 12, 14, 16, 18]的模糊PID控制器的参数,初始值分别为:比例系数k_p=1,积分系数k_i=0.1,微分系数k_d=0.5,权重参数w=0.7和常数c=0.5,变化范围为0.1。输出优化后的模糊PID控制器参数值,即可得到最优化模糊PID控制器的具体参数。
阅读全文