d=xlsread('Demand06.xlsx');
P=xlsread('Profit06.xlsx');
for w=1:2
    j=1;
    while diadromi(w,j)~=0
        j=j+1;
    end
    j=j-1;
    for k=2:j
        MM=diadromi(w,k);
        P(MM,:)=0;
        d(MM,:)=inf;
    end
end

for i=1:N
    for j=1:N
        if i~=j
            x=(Coordinates(i,1)-Coordinates(j,1))^2;
            y=(Coordinates(i,2)-Coordinates(j,2))^2;
            T(i,j)=sqrt(x+y);
            T(j,i)=T(i,j);
        else
            T(j,i)=inf;
        end
    end
end
kostosnew=zeros(1,1);
new_Q=zeros(1,1);
d(1)=inf;
j=1;
while diadromi(1,j)~=0
    j=j+1;
end
j=j-1;
for k=2:j
       DD=diadromi(1,k);
        P(DD,:)=0;
        d(DD,:)=inf;
end
%for h=1:1
    for i=1:25
        [M,I]=max(Apostasis(1,:));
        temp_Apostasis=Apostasis(1:1,1:N);
        
         f=I;
         [~,n]=max(P(:,1));
         [~,nn]=min(d(:,1));
  
        New_route=diadromi(1:1,1:N);
        New_apostasis=Apostasis(1:1,1:N);
        New_route(1,f)=n;%exchange
         %pp=xxx;
         New_route=[New_route(1:f) 0 New_route(f+1:end-1)]; %metakinw mia thesi dipla toy gia na ginei to 2-1 exchange 
       
         New_route(1,f+1)=nn;%vazw kai 2o pelati mesa sthn diadromi
         
         
          New_apostasis=[New_apostasis(1:f) 0 0 New_apostasis(f+2:end-1)];
          KK=diadromi(1,f-1);
          JJ=diadromi(1,f+1);
          New_apostasis(1,f)=T(KK,n);
          New_apostasis(1,f+1)=T(n,nn);
          New_apostasis(1,f+2)=T(nn,JJ);
         kostosnew(1,1)=sum(New_apostasis(1,:));
         New_Q=QQ(1:1,1:N);
         New_Q=[New_Q(1:f) 0 New_Q(f+1:end-1)];
         New_Q(1,f)=d(n);
         New_Q(1,f+1)=d(nn);
         new_Q(1,1)=sum(New_Q(1,:));%to athroisma twn newn Q
         New_P=PP(1:1,1:N);
         New_P=[New_P(1:f) 0 New_P(f+1:end-1)];
         New_P(1,f)=P(n);
         New_P(1,f+1)=P(nn);
         if kostosnew(1)<=200 & new_Q<=160
               kostos(1)=kostosnew(1,1)
              
               
               diadromi(1:1,1:N)=New_route(1:1,1:N);
               Apostasis(1:1,1:N)=New_apostasis(1:1,1:N);
               fa(i,:)=New_apostasis;
               fp(i,:)=New_P;
               fq(i,:)=New_Q;
               QQ(1:1,1:N)=New_Q(1:1,1:N);
               PP(1:1,1:N)=New_P(1:1,1:N);
              feasible_P(i,:)=sum(New_P(1,:));
               P(n, :)=0;
               P(nn,:)=0;
               d(nn,:)=inf;
               d(n,:)=inf;
               feasible_routes(i,:)=New_route;
               feasible_Q(i,:)=new_Q;
               feasible_K(i,:)=kostos(1);
               
             
         else 
            
             New_route=diadromi(1,1:N);
             Apostasis(1,1:N)=temp_Apostasis;
             New_Q=QQ(1,1:N);
             New_P=PP(1,1:N);
             kostos(1:1,1:N)=kostosnew(1,:);
             P(n,:)=0;
             P(nn,:)=0
             d(nn,:)=inf;
             d(n,:)=inf;
             
         end
         
         
      end