
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

axis = [2,3];
c = Data(:,axis);       %coordinates

nodes = Data(:,1);      %nodes
d = Data(:,4);          %demand
e = Data(:,5);          %top window / earliest time
l = Data(:,6);          %bottom window / latest time
s = Data(:,7);          %sevice time


D(N,N) = zeros;         %distance table
 
% Distance Cost calculation
   
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));
        else
            D(i,j) = inf;
        end
    end
end

TR = length(route);         % Total rows
TC = length(route(:,1));    % Total collumns

[row,col] = find(w>0);
Z = length(row);

for i = 1:Z
    nt_rel(row(i),col(i)) = route(row(i),col(i));       %nodes to relocate
    earliest(row(i),col(i)) = e(route(row(i),col(i))+1);
end


L = 1;
new_route = route;
new_cost = cost;
i=0;
T = length(route);
K = length(route(:,1));

while L <= T
    i = i + 1;
    if w(L,i) > 0
        for n = 1:T
            for j = 1:K
                if nt_rel ~= 0
                    check = routecost(L,i-1) + D(route(L,i-1)+1,nt_rel(n,j)+1)
                    if  check < e(nd_rel(n,j)+1)
                        wait = e(nd_rel(n,j)+1) - check;
                        if  wait < w(L,i)
                            new_route(L,i) = nt_rel(n,j);                            
                            new_cost(L,i) = check + s(new_route(L,i-1)+1) + wait;
                            new_cost(L,i+1) = new_cost(L,i) + D(new_route(L,i)+1,route(L,i+1)+1) + s(new_route(L,i)+1);                            
                            calc_cost(i) = new_cost(L,i) + new_cost(L,i+1) - (cost(L,i) + cost(L,i+1));                            
                        end
                        if calc_cost(i) < 0 
                            route(L,:) = new_route(L,:);
                            cost(L,:) = new_cost(L,:);
                        else
                            L=L+1;
                        end    
                    end
                end
            end
        end
    end
    if i == T | i < 11
        i = 0;
        L = L + 1;
    end
end
    
                   
        
         
        