-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover_time_kyra.py
138 lines (99 loc) · 3.41 KB
/
cover_time_kyra.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import random
import numpy as np
import networkx as nx
from options.graph.spectrum import ComputeFiedlerVector, ComputeConnectivity
from options.util import AddEdge, neighbor
def ComputeCoverTimeS(G, s, sample=1000):
##########################
# PLEASE WRITE A TEST CODE
##########################
'''
Args:
G (numpy 2d array): Adjacency matrix (may be an incidence matrix).
s (integer): index of the initial state
sample (integer): number of trajectories to sample
Returns:
(float): the expected cover time from state s
Summary:
Given a graph adjacency matrix, return the expected cover time starting from node s. We sample a set of trajectories to get it.
'''
N = G.shape[0]
n_steps = []
for i in range(sample):
visited = np.zeros(N, dtype=int)
cur_s = s
cur_steps = 0
while any(visited == 0):
s_neighbor = neighbor(G, cur_s)
next_s = random.choice(s_neighbor)
visited[next_s] = 1
cur_s = next_s
cur_steps += 1
n_steps.append(cur_steps)
# print('n_steps=', n_steps)
avg_steps = sum(n_steps) / sample
return avg_steps
G1 = np.array([[0, 1], [1, 0]])
s1 = 0
s2 = 1
G2 = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]])
s3 = 0
s4 = 1
s5 = 2
G3 = np.array([0, 1, 0], [1, 0, 1], [0, 1, 1])
def ComputeCoverHelperTest():
if 0.9 < ComputeCoverTimeS(G1, s1, sample=1000) < 1.1:
print("test for G1, s1, s = 1000 passed ")
else:
print("test for G1, s1, s = 1000 failed")
if 0.9 < ComputeCoverTimeS(G1, s2, sample=1000) < 1.1:
print("test for G1, s2, s = 1000 passed ")
else:
print("test for G1, s2, s = 1000 failed")
if 1.85 < ComputeCoverTimeS(G2, s3, sample=1000) < 2.15:
print("test for G2, s3, s = 1000 passed ")
else:
print("test for G2, s3, s = 1000 failed")
if 2.85 < ComputeCoverTimeS(G2, s4, sample=1000) < 3.15:
print("test for G2, s4, s = 1000 passed ")
else:
print("test for G2, s4, s = 1000 failed")
if 1.85 < ComputeCoverTimeS(G2, s5, sample=1000) < 2.15:
print("test for G2, s5, s = 1000 passed ")
else:
print("test for G2, s5, s = 1000 failed")
def ComputeCoverTime(G, samples=1000):
##########################
# PLEASE WRITE A TEST CODE
##########################
'''
Args:
G (numpy 2d array): Adjacency matrix (or incidence matrix)
Returns:
(float): the expected cover time
Summary:
Given a graph adjacency matrix, return the expected cover time.
'''
N = G.shape[0]
c_sum = 0
for i in range(samples):
init = random.randint(0, N-1)
c_i = ComputeCoverTimeS(G, init, sample=1)
c_sum += c_i
return float(c_sum) / float(samples)
if __name__ == "__main__":
# PlotConnectivityAndCoverTime(100)
# exit(0)
Gnx = nx.path_graph(4)
graph_ = nx.to_numpy_matrix(Gnx)
graph = np.asarray(graph_)
v = ComputeFiedlerVector(Gnx) # numpy array of floats
augGraph = AddEdge(graph, np.argmax(v), np.argmin(v))
# print('Graphs')
# print(graph)
# print(augGraph)
t2 = ComputeCoverTime(augGraph)
print('CoverTime Aug1', t2)
lb2 = nx.algebraic_connectivity(nx.to_networkx_graph(augGraph))
print('lambda ', lb2)
ComputeCoverHelperTest()