Advertisement
ZergRushA

rt-ai-6

Dec 1st, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.02 KB | None | 0 0
  1. #Task 1
  2. '''
  3. def qZ(x, y):
  4.    return (x - 3*y - 2) / ((x**2) + (y**2) + 1)
  5. def sumZ(Z):
  6.    return sum(Z)
  7. def exchangeScheme(pre_x, pre_y, sorted_id):
  8.    X = [0 for _ in range(4)]
  9.    Y = [0 for _ in range(4)]  
  10.    X[0] = pre_x[sorted_id[0]]
  11.    X[1] = pre_x[sorted_id[1]]    
  12.    X[2] = pre_x[sorted_id[2]]    
  13.    X[3] = pre_x[sorted_id[2]]    
  14.    
  15.    Y[0] = pre_y[sorted_id[2]]
  16.    Y[1] = pre_y[sorted_id[2]]    
  17.    Y[2] = pre_y[sorted_id[0]]    
  18.    Y[3] = pre_y[sorted_id[1]]  
  19.    
  20.    return X, Y
  21. def sorting(Z):
  22.    sortedId = sorted(range(len(Z)), key=lambda k: Z[k])
  23.    return sortedId
  24. def evoStep(X, Y, Z):
  25.    _, minId = min((value, id) for (id, value) in enumerate(Z))
  26.    X = X[:]
  27.    Y = Y[:]    
  28.    Z = Z[:]    
  29.    X.pop(minId)
  30.    Y.pop(minId)
  31.    Z.pop(minId)
  32.    
  33.    return X, Y, Z
  34. def evoSteps(X, Y, steps_num = 4):
  35.    results = []
  36.    for i in range(steps_num):
  37.        arrZ = [qZ(x, Y[i]) for (i, x) in enumerate(X)]
  38.        X, Y, Z = evoStep(X, Y, arrZ)
  39.        X, Y = exchangeScheme(X, Y, sorting(Z))
  40.        results.append([X, Y, sumZ(arrZ), arrZ])
  41.    return X, Y, results
  42. X = [-1, 0, 2, 3]
  43. Y = [-2, 1, 0, -1]
  44. results = evoSteps(X, Y)
  45. for i in range(len(results[2])):
  46.    print(f"max_{i+1}_step: {results[2][i][2]}")
  47. qualityArrZ = []
  48. for i in range(len(results[2])):
  49.    qualityArrZ += results[2][i][3]
  50. print(f"max Z: {max(qualityArrZ)}")
  51.  
  52.  
  53.  
  54.  
  55. #Task 2
  56. import networkx as nx
  57. from math import e
  58.  
  59. 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)]
  60. V = [1,3,4,5,6,2,1]
  61. Z = [(4, 5), (5, 2), (6,3), (2,3)]
  62. P = [33,82,51,76]
  63. T = 100
  64.  
  65. def probability(delta, T):
  66.    return 100 * e**(-delta/T)
  67. def reductTemp(prevT):
  68.    nextT = 0.5*prevT
  69.    return nextT
  70. # graph = nx.Graph()
  71. # graph.add_weighted_edges_from(distances)
  72. # nx.draw_kamada_kawai(graph, node_color="#fb7258", node_size=2000, with_labels=True)
  73.  
  74. def edgeLength(i, j, distances, roundTrip=True):
  75.    if roundTrip:
  76.        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])
  77.    else:
  78.        return max([(item[2] if (item[0]==i and item[1]==j) else -1) for item in distances])
  79. def routeLength(V, distances):
  80.    edges = []
  81.    for i in range(len(V)-1):
  82.        edges.append(edgeLength(V[i], V[i+1], distances))
  83.    return sum(edges)
  84. def routeOneReplacement(arrV, Z, replacementByName=True):
  85.    decrement = 1 if replacementByName else 0
  86.    arrV[Z[0] - decrement], arrV[Z[1]-decrement] = arrV[Z[1]-decrement], arrV[Z[0]-decrement]
  87.    return arrV
  88. def routeReplacement(V, Z):
  89.    for z in Z:
  90.        V = routeOneReplacement(V, z)
  91.    return V
  92. def chooseRoute(distances, V,Z,T,P):
  93.    sumLength = routeLength(V, distances)
  94.    arrSum = [sumLength]
  95.    
  96.    for i in range(len(Z)):
  97.        newV = routeOneReplacement(V[:], Z[i])
  98.        newS = routeLength(newV, distances)
  99.        arrSum.append(newS)
  100.        deltaS = newS-sumLength
  101.        if deltaS > 0:
  102.            p = probability(deltaS, T)
  103.            if p > P[i]:
  104.                V = newV
  105.                sumLength = newS
  106.        else:
  107.            V = newV
  108.            sumLength = newS
  109.        T = reductTemp(T)
  110.    return V, arrSum
  111. def drawRouteGraph(distances, bestRoute):
  112.    newDistances = []
  113.    for i in range(len(bestRoute)-1):
  114.        for distance in distances:
  115.            if distance[0] == bestRoute[i] and distance[1] == bestRoute[i+1] or distance[1] == bestRoute[i] and distance[0]==bestRoute[i+1]:
  116.                newDistances.append(distance)
  117.    graph = nx.Graph()
  118.    graph.add_weighted_edges_from(newDistances)
  119.    nx.draw_kamada_kawai(graph, node_color="#fb7258", node_size=2000, with_labels=True)
  120.    
  121. bestRoute, arrLength = chooseRoute(distances, V,Z,T,P)
  122. print(f"best route: {bestRoute}")
  123. print(f"length of best route: {routeLength(bestRoute, distances)}")
  124. print(f"length of all routes: {arrLength}")
  125.  
  126. drawRouteGraph(distances, bestRoute)
  127. '''
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement