写一段两输入单输出的模糊控制程序
时间: 2024-05-16 16:14:40 浏览: 166
matlab平台的模糊控制器,有两个输入一个输出
5星 · 资源好评率100%
以下是一个简单的两输入单输出的模糊控制程序示例,用于调节一个水槽中水位的高低。
首先,定义模糊集合和规则库:
```python
import numpy as np
import skfuzzy as fuzz
# 定义模糊集合
water_level = np.arange(0, 101, 1)
water_flow = np.arange(0, 11, 1)
water_out = np.arange(0, 11, 1)
# 定义隶属函数
water_level_lo = fuzz.trimf(water_level, [0, 0, 50])
water_level_md = fuzz.trimf(water_level, [0, 50, 100])
water_level_hi = fuzz.trimf(water_level, [50, 100, 100])
water_flow_lo = fuzz.trimf(water_flow, [0, 0, 5])
water_flow_md = fuzz.trimf(water_flow, [0, 5, 10])
water_flow_hi = fuzz.trimf(water_flow, [5, 10, 10])
water_out_lo = fuzz.trimf(water_out, [0, 0, 5])
water_out_md = fuzz.trimf(water_out, [0, 5, 10])
water_out_hi = fuzz.trimf(water_out, [5, 10, 10])
# 定义规则库
rule1 = np.fmin(water_level_lo, water_flow_lo)
rule2 = np.fmin(water_level_lo, water_flow_md)
rule3 = np.fmin(water_level_md, water_flow_lo)
rule4 = np.fmin(water_level_md, water_flow_md)
rule5 = np.fmin(water_level_md, water_flow_hi)
rule6 = np.fmin(water_level_hi, water_flow_md)
rule7 = np.fmin(water_level_hi, water_flow_hi)
out_lo = np.fmax(np.fmax(rule1, rule2), np.fmax(rule3, rule4))
out_md = np.fmax(np.fmax(rule5, rule6), rule7)
out_hi = np.fmax(np.fmax(rule1, rule6), np.fmax(rule2, rule5))
# 定义输出
out = np.zeros_like(water_out)
out[water_out == 0] = out_lo
out[water_out == 5] = out_md
out[water_out == 10] = out_hi
```
然后,输入两个变量,计算输出:
```python
# 输入水位和水流量
water_level_input = 75
water_flow_input = 8
# 计算隶属度
water_level_lo_degree = fuzz.interp_membership(water_level, water_level_lo, water_level_input)
water_level_md_degree = fuzz.interp_membership(water_level, water_level_md, water_level_input)
water_level_hi_degree = fuzz.interp_membership(water_level, water_level_hi, water_level_input)
water_flow_lo_degree = fuzz.interp_membership(water_flow, water_flow_lo, water_flow_input)
water_flow_md_degree = fuzz.interp_membership(water_flow, water_flow_md, water_flow_input)
water_flow_hi_degree = fuzz.interp_membership(water_flow, water_flow_hi, water_flow_input)
# 模糊推理
rule_activation = np.fmin(np.fmin(np.fmax(np.fmin(water_level_lo_degree, water_flow_lo_degree), np.fmin(water_level_lo_degree, water_flow_md_degree)), np.fmin(water_level_md_degree, water_flow_lo_degree)), np.fmin(water_level_md_degree, water_flow_md_degree))
out_activation = np.fmax(np.fmax(np.fmin(rule_activation, out_lo), np.fmin(np.fmax(rule_activation, out_md), out_md)), np.fmin(np.fmax(rule_activation, out_hi), out_hi))
# 计算输出
water_out_output = fuzz.defuzz(water_out, out_activation, 'centroid')
```
最后,输出结果:
```python
print('水位:', water_level_input)
print('水流量:', water_flow_input)
print('水出流量:', water_out_output)
```
输出结果示例:
```
水位: 75
水流量: 8
水出流量: 6.940114057866083
```
阅读全文