Advertisement
Guest User

Untitled

a guest
May 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import copy
  2. #from collections import deque
  3. f = open("tabutata2.txt", "r")
  4.  
  5. dictofneighbours = {}
  6.  
  7. for line in f:
  8. if line.split()[0] not in dictofneighbours:
  9. lista = []
  10. lista.append([line.split()[1], line.split()[2]])
  11. dictofneighbours[line.split()[0]] = lista
  12. else:
  13. dictofneighbours[line.split()[0]].append([line.split()[1], line.split()[2]])
  14. if line.split()[1] not in dictofneighbours:
  15. lista = []
  16. lista.append([line.split()[0], line.split()[2]])
  17. dictofneighbours[line.split()[1]] = lista
  18. else:
  19. dictofneighbours[line.split()[1]].append([line.split()[0], line.split()[2]])
  20. f.close()
  21.  
  22. # print(dictofneighbours)
  23.  
  24. f = open("tabutata2.txt", "r")
  25. start_node = f.read(1)
  26. end_node = start_node
  27.  
  28. first_solution = []
  29.  
  30. visiting = start_node
  31.  
  32. distance_of_first_solution = 0
  33.  
  34. while visiting not in first_solution:
  35. minim = 10000
  36. for k in dictofneighbours[visiting]:
  37. if int(k[1]) < int(minim) and k[0] not in first_solution:
  38. minim = k[1]
  39. best_node = k[0]
  40.  
  41. first_solution.append(visiting)
  42. distance_of_first_solution = distance_of_first_solution + int(minim)
  43. visiting = best_node
  44.  
  45. first_solution.append(end_node)
  46.  
  47. position = 0
  48. for k in dictofneighbours[first_solution[-2]]:
  49. if k[0] == start_node:
  50. break
  51. position += 1
  52.  
  53. distance_of_first_solution = distance_of_first_solution + int(dictofneighbours[first_solution[-2]][position][1]) - 10000
  54.  
  55. # print(first_solution)
  56. # print(distance_of_first_solution)
  57. #print(dictofneighbours)
  58.  
  59.  
  60. def findneighbours(solution):
  61.  
  62. neighborhood_of_solutions = []
  63.  
  64. for n in solution[1:-1]:
  65. idx1 = solution.index(n)
  66. for kn in solution[1:-1]:
  67. idx2 = solution.index(kn)
  68. if n == kn:
  69. continue
  70.  
  71. _tmp = copy.deepcopy(solution)
  72. _tmp[idx1] = kn
  73. _tmp[idx2] = n
  74.  
  75. distance = 0
  76.  
  77. for k in _tmp[:-1]:
  78. next_node = _tmp[_tmp.index(k)+1]
  79. for i in dictofneighbours[k]:
  80. if i[0] == next_node:
  81. distance = distance + int(i[1])
  82. _tmp.append(distance)
  83.  
  84. if _tmp not in neighborhood_of_solutions:
  85. neighborhood_of_solutions.append(_tmp)
  86.  
  87. for sl in neighborhood_of_solutions:
  88. indexOfLastItemInTheList = len(sl)-1
  89.  
  90. neighborhood_of_solutions.sort(key=lambda x: x[indexOfLastItemInTheList])
  91. return neighborhood_of_solutions
  92.  
  93.  
  94. # neighborhood = findneighbours(first_solution)
  95. # print(first_solution)
  96. # print(neighborhood[0])
  97.  
  98.  
  99. count = 1
  100. solution = first_solution
  101. tabu_list = []
  102.  
  103. while count <= 4:
  104. neighborhood = findneighbours(solution)
  105. index_of_best_solution = 0
  106. best_solution = neighborhood[index_of_best_solution]
  107.  
  108. i = 0
  109. while i < len(best_solution):
  110.  
  111. if best_solution[i] != solution[i]:
  112. first_exchange_node = best_solution[i]
  113. second_exchange_node = solution[i]
  114. print(first_exchange_node, second_exchange_node)
  115. continue
  116.  
  117. # if [first_solution, second_exchange_node] not in tabu_list:
  118. # tabu_list.append([first_exchange_node, second_exchange_node])
  119. # solution = best_solution
  120. # print(solution)
  121. # else:
  122. # solution = neighborhood[index_of_best_solution+1]
  123. # print(solution)
  124.  
  125. count = count + 1
  126. print("e")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement