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

📄 ruleview.m

📁 最新模糊逻辑工具箱
💻 M
📖 第 1 页 / 共 4 页
字号:
    bottom=border+spacing;


   % The STATUS frame 
    top=bottom+btnHt;
    right=maxRight-border-2*btnWid-5*spacing;

    left=border+spacing;
    frmBorder=spacing;
    frmPos=[left-frmBorder bottom-frmBorder ...
        right-left+frmBorder*2 top-bottom+frmBorder*2]+[1 0 1 0];
    dataFrmHndl=uicontrol( ...
        'Style','frame', ...
        'Units','pixel', ...
        'Position',frmPos, ...
        'BackgroundColor',frmColor);



    %------------------------------------
    % The STATUS text window
    labelStr=['Opened system ' fisName ', ' num2str(numRules) ' rules'];
    name='status';
    pos=[left bottom right-left btnHt];
    hndl=uicontrol( ...
        'Style','text', ...
        'BackgroundColor',frmColor, ...
        'HorizontalAlignment','left', ...
        'Units','pixel', ...
        'Position',pos, ...
        'Tag',name, ...
        'String',labelStr);

    ruleview #update

    % Normalize all coordinates
    hndlList=findobj(figNumber,'Units','pixels');
    set(hndlList,'Units','normalized');
 
    % Uncover the figure
    set(figNumber, ...
        'Visible','on', ...
        'HandleVisibility','callback');
       
