Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- V = [1,3,4,5,6,2,1]
- Z = [
- (4,5),
- (5,6),
- (2,4),
- (6,2)
- ]
- P = [78,24,63,17]
- T = 100
- def probability(delta, T):
- return 100 * e ** (-delta/T)
- def reductTemp(prevT):
- nextT = 0.1 * prevT
- return nextT
- def edgeLength(i,j,distances,roundTrip=True):
- if roundTrip:
- return max([(item[2] if (item[0] == i and item[1] == j) or (item[1] == i and item[0] == j) else -1)
- for item in distances])
- else:
- return max([(item[2] if (item[0] == i and item[1] == j) else -1) for item in distances])
- def routeLength(V, distances):
- edges = []
- for i in range(len(V) - 1):
- edges.append(edgeLength(V[i], V[i+1], distances))
- return sum(edges)
- def routeOneReplacement(arrV, Z, replacementByName = True):
- decrement = 1 if replacementByName else 0
- arrV[Z[0] - decrement], arrV[Z[1] - decrement] = arrV[Z[1] - decrement], arrV[Z[0] - decrement]
- return arrV
- def routeReplacement(V, Z):
- for z in Z:
- V = routeOneReplacement(V, z)
- return V
- def chooseRoute(distances,V,Z,T,P):
- sumLength = routeLength(V, distances)
- arrSum = [sumLength]
- for i in range(len(Z)):
- newV = routeOneReplacement(V[:], Z[i])
- newS = routeLength(newV, distances)
- arrSum.append(newS)
- deltaS = newS - sumLength
- if deltaS > 0:
- p = probability(deltaS, T)
- if p > P[i]:
- V = newV
- sumLength = newS
- else:
- V = newV
- sumLength = newS
- T = reductTemp(T)
- return V, arrSum
- def drawRouteGraph(distances, bestRoute):
- newDistances = []
- for i in range(len(bestRoute)-1):
- for distance in distances:
- if distance[0] == bestRoute[i] and distance[1] == bestRoute[i+1]\
- or distance[1] == bestRoute[i] and distance[0] == bestRoute[i+1]:
- newDistances.append(distance)
- graph = nx.Graph()
- graph.add_weighted_edges_from(newDistances)
- nx.draw_kamada_kawai(graph, node_color = '#fb7258', node_size = 2000, with_labels = True)
- data = [26,42,44,31,24,20,34,40,15,23,43,20,27,22,26]
- m = -1
- distances = []
- for i in range(1,6):
- for j in range(1,6-i+1):
- m += 1
- distances.append((i, j+i, data[m]))
- bestRoute, arrLength = chooseRoute(distances,V,Z,T,P)
- print(f'Best choosen route: {bestRoute}')
- print(f'Length of best choosen route: {routeLength(bestRoute, distances)}')
- print(f'Length of all viewed routes: {arrLength}')
- drawRouteGraph(distances, bestRoute)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement