
NUMBER=1757;

a=2;                                                               
b=a+NUMBER-1;
ListBegin=num2str(a);
ListEnd=num2str(b);

load Duro.txt
load Conva.txt

fid = fopen('CNTRY.txt');
CA = textscan(fid,'%s','Delimiter','\n');
CNTRY=CA{1,1}(:,1);

load OPW.txt
load CF_Constraint.txt
load Buckets.txt


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Country=zeros(NUMBER,1);

for k=1:NUMBER                                                                              
    if strcmp(CNTRY(k),'SNAT')                                              % 1 -> SNAT
        Country(k,1)=1;
    elseif strcmp(CNTRY(k),'US')                                            % 2 -> USA
        Country(k,1)=2;       
    elseif strcmp(CNTRY(k),'EN')                                            % 3 -> ENGLAND
        Country(k,1)=3;
    elseif strcmp(CNTRY(k),'CA')                                            % 4 -> CANADA
        Country(k,1)=4;
    elseif strcmp(CNTRY(k),'AU')                                            % 5 -> AUSTRALIA
        Country(k,1)=5;
    elseif strcmp(CNTRY(k),'NZ')                                            % 6 -> NEW ZEALAND
        Country(k,1)=6;
    elseif strcmp(CNTRY(k),'SI')                                            % 7 -> SINGAPORE
        Country(k,1)=7;
    elseif strcmp(CNTRY(k),'SW')                                            % 8 -> SWEDEN
        Country(k,1)=8;
    elseif strcmp(CNTRY(k),'NO')                                            % 9 -> NORWAY
        Country(k,1)=9;
    elseif strcmp(CNTRY(k),'DE')                                            % 10 -> DENMARK
        Country(k,1)=10;
    elseif strcmp(CNTRY(k),'AS')                                            % 11 -> AUSTRIA
        Country(k,1)=11;
    elseif strcmp(CNTRY(k),'BE')                                            % 12 -> BELGIUM
        Country(k,1)=12;
    elseif strcmp(CNTRY(k),'GE')                                            % 13 -> GERMANY
        Country(k,1)=13;
    elseif strcmp(CNTRY(k),'FI')                                            % 14 -> FINLAND
        Country(k,1)=14;
    elseif strcmp(CNTRY(k),'FR')                                            % 15 -> FRANCE
        Country(k,1)=15;    
    elseif strcmp(CNTRY(k),'LX')                                            % 16 -> LUXEMBOURG
        Country(k,1)=16;
    elseif strcmp(CNTRY(k),'NL')                                            % 17 -> NETHERLANDS
        Country(k,1)=17;
    else
        disp('The following number is from an unregistered country!');
        disp(k);
    end                                                         
end

%---------------------------CountryIndicator-------------------------------

CountryIndicator=zeros(NUMBER,17);                                          

for k=1:NUMBER                                                     
    a=Country(k);                                                           
    CountryIndicator(k,a)=1;                                                
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


ub=ones(NUMBER,1);
lb=zeros(NUMBER,1);

Aeq=ones(1,NUMBER);                                                          
beq(1,1)=1;
Aeq(2,:)=Duro';                                              
beq(2,1)=Buckets(6,5);                                             
Aeq(3,:)=Conva';                                                                                                  
beq(3,1)=Buckets(6,6);    

A(1,:)=(CountryIndicator(:,1))';                                             
A(2,:)=(CountryIndicator(:,2))';                                             
A(3,:)=(CountryIndicator(:,3))';                                             
A(4,:)=(CountryIndicator(:,4))';                                             
A(5,:)=(CountryIndicator(:,5))';                                             
A(6,:)=(CountryIndicator(:,6))';                                             
A(7,:)=(CountryIndicator(:,7))';                                             
A(8,:)=(CountryIndicator(:,8))';                                             
A(9,:)=(CountryIndicator(:,9))';                                             
A(10,:)=(CountryIndicator(:,10))';                                           
A(11,:)=(CountryIndicator(:,11))';                                           
A(12,:)=(CountryIndicator(:,12))';                                           
A(13,:)=(CountryIndicator(:,13))';                                           
A(14,:)=(CountryIndicator(:,14))';                                          
A(15,:)=(CountryIndicator(:,15))';                                          
A(16,:)=(CountryIndicator(:,16))';                                          
A(17,:)=(CountryIndicator(:,17))';                                          
b(1,1)=0.1;                                                                 
b(2,1)=0.2;                                                                 
b(3,1)=0.05;                                                                
b(4,1)=0.05;                                                                
b(5,1)=0.05;                                                                
b(6,1)=0.05;                                                                
b(7,1)=0.05;                                                                
b(8,1)=0.05;                                                                
b(9,1)=0.05;                                                                
b(10,1)=0.05;                                                               
b(11,1)=0.05;                                                               
b(12,1)=0.05;                                                               
b(13,1)=0.15;                                                               
b(14,1)=0.05;                                                               
b(15,1)=0.1;                                                                
b(16,1)=0.05;                                                               
b(17,1)=0.05;             

A(18,:)=-(CF_Constraint');
b(18,1)=-(sum(Buckets(:,2)));

% Optimierung

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  1.

C=eye(NUMBER);
[x1,resnorm1]=lsqlin(C,OPW,A,b,Aeq,beq,lb,ub,OPW);

R=resnorm1
S=sum(x1)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  2.
% func=@(x)TransVol(x,OPW);
% [x1,fval1,exitflag1]=fmincon(func,OPW,A,b,Aeq,beq,lb,ub)
% 
% R=sum(abs(x1-OPW))
% S=sum(x1)

            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            