-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCBBA_plot.py
181 lines (145 loc) · 4.42 KB
/
CBBA_plot.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from CBBA import CBBA_agent
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
# For build GIF
import imageio
import os
np.random.seed(3)
task_num = 20
robot_num = 4
task = np.random.uniform(low=0,high=1,size=(task_num,2))
robot_list = [CBBA_agent(id=i, vel=1, task_num=task_num, agent_num=robot_num, L_t=task.shape[0]) for i in range(robot_num)]
# Network Initialize
G = np.ones((robot_num, robot_num)) # Fully connected network
# disconnect link arbitrary
G[2,3]=0
G[3,2]=0
G[1,2]=0
G[2,1]=0
G[1,3]=0
G[3,1]=0
fig, ax = plt.subplots()
ax.set_xlim((-0.1,1.1))
ax.set_ylim((-0.1,1.1))
ax.plot(task[:,0],task[:,1],'rx',label="Task")
robot_pos = np.array([r.state[0].tolist() for r in robot_list])
ax.plot(robot_pos[:,0],robot_pos[:,1],'b^',label="Robot")
for i in range(robot_num-1):
for j in range(i+1,robot_num):
if G[i][j] == 1:
ax.plot([robot_pos[i][0],robot_pos[j][0]],[robot_pos[i][1],robot_pos[j][1]],'g--',linewidth=1)
handles, labels = ax.get_legend_handles_labels()
custom_line = Line2D([0], [0], color="g",linestyle="--",label="communication")
handles.append(custom_line)
ax.legend(handles=handles)
t = 0 # Iteration number
assign_plots = []
max_t = 100
plot_gap = 0.1
save_gif = False
filenames = []
if save_gif:
if not os.path.exists("my_gif"):
os.makedirs("my_gif")
while True:
converged_list = []
print("==Iteration {}==".format(t))
## Phase 1: Auction Process
print("Auction Process")
for robot_id, robot in enumerate(robot_list):
# select task by local information
robot.build_bundle(task)
## Plot
if len(robot.p) > 0:
x_data=[robot.state[0][0]]+task[robot.p,0].tolist()
y_data=[robot.state[0][1]]+task[robot.p,1].tolist()
else:
x_data=[robot.state[0][0]]
y_data=[robot.state[0][1]]
if t == 0:
assign_line, = ax.plot(x_data,y_data,'k-',linewidth=1)
assign_plots.append(assign_line)
else:
assign_plots[robot_id].set_data(x_data,y_data)
print("Bundle")
for robot in robot_list:
print(robot.b)
print("Path")
for robot in robot_list:
print(robot.p)
## Plot
ax.set_title("Time Step:{}, Bundle Construct".format(t))
plt.pause(plot_gap)
if save_gif:
filename = f'{t}_B.png'
filenames.append(filename)
plt.savefig(filename)
## Communication stage
print("Communicating...")
# Send winning bid list to neighbors (depend on env)
message_pool = [robot.send_message() for robot in robot_list]
for robot_id, robot in enumerate(robot_list):
# Recieve winning bidlist from neighbors
g = G[robot_id]
connected, = np.where(g==1)
connected = list(connected)
connected.remove(robot_id)
if len(connected) > 0:
Y = {neighbor_id:message_pool[neighbor_id] for neighbor_id in connected}
else:
Y = None
robot.receive_message(Y)
## Phase 2: Consensus Process
print("Consensus Process")
for robot_id, robot in enumerate(robot_list):
# Update local information and decision
if Y is not None:
converged = robot.update_task()
converged_list.append(converged)
## Plot
if len(robot.p) > 0:
x_data=[robot.state[0][0]]+task[robot.p,0].tolist()
y_data=[robot.state[0][1]]+task[robot.p,1].tolist()
else:
x_data=[robot.state[0][0]]
y_data=[robot.state[0][1]]
assign_plots[robot_id].set_data(x_data,y_data)
## Plot
ax.set_title("Time Step:{}, Consensus".format(t))
plt.pause(plot_gap)
if save_gif:
filename = f'./my_gif/{t}_C.png'
filenames.append(filename)
plt.savefig(filename)
print("Bundle")
for robot in robot_list:
print(robot.b)
print("Path")
for robot in robot_list:
print(robot.p)
t += 1
if sum(converged_list) == robot_num:
ax.set_title("Time Step:{}, Converged!".format(t))
break
if t>max_t:
ax.set_title("Time Step:{}, Max time step overed".format(t))
break
if save_gif:
filename = f'./my_gif/{t}_F.png'
filenames.append(filename)
plt.savefig(filename)
#build gif
files=[]
for filename in filenames:
image = imageio.imread(filename)
files.append(image)
imageio.mimsave("./my_gif/mygif.gif", files, format='GIF', fps = 0.5)
with imageio.get_writer('./my_gif/mygif.gif', mode='I') as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
# Remove files
for filename in set(filenames):
os.remove(filename)
plt.show()