% nearest neighbour 

Data = importdata('E8.txt');    % DATA

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
 
   % Total 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
   
   
   
   k = 0;                       %numerator na oume :P
   d_cost = D(depot,:);         %distance cost from depot to a node
   D(:,depot) = inf;
   latest_time = l(depot);
   l(depot) = 0;
   while max(nodes) ~= 0
       pos = depot;
       m = depot;
       k = k + 1;
       route(k,m) = nodes(depot);
       cost(k,m) = 0;
       routecost(k,m) = 0;       
       Q(k) = 0;
       w(k,m)=0;
       totalcost(k) = 0;
       a = 0;
       while a ~= 1 
           from = pos;
           [val,to] = min(D(from,:));
           if totalcost(k) + val <= l(to) && totalcost(k) + val + s(to) <= latest_time
               if Q(k) + d(to) <= C
                   if e(to) > totalcost(k) + val
                       m = m + 1;
                       route(k,m) = nodes(to);
                       w(k,m) = e(to) - totalcost(k);
                       cost(k,m) = val + (e(to) - totalcost(k))+s(to);
                       totalcost(k) = totalcost(k) + cost(k,m);
                       routecost(k,m) = totalcost(k);
                       Q(k) = Q(k) + d(to);
                       pos = to;
                       nodes(to) = 0;
                       l(to) = 0;
                       backup = D(depot,to);
                       D(:,to) = inf;
                   else 
                       m = m + 1;
                       route(k,m) = nodes(to);
                       w(k,m) = 0;
                       cost(k,m) = val + s(to);
                       totalcost(k) = totalcost(k) + cost(k,m);
                       routecost(k,m) = totalcost(k);
                       Q(k) = Q(k) + d(to);
                       pos = to;
                       nodes(to) = 0;
                       l(to) = 0;
                       backup = D(depot,to);
                       D(:,to) = inf;
                   end
               end
           elseif totalcost(k) > max(l) || Q(k) + min(d) > C || val == inf
               a = 1;
           else
               D(from,to) = inf;
           end
       end
       m = m + 1;
       route(k,m) = nodes(depot);
       cost(k,m) = d_cost(route(k,m-1)+1) + s(route(k,m-1)+1);
       totalcost(k) = totalcost(k) + cost(k,m);
       routecost(k,m) = totalcost(k);
   end
       
  

dlmwrite('route.txt',route)
dlmwrite('routecost.txt',routecost)
dlmwrite('cost.txt',cost)
dlmwrite('Q.txt',Q)
dlmwrite('waiting.txt',w) 