tslipa

code

Apr 10th, 2021
977
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. import random as rnd
  4. import copy as cp
  5. from datetime import datetime as dt
  6.  
  7. G = nx.Graph()
  8. num_of_nodes = 20
  9. num_of_edges = 28
  10. m = 1500
  11. p = 0.98
  12. edges = [(6, 12), (9, 10), (0, 1), (0, 3), (0, 4), (0, 8), (2, 3), (2, 4),
  13.          (2, 8), (2, 9), (3, 6), (4, 5), (5, 6), (5, 9), (7, 8), (10, 13),
  14.          (10, 19), (11, 12), (11, 18), (11, 19), (10, 12), (12, 15), (13, 14),
  15.          (14, 16), (15, 16), (15, 18), (16, 19), (17, 18)]
  16. matrix = []
  17. a = []
  18. c = {}
  19. g_sum = 0
  20. t_max = 0.00045
  21.  
  22.  
  23. def generate_graph():
  24.     G.add_nodes_from(range(0, num_of_nodes))
  25.     G.add_edges_from(edges)
  26.  
  27.  
  28. def print_graph():
  29.     plt.subplot()
  30.     nx.draw_kamada_kawai(G, with_labels=True, font_weight='normal')
  31.     plt.savefig("a.png")
  32.  
  33.  
  34. def generate_matrix():
  35.     global g_sum
  36.     for row in range(0, num_of_nodes):
  37.         matrix_row = []
  38.         for col in range(0, num_of_nodes):
  39.             if col == row:
  40.                 matrix_row.append(0)
  41.             elif (3 * col + 2 * row) % 7 == 0:
  42.                 matrix_row.append(rnd.randrange(900, 1000, 1))
  43.             else:
  44.                 matrix_row.append(rnd.randrange(100))
  45.             g_sum = g_sum + matrix_row[col]
  46.         matrix.append(matrix_row)
  47.  
  48.  
  49. def fnc_a(graph, e, f) -> int:
  50.     a1 = 0
  51.  
  52.     for row in range(0, num_of_nodes):
  53.         for col in range(0, num_of_nodes):
  54.             try:
  55.                 sp = nx.shortest_path(graph, row, col)
  56.             except nx.NetworkXNoPath:
  57.                 return -1
  58.  
  59.             for k in range(1, len(sp)):
  60.                 if sp[k - 1] == e and sp[k] == f:
  61.                     a1 = a1 + matrix[row][col]
  62.                 elif sp[k - 1] == f and sp[k] == e:
  63.                     a1 = a1 + matrix[row][col]
  64.     return a1
  65.  
  66.  
  67. def fnc_c(i) -> int:
  68.     if i == 0 or i == 1:
  69.         return int(a[i] * 2.4 * m)
  70.     return int(a[i] * 1.9 * m)
  71.  
  72.  
  73. def print_edge_stats():
  74.     for index in range(0, num_of_edges):
  75.         print(str(edges[index]) + ";" + str(a[index]) + ";" + str(c[str(edges[index])]))
  76.  
  77.  
  78. def dot2():
  79.     how_many_good = 0
  80.     rnd.seed(dt.now())
  81.     for i in range(0, 200):
  82.         current_graph = cp.deepcopy(G)
  83.  
  84.         for edge_index in range (0, num_of_edges):
  85.             rand = rnd.uniform(0, 1)
  86.             if rand > p:
  87.                 e, f = edges[edge_index]
  88.                 current_graph.remove_edge(e, f)
  89.  
  90.         sum_e = 0
  91.         for edge_index in range(0, current_graph.number_of_edges()):
  92.             e, f = list(current_graph.edges)[edge_index]
  93.             a_current = fnc_a(current_graph, e, f)
  94.             if a_current == -1 or c[str(list(current_graph.edges)[edge_index])] / m <= a_current:
  95.                 sum_e = -1
  96.                 break
  97.             sum_e = sum_e + (a_current / (c[str(list(current_graph.edges)[edge_index])] / m - a_current)) # to ma być większe od zera
  98.  
  99.         if sum_e == -1:
  100.             continue
  101.  
  102.         t = sum_e / g_sum
  103.         print(str(t) + "  " + str(t_max))
  104.         if t < t_max:
  105.             how_many_good = how_many_good + 1
  106.  
  107.     print(how_many_good / 200)
  108.  
  109.  
  110. if __name__ == '__main__':
  111.     generate_graph()
  112.     print_graph()
  113.     generate_matrix()
  114.  
  115.     for edge in range(0, num_of_edges):
  116.         edge_e, edge_f = edges[edge]
  117.         a.append(fnc_a(G, edge_e, edge_f))
  118.  
  119.     # kropka 1:
  120.  
  121.     for edge in range(0, num_of_edges):
  122.         c[str(edges[edge])] = fnc_c(edge)
  123.  
  124.     print_edge_stats()
  125.  
  126.     # dot2()
  127.  
  128.     #testg = nx.Graph()
  129.     #testg.add_nodes_from(range(0, 4))
  130.     #testg.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
  131.     #print(list(nx.all_shortest_paths(testg, 0, 3)))
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×