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

N = 8;

% 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

% ibtype = {[1 1 1 2 3]};   % Big Example 1
% dibtype = {[2 2 3 4]};    % Big Example 1

% ibtype = {[1 1 1 1 2 2]};   % Big Example 2
% dibtype = {[2 2 2 3 3]};    % Big Example 2

ibtype = {[1 1 1 1 1 1 2]};   % Big Example 3
dibtype = {[2 2 2 2 2 3]};    % Big Example 3

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

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


% % comparing plot
figure;
plot(IBDOF,IBobjval,'b');
hold on;
plot(IBDOF,IBobjval,'*b');
grid on;

figure;
plot(DIBDOF,DIBobjval,'r');
hold on;
plot(DIBDOF,DIBobjval,'*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,IBobjval,'b');
% hold on;
% plot(IBDOF,IBobjval,'*b');
% grid on;
% 
% plot(DIBDOF,DIBobjval,'r');
% hold on;
% plot(DIBDOF,DIBobjval,'*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')

%% 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;
plot(IBDOF,IBobjvalsum)               %using nonscaling obj. fun
grid on;
hold on
end

% plotting (labels, title, legend..)
xlabel('DOF')
ylabel('worst case error (outputs)')
title('Input blocking sum')

%% 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;
plot(DIBDOF,DIBobjvalsum)               %using nonscaling obj. fun
grid on;
hold on
end

% plotting (labels, title, legend..)
xlabel('DOF')
ylabel('worst case error (outputs)')
title('Delta input blocking sum')

%% just plot plot sum of WCE for Nsim Input and Delta Input blocking

% for Nplot = Nsim
% 
% load(['Objsum_IB_Nsim' num2str(Nplot) '.mat'])
% figure
% plot(IBDOF,IBobjvalsum,'LineWidth',2)               %using nonscaling obj. fun
% hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% xlabel('DOF')
% ylabel('sum of worst case error (outputs)')
% title('Input blocking sum');
% 
% for Nplot = Nsim
% 
% load(['Objsum_DIB_Nsim' num2str(Nplot) '.mat'])
% figure
% plot(DIBDOF,DIBobjvalsum,'LineWidth',2)               %using nonscaling obj. fun
% hold on
% end
% 
% % plotting (labels, title, legend..)
% 
% xlabel('DOF')
% ylabel('sum of worst case error (outputs)')
% title('Delta input blocking sum');

%% save data
% save MBWCE_E1
% save MBWCE_E2
save MBWCE_E3

