daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Dec 9th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import pandas as pd
  3. import math
  4. from sklearn import cluster, datasets
  5. from operator import itemgetter
  6. import collections
  7. import tsp
  8. import adj
  9.  
  10. #from matplotlib import pyplot as plt
  11. data = pd.read_csv("2051a.csv")
  12. item = data['item'].tolist()
  13. f1 = data['X'].tolist()
  14. f2 = data['Y'].tolist()
  15. f3 = data['capacity'].tolist()
  16. VC = data['vehicle_capacity'].tolist()
  17. VC = VC[0]
  18.  
  19.  
  20. for i in range(0,len(item)):
  21.     item[i] = [i,f1[i],f2[i],f3[i]]
  22.  
  23. item = sorted(item, key=lambda tup: tup[3])
  24. item.reverse()
  25. print(item)
  26.  
  27. #決定分群數
  28. K = math.ceil(sum(f3)/70)
  29.  
  30. #決定K個質心
  31. seeds = item[:K]
  32. item1 = item[K:]
  33.  
  34. grav = [[],[]]
  35. for i in range(0,K):
  36.     grav[0].append(seeds[i][1])
  37.     grav[1].append(seeds[i][2])
  38.  
  39.  
  40.    
  41. count1 = 0
  42. distance = []
  43.  
  44. full = 0                      #總需求量
  45. for i  in range(0,len(item)):
  46.     full = full + item[i][3]
  47. #print(full)
  48.  
  49. def get_AllDistance(list1 , grav):                    #找出每個需求點與所有重心的距離
  50.     Q=[]
  51.     for i in range(0,len(list1)):
  52.             for j in range(0,K):
  53.                 Q.append(math.sqrt((list1[i][1]-grav[0][j])**2 + (list1[i][2]-grav[1][j])**2))
  54.    
  55.     distance = [(Q[i:i+K])  for i in range(0,len(Q),5)]
  56.     sort = []
  57.     for i in range(0,len(distance)):
  58.         sort.append(sorted(range(len(distance[i])), key=lambda k: distance[i][k]))
  59.  
  60.     return(distance,sort)
  61.  
  62. def get_distribute(sort1 , i1 , time):
  63.     temp = sort1[i][0 + time]
  64.     if (cap_list[temp] + item1[i1][3] <= 70):
  65.         cap_list[temp] = cap_list[temp] + item1[i1][3]
  66.         best.append(temp)
  67.         cluster_list[temp].append(item1[i1])
  68.     else:
  69.         time = time+1
  70.         get_distribute(sort1 , i1 ,time)
  71.  
  72. def get_Gravity(list1):
  73.     grav = [[],[]]
  74.     for i in range(0,len(list1)):
  75.         temp = 0
  76.         temp1 = 0
  77.         for j in range(0,len(list1[i])):
  78.             temp = list1[i][j][1]/len(list1[i]) + temp
  79.             temp1 = list1[i][j][2]/len(list1[i]) + temp1
  80.         grav[0].append(temp)
  81.         grav[1].append(temp1)
  82.    
  83.     return grav
  84.  
  85. def adj_cluster(distance_list):
  86.     adj_list = []
  87.     for i in range (0,len(distance_list)):
  88.         if ((distance_list[i][1]-distance_list[i][0])/distance_list[i][0] < 0.3 ):
  89.             print(distance_list[i])
  90.             adj_list.append(item1[i])
  91.     return adj_list
  92.  
  93.  
  94.  
  95.    
  96.    
  97.  
  98.        
  99.  
  100.  
  101. count = 0
  102. temp_cap = []
  103. while True:
  104.     distance,sort = get_AllDistance(item1,grav)
  105.     cap_list = [0]*K
  106.     best = []
  107.     cluster_list = [[] for _ in range(K)]
  108.  
  109.    
  110.  
  111.     if(count == 0):
  112.         for i in range (0,K):
  113.             cap_list[i] = seeds[i][3]
  114.             cluster_list[i].append(seeds[i])
  115.    
  116.     for i in range(0,len(sort)):
  117.         get_distribute(sort , i , 0)
  118.    
  119.     #print(cluster_list)
  120.  
  121.     grav = get_Gravity(cluster_list)
  122.     seeds =[]
  123.     for i in range(0,K):
  124.         temp = [i,grav[0][i],grav[1][i]]
  125.         seeds.append(temp)
  126.     item1 = item
  127.     #print(grav)
  128.     if(temp_cap == cap_list):
  129.         break
  130.     temp_cap = cap_list
  131.    
  132.    
  133.     count = count+1
  134.  
  135.  
  136.  
  137. final_distance , final_sort = get_AllDistance(item1 , grav)
  138. #print(final_distance,final_sort)
  139. for i in range(0,len(final_distance)):
  140.     final_distance[i].sort()
  141.  
  142.  
  143. adj_list = adj_cluster(final_distance)
  144. print(adj_list)
  145.  
  146. init_cluster = cluster_list
  147.  
  148. for i in range(0,len(adj_list)):
  149.     for j in range(0,len(cluster_list)):
  150.         for k in range(0,len(cluster_list[j])):
  151.             if(cluster_list[j][k][0] == adj_list[i][0]):
  152.                 del cluster_list[j][k]
  153.                 break
  154.  
  155. cluster_list = adj.FindMinDist(cluster_list,adj_list,final_sort,VC)
  156.  
  157. print(cluster_list)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top