Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. from populacja import Populacja
  2. from scipy.spatial.distance import squareform, pdist
  3. import numpy as np
  4. from operator import itemgetter
  5. import csv
  6.  
  7. def generate_disntance_matrix(coordinates_array):
  8. dist_array = pdist(coordinates_array)
  9. dist_matrix = squareform(dist_array)
  10. return dist_matrix
  11.  
  12. file_name = 'medium_4.ttp'
  13. file = open(file_name, 'r')
  14. # print(file.read())
  15. problem_name = file.readline()
  16. print(problem_name)
  17. knapsack_data_type = file.readline()
  18. print(knapsack_data_type)
  19. dimension = file.readline()
  20. print(dimension)
  21. nr_of_items_line = file.readline()
  22. print(nr_of_items_line)
  23. capacity_of_k_line = file.readline()
  24. print(capacity_of_k_line)
  25. min_speed_line = file.readline()
  26. print(min_speed_line)
  27. max_speed_line = file.readline()
  28. print(max_speed_line)
  29. renting_ratio_line = file.readline()
  30. print(renting_ratio_line)
  31. edge_weight_type = file.readline()
  32. print(edge_weight_type)
  33. sht = file.readline()
  34. print(sht)
  35. location_list = []
  36. loc = file.readline()
  37. while (loc[0].isdigit()):
  38. loc = list(map(float, loc.split()))
  39. location_list.append(loc)
  40. loc = file.readline()
  41. items = file.readline()
  42. items_list = [list(map(float, items.split()))]
  43. item = file.readline()
  44. while bool(item) and item[0].isdigit():
  45. item = list(map(float, item.split()))
  46. items_list.append(item)
  47. item = file.readline()
  48. print(location_list)
  49. print()
  50. print(items_list)
  51. print()
  52. l_os = 0
  53. ww = []
  54. tour_size = 0 #trivial 8
  55. item_eff_meter = 0.0
  56. x_chance = 0.22 # trivial 0.3
  57. m_chance = 0.05 # trivial 0.18
  58. # items_list = [[1.0,100.0,3.0,3.0], [2.0,40.0,2.0,3.0],[3.0,40.0,1.0,3.0], [4.0,20.0,2.0,2.0], [5.0,20.0,2.0,4.0]]
  59. best_r = -11111111110
  60. l_os = 160
  61. tour_size = 100
  62. # d_arr = [[0,5,6,6], [5,0,5,6], [6,5,0,4],[6,6,4,0]]
  63. for tr in range(1):
  64. run_times = 500
  65.  
  66. dist_arr = np.array(location_list)[:,[1,2]]
  67. dist_arr = generate_disntance_matrix(dist_arr)
  68. sec_item_list =list(map(lambda x: [int(x[0]), x[1],x[2],int(x[3]), round(x[1]/x[2],5)],items_list))
  69. sec_item_list = sorted(sec_item_list, key=itemgetter(4))
  70. current_run = 0
  71. best_arr = []
  72. licznik = 0
  73. d = []
  74. for i in range(10):
  75. best_a = -10000000
  76. d= []
  77. current_run = 0
  78. best_array_one = []
  79. # pop = Populacja(l_os,
  80. # 4,
  81. # len(items_list),
  82. # 3,
  83. # 1.0,
  84. # max_speed_line.split()[-1],
  85. # min_speed_line.split()[-1], m_chance, x_chance, tour_size, items_list, location_list, d_arr,
  86. # sec_item_list, item_eff_meter)
  87. pop = Populacja(l_os,
  88. len(location_list),
  89. len(items_list),
  90. capacity_of_k_line.split()[-1],
  91. renting_ratio_line.split()[-1],
  92. max_speed_line.split()[-1],
  93. min_speed_line.split()[-1], m_chance, x_chance, tour_size, items_list, location_list, dist_arr,
  94. sec_item_list, item_eff_meter)
  95. # pop.pop[0].trasa = [4, 1, 3, 7, 8, 2, 6, 0, 5]
  96. # pop.pop[1].trasa = [4, 0, 1, 3, 8, 6, 2, 7, 5]
  97. # pop.pop[2].trasa = [4, 3, 7, 1, 8, 2, 0, 6, 5]
  98. # pop.pop[3].trasa = [4, 6, 1, 8, 7, 0, 2, 3, 5]
  99. # pop.pop[0].trasa = [0,2,1,3]
  100. # pop.pop[0].plecak = [-1,2,-1,1,-1]
  101. # pop.ocen_pop()
  102. # w = pop.pop[0].wartosc_funkcji
  103. # a = 0
  104. # print(w)
  105. # pop.pop[0].trasa = [4,0,3,2,1]
  106. # pop.pop[0].plecak = [1,4,2,3]
  107. # pop.pop[0].ocen2()
  108. # o = pop.pop[0].wartosc_funkcji
  109.  
  110. # print(a)
  111. # best_array_one.append(a)
  112. current_run = current_run + 1
  113. print(dist_arr)
  114. while current_run < run_times:
  115. print(str(i) + ' ' + str(current_run))
  116. print('start')
  117. licznik = licznik+1
  118. new_pop_list = []
  119. index = 0
  120. # print('ocena')
  121. pop.ocen_pop()
  122. # print('a')
  123. best, worst, aver, najl_osobnik = pop.get_best_worst_average()
  124. if best > best_a:
  125. best_a = best
  126. if best > best_r:
  127. best_r = best
  128. a = (best, aver, worst, najl_osobnik, best_a, best_r)
  129. # print(a)
  130. d = a
  131. # best_array_one.append(a)
  132. current_run = current_run + 1
  133. # best_arr.append(best_array_one)
  134. # print('selekcja')
  135. while index < l_os:
  136. # print(index)
  137. new_pop_list.append(pop.turniej())
  138. index = index + 1
  139. pop.pop = new_pop_list
  140. # print('mut')
  141. pop.mutuj_pop()
  142. # print('kross')
  143. pop.krosuj_pop()
  144. best_array_one.append(d)
  145. print('appending')
  146. ww.append(best_array_one[-1])
  147. with open('dd' +file_name+' 3item_eff_meter '+ str(item_eff_meter)+' los ' + str(l_os)+' x_chance ' + str(x_chance)+' m_chance ' + str(m_chance)+ ' tour_size '+ str(tour_size)+ ' run_times ' +str(run_times)+'.csv', 'w') as myfile:
  148. wr = csv.writer(myfile)
  149. wr.writerows(ww)
  150. # best_arr.append(best_array_one)
  151. # # print(best_arr)
  152. #
  153. # size = len(best_arr)
  154. # ind = 0
  155. # ret_arr = []
  156. # for row in best_arr[0]:
  157. # suma_najl = 0
  158. # suma_najg = 0
  159. # suma_avg = 0
  160. # suma_best = 0
  161. # s_best_best = 0
  162. # for lists in range(0,size-1):
  163. # suma_najl = suma_najl + best_arr[lists][ind][0]
  164. # val = best_arr[lists][ind][0]
  165. # check = [lists, ind,0]
  166. # print(suma_najg)
  167. # val2 = best_arr[lists][ind][2]
  168. # suma_najg = suma_najg + val2
  169. # check2 = [lists, ind, 2]
  170. # suma_avg = suma_avg + best_arr[lists][ind][1]
  171. # suma_best = suma_best + best_arr[lists][ind][4]
  172. # s_best_best = s_best_best + best_arr[lists][ind][5]
  173. # wyn = [suma_najl/size, suma_avg/size, suma_najg/size, suma_best/size, s_best_best/size]
  174. # ret_arr.append(wyn)
  175. # ind = ind + 1
  176. #
  177. # b_a = [item for sublist in best_arr for item in sublist]
  178. # with open(file_name+' 3item_eff_meter '+ str(item_eff_meter)+' los ' + str(l_os)+' x_chance ' + str(x_chance)+' m_chance ' + str(m_chance)+ ' tour_size '+ str(tour_size)+ ' run_times ' +str(run_times)+'.csv', 'w') as myfile:
  179. # wr = csv.writer(myfile)
  180. # wr.writerows(b_a)
  181. #
  182. #
  183. # with open(file_name+' 3avg item_eff_meter '+ str(item_eff_meter)+' los '+ str(l_os)+' x_chance ' + str(x_chance)+' m_chance ' + str(m_chance)+ ' tour_size '+ str(tour_size)+ ' run_times ' +str(run_times)+'.csv', 'w') as myfile:
  184. # wr = csv.writer(myfile)
  185. # wr.writerows(ret_arr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement