?? parsrule.m
字號:
function [outFis,outTxtRuleList,errorStr]=parsrule(fis,inTxtRuleList,ruleFormat,lang)
%PARSRULE Parse fuzzy rules.
% This function parses the text that defines the rules (txtRuleList) for a
% fuzzy system (fis) and returns a FIS structure with the appropriate rule
% list in place. If the original input FIS structure, fis, has any rules
% initially, they are replaced in the new matrix fis2. Three different rule
% formats (indicated by ruleFormat) are supported: *verbose*, *symbolic*, and
% *indexed*. The default format is *verbose*. When the optional language
% argument (lang) is used, the rules are parsed in verbose mode, assuming the
% key words are in the language, lang. This language must be either
% 'english', 'francais', or 'deutsch'. The key language words in English are:
% IF, THEN, IS, AND, OR, and NOT.
%
% outFIS = PARSRULE(inFIS,inRuleList) parses the rules in the string
% matrix inRuleList and returns the updated FIS matrix outFIS.
%
% outFIS = PARSRULE(inFIS,inRuleList,ruleFormat) parses the rules using
% the given ruleFormat.
%
% outFIS = PARSRULE(inFIS,inRuleList,ruleFormat,lang) parses the
% rules in verbose mode assuming the key words are in the language, lang.
%
% 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 1994-2002 The MathWorks, Inc.
% $Revision: 1.29 $ $Date: 2002/04/19 02:49:30 $
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=' wenn ';
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,:);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -