%% Comparison of IB and DIB
clc
clear all
close all

N = 8;

% ibtype = {[8] [4 4] [2 3 3] [1 2 2 3] [1 1 1 2 3] [1 1 1 1 1 3] [1 1 1 1 1 1 2]};   %input blocking type for different calc.DOF 1-7
% ibtype = {[8] [4 4] [1 2 5] [1 1 3 3] [1 1 1 2 3] [1 1 1 1 2 2] [1 1 1 1 1 1 2]};   %input blocking type for different calc.DOF 1-7
% ibtype = {[1 3 4] [4 3 1] [4 2 1 1] [1 1 2 4]};   % compar input blocking types with same DOF = 3,4
% ibtype = {[1 1 6] [1 2 5] [1 3 4] [2 2 4] [2 3 3]};   % compar input blocking types with same DOF = 3 a
% ibtype = {[6 1 1] [5 2 1] [4 3 1] [4 2 2] [3 3 2]};   % compar input blocking types with same DOF = 3 b
% ibtype = {[1 4 3]};   %input blocking type for ploting u differences


% dibtype = {[8] [7 2] [6 2 2] [5 2 2] [4 2 2 2 2] [3 2 2 2 2 2]};   %input blocking type for different calc.DOF 1-7 free last
% dibtype = {[8] [2 7] [2 2 6] [2 2 2 5] [2 2 2 2 4] [2 2 2 2 2 3]}; %input blocking type for different calc.DOF 1-7 free first
% dibtype = {[2 2 6] [2 3 5] [2 4 4]};   % compar input blocking types with same DOF = 4 a
dibtype = {[6 2 2] [5 3 2] [4 4 2]};   % compar input blocking types with same DOF = 4 b

% ibtype = {[1 7] [1 1 6] [1 1 1 5] [1 1 1 1 4] [1 1 1 1 1 3] [1 1 1 1 1 1 2]};   % 1 choise
% dibtype = {[8] [2 7] [2 2 6] [2 2 2 5] [2 2 2 2 4] [2 2 2 2 2 3]};  % 1 choise
% ibtype = {[7 1] [6 1 1] [5 1 1 1] [4 1 1 1 1] [3 1 1 1 1 1] [2 1 1 1 1 1 1]};   % 2 choise
% dibtype = {[8] [7 2] [6 2 2] [5 2 2] [4 2 2 2 2] [3 2 2 2 2 2]};   % 2
% choise




% run IB
% [IBobjval IBDOF d01IB d02IB nfull nred Nsim]= dif_mb_WCE(N,ibtype)

% run DIB
[DIBobjval DIBDOF d01DIB d02DIB nfull nred Nsim]= dif_delta_mb_WCE(N,dibtype)



%% save Nsim.mat nred objval
save(['MB_Nsim' num2str(Nsim) '.mat'])
%% calculation and plot sum of WCE using MPC simulation and information
%% from the end of Nsim for Input blocking

% Nsum = 10;  % lenght Nsum
% for Nplot = Nsim
% load(['MB_Nsim' num2str(Nplot) '.mat'])
% [IBobjvalsum] = sum_IB_WCE(d01IB,d02IB,nfull,nred,Nsim,Nsum,ibtype); 


% % Figure 1. WCE for a set of DOF using different input blocking 
% figure
% plot(IBDOF,IBobjvalsum,'LineWidth',2);
% xlabel('degrees of freedom (DOF)')
% ylabel('worst-case error (outputs)')
% title('Input blocking');
% hold on ;
% plot(IBDOF,IBobjvalsum,'r*');
% grid on;
% legend('IB','ibtype = ([8], [4 4], [2 3 3], [1 2 2 3], [1 1 1 2 3], [1 1 1 1 1 3], [1 1 1 1 1 1 2])')
% 
% % Figure 2.WCE for a set of DOF using different input blocking Zoom
% figure
% plot(IBDOF,IBobjvalsum,'LineWidth',2);
% xlabel('degrees of freedom (DOF)')
% ylabel('worst-case error (outputs)')
% title('Input blocking');
% hold on ;
% plot(IBDOF,IBobjvalsum,'r*');
% grid on;
% XLIM([3.9999999 7])
% legend('IB','ibtype = ( [1 2 2 3], [1 1 1 2 3], [1 1 1 1 1 3], [1 1 1 1 1 1 2])')
% 
% % Figure 3. Input blocking types for same degree of freedom – first free
% % Figure 4. Input blocking types for same degree of freedom – last free

% colors for plot

% G = cell(5,1);
% G{1,1} = 'b';
% G{2,1} = 'g';
% % G{3,1} = 'r*'; 
% G{3,1} = 'r';
% G{4,1} = 'c';
% G{5,1} = 'k';
% G{6,1} = 'y';
% x = 1:length(ibtype);
% 
% set(gca,'XLim',[0.5 5.5]); % rozsah pre ktory to chces robit
% set(gca,'XTick',[1:10]);
% % axe = ['[1 1 6]';'[1 2 5]';'[1 3 4]';'[2 2 4]';'[2 3 3]'];
% axe = ['[6 1 1]';'[5 2 1]';'[4 3 1]';'[4 2 2]';'[3 3 2]'];
% set(gca,'XTickLabel',axe);
% 
% % calculation for different IB types
% 
%     for i = 1:length(ibtype)
%         % ploting
%         hold on;
%         bar(x(i),IBobjvalsum(i), G{i,1});
%         xlabel('Input blocking type');
%         ylabel('worst-case error (outputs)');
% %         legend('IB = [1 3 4]','IB = [4 3 1]','IB = [4 2 1 1]','IB = [1 1 2 4]');
% %         legend('IB = [1 1 6]','IB = [1 2 5]','IB = [1 3 4]','IB = [2 2 4]','IB = [2 3 3]');
%         legend('IB = [6 1 1]','IB = [5 2 1]','IB = [4 3 1]','IB = [4 2 2]','IB = [3 3 2]');    
%         grid on;
%       
%     end
%        
% 
% 
% 
% end




%% calculation and plot sum of WCE using MPC simulation and information
%% from the end of Nsim for Delta Input blocking


Nsum = 10;  % lenght Nsum
for Nplot = Nsim
load(['MB_Nsim' num2str(Nplot) '.mat'])
[DIBobjvalsum] = sum_DIB_WCE(d01DIB,d02DIB,nfull,nred,Nsim,Nsum,dibtype); 


%% Figure 1. WCE for a set of DOF using different input blocking 
% Figure 2.WCE for a set of DOF using different input blocking Zoom
% figure
% plot(DIBDOF,DIBobjvalsum,'LineWidth',2);
% xlabel('degrees of freedom (DOF)')
% ylabel('worst-case error (outputs)')
% title('Delta input blocking');
% hold on ;
% plot(DIBDOF,DIBobjvalsum,'r*');
% grid on;
% xlim([1.999999999 7])
% legend('DIB','dibtype = ([8], [7 2], [6 2 2], [5 2 2 2], [4 2 2 2 2], [3 2 2 2 2 2])')  % last free 
% % legend('DIB','dibtype = ([8], [2 7], [2 2 6], [2 2 2 5], [2 2 2 2 4], [2 2 2 2 2 3])')    % first free


%% Figure 3. Input blocking types for same degree of freedom – first free
% Figure 4. Input blocking types for same degree of freedom – last free

% DOF = 4
% colors for plot
G = cell(5,1);
G{1,1} = 'b';
G{2,1} = 'g';
% G{3,1} = 'r*'; 
G{3,1} = 'r';
G{4,1} = 'c';
G{5,1} = 'k';
G{6,1} = 'y';
x = 1:length(dibtype);

set(gca,'XLim',[0.5 3.5]); % rozsah pre ktory to chces robit
set(gca,'XTick',[1:3]);
% axe = ['[2 2 6]';'[2 3 5]',;'[2 4 4]'];
axe = ['[6 2 2]';'[5 3 2]';'[4 4 2]'];
set(gca,'XTickLabel',axe);


% calculation for different DIB types

    for i = 1:length(dibtype)
     
        hold on;
        bar(x(i),DIBobjvalsum(i), G{i,1});
        xlabel('Delta input blocking type');
        ylabel('worst-case error (outputs)');
%         legend('DIB = [2 2 6]','DIB = [2 3 5]','DIB = [2 4 4]');
        legend('DIB = [6 2 2]','DIB = [5 3 2]','DIB = [4 4 2]');
        grid on;
    end




end

%% IB vs DIB


% Nsum = 10;  % lenght Nsum
% for Nplot = Nsim
% load(['MB_Nsim' num2str(Nplot) '.mat'])
% [DIBobjvalsum] = sum_DIB_WCE(d01DIB,d02DIB,nfull,nred,Nsim,Nsum,dibtype); 
% [IBobjvalsum] = sum_IB_WCE(d01IB,d02IB,nfull,nred,Nsim,Nsum,ibtype); 
% 
% 
% figure;
% plot(IBDOF,IBobjvalsum,'b');
% hold on;
% plot(IBDOF,IBobjvalsum,'*b');
% grid on;
% 
% plot(DIBDOF,DIBobjvalsum,'r');
% hold on;
% plot(DIBDOF,DIBobjvalsum,'*r');
% grid on;
% 
% legend('IB','ibtype = ([1 7], [1 1 6], [1 1 1 5], [1 1 1 1 4], [1 1 1 1 1 3], [1 1 1 1 1 1 2])', 'DIB', 'dibtype = ([8], [2 7], [2 2 6], [2 2 2 5], [2 2 2 2 4], [2 2 2 2 2 3])') % 1 choise
% % legend('IB','ibtype = ([7 1], [6 1 1], [5 1 1 1], [4 1 1 1 1], [3 1 1 1 1 1], [2 1 1 1 1 1 1])', 'DIB', 'dibtype = ([8], [7 2], [6 2 2], [5 2 2], [4 2 2 2 2], [3 2 2 2 2 2])') % 2 choise
% title('Comparison IB and DIB for different DOF');
% xlabel('DOF')
% ylabel('WCE')
% 
% figure;
% plot(IBDOF,IBobjvalsum,'b');
% hold on;
% plot(IBDOF,IBobjvalsum,'*b');
% grid on;
% 
% plot(DIBDOF,DIBobjvalsum,'r');
% hold on;
% plot(DIBDOF,DIBobjvalsum,'*r');
% grid on;
% 
% legend('IB','ibtype = ([1 1 6], [1 1 1 5], [1 1 1 1 4], [1 1 1 1 1 3], [1 1 1 1 1 1 2])', 'DIB', 'dibtype = ([8], [2 7], [2 2 6], [2 2 2 5], [2 2 2 2 4], [2 2 2 2 2 3])') % 1 choise
% % legend('IB','ibtype = ([6 1 1], [5 1 1 1], [4 1 1 1 1], [3 1 1 1 1 1], [2 1 1 1 1 1 1])', 'DIB', 'dibtype = ([8], [7 2], [6 2 2], [5 2 2], [4 2 2 2 2], [3 2 2 2 2 2])') % 2 choise
% title('Comparison IB and DIB for different DOF');
% xlim([3 7])
% ylabel('WCE')
% xlabel('DOF')
% 
% 
% end