elseif strcmp(action,'#update'),
    %====================================
    figNumber=watchon;
    oldfis=get(figNumber,'UserData');
    fis=oldfis{1};
    % Find and destroy all axes if they exist, since we might be updating
    % a previously existing system
    axHndlList=findobj(figNumber,'Type','axes');
    delete(axHndlList);
    set(figNumber,'Units','pixel')

    inputDispHndl=findobj(figNumber,'Type','uicontrol','Tag','inputdisp');
    set(inputDispHndl,'Enable','off');

    % First some error-checking
    if isfield(fis, 'input')
     numInputs=length(fis.input);
    else
     numInputs=0;
    end
    if isfield(fis, 'output')
     numOutputs=length(fis.output);
    else
     numOutputs=0;
    end
    if (numInputs<1) | (numOutputs<1),
        statmsg(figNumber,'Need at least one input and one output to view rules');
        watchoff(figNumber)
        return
    end
    numRules=length(fis.rule);
    if numRules<1,
        statmsg(figNumber,'Need at least one rule to view rules');
        watchoff(figNumber)
        return
    end

    border=6; 
    spacing=6;
    btnHt=22;
    figPos=get(figNumber,'Position');
    maxRight=figPos(3);
    maxTop=figPos(4);
    axColor='black';

    bottom=border;
    top=bottom+2*btnHt+5*spacing;
    right=maxRight-border;
 
    % The mainAxPos is a border that defines where the rules will be displayed
    % Getting it the right size was mostly trial and error
    mainAxPos=[border top-bottom+border*6 maxRight-border*2 maxTop-top-border*10];

    % Now build all the appropriate axes
    % For each rule, there will be as many axes as (numInputs+numOutputs)
    ruleList=getfis(fis, 'ruleList');
    numRules=size(ruleList,1);
    if isfield(fis, 'input')
     numInputs=length(fis.input);
    else
     numInputs=0;
    end
    if isfield(fis, 'output')
     numOutputs=length(fis.output);
    else
     numOutputs=0;
    end
    for i=1:numInputs
      numInputMFs(i)=length(fis.input(i).mf);
    end
    for i=1:numOutputs
      numOutputMFs(i)=length(fis.output(i).mf);
    end
    
    
    for i=1:numInputs
         inRange(i, 1:2)=fis.input(i).range;
    end

    for i=1:numOutputs
         outRange(i,1:2)=fis.output(i).range;
    end
    totalVars=numInputs+numOutputs;
    fisType=fis.type;

    boxWid=max((1/totalVars)*(maxRight-border*2), 30);
    boxHt=max((1/(numRules+3))*(maxTop-top-border*2), 10);
    xInset=boxWid/5;
    yInset=boxHt/10;

    % Here we're precalculating all MF x and y values for plotting later
    numPts=101;
    pthndl=findobj(gcf, 'Tag', 'numdisp');
    temp=str2num(get(pthndl, 'String'));
    if ~isempty(temp) & temp>=101
      numPts=temp;
    end
    [xIn,yIn,xOut,yOut,R]=discfis(fis,numPts);
    inputVector1=get(inputDispHndl,'Value');
    inputValidFlag=0;
    % If the previous value of the input vector is still valid, use it.
    if length(inputVector1)==numInputs,
        if all(inputVector1'>inRange(:,1)) & all(inputVector1'<inRange(:,2)),
            inputValidFlag=1;
        end
    end
    if inputValidFlag,
        inputVector=inputVector1;
    else
        inputVector=mean(inRange');
    end

    % Do the actual FIS evaluation
    numPts=101;
    pthndl=findobj(gcf, 'Tag', 'numdisp');
    temp=str2num(get(pthndl, 'String'));
    if ~isempty(temp) & temp>=101
      numPts=temp;
    else
      set(pthndl, 'String', num2str(numPts));
    end
    [v,irr,orr,arr]=evalfis(inputVector,fis, numPts);
    axHndlMat=zeros(numRules+1,totalVars);
    if strcmp(fisType,'sugeno'),
        sugenoOutputRange=sugmax(fis);
    end

    for ruleIndex=1:(numRules+1),
%        boxBtm=(rem(numRules+1-ruleIndex,(numRules+1))/(numRules+1))*mainAxPos(4)+ ...
%            mainAxPos(2);
        xInset2=xInset*0.1;
        yInset2=yInset*0.2;
        boxBtm= mainAxPos(2)+mainAxPos(4)-ruleIndex*(boxHt+yInset2); 
        for varIndex=1:totalVars,
%            boxLft=max((rem(varIndex-1,totalVars)/totalVars)*mainAxPos(3)+mainAxPos(1), mainAxPos(1)+(varIndex-1)*(boxWid+xInset2));
            boxLft=(rem(varIndex-1,totalVars)/totalVars)*mainAxPos(3)+mainAxPos(1);
            if (varIndex==1) & (ruleIndex<=numRules),
                %====================================
                % RULE NUMBER
                %====================================
                % Every rule number has its own axis
                axPos=[boxLft+xInset2 boxBtm+yInset+yInset2 ...
                    xInset-2*xInset2 boxHt-2*yInset-2*yInset2];
                axes( ...
                    'Units','pixel', ...
                    'Box','on', ...
                    'Visible','off', ...
                    'XTick',[],'YTick',[], ...
                    'XLim',[-1 1],'YLim',[-1 1], ...
                    'Position',axPos, ...
                    'Tag', 'Axes');
               text(0,0,num2str(ruleIndex), ...
                    'ButtonDownFcn','ruleview #disprule', ...
                    'FontSize',8, ...
                    'Tag','rulelabel', ...
                    'HorizontalAlignment','center');
            end

            axPos=[boxLft+xInset boxBtm+yInset boxWid-2*xInset boxHt-2*yInset];
            axHndlMat(ruleIndex,varIndex)=axes( ...
                'Units','pixel', ...
                'Box','on', ...
                'XColor',axColor,'YColor',axColor, ...
                'XTick',[],'YTick',[], ...
                'Position',axPos, ...
                'Tag', 'Axes');

            if (ruleIndex<=numRules) & (varIndex<=numInputs),
                %====================================
                % INPUT MFs
                %====================================
                % Here we are plotting the input variable membership functions
                % R is the modified rule list returned by discfis.
                mfColIndex=abs(R(ruleIndex,varIndex));
                % Use abs because negative refers to the use of NOT
                

                if mfColIndex,
                    % Don't plot it if the index is zero
                    x=xIn(:,mfColIndex);
                    y=yIn(:,mfColIndex);
                    if R(ruleIndex,varIndex)<0,
                        % Invert the MF if a NOT has been applied
                        y=1-y;
                    end
                    line(x,y,'Color',[.5 .5 0]);
                    xMin=min(x);
                    xMax=max(x);
                    set(gca,'XLim',[xMin xMax],'YLim',[-0.05 1.05]);
                    xData=[xMin; x; xMax];
                    yData=min(irr(ruleIndex,varIndex),[0; y; 0]);
                    MFHndlMat(ruleIndex,varIndex)=patch( ...
                        xData,yData,[1 1 0]);
                end

                if ruleIndex==numRules, 
                    % Display the axis limits
                    set(gca,'XTick',[xMin xMax],'FontSize',8);
                end

            end

            if (ruleIndex<=numRules) & (varIndex>numInputs),
                %====================================
                % OUTPUT MFs
                %====================================
                % Here we are plotting the output variable membership functions
                % Remember the index may be negative
                outputVarIndex=varIndex-numInputs;
                % we have to subtract off the number of inputs since the
                % inputs are given before the outputs

                if ruleIndex==1, 
                    % Plot the variable name at the top of each column
                    varName=fis.output(varIndex-numInputs).name;
                    titleH=title([varName ' = ' num2str(v(varIndex-numInputs),3)], 'visible', 'on', 'fontSize', 8);
                end

                mfIndex=abs(ruleList(ruleIndex,varIndex));
                if mfIndex,
                    % Plot it only if the index is nonzero
                    mfColIndex=sum(numOutputMFs(1:(outputVarIndex-1)))+mfIndex;
                    if strcmp(fisType,'mamdani'),
                        % MAMDANI system
                        x=xOut(:,mfColIndex);
                        y=yOut(:,mfColIndex);
                        if R(ruleIndex,varIndex)<0,
                            % Invert the MF if a NOT has been applied
                            y=1-y;
                        end
                        xMin=min(x);
                        xMax=max(x);
                        set(gca,'XLim',[xMin xMax],'YLim',[-0.05 1.05]);
                        xData=[xMin; x; xMax];
                        yData=[0; orr(:,ruleIndex+(varIndex-numInputs-1)*numRules); 0];
                        MFHndlMat(ruleIndex,varIndex)=patch( ...
                            xData,yData,[.3 .3 1]);
                        line(x,y, ...
                             'Color',[0 0 .5]);
                    else
                        % SUGENO system
                        range=fis.output(varIndex-numInputs).range;
                        % The next line represents an educated guess at what the x axis
                        % limits should be
                        outRange=sugenoOutputRange(varIndex-numInputs,:);
                        outRange=outRange+0.1*(outRange(2)-outRange(1))*[-1 1];
                   %     outRange=.3*outRange;
                        outRange=eval(mat2str(outRange,4));
                        if outRange(1)==outRange(2)
                           outRange=outRange+[0 .1];
                        end
                        set(gca, ...
                            'XLim',outRange, ...
                            'YLim',[-0.05 1.05]);
                        xData2=[1 1]*orr(ruleIndex,varIndex-numInputs);
                        yData2=[0 1];
                        % The handle for the thin line will be carried by the fat line
                        lineHndl=line(xData2,yData2, ...
                            'LineWidth',5, ...
                            'LineStyle','-', ...
                            'Visible','on', ...
                            'Color',[.6 .8 .8]);
                        xData1=xData2;
                        yData1=[0 1]*arr(ruleIndex,varIndex-numInputs);
                        MFHndlMat(ruleIndex,varIndex)=line(xData1,yData1, ...

⌨️ 快捷键说明

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