-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
84 lines (69 loc) · 2.87 KB
/
demo.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
np.random.seed(1) # Set random seed
class network():
def __init__(self):
self.L = np.empty
def Laplacian(self) -> np.ndarray:
# Set graph Laplacian matrix
# If you want to change network, please edit the below matrix 'a'
a = np.array([
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
])
A = a + a.T
D = np.diag(np.sum(A, axis=0))
L = D - A
return L
def Consensus(dimention: int, step: int) -> np.ndarray:
G = network()
L = G.Laplacian()
n = L.shape[0] # Get the number of nodes from matrix shape
x = np.zeros((n, dimention, step))
x[:, :, 0] = np.random.uniform(size=(n, dimention)) # Generate the state vector with random function
for k in range(1, step):
x[:, :, k] = x[:, :, k - 1] - 0.05 * L @ x[:, :, k - 1] # Calculate control input
return x
def static_figure() -> None:
x = Consensus(2, 300)
plt.rcParams["font.size"] = 24
fig1 = plt.figure(figsize=(16, 12), tight_layout=True)
ax1 = fig1.add_subplot(211)
for i in range(x.shape[0]):
ax1.plot(x[i, 0, :], marker='o', markersize=1, label='Agent {}'.format(i))
ax1.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
ax1.set_title("Control Input for x-axis")
ax2 = fig1.add_subplot(212)
for i in range(x.shape[0]):
ax2.plot(x[i, 1, :], marker='o', markersize=1, label='Agent {}'.format(i))
ax2.set_title("Control Input for y-axis")
fig1.savefig('Result.png')
plt.show()
def RenderGIF() -> None:
x = Consensus(2, 300)
plt.rcParams["font.size"] = 24
fig2 = plt.figure(figsize=(12, 12), tight_layout=True)
ax3 = fig2.add_subplot(111)
ax3.set_xlim(min(x[:, 0, 0]) - 0.1, max(x[:, 0, 0]) + 0.1)
ax3.set_ylim(min(x[:, 1, 0]) - 0.1, max(x[:, 1, 0]) + 0.1)
ax3.set_title("Agent Trajectory")
scatter_plots = [ax3.scatter(x[i, 0, 0], x[i, 1, 0], marker='o', s=100) for i in range(x.shape[0])]
def update(frame):
for i in range(x.shape[0]):
scatter_plots[i].set_offsets(np.column_stack([x[i, 0, frame], x[i, 1, frame]]))
return scatter_plots
anim = animation.FuncAnimation(fig2, update, frames=x.shape[2], blit=True)
anim.save('Result.gif', writer='pillow', fps=40)
plt.show()
if __name__ == '__main__':
RenderGIF()
static_figure()