%This is the main file to solve Self Optimizing Control Problems for Kaibel column case study
% Case 2.2 in paper to find optimal subset of measurement (i.e. full H)
% Case 2.4 in paper to select measurements from each segment (i.e. full H with structural constraints)
% Case 2.5 in paper to add extra measurements to a given set of measurements

load KaibelTempData.mat;
Wd = Wd/2;

[ny,nu]=size(Gy)
nd = size(Gdy,2);

bigHF = []; bigHFS = []; MeasVec = [];
for i = 5
    i
    bigM =8; 

% Case 2.2 : Optimal subset of measurements

%     StructCon = [];
%     Con =[];
%     [HF,lossF,HGyF,nmesF,ct]= cplex_miqp_full_structure_Kaibel(Gy,Gdy,Juu,Jud,Wd,Wn,i,bigM,StructCon,Con,[]);%bigX0(i,2:end)');
%     sset{i,1}=nonzeros(nmesF)';
%     LF(i-1) = lossF;
%     ctF(i-1) = ct;
%     HF(:,nmesF);
%     nmesF'
%     bigHF = [bigHF;i reshape(HF',1,ny*nu)];

% Case 2.4 : To select measurements from each segment

%         StructCon =[zeros(4,ny*nu) [ones(1,20) zeros(1,ny-20);...
%         zeros(1,20) ones(1,20) zeros(1,ny-40);...
%         zeros(1,40) ones(1,20) zeros(1,10) 1;...
%         zeros(1,60) ones(1,10) 0]];
%     Con=[1;1;1;1];
%  [HFS,lossFS,HGyFS,nmesFS,ct]= cplex_miqp_full_structure_Kaibel(Gy,Gdy,Juu,Jud,Wd,Wn,i,bigM,StructCon,Con,[]);%bigX0(i,2:end)');
%     ssetS{i,1}=nonzeros(nmesFS)';
%     LFS(i-1) = lossFS;
%     ctS(i-1) = ct;
%     HFS(:,nmesFS);
%     nmesFS'

% Case 2.5 : Including extra measurements to a given set of measurements
% In this case, {T12, T25, T45, T62} are taken as the given set of measurements
 
%     StructCon =[zeros(4,ny*nu) [zeros(1,11) 1 zeros(1,59);...
%         zeros(1,24) 1 zeros(1,46);...
%         zeros(1,44) 1 zeros(1,26);...
%         zeros(1,61) 1 zeros(1,9)]];
%     Con=[1;1;1;1];
%  [HFS,lossFS,HGyFS,nmesFS,ct]= cplex_miqp_full_structure_Kaibel(Gy,Gdy,Juu,Jud,Wd,Wn,i,bigM,StructCon,Con,[]);%bigX0(i,2:end)');
%     ssetS{i,1}=nonzeros(nmesFS)';
%     LFS(i-1) = lossFS;
%     ctS(i-1) = ct;
%     HFS(:,nmesFS);
%     nmesFS'
    
    
StructCon =[zeros(5,ny*nu) [zeros(1,11) 1 zeros(1,59);...
        zeros(1,24) 1 zeros(1,46);...
        zeros(1,44) 1 zeros(1,26);...
        zeros(1,61) 1 zeros(1,9);...
        ones(1,71)]];
    Con=[1;1;1;1;5];
 [HFS,lossFS,HGyFS,nmesFS,ct]= cplex_miqp_full_structure_Kaibel(Gy,Gdy,Juu,Jud,Wd,Wn,i,bigM,StructCon,Con,[]);%bigX0(i,2:end)');
    ssetS{i,1}=nonzeros(nmesFS)';
    LFS(i-1) = lossFS;
    ctS(i-1) = ct;
    HFS(:,nmesFS);
    nmesFS'

end

no = 2:i;