⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parsrule.m

📁 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱 模糊逻辑工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
function [outFis,outTxtRuleList,errorStr]=parsrule(fis,inTxtRuleList,ruleFormat,lang)
%PARSRULE Parse fuzzy rules.
%   outFIS = PARSRULE(inFIS,inRuleList) parses the rules in the string
%   matrix inRuleList and returns the updated FIS matrix outFIS. Rules
%   are assumed to be in 'verbose' format.
%
%   outFIS = PARSRULE(inFIS,inRuleList,ruleFormat) parses the rules in
%   any of three formats: 'verbose', 'symbolic', or 'indexed'.
%
%   outFIS = PARSRULE(inFIS,inRuleList,ruleFormat,lang) parses the 
%   rules in verbose mode assuming the key words are in the language
%   given by lang, which must be either 'english', 'francais', or
%   'deutsch'. Key words are (in English) IF, THEN, IS, AND, OR, and NOT.
%
%   For example:
%
%           a=newfis('tipper');
%           a=addvar(a,'input','service',[0 10]);
%           a=addmf(a,'input',1,'poor','gaussmf',[1.5 0]);
%           a=addvar(a,'input','food',[0 10]);
%           a=addmf(a,'input',2,'rancid','trapmf',[-2 0 1 3]);
%           a=addvar(a,'output','tip',[0 30]);
%           a=addmf(a,'output',1,'cheap','trimf',[0 5 10]);
%           rule1='if service is poor or food is rancid then tip is cheap';
%           a=parsrule(a,rule1);
%           showrule(a)
%   
%   See also ADDRULE, RULEEDIT, SHOWRULE.

%   [outFIS,outRuleList,errorStr] = PARSRULE(...) returns the text version
%   of the newly-parsed rules. If there have been errors in the parsing,
%   the error message is returned in errorStr, and the # character is placed
%   at the beginning of the offending line in outRuleList.
%
%   Ned Gulley, 4-27-94
%   Copyright (c) 1994-98 by The MathWorks, Inc.
%   $Revision: 1.22 $  $Date: 1997/12/01 21:45:11 $

if nargin<3,
    ruleFormat='verbose';
end

if nargin<4,
    lang='english';
end
outTxtRuleList=[];
errorStr=[];
if ~strcmp(ruleFormat,'indexed'),
    if strcmp(lang,'english'),
        ifStr=' if ';
        andStr='and';
        orStr='or';
        thenStr='then';
        equalStr=' is ';
        isStr=' is ';
        notStr='not';
    elseif strcmp(lang,'francais'),
        ifStr=' si ';
        andStr='et';
        orStr='ou';
        thenStr='alors';
        equalStr=' est ';
        isStr=' est ';
        notStr='n''est_pas';
    elseif strcmp(lang,'deutsch'),
        ifStr=' senn ';
        andStr='und';
        orStr='oder';
        thenStr='dann';
        equalStr=' ist ';
        isStr=' ist ';
        notStr='nicht';
    elseif strcmp(lang,'svenska'),
        ifStr=' om ';
        andStr='och';
        orStr='eller';
        thenStr='innebar_att';
        equalStr=' aer ';
        isStr=' aer ';
        notStr='inte';
    end
end

% Determine all the necessary constants
numInputs=length(fis.input);
numOutputs=length(fis.output);

for i=1:length(fis.input)
 numInputMFs(i)=length(fis.input(i).mf);
end
numOutputMFs=[];
for i=1:length(fis.output)
 numOutputMFs(i)=length(fis.output(i).mf);
end

if isempty(inTxtRuleList),
    outFis=setfis(fis,'ruleList',[]);
    return
end

