📄 ballbase.m
字号:
function U = ballbase(X34,BallRules)
% function U = ballbase(X34,BallRules)
%
% Ball rule base for Cart-Ball model
%
% X34 States 3 and 4 (Phi,dPhi) as a vector
% BallRules The rules selected as active, binary [1x9]
% U Control signal
%
% X34 may be a matrix generated by 'cart' for example,
% with one row for each input instance. The result
% is then a vector of control signals.
% Jantzen 943003
%
% April 1995: Rule base read from a text file instead of
% being directly declared in this function.
%
% Petur Snaeland.
% Revised Hordur Kvaran, 20.10.97
Phi = X34(:,1) ;
dPhi = X34(:,2) ;
load ballsets; % Load fuzzy set declarations for the ball
[hBaseFile,message] = fopen('ballbase.txt','rt+'); % Open the rule base text file
if (hBaseFile==-1), %A little error catching. In case not in correct directory
filepath=which('ballbase.txt');
if isempty(filepath),
doevent('MainExit');
error('Ballbase.txt not found in search path.');
else hBaseFile = fopen(filepath,'rt+');
end;
end;
RuleBase = fscanf(hBaseFile,'%s'); % Read all rule characters into one string
fclose(hBaseFile); % ... and then close the file again.
%
% The rules contained in RuleBase are in a continuous string, e.g. 'ISMSSTOOOISRMVRPHR...'
% Each rule therefore takes 9 characters. Use this to make up a two dimensional string
% matrix, where each line contains one rule, e.g. 'ISM SST OOO'.
%
Col(:,1) = RuleBase(1:9:length(RuleBase))'; % 1. character of ISL, ISM or ISR.
Col(:,2) = RuleBase(2:9:length(RuleBase))'; % 2. character of ISL, ISM or ISR.
Col(:,3) = RuleBase(3:9:length(RuleBase))'; % 3. character of ISL, ISM or ISR.
Col(:,4) = setstr(32)*ones(9,1); % Separate with a column of spaces
Col(:,5) = RuleBase(4:9:length(RuleBase))'; % 1. character of MVL, SST or MVR.
Col(:,6) = RuleBase(5:9:length(RuleBase))'; % 2. character of MVL, SST or MVR.
Col(:,7) = RuleBase(6:9:length(RuleBase))'; % 3. character of MVL, SST or MVR.
Col(:,8) = setstr(32)*ones(9,1); % Separate with a column of spaces
Col(:,9) = RuleBase(7:9:length(RuleBase))'; % 1. character of PHL, PSL, OOO, PSR or PHR.
Col(:,10) = RuleBase(8:9:length(RuleBase))'; % 2. character of PHL, PSL, OOO, PSR or PHR.
Col(:,11) = RuleBase(9:9:length(RuleBase))'; % 3. character of PHL, PSL, OOO, PSR or PHR.
%
% Now build the Rule matrix line by line by evaluating the rules one by one,
% e.g. by evaluating 'Rules(i,:) = [ISM SST OOO]'.
%
Rules = zeros(9,11);
for i=1:9,
eval([ 'Rules(i,:) = [' sprintf('%s',Col(i,:)) ']; '])
end;
%
% The following may look funny - it is designed for fast execution
% when the inputs are vectors.
%
N = size(Rules,1) ;
Allifs = zeros(length(Phi), N) ;
for I = 1:N,
Allifs(:,I) = fand(is(Phi, Rules(I,1:5)), is(dPhi, Rules(I,6:10))) ;
end ;
AllThens = Rules(:,11).*BallRules' ;
%
% Height defuzzification
%
index = find(BallRules);
Nums = Allifs(:,index) * AllThens(index) ; % Note the matrix product * ;
Dens = sum(Allifs(:,index)')' ;
if (find(Dens==0)),
MainPos = get(gcf,'Position');
MainX = MainPos(1); MainY = MainPos(2); MainW = MainPos(3); MainH = MainPos(4);
BoxW = 180; BoxH = 100;
posBox = [MainX+(MainW-BoxW)/2 MainY+(MainH-BoxH)/2 BoxW BoxH];
figure('Visible','on','NumberTitle','off','MenuBar','none', ...
'Name','Warning','Position',posBox,'Resize','off','Color',[.75 .75 .75] );
button('OK',[40 20 100 25],'delete(gcf)');
uicontrol('Style','text','String','The rulebase is incomplete','Position',[10 60 170 30], ...
'BackgroundColor',[.75 .75 .75],'HorizontalAlignment','center');
end;
U = Nums ./ Dens ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -