%% Script for experimentations of the FRI methods with two fuzzy systems and two observations
% Read fuzzy system 1
fis(1)=ireadfis('system_1.fis');
% Read fuzzy system 2
fis(2)=ireadfis('system_2.fis');
% Read observation 1 and store values in temporary variables
[obs1,obsold1]=ReadObs3('observation_1.obs');
% Store observation values in arrays
obsa(1)=obs1;
obsolda{1}=obsold1;
% Delete temporary variables
clear obs1
clear obsold1
% Read observation 2 and store values in temporary variables
[obs2,obsold2]=ReadObs3('observation_2.obs');
% Store observation values in arrays
obsa(2)=obs2;
obsolda{2}=obsold2;
% Delete temporary variables
clear obs2
clear obsold2
% Implemented Fuzzy Rule Interpolation methods
Methods={'KH';'KHstabilized';'MACI';'IMUL';'ConsRelFuzz';'FIVE';...
    'VKK';'LESFRI';'FRIPOC';'VEIN';'GM'};
MethodsNo=size(Methods,1);
% For each system
for i=1:2
   % For each observation
   for j=1:2 
       % Plot antecedent partitions of the system
       InDispChar=DisplayAntecedent(fis(i),501);
       obs=obsolda{j};
       % Graphical representation of the observation
       DisplayObservation(obs,501,fis(i).input,...
           InDispChar.FigureHandle,InDispChar.RowNr,...
           InDispChar.ColNr,'-k',2);
       % Delete temporary variable
       clear obs
       % Resize the window
       h=InDispChar.FigureHandle;
       set(h,'Units','centimeters')
       pos=get(h,'Position');
       pos(3:4)=[17 4.5];
       set(h,'Position',pos);
       % For each method
       for k=1:MethodsNo
           % Plot consequent partitions of the system
           OutDispChar=DisplayConsequent(fis(i),501);
           % Set the default inference options
           params=DefaultInferenceOptions(Methods{k});
           % Inference
           if k==1 || k==2 || k==3 || k==4 || k==5 || k==7
               %----------------------------------------------------------------------
               % KH method, stabilized version of the KH method, MACI, IMUL, CRF,
               % VKK
               %----------------------------------------------------------------------
               % The KH, stabilized version of the KH method, MACI, IMUL methods
               % use the old-type observation structure.
               obs=obsolda{j};
               % Calculate the interpolated conclusion
               output=ievalfis(obs,fis(i),params.InterpolationType,params);
               
               % Graphical representation of the conclusion
               DisplayConclusionFromaCut(output.concl,...
                   params.InterpolationType,...
                   params.NumOfPoints,output.fis,...
                   OutDispChar.FigureHandle,OutDispChar.RowNr,...
                   OutDispChar.ColNr,'-k',2);
           elseif k==8 % LESFRI
               obs=obsa(j);
               % Calculate the interpolated conclusion
               output=ievalfis(obs,fis(i),params.InterpolationType,params);
               % Graphical representation of the conclusion
               DisplayConclusion(output.fis.InterpolatedConclusion,'LESFRI',...
                   params.NumOfPoints,output.fis,OutDispChar.FigureHandle,...
                   OutDispChar.RowNr,OutDispChar.ColNr,...
                   '-k',2);
           elseif k==9 % FRIPOC
               obs=obsa(j);
               % Calculate the interpolated conclusion
               output=ievalfis(obs,fis(i),params.InterpolationType,params);
               % Graphical representation of the conclusion
               DisplayInterpolatedPartFrompCut(output.fis.InterpolatedConclusion,...
                   output.fis.theta,fis(i).output,...
                   OutDispChar.FigureHandle,OutDispChar.RowNr,OutDispChar.ColNr,...
                   '-k',2);
               Name=['Consequent universe - ' params.InterpolationType ' method'];
               set(OutDispChar.FigureHandle,'Name',Name);
               set(OutDispChar.FigureHandle,'NumberTitle','off');
               set(OutDispChar.FigureHandle,'Color','white');
           elseif k==10 % VEIN
               obs=obsa(j);
               % Calculate the interpolated conclusion
               output=ievalfis(obs,fis(i),'VEIN',params);
               % Graphical representation of the conclusion
               DisplayConclusion(output.fis.InterpolatedConclusion,'VEIN',...
                   params.NumOfPoints,output.fis,OutDispChar.FigureHandle,...
                   OutDispChar.RowNr,OutDispChar.ColNr,...
                   '-k',2);
           elseif k==6 % FIVE
               % Modify the parameters of the sets conform the requirements of FIVE.
               % Cut that parts of the sets which fall outside the range of the linguistic
               % variable.
               fisf=ModifyMfParams4FIVE(fis(i));
               obs=obsolda{j};
               % Calculate the interpolated conclusion
               output=ievalfis(obs,fisf,params.InterpolationType,params);
               % Graphical representation of the conclusion
               DisplayConclusion(output.concl,'FIVE: ',...
                params.NumOfPoints,output.fis,OutDispChar.FigureHandle,...
                OutDispChar.RowNr,OutDispChar.ColNr,...
                '-k',2);
           elseif k==11 % GM: SCM+FERI+FPL
               obs=obsolda{j};
               % Calculate the interpolated conclusion
               output=ievalfis(obs,fis(i),'GM',params);
               % Graphical representation of the conclusion
               DisplayConclusion(output.concl,['GM: ' params.SetInterpolation ' - ' ...
                params.ConclusionPosition ' - ' params.SingleRuleReasoning],...
                params.NumOfPoints,output.fis,OutDispChar.FigureHandle,...
                OutDispChar.RowNr,OutDispChar.ColNr,...
                '-k',2);
           end % if k==...
           % Resize the windows
           h=OutDispChar.FigureHandle;
           set(h,'Units','centimeters')
           pos=get(h,'Position');
           pos(3:4)=[8.5 4.5];
           set(h,'Position',pos);
           % Delete temporary variable
           clear output
       end % for k=1:MethodsNo
   end % for j=1:2
end % for i=2:2