% Remove any rows that are nothing but blanks
index=find(inTxtRuleList==0);
inTxtRuleList(index)=32*ones(size(index));
inTxtRuleList(all(inTxtRuleList'==32),:)=[];
if isempty(inTxtRuleList),
    outFis=setfis(fis,'ruleList',[]);
    return
end

% Replace all punctuation (and zeros) with spaces (ASCII 32)
inTxtRuleList2=inTxtRuleList;
index=[
    find(inTxtRuleList2==',')
    find(inTxtRuleList2=='#')
    find(inTxtRuleList2==':')
    find(inTxtRuleList2=='(')
    find(inTxtRuleList2==')')];
inTxtRuleList2(index)=32*ones(size(index));
inTxtRuleList2(all(inTxtRuleList2'==32),:)=[];

if isempty(inTxtRuleList2),
    outFis=setfis(fis,'ruleList',[]);
    return
end

inTxtRuleList2=setstr(inTxtRuleList2);
numRules=size(inTxtRuleList2,1);

if strcmp(ruleFormat,'symbolic'),
    symbFlag=1;
else
    symbFlag=0;
end

if strcmp(ruleFormat,'indexed'),
    ruleList=zeros(numRules,numInputs+numOutputs+2);
    errRuleIndex=[];
    goodRuleIndex=[];
    errorFlag=0;
    for ruleIndex=1:numRules,
        str=['[' inTxtRuleList2(ruleIndex,:) ']'];
        rule=eval(str,'[]');
        if isempty(rule),
            errorStr=['Rule did not evaluate properly.'];
            if nargout<3,
                error(errorStr);
            else
                errorFlag=1;
            end
        elseif length(rule)<(numInputs+numOutputs)+2,
            errorStr=['Not enough columns for this rule. The rule is not complete.'];
            if nargout<3,
                error(errorStr);
            else
                errorFlag=1;
            end
        elseif length(rule)>(numInputs+numOutputs)+2,
            errorStr=['Too many columns for this rule.'];
            if nargout<3,
                error(errorStr);
            else
                errorFlag=1;
            end
        elseif any(abs(rule(1:numInputs))>numInputMFs),
            errorStr=['Input MF index is too high'];
            if nargout<3,
                error(errorStr);
            else
                errorFlag=1;
            end
        elseif any(abs(rule((1:numOutputs)+numInputs))>numOutputMFs),
            errorStr=['Output MF index is too high'];
            if nargout<3,
                error(errorStr);
            else
                errorFlag=1;
            end
        elseif rule(numOutputs+numInputs+1)>1 | rule(numOutputs+numInputs+1)<0,
            errorStr=['Rule weight must be between 0 and 1'];
            if nargout<3,
                error(errorStr);
            else
                errorFlag=1;
            end
        elseif rule(numOutputs+numInputs+2)~=1 & rule(numOutputs+numInputs+2)~=2,
            errorStr=['Fuzzy operator must be either 1 (AND) or 2 (OR)'];
            if nargout<3,
                error(errorStr);
            else
                errorFlag=1;
            end
        else
            ruleList(ruleIndex,:)=rule;
        end

        if errorFlag,
            errRuleIndex=[errRuleIndex ruleIndex];
        else
            goodRuleIndex=[goodRuleIndex ruleIndex];
        end
        errorFlag=0;
    end

else
    % symbolic format is first converted to verbose and then
    % everything is parsed as verbose text

    inLabels=lower(getfis(fis,'inLabels'));
    inMFLabels=lower(getfis(fis,'inMFLabels'));
    outLabels=lower(getfis(fis,'outLabels'));
    outMFLabels=lower(getfis(fis,'outMFLabels'));

    % String pre-processing
    % Use "lower" to make it case insensitive
    inTxtRuleList2=lower(inTxtRuleList2);

    antCode=zeros(numRules,numInputs);
    consCode=zeros(numRules,numOutputs);
    andOrCode=ones(numRules,1);
    wtCode=ones(numRules,1);

    errorFlag=0;
    errRuleIndex=[];
    goodRuleIndex=[];

    for ruleIndex=1:numRules,
        txtRule=inTxtRuleList2(ruleIndex,:);

        if symbFlag
            % If the rules are in symbolic format, a little pre-processing
            % will save the day

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -