
Data = importdata('E8.txt');                %data
route = importdata('route.txt');            %route
routecost = importdata('routecost.txt');    %totalcost
cost = importdata('cost.txt');              %cost
Q = importdata('Q.txt');                    %route's quantity
w = importdata('waiting.txt');              %waiting time

N = Data(1,1);          %Number of Nodes
C = Data(2,1);          %Truck's Capacity
depot = 1;              %Depot

deletes = [1,2];
Data(deletes,:) = [];   %complete data

% sort data by the earliest time

[Esort,I] = sort(Data(:,5));   

new_Data(N,length(Data(1,:))) = zeros;  

for i = 1:N
    new_Data(i,:) = Data(I(i),:);      
end

% initialization

axis = [2,3];
c = new_Data(:,axis);       % coordinates

nodes = new_Data(:,1);      % nodes
d = new_Data(:,4);          % demand
e = new_Data(:,5);          % top window / earliest time
b = new_Data(:,6);          % bottom window / latest time
s = new_Data(:,7);          % sevice time

D(N,N) = zeros;             % distance table
 

for i = 1:N
       for j = 1:N
          if i ~= j
              D(i,j) = sqrt(((c(i,1)-c(j,1))^2+(c(i,2)-c(j,2))^2));     % Distance Cost calculation 
          else
              D(i,j) = inf;
          end 
       end
   end
   
LatestTime = b(depot);           % Latest time
T = 0;                           % number of trucks

H = length(route);         % Height
L = length(route(1,:));    % Length

for i = 1:H
    for j = 1:L
        if w(i,j) > 0 
            for z = 1:N
                if routecost(i,j-1) + D(route(i,j-1)+1,z) <= b(z)
                    if e(z)

[row,col] = find(w>0);
Z = length(row);
nt_w(N,N) = zeros;

for i = 1:Z
    nt_w(row(i),col(i)) = route(row(i),col(i));       %nodes with waiting time
    earliest(row(i),col(i)) = e(route(row(i),col(i))+1);
end


new_cost(N,N) = zeros;
new_route(N,N) = zeros;
new_Q(N,N) = zeros;
w(N,N) = zeros;

new_cost(1:TR,1:TC) = cost;
new_route(1:TR,1:TC) = route;
help_route = route;
%new_Q(1:TR,1:length(Q(1,:))) = Q;
w(1:TR,1:length(w(1,:))) = w;

L = 1;
i = i;

while L <= TR
    if i >= TC
        L = L + 1;
        i = 1;
    else
        i = i + 1;
    end
    if w(L,i) > 0
        
        for n = 1:TR
            for k = 1:length(route(L,:))
                if w(n,k) > 0                    
                    temp = D(help_route(L,i-1)+1,nt_w(n,k)+1);
                    temp_cost = routecost(L,i-1) + temp;
                    temp_costA = temp_cost + s(nt_w(n,k)+1);
                    temp_costB = temp_costA + D(nt_w(n,k)+1,route(L,i)+ 1) + s(help_route(L,i)+1);
                    if routecost(L,i) >= temp_costB && temp_costA > routecost(L,i-1)
                        num = max(Q(L,:));
                        if num + d(nt_w(n,k)+1) <= C
                            temp_R = [ new_route(L,1:i) nt_w(n,k) new_route(L,i+1:TC)];
                            new_route(L,1:length(temp_R)) = temp_R;
                        
                            temp_cost = [ new_cost(L,1:i) temp_costA temp_costB new_cost(L,i+2:TC)];
                            new_cost(L,1:length(temp_cost)) = temp_cost;
                                                
                            %temp_Q = [ new_Q(L,1:i) d(nt_w(n,k)+1) new_Q(L,i+1:TC)];
                            
                            w(L,i) = e(route(L,i)+1) - (temp_costA + D(nt_w(n,k)+1,route(L,i)+ 1));
                            %nt_w(n,k) = 0
                            
                        end
                    end
                end
            end
        end
    end
end



              
                    
                    
                    
                    
                    