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

📄 ruleview.m

📁 交流 模糊控制 交流 模糊控制
💻 M
📖 第 1 页 / 共 3 页
字号:
    for i=1:numOutputs
         outRange(i,1:2)=fis.output(i).range;
    end
    totalRange = [inRange;outRange];
    totalRange = totalRange + [totalRange(:,1) == totalRange(:,2)] * [-eps +eps];
    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=str2double(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=str2double(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);
               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, ...
                'Xlim', totalRange(varIndex,:), 'Ylim', [-0.05 1.05], ...
                'XTick',[],'YTick',[], ...
                'Position',axPos);

            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);
                    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',totalRange(varIndex,:),'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, 'interpreter','none');
                    set(gca, 'Tag', ['output' num2str(varIndex-numInputs)]);
                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);
                        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, ...
                            'LineWidth',4, ...
                            'UserData',lineHndl, ...
                            'Color',[0 0 1]);
                    end
                end
            end

            if (ruleIndex>numRules) & (varIndex<=numInputs),
                %====================================
                % MOVEABLE INDEX LINES
                %====================================
                % Draw in moveable input index lines
                % This axes position covers all axes in this input column
 %       boxBtm= mainAxPos(2)+mainAxPos(4)-ruleIndex*(10*yInset+yInset2); 
%                axPos=[boxLft+xInset mainAxPos(2) boxWid-2*xInset mainAxPos(4)];
                axPos=[boxLft+xInset boxBtm+yInset boxWid-2*xInset ruleIndex*(10*yInset+yInset2)];
                colIndex=sum(numInputMFs(1:(varIndex-1)))+1;
                xMin=inRange(varIndex,1);
                xMax=inRange(varIndex,2);
                inputVal=inputVector(varIndex);
                set(gca, ...
                    'Units','pixel', ...
                    'Visible','off', ...                  
                    'XLim',[xMin xMax],'YLim',[0 1], ...
                    'Position',axPos);
                line([1 1]*inputVal,[0.01 1], ...
                    'LineWidth',0.5, ...
                    'Color',[1 0 0], ...
                    'ButtonDownFcn','ruleview #clickline', ...
                    'UserData',varIndex, ...
                    'Tag','indexline', ...
                    'LineStyle','-');
                % The following patch is used to allow click-anywhere 
                % positioning of the index line
                patchHndl=patch([xMin xMax xMax xMin xMin],[0 0 1 1 0],'black');
                set(patchHndl, ...
                    'ButtonDownFcn','ruleview #patchclick', ...
                    'FaceColor','none', ...
                    'EdgeColor','none');
                if ruleIndex==numRules+1, 
                    % Plot the variable name at the top of each column
                    varName=fis.input(varIndex).name;
                    title([varName ' = ' num2str(inputVal,3)], 'visible', 'on', 'fontSize', 8, 'interpreter','none')
                end

            end

            if (ruleIndex>numRules) & (varIndex>numInputs),
                %====================================
                % AGGREGATE MF PLOT
                %====================================
                varName=fis.output(varIndex-numInputs).name;
                mfColIndex=sum(numOutputMFs(1:(varIndex-numInputs-1)))+1;
                if mfColIndex<=size(xOut,2),
                    x=xOut(:,mfColIndex);
                else
                    x=zeros(size(arr,1),1);
                end
%                compStr=computer;
%                if compStr(1:2)=='PC',
%                    eraseMode='normal';
%                else
%                    eraseMode='background';
%                end     
%                xlabel(num2str(v(varIndex-numInputs),3), ...
%                    'FontSize',8,'EraseMode',eraseMode);
                if strcmp(fisType,'mamdani'),
                    % MAMDANI
                    xMin=outRange(varIndex-numInputs,1);
                    xMax=outRange(varIndex-numInputs,2);
                    set(gca, ...
                        'XLim',totalRange(varIndex,:),'YLim',[-0.1 1.1], ...
                        'XTick',totalRange(varIndex,:), ...
                        'FontSize',8, ...
                        'XColor','b','YColor','b')
                    xData=[xMin; x; xMax];
                    yData=[0; arr(:,varIndex-numInputs); 0];
                    MFHndlMat(ruleIndex,varIndex)=patch( ...
                        xData,yData,[.3 .3 1]);
                    line(v(varIndex-numInputs)*[1 1],[-0.05 1.05], ...
                        'Color',[1 0 0], ...
                        'MarkerSize',12, ...
                        'LineWidth',3)
                else
                    % SUGENO system
                    set(gca, ...
                        'XLim',outRange, ...
                        'YLim',[-0.05 1.05], ...
                        'XTick',outRange, ...
                        'XColor','black','YColor','black')
                    lineHndl=line(v(varIndex-numInputs)*[1 1],[-0.05 1.05], ...
                        'Color',[1 0 0], ...
                        'MarkerSize',15, ...
                        'LineWidth',2);

                    xData=orr(:,varIndex-numInputs)';
                    xData=[xData; xData; NaN*ones(size(xData))];
                    yData=arr(:,varIndex-numInputs)';
                    yData=[zeros(size(yData)); yData; NaN*ones(size(yData))];

                    MFHndlMat(ruleIndex,varIndex)=line(xData(:),yData(:), ...
                        'LineWidth',4, ...
                        'UserData',lineHndl, ...
                        'Color',[.3 .3 1]);
                end
            end
        end
    end

    % The UserData will contain the varIndex to simplify 
    % calculations later on.
    set(inputDispHndl, ...
        'Value',inputVector, ...
        'String',[' ' mat2str(inputVector,4)]);

    % Get handles to axes for plotting
    frameName='dataframe';
    dataFrmHndl=findobj(figNumber,'Type','uicontrol', ...
        'Style','frame','Tag',frameName);
    set(dataFrmHndl,'UserData',MFHndlMat);

    % Normalize all coordinates
    hndlList=findobj(figNumber,'Units','pixels');
    set(hndlList,'Units','normalized');
 
    set(inputDispHndl,'Enable','on');
    watchoff(figNumber)

elseif strcmp(action,'#clickline'),
    %====================================
    figNumber=gcf;
    set(figNumber,'WindowButtonMotionFcn','ruleview #dragline');
    set(figNumber,'WindowButtonUpFcn','ruleview #updateinputs');
    ruleview #dragline

elseif strcmp(action,'#dragline'),
    %====================================
    lineHndl=gco;
    axHndl=get(lineHndl,'Parent');
    textHndl1=get(axHndl,'Title');
    ptMat=get(axHndl,'CurrentPoint');
    x=ptMat(1,1);
    xLims=get(axHndl,'XLim');
    if (x < xLims(1)),
        x=xLims(1);
    elseif (x > xLims(2)),
        x=xLims(2);
    end
    set(lineHndl,'XData',[x x]);
    oldtext = get(textHndl1, 'String');
    stopn=find(oldtext=='=');
    set(textHndl1,'String',[oldtext(1:stopn),' ', num2str(x(1),3)]);
% Uncomment the following lines if you want to see continuous update
% during a line drag
%    ruleview #updateinputs
%    set(figNumber,'WindowButtonMotionFcn','ruleview #dragline');
%    set(figNumber,'WindowButtonUpFcn','ruleview #updateinputs');

elseif strcmp(action,'#updateinputs'),
    %====================================
    figNumber=gcf;
    oldfis=get(figNumber,'UserData');
    fis=oldfis{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

    if strcmp(get(gco,'Type'),'line'),
        % We're here because the moveable line indices have been moved
        lineHndl=gco;
        xData=get(lineHndl,'XData');
        varIndex=get(lineHndl,'UserData');
        inputDispHndl=findobj(figNumber,'Type','uicontrol','Tag','inputdisp');
        inputVector=get(inputDispHndl,'Value');
        inputVector(varIndex)=xData(1);
        set(inputDispHndl,'Value',inputVector);
        set(inputDispHndl,'String',[' ' mat2str(inputVector,4)]);    
    else
        % We're here because the input vector text field has been changed
        inputDispHndl=gco;

        % Error-checking
        % The backupInputVector is the previous (or safety) value
        backupInputVector=get(inputDispHndl,'Value');
        % Use try-catch eval statement to keep out ASCII trash

        newInputStr=get(inputDispHndl,'String');
        % We'll put the brackets in later; no point in dealing with the hassle
        index=[find(newInputStr=='[') find(newInputStr==']')];
        newInputStr(index)=32*ones(size(index));
        newInputStr=['[' newInputStr ']'];

        % Use eval try-catch to prevent really weird stuff...
        inputVector=eval(newInputStr,'backupInputVector');
        if length(inputVector)<numInputs,
            inputVector=backupInputVector;
        else
            inputVector=inputVector(1:numInputs);
        end

        for i=1:numInputs
         inRange(i, 1:2)=fis.input(i).range;
        end
        
        for count=1:numInputs,
            % Find the appropriate index line
            indexLineHndl=findobj(figNumber, ...
                'Type','line','Tag','indexline','UserData',count);
            textHndl=get(get(indexLineHndl,'Parent'),'Title');
            xLims=inRange(count,:);

⌨️ 快捷键说明

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