Advertisement
juxtapositions

Untitled

Mar 15th, 2023
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. V = [1,3,4,5,6,2,1]
  2. Z = [
  3.     (4,5),
  4.      (5,6),
  5.      (2,4),
  6.      (6,2)
  7. ]
  8. P = [78,24,63,17]
  9.  
  10. T = 100
  11.  
  12.  
  13. def probability(delta, T):
  14.     return 100 * e ** (-delta/T)
  15.  
  16. def reductTemp(prevT):
  17.     nextT = 0.1 * prevT
  18.     return nextT
  19.  
  20. def edgeLength(i,j,distances,roundTrip=True):
  21.     if roundTrip:
  22.         return max([(item[2] if (item[0] == i and item[1] == j) or (item[1] == i and item[0] == j) else -1)
  23.                  for item in distances])
  24.     else:
  25.         return max([(item[2] if (item[0] == i and item[1] == j) else -1) for item in distances])
  26.  
  27. def routeLength(V, distances):
  28.     edges = []
  29.     for i in range(len(V) - 1):
  30.         edges.append(edgeLength(V[i], V[i+1], distances))
  31.         return sum(edges)
  32.  
  33. def routeOneReplacement(arrV, Z, replacementByName = True):
  34.     decrement = 1 if replacementByName else 0
  35.     arrV[Z[0] - decrement], arrV[Z[1] - decrement] = arrV[Z[1] - decrement], arrV[Z[0] - decrement]
  36.     return arrV
  37.  
  38. def routeReplacement(V, Z):
  39.     for z in Z:
  40.         V = routeOneReplacement(V, z)
  41.     return V
  42.  
  43. def chooseRoute(distances,V,Z,T,P):
  44.     sumLength = routeLength(V, distances)
  45.     arrSum = [sumLength]
  46.     for i in range(len(Z)):
  47.         newV = routeOneReplacement(V[:], Z[i])
  48.         newS = routeLength(newV, distances)
  49.         arrSum.append(newS)
  50.         deltaS = newS - sumLength
  51.  
  52.         if deltaS > 0:
  53.             p = probability(deltaS, T)
  54.             if p > P[i]:
  55.                 V = newV
  56.                 sumLength = newS
  57.         else:
  58.             V = newV
  59.             sumLength = newS
  60.             T = reductTemp(T)
  61.     return V, arrSum
  62.  
  63. def drawRouteGraph(distances, bestRoute):
  64.     newDistances = []
  65.     for i in range(len(bestRoute)-1):
  66.        
  67.         for distance in distances:
  68.             if distance[0] == bestRoute[i] and distance[1] == bestRoute[i+1]\
  69.             or distance[1] == bestRoute[i] and distance[0] == bestRoute[i+1]:
  70.                 newDistances.append(distance)
  71.     graph = nx.Graph()
  72.     graph.add_weighted_edges_from(newDistances)
  73.     nx.draw_kamada_kawai(graph, node_color = '#fb7258', node_size = 2000, with_labels = True)
  74.  
  75.  
  76. data = [26,42,44,31,24,20,34,40,15,23,43,20,27,22,26]
  77. m = -1
  78. distances = []
  79. for i in range(1,6):
  80.     for j in range(1,6-i+1):
  81.         m += 1
  82.         distances.append((i, j+i, data[m]))
  83.  
  84.  
  85.  
  86. bestRoute, arrLength = chooseRoute(distances,V,Z,T,P)
  87.  
  88. print(f'Best choosen route: {bestRoute}')
  89. print(f'Length of best choosen route: {routeLength(bestRoute, distances)}')
  90. print(f'Length of all viewed routes: {arrLength}')
  91.  
  92. drawRouteGraph(distances, bestRoute)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement