4. 模糊推理系统(FIS)编辑器使用

任务目标

使用MATLAB模糊逻辑工具箱,通过命令行脚本方式创建一个Mamdani型模糊推理系统(FIS),完成输入/输出变量定义、隶属度函数设置、模糊规则编写及推理方法配置,并导出系统以验证配置正确性。

环境依赖

MATLAB R2016b及以上版本,需要Fuzzy Logic Toolbox。本案例完全通过脚本实现,不依赖FIS图形编辑器的手动操作,便于批量修改与版本控制。

关键完整代码

% create_fis_demo.m
% 创建Mamdani型FIS:输入为误差e和误差变化率ec,输出为控制增量du

% 1. 创建FIS结构体
fis = newfis('speed_control', 'FISType', 'mamdani');

% 2. 添加输入变量
fis = addvar(fis, 'input', 'e', [-1 1]);      % 误差范围[-1,1]
fis = addvar(fis, 'input', 'ec', [-1 1]);     % 误差变化率范围[-1,1]

% 3. 添加输出变量
fis = addvar(fis, 'output', 'du', [-1 1]);    % 控制增量范围[-1,1]

% 4. 定义输入e的隶属度函数(三角形)
fis = addmf(fis, 'input', 1, 'NB', 'trimf', [-1.5 -1 -0.5]);
fis = addmf(fis, 'input', 1, 'NS', 'trimf', [-1 -0.5 0]);
fis = addmf(fis, 'input', 1, 'ZO', 'trimf', [-0.5 0 0.5]);
fis = addmf(fis, 'input', 1, 'PS', 'trimf', [0 0.5 1]);
fis = addmf(fis, 'input', 1, 'PB', 'trimf', [0.5 1 1.5]);

% 5. 定义输入ec的隶属度函数(高斯型)
fis = addmf(fis, 'input', 2, 'NB', 'gaussmf', [0.2 -1]);
fis = addmf(fis, 'input', 2, 'NS', 'gaussmf', [0.2 -0.5]);
fis = addmf(fis, 'input', 2, 'ZO', 'gaussmf', [0.2 0]);
fis = addmf(fis, 'input', 2, 'PS', 'gaussmf', [0.2 0.5]);
fis = addmf(fis, 'input', 2, 'PB', 'gaussmf', [0.2 1]);

% 6. 定义输出du的隶属度函数(三角形)
fis = addmf(fis, 'output', 1, 'NB', 'trimf', [-1.5 -1 -0.5]);
fis = addmf(fis, 'output', 1, 'NS', 'trimf', [-1 -0.5 0]);
fis = addmf(fis, 'output', 1, 'ZO', 'trimf', [-0.5 0 0.5]);
fis = addmf(fis, 'output', 1, 'PS', 'trimf', [0 0.5 1]);
fis = addmf(fis, 'output', 1, 'PB', 'trimf', [0.5 1 1.5]);

% 7. 添加模糊规则(共25条,覆盖全部组合)
ruleList = [
    1 1 1 1 1;   % e=NB & ec=NB → du=NB
    1 2 1 1 1;   % e=NB & ec=NS → du=NB
    1 3 2 1 1;   % e=NB & ec=ZO → du=NS
    1 4 2 1 1;
    1 5 3 1 1;
    2 1 1 1 1;
    2 2 2 1 1;
    2 3 2 1 1;
    2 4 3 1 1;
    2 5 3 1 1;
    3 1 2 1 1;
    3 2 2 1 1;
    3 3 3 1 1;
    3 4 4 1 1;
    3 5 4 1 1;
    4 1 3 1 1;
    4 2 3 1 1;
    4 3 4 1 1;
    4 4 4 1 1;
    4 5 5 1 1;
    5 1 4 1 1;
    5 2 4 1 1;
    5 3 5 1 1;
    5 4 5 1 1;
    5 5 5 1 1;
];
fis = addrule(fis, ruleList);

% 8. 设置推理方法
fis.defuzzMethod = 'centroid';   % 重心法解模糊
fis.andMethod = 'min';           % 与运算取小
fis.orMethod = 'max';            % 或运算取大
fis.impMethod = 'min';           % 蕴含取小
fis.aggMethod = 'max';           % 聚合取大

% 9. 保存FIS到工作区及文件
assignin('base', 'fis', fis);
writefis(fis, 'speed_control');  % 生成 speed_control.fis 文件

% 10. 验证:输出系统基本信息
fprintf('FIS名称: %s\n', fis.name);
fprintf('输入变量数: %d, 输出变量数: %d\n', ...
    numel(fis.input), numel(fis.output));
fprintf('规则数: %d\n', numel(fis.rule));
fprintf('解模糊方法: %s\n', fis.defuzzMethod);

运行方式

  1. 将上述代码保存为 create_fis_demo.m;
  2. 在MATLAB中切换到该文件所在目录,直接运行:
>> create_fis_demo

验证方法

运行后观察命令行输出,应显示:

FIS名称: speed_control
输入变量数: 2, 输出变量数: 1
规则数: 25
解模糊方法: centroid

同时当前目录下生成 speed_control.fis 文件。可进一步在命令行执行 fis = readfis('speed_control') 重新读取该文件,确认系统可被完整加载。若需可视化验证,可执行 ruleview('speed_control') 打开规则浏览器,拖动输入观察输出变化是否符合规则表预期(例如当e=NB且ec=NB时,du应为NB)。

关键说明

  • 规则表每行格式为 [e_index ec_index du_index weight AND_rule],其中权重固定为1,AND规则为1表示使用“与”连接;
  • 隶属度函数参数中,三角形函数 trimf 的三个参数为左右端点及顶点位置;高斯函数 gaussmf 的两个参数为σ和均值;
  • 本案例使用命令行方式创建FIS,与图形化FIS编辑器等效,且便于自动化测试与参数批量调整。