%This is the main file to solve Self Optimizing Control Problems
% Find H with triangular structure
% as combinations of measurement subsets

% load 'EvaporatorData.mat'
load 'OriginalData.mat'
[ny,nu]=size(Gy)

for i =41
    i
    bigM = 2;
    bsy =[floor(ny/2) ny-floor(ny/2)];
    bsu = [nu/2 nu/2];
    StructCon =[zeros(1,ny*nu) zeros(1,floor(ny/2)) ones(1,ny-floor(ny/2)) zeros(1,nu*nu);...
        zeros(1,ny*nu) ones(1,floor(ny/2)) ones(1,ny-floor(ny/2)) zeros(1,nu*nu)];
    Con = [i-floor(i/2);i];
    [HBD,lossBD,HGyBD,nmesBD,ctd]= cplex_miqp_decentralized_triangular_final(Gy,Gdy,Juu,Jud,Wd,Wn,i,bsy,bsu,bigM,StructCon,Con,@HBlkDiag_congress);
    ssetBD{i,1}=nonzeros(nmesBD)';
    LBD(i) = lossBD;
    ctD(i) = ctd;
    HBD(:,nmesBD)
    
    bigM = 2;
    StructCon =[zeros(1,ny*nu) zeros(1,floor(ny/2)) ones(1,ny-floor(ny/2));...
    zeros(1,ny*nu) ones(1,floor(ny/2)) ones(1,ny-floor(ny/2))];
    Con = [i-floor(i/2);i];
    [HBDL,lossBDL,HGyBDL,nmesBDL,ctdl]= cplex_miqp_decentralized_less_final(Gy,Gdy,Juu,Jud,Wd,Wn,i,bsy,bsu,bigM,StructCon,Con,@HBlkDiag_congress);
    ssetBDL{i,1}=nonzeros(nmesBDL)';
    LBDL(i) = lossBDL;
    ctDL(i) = ctdl;
    HBDL(:,nmesBDL)
end
no = 2:i;

figure(10)
plot(no,LBD,'k-*')
hold on
plot(no,LBDL,'c:o');
% xlabel('No. of measurements');
% ylabel('Loss');
% legend('H Full','H Full Structure','H Block Diagonal','H Block Diagonal Less');

% semilogy(2:10,data(:,4),'b-^')
% hold on
% semilogy(2:10,data(:,1),'r-*')
% semilogy(2:10,data(:,2),'b:')
% semilogy(2:10,data(:,3),'k:x')
% axis tight;
% ylabel('Average Loss')
% xlabel('No. of measurements')
% legend('Exhaustive Search*','MIQP','Downwards BAB','PB_3');

% nc = 0;
% for zz = nu:ny
%     nc =  nchoosek(ny,zz);
%     ctes(zz) = nc*0.01;
% end
% syms x x1
% x = [x1];
% for l = 2:nu*ny
%     x = [x;sprintf('x%d',l)];
% end
% for l = 1:ny
%     x = [x;sprintf('y%d',l)];
% end
% for l = 1:nu*nu
%     x = [x;sprintf('z%d',l)];
% end


% figure(20)
% semilogy(no,ct(:,1),'r-o')
% hold on
% semilogy(no,ct(:,2),'b:*')
% semilogy(no,ct(:,3),'k-x')