Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Task 1
- '''
- def qZ(x, y):
- return (x - 3*y - 2) / ((x**2) + (y**2) + 1)
- def sumZ(Z):
- return sum(Z)
- def exchangeScheme(pre_x, pre_y, sorted_id):
- X = [0 for _ in range(4)]
- Y = [0 for _ in range(4)]
- X[0] = pre_x[sorted_id[0]]
- X[1] = pre_x[sorted_id[1]]
- X[2] = pre_x[sorted_id[2]]
- X[3] = pre_x[sorted_id[2]]
- Y[0] = pre_y[sorted_id[2]]
- Y[1] = pre_y[sorted_id[2]]
- Y[2] = pre_y[sorted_id[0]]
- Y[3] = pre_y[sorted_id[1]]
- return X, Y
- def sorting(Z):
- sortedId = sorted(range(len(Z)), key=lambda k: Z[k])
- return sortedId
- def evoStep(X, Y, Z):
- _, minId = min((value, id) for (id, value) in enumerate(Z))
- X = X[:]
- Y = Y[:]
- Z = Z[:]
- X.pop(minId)
- Y.pop(minId)
- Z.pop(minId)
- return X, Y, Z
- def evoSteps(X, Y, steps_num = 4):
- results = []
- for i in range(steps_num):
- arrZ = [qZ(x, Y[i]) for (i, x) in enumerate(X)]
- X, Y, Z = evoStep(X, Y, arrZ)
- X, Y = exchangeScheme(X, Y, sorting(Z))
- results.append([X, Y, sumZ(arrZ), arrZ])
- return X, Y, results
- X = [-1, 0, 2, 3]
- Y = [-2, 1, 0, -1]
- results = evoSteps(X, Y)
- for i in range(len(results[2])):
- print(f"max_{i+1}_step: {results[2][i][2]}")
- qualityArrZ = []
- for i in range(len(results[2])):
- qualityArrZ += results[2][i][3]
- print(f"max Z: {max(qualityArrZ)}")
- #Task 2
- import networkx as nx
- from math import e
- distances = [(1,2,264), (1,3,41), (1,4,36),(1,5,22), (1,6,19), (2,3,21), (2,4,33), (2,5,33), (2,6,14), (3,4,27), (3,5,39), (3, 6, 23), (4, 5, 20), (4,6, 20), (5,6,19)]
- V = [1,3,4,5,6,2,1]
- Z = [(4, 5), (5, 2), (6,3), (2,3)]
- P = [33,82,51,76]
- T = 100
- def probability(delta, T):
- return 100 * e**(-delta/T)
- def reductTemp(prevT):
- nextT = 0.5*prevT
- return nextT
- # graph = nx.Graph()
- # graph.add_weighted_edges_from(distances)
- # nx.draw_kamada_kawai(graph, node_color="#fb7258", node_size=2000, with_labels=True)
- 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)
- bestRoute, arrLength = chooseRoute(distances, V,Z,T,P)
- print(f"best route: {bestRoute}")
- print(f"length of best route: {routeLength(bestRoute, distances)}")
- print(f"length of all routes: {arrLength}")
- drawRouteGraph(distances, bestRoute)
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement