-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
66 lines (49 loc) · 1.55 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import random
random.seed(0) # For repeatability
from tools import *
MAX_DIS = 10 # maximum distance
MIN_DIS = 1 # minimum distance
def print_matrix(am, M, msg):
print(msg)
labels = [str(i) for i in range(1, M + 1)]
df = pd.DataFrame(am, columns=labels)
df.index = labels
print(df, end='\n\n')
def plot_graph(am, dis_m):
g = nx.DiGraph()
print_matrix(dis_m, len(dis_m), 'Distance Matrix')
for i in range(len(am)):
for j in range(i + 1, len(am[0])):
if am[i][j] == 1:
g.add_edge(i + 1, j + 1, weight=dis_m[i][j])
pos = nx.circular_layout(g)
edge_labels = {(u, v): d['weight'] for u, v, d in g.edges(data=True)}
nx.draw_networkx_nodes(g, pos)
nx.draw_networkx_edges(g, pos)
nx.draw_networkx_labels(g, pos)
nx.draw_networkx_edge_labels(g, pos, edge_labels=edge_labels)
plt.show()
def P(i, j, nu, beta, tau, alpha, k):
pass
def get_adjacency_matrix(n):
matrix = np.array([[1 for i in range(n)] for j in range(n)])
# No vertex connects to itself
for i in range(n):
matrix[i][i] = 0
# If i is connected to j, j is connected to i
for i in range(n):
for j in range(n):
matrix[j][i] = matrix[i][j]
return matrix
def fill_dis_matr(n):
m = np.zeros((n, n))
for i in range(n):
for j in range(i + 1, n):
t = random.randint(MIN_DIS, MAX_DIS)
m[i][j], m[j][i] = t, t
m[m == 0] = np.inf
return m