Advertisement
_who___

minimal spanning tree

May 17th, 2024
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.15 KB | None | 0 0
  1. from scipy.optimize import linprog
  2. import numpy as np
  3. import networkx as nx
  4. import matplotlib.pyplot as plt
  5. np.set_printoptions(threshold=np.inf)
  6.  
  7. def random_matrix(N, max_value):
  8.     graph = np.zeros((N, N))
  9.     for i in range(N):
  10.         edges = np.random.choice(np.delete(np.arange(N), i), 2, replace=False)
  11.         for j in edges:
  12.             graph[i, j] = np.random.randint(1, 100)
  13.  
  14.     return graph
  15.  
  16. def visualize_overlapping_graphs(graph1_array, graph2_array, node_color='pink', edge_color1='lightgray', edge_color2='black'):
  17.   graph1 = nx.from_numpy_array(graph1_array)
  18.   graph2 = nx.from_numpy_array(graph2_array)
  19.   combined_graph = nx.compose(graph1, graph2)
  20.   pos = nx.circular_layout(combined_graph, scale=0.8)  # Круговое расположение
  21.   nx.draw(combined_graph, pos, node_color=node_color, with_labels=False)
  22.   edges1 = list(graph1.edges())
  23.   nx.draw_networkx_edges(combined_graph, pos, edgelist=edges1, edge_color=edge_color1, width=2)
  24.   edges2 = list(graph2.edges())
  25.   nx.draw_networkx_edges(combined_graph, pos, edgelist=edges2, edge_color=edge_color2, width=1)
  26.   plt.show()
  27.  
  28. def visualize_overlapping_oriented_circular_graphs(graph1_array, graph2_array, node_color='pink', edge_color1='lightgray', edge_color2='black'):
  29.     from scipy.optimize import linprog
  30.     import numpy as np
  31.     import networkx as nx
  32.     import matplotlib.pyplot as plt
  33.     np.set_printoptions(threshold=np.inf)
  34.  
  35.     def random_matrix(N, max_value):
  36.         graph = np.zeros((N, N))
  37.         for i in range(N):
  38.             edges = np.random.choice(np.delete(np.arange(N), i), 2, replace=False)
  39.             for j in edges:
  40.                 graph[i, j] = np.random.randint(1, 100)
  41.  
  42.         return graph
  43.  
  44.     def visualize_overlapping_graphs(graph1_array, graph2_array, node_color='pink', edge_color1='lightgray',
  45.                                      edge_color2='black'):
  46.         graph1 = nx.from_numpy_array(graph1_array)
  47.         graph2 = nx.from_numpy_array(graph2_array)
  48.         combined_graph = nx.compose(graph1, graph2)
  49.         pos = nx.circular_layout(combined_graph, scale=0.8)  # Круговое расположение
  50.         nx.draw(combined_graph, pos, node_color=node_color, with_labels=False)
  51.         edges1 = list(graph1.edges())
  52.         nx.draw_networkx_edges(combined_graph, pos, edgelist=edges1, edge_color=edge_color1, width=2)
  53.         edges2 = list(graph2.edges())
  54.         nx.draw_networkx_edges(combined_graph, pos, edgelist=edges2, edge_color=edge_color2, width=1)
  55.         plt.show()
  56.  
  57.     def visualize_overlapping_oriented_circular_graphs(graph1_array, graph2_array, node_color='pink',
  58.                                                        edge_color1='lightgray', edge_color2='black'):
  59.         graph1 = nx.from_numpy_array(graph1_array, create_using=nx.DiGraph)
  60.         graph2 = nx.from_numpy_array(graph2_array, create_using=nx.DiGraph)
  61.         combined_graph = nx.compose(graph1, graph2)
  62.         pos = nx.circular_layout(combined_graph)  # Расположение с фиксированным seed
  63.         nx.draw(graph1, pos, node_color=node_color, with_labels=False, arrows=True)  # Убираем номера вершин
  64.         edges1 = list(graph1.edges())
  65.         nx.draw_networkx_edges(graph2, pos, edgelist=edges1, edge_color=edge_color1, width=2)
  66.         edges2 = list(graph2.edges())
  67.         nx.draw_networkx_edges(combined_graph, pos, edgelist=edges2, edge_color=edge_color2, width=1)
  68.         plt.show()
  69.  
  70.     N = 17
  71.  
  72.     d = random_matrix(N, 100)
  73.  
  74.     c = -d.flatten()
  75.  
  76.     b_eq = np.ones(N)
  77.     b_ub = np.ones(N)
  78.  
  79.     A_eq = np.zeros((N, N ** 2))
  80.  
  81.     for i in range(N):
  82.         a = np.zeros((N, N))
  83.         for j in range(N):
  84.             if d[i][j] != 0:
  85.                 a[i][j] = 1
  86.         a = a.flatten()
  87.         A_eq[i] = a
  88.  
  89.     A_ub = np.zeros((N, N * N))
  90.     for i in range(N):
  91.         for j in range(N):
  92.             if d[i][j] != 0:
  93.                 A_ub[i][j + i * N] = 1
  94.  
  95.     b_l = np.zeros(N ** 2)
  96.     b_u = d.flatten()
  97.     bounds = list(zip(b_l, b_u))
  98.  
  99.     res = linprog(c, b_ub=b_ub, bounds=bounds, A_ub=A_ub, A_eq=A_eq, b_eq=b_eq)
  100.     result = np.reshape(res.x, (N, N)) * d
  101.  
  102.     visualize_overlapping_graphs(d, result)
  103.     print(result)
  104.  
  105.     M = 500
  106.  
  107.     D = np.random.randint(50, 100, size=(N, N))
  108.     np.fill_diagonal(D, 0)
  109.     print(D)
  110.  
  111.     c2 = np.concatenate((-D.flatten(), np.zeros(N * 2)))
  112.  
  113.     b_eq2 = np.ones(N)
  114.     b_ub2 = np.array([M] * N)
  115.     print(b_ub2)
  116.     A_eq2 = np.zeros((N, N ** 2 + 2 * N))
  117.     for i in range(N):
  118.         a = np.zeros((N, N))
  119.         zh = np.zeros(2 * N)
  120.         for j in range(N):
  121.             if D[i][j] != 0:
  122.                 a[i][j] = 1
  123.                 zh[N + j] = 1
  124.         zh[i] = 1
  125.         a = np.concatenate((a.flatten(), zh))
  126.         A_eq2[i] = a
  127.     np.savetxt("A_eq.txt", A_eq2, fmt='%d')
  128.  
  129.     A_ub2 = np.zeros((N, N * N + 2 * N))
  130.     for i in range(N):
  131.         for j in range(N):
  132.             if D[i][j] != 0:
  133.                 A_ub2[i][j + i * N] = -1
  134.             A_ub2[i][j + N ** 2 + N] = -M
  135.     np.savetxt("A_ub.txt", A_ub2, fmt='%d')
  136.  
  137.     b_l2 = np.zeros(N ** 2 + 2 * N)
  138.     b_u2 = np.concatenate((D.flatten(), np.ones(2 * N)))
  139.     bounds2 = list(zip(b_l2, b_u2))
  140.     print(bounds2)
  141.  
  142.     res = linprog(c=c2, b_ub=b_ub2, bounds=bounds2, A_ub=A_ub2, A_eq=A_eq2, b_eq=b_eq2)
  143.     result = np.reshape(res.x[:N * N], (N, N)) * D
  144.  
  145.     print(result)
  146.     visualize_overlapping_oriented_circular_graphs(D, result)
  147.  
  148. N = 17
  149.  
  150. d = random_matrix(N, 100)
  151.  
  152. c = -d.flatten()
  153.  
  154. b_eq = np.ones(N)
  155. b_ub = np.ones(N)
  156.  
  157. A_eq = np.zeros((N, N**2))
  158.  
  159. for i in range(N):
  160.     a = np.zeros((N, N))
  161.     for j in range(N):
  162.         if d[i][j] != 0:
  163.             a[i][j] = 1
  164.     a = a.flatten()
  165.     A_eq[i] = a
  166.  
  167. A_ub = np.zeros((N, N*N))
  168. for i in range(N):
  169.     for j in range(N):
  170.         if d[i][j] != 0:
  171.             A_ub[i][j+i*N] = 1
  172.  
  173. b_l = np.zeros(N**2)
  174. b_u = d.flatten()
  175. bounds = list(zip(b_l, b_u))
  176.  
  177. res = linprog(c, b_ub=b_ub, bounds=bounds, A_ub=A_ub, A_eq=A_eq, b_eq=b_eq)
  178. result = np.reshape(res.x, (N, N)) * d
  179.  
  180. visualize_overlapping_graphs(d, result)
  181. print(result)
  182.  
  183.  
  184. M = 500
  185.  
  186. D = np.random.randint(50, 100, size=(N, N))
  187. np.fill_diagonal(D, 0)
  188. print(D)
  189.  
  190. c2 = np.concatenate((-D.flatten(), np.zeros(N*2)))
  191.  
  192. b_eq2 = np.ones(N)
  193. b_ub2 = np.array([M]*N)
  194. print(b_ub2)
  195. A_eq2 = np.zeros((N, N**2 + 2*N))
  196. for i in range(N):
  197.     a = np.zeros((N, N))
  198.     zh = np.zeros(2*N)
  199.     for j in range(N):
  200.         if D[i][j] != 0:
  201.             a[i][j] = 1
  202.             zh[N + j] = 1
  203.     zh[i] = 1
  204.     a = np.concatenate((a.flatten(), zh))
  205.     A_eq2[i] = a
  206. np.savetxt("A_eq.txt", A_eq2, fmt='%d')
  207.  
  208. A_ub2 = np.zeros((N, N*N+2*N))
  209. for i in range(N):
  210.     for j in range(N):
  211.         if D[i][j] != 0:
  212.             A_ub2[i][j+i*N] = -1
  213.         A_ub2[i][j + N**2+N] = -M
  214. np.savetxt("A_ub.txt", A_ub2, fmt='%d')
  215.  
  216. b_l2 = np.zeros(N**2 + 2*N)
  217. b_u2 = np.concatenate((D.flatten(), np.ones(2*N)))
  218. bounds2 = list(zip(b_l2, b_u2))
  219. print(bounds2)
  220.  
  221. res = linprog(c=c2, b_ub=b_ub2, bounds=bounds2, A_ub=A_ub2, A_eq=A_eq2, b_eq=b_eq2)
  222. result = np.reshape(res.x[:N*N], (N, N)) * D
  223.  
  224. print(result)
  225. visualize_overlapping_oriented_circular_graphs(D, result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement