WaterlessStraw

Traveling Salesman

Feb 4th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. import random
  2. import math
  3. import pprint
  4. from matplotlib import pyplot as plt
  5.  
  6. def create_nodes(num_nodes, num_rows):
  7. elements = range(1, num_nodes + 1)
  8. return [random.sample(elements, num_nodes) for _ in range(num_rows)]
  9.  
  10. def mutate(table, node_table, mutate_probability, cross_probability):
  11. for next_id, row in enumerate(table, 1):
  12. nodes = len(row)
  13. # print
  14. # print "Original: ", row
  15. #mutation
  16. if random.random() > mutate_probability:
  17. mini, maxi = sorted(random.sample(range(nodes),2))
  18. row[mini:maxi+1] = row[mini:maxi+1][::-1]
  19. # print "After mutation: ", row
  20. # print "Between: ", mini, maxi
  21.  
  22. #crossover
  23. if random.random() > cross_probability:
  24. try:
  25. next_row = table[next_id]
  26. # print "Parent: ", next_row
  27. except IndexError:
  28. pass
  29. else:
  30. half_length = nodes//2
  31. mini = random.randint(0, half_length)
  32. maxi = mini + half_length - 1 + (nodes % 2)
  33.  
  34. crossed = [None] * nodes
  35. # print "Before crossed: ", row
  36. crossed[mini:maxi+1] = next_row[mini:maxi+1]
  37. # print "Cross with: ", crossed
  38. iterator = 0
  39. for element in row:
  40. if element in crossed:
  41. continue
  42. while mini <= iterator <= maxi:
  43. iterator += 1
  44. crossed[iterator] = element
  45. iterator += 1
  46. row[:] = crossed
  47. # print "After crossed: ", row
  48. # print "Between: ", mini, maxi
  49. def sample_best(table, node_table):
  50. t1, t2 = random.sample(table[1:], 2)
  51. # print "t1: ", t1
  52. # print "t2: ", t2
  53. return distance(t1, t2, node_table)
  54.  
  55. def distance(s1, s2, node_table):
  56. distance1 = sum_distances(s1, node_table)
  57. # print "Distance 1: ", distance1
  58. distance2 = sum_distances(s2, node_table)
  59. # print "Distance 2: ", distance2
  60.  
  61. if distance1 < distance2:
  62. return s1, distance1
  63. else:
  64. return s2, distance2
  65.  
  66. def sum_distances(strategy, node_table):
  67. dist = 0
  68. first_row, second_row = node_table
  69.  
  70. for idx_next_node, node1 in enumerate(strategy, 1):
  71. try:
  72. node2 = strategy[idx_next_node]
  73. except IndexError:
  74. node2 = strategy[0]
  75. dist += math.hypot(
  76. first_row[node2-1] - first_row[node1-1],
  77. second_row[node2-1] - second_row[node1-1])
  78.  
  79. return dist
  80.  
  81. def draw_graph(node_table, strategy):
  82. graphX = [node_table[0][index - 1] for index in strategy]
  83. graphY = [node_table[1][index - 1] for index in strategy]
  84.  
  85. plt.scatter(graphX, graphY)
  86. plt.plot(graphX, graphY)
  87. plt.show()
  88.  
  89.  
  90. def main(nodes=8, strategies=6, generations=100, mutateP=.7, crossP=.7):
  91. #create node locations
  92. node_table = create_nodes(nodes, 2)
  93. # for i in range(2):
  94. # print node_table[i]
  95.  
  96. #create first generation
  97. table = create_nodes(nodes, strategies)
  98. # for i in range(strategies):
  99. # print i
  100. # print table[i]
  101.  
  102. print "TOP MEN are looking through:"
  103. print strategies, "strategies in", generations, "generations with",
  104. print nodes, "nodes in each strategy..."
  105.  
  106. best_score = None
  107. for count in range(generations):
  108. mutate(table, node_table, mutateP, crossP)
  109. # crossover(table, node_table, crossP)
  110. strategy, score = sample_best(table, node_table)
  111.  
  112. if best_score is None or score < best_score:
  113. best_strategy = strategy
  114. best_score = score
  115.  
  116. if count % 100 == 0:
  117. print "Foraged", count, "berries"
  118. print "Best we got so far:", best_score, "with: ", best_strategy
  119.  
  120.  
  121. # if count % 2 == 0:
  122. # print "new table:"
  123. # print count
  124. # for i in range(strategies):
  125. # print table[i]
  126.  
  127.  
  128.  
  129. print "=========================================================================="
  130. print "Best we could find: ", best_score, "for strategy", best_strategy
  131.  
  132. draw_graph(node_table, best_strategy)
  133.  
  134. main()
Advertisement
Add Comment
Please, Sign In to add comment