-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathagent.py
235 lines (188 loc) · 10.3 KB
/
agent.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import time
import numpy as np
import torch
import matplotlib.pyplot as plt
import copy
from utils import *
from parameter import *
from node_manager import NodeManager
class Agent:
def __init__(self, policy_net, device='cpu', plot=False):
self.device = device
self.policy_net = policy_net
self.plot = plot
# location and map
self.location = None
self.map_info = None
# map related parameters
self.cell_size = CELL_SIZE
self.node_resolution = NODE_RESOLUTION
self.updating_map_size = UPDATING_MAP_SIZE
# map and updating map
self.map_info = None
self.updating_map_info = None
# frontiers
self.frontier = set()
# node managers
self.node_manager = NodeManager(plot=self.plot)
# graph
self.node_coords, self.utility, self.guidepost = None, None, None
self.current_index, self.adjacent_matrix, self.neighbor_indices = None, None, None
def update_map(self, map_info):
# no need in training because of shallow copy
self.map_info = map_info
def update_updating_map(self, location):
self.updating_map_info = self.get_updating_map(location)
def update_location(self, location):
self.location = location
node = self.node_manager.nodes_dict.find(location.tolist())
if self.node_manager.nodes_dict.__len__() == 0:
pass
else:
node.data.set_visited()
def update_frontiers(self):
self.frontier = get_frontier_in_map(self.updating_map_info)
def get_updating_map(self, location):
# the map includes all nodes that may be updating
updating_map_origin_x = (location[
0] - self.updating_map_size / 2)
updating_map_origin_y = (location[
1] - self.updating_map_size / 2)
updating_map_top_x = updating_map_origin_x + self.updating_map_size
updating_map_top_y = updating_map_origin_y + self.updating_map_size
min_x = self.map_info.map_origin_x
min_y = self.map_info.map_origin_y
max_x = (self.map_info.map_origin_x + self.cell_size * (self.map_info.map.shape[1] - 1))
max_y = (self.map_info.map_origin_y + self.cell_size * (self.map_info.map.shape[0] - 1))
if updating_map_origin_x < min_x:
updating_map_origin_x = min_x
if updating_map_origin_y < min_y:
updating_map_origin_y = min_y
if updating_map_top_x > max_x:
updating_map_top_x = max_x
if updating_map_top_y > max_y:
updating_map_top_y = max_y
updating_map_origin_x = (updating_map_origin_x // self.cell_size + 1) * self.cell_size
updating_map_origin_y = (updating_map_origin_y // self.cell_size + 1) * self.cell_size
updating_map_top_x = (updating_map_top_x // self.cell_size) * self.cell_size
updating_map_top_y = (updating_map_top_y // self.cell_size) * self.cell_size
updating_map_origin_x = np.round(updating_map_origin_x, 1)
updating_map_origin_y = np.round(updating_map_origin_y, 1)
updating_map_top_x = np.round(updating_map_top_x, 1)
updating_map_top_y = np.round(updating_map_top_y, 1)
updating_map_origin = np.array([updating_map_origin_x, updating_map_origin_y])
updating_map_origin_in_global_map = get_cell_position_from_coords(updating_map_origin, self.map_info)
updating_map_top = np.array([updating_map_top_x, updating_map_top_y])
updating_map_top_in_global_map = get_cell_position_from_coords(updating_map_top, self.map_info)
updating_map = self.map_info.map[
updating_map_origin_in_global_map[1]:updating_map_top_in_global_map[1]+1,
updating_map_origin_in_global_map[0]:updating_map_top_in_global_map[0]+1]
updating_map_info = MapInfo(updating_map, updating_map_origin_x, updating_map_origin_y, self.cell_size)
return updating_map_info
def update_planning_state(self, global_map_info, location):
self.update_map(global_map_info)
self.update_location(location)
self.update_updating_map(self.location)
self.update_frontiers()
self.node_manager.update_graph(self.location,
self.frontier,
self.updating_map_info,
self.map_info)
self.node_coords, self.utility, self.guidepost, self.adjacent_matrix, self.current_index, self.neighbor_indices = \
self.update_observation()
def update_observation(self):
all_node_coords = []
for node in self.node_manager.nodes_dict.__iter__():
all_node_coords.append(node.data.coords)
all_node_coords = np.array(all_node_coords).reshape(-1, 2)
utility = []
guidepost = []
n_nodes = all_node_coords.shape[0]
adjacent_matrix = np.ones((n_nodes, n_nodes)).astype(int)
node_coords_to_check = all_node_coords[:, 0] + all_node_coords[:, 1] * 1j
for i, coords in enumerate(all_node_coords):
node = self.node_manager.nodes_dict.find((coords[0], coords[1])).data
utility.append(node.utility)
guidepost.append(node.visited)
for neighbor in node.neighbor_set:
index = np.argwhere(node_coords_to_check == neighbor[0] + neighbor[1] * 1j)
assert index is not None
index = index[0][0]
adjacent_matrix[i, index] = 0
utility = np.array(utility)
guidepost = np.array(guidepost)
current_index = np.argwhere(node_coords_to_check == self.location[0] + self.location[1] * 1j)[0][0]
neighbor_indices = np.argwhere(adjacent_matrix[current_index] == 0).reshape(-1)
return all_node_coords, utility, guidepost, adjacent_matrix, current_index, neighbor_indices
def get_observation(self):
node_coords = self.node_coords
node_utility = self.utility.reshape(-1, 1)
node_guidepost = self.guidepost.reshape(-1, 1)
current_index = self.current_index
edge_mask = self.adjacent_matrix
current_edge = self.neighbor_indices
n_node = node_coords.shape[0]
current_node_coords = node_coords[self.current_index]
node_coords = np.concatenate((node_coords[:, 0].reshape(-1, 1) - current_node_coords[0],
node_coords[:, 1].reshape(-1, 1) - current_node_coords[1]),
axis=-1) / UPDATING_MAP_SIZE
node_utility = node_utility / (SENSOR_RANGE * 3.14 // FRONTIER_CELL_SIZE)
node_inputs = np.concatenate((node_coords, node_utility, node_guidepost), axis=1)
node_inputs = torch.FloatTensor(node_inputs).unsqueeze(0).to(self.device)
assert node_coords.shape[0] < NODE_PADDING_SIZE, print(node_coords.shape[0], NODE_PADDING_SIZE)
padding = torch.nn.ZeroPad2d((0, 0, 0, NODE_PADDING_SIZE - n_node))
node_inputs = padding(node_inputs)
node_padding_mask = torch.zeros((1, 1, n_node), dtype=torch.int16).to(self.device)
node_padding = torch.ones((1, 1, NODE_PADDING_SIZE - n_node), dtype=torch.int16).to(
self.device)
node_padding_mask = torch.cat((node_padding_mask, node_padding), dim=-1)
current_index = torch.tensor([current_index]).reshape(1, 1, 1).to(self.device)
edge_mask = torch.tensor(edge_mask).unsqueeze(0).to(self.device)
padding = torch.nn.ConstantPad2d(
(0, NODE_PADDING_SIZE - n_node, 0, NODE_PADDING_SIZE - n_node), 1)
edge_mask = padding(edge_mask)
current_in_edge = np.argwhere(current_edge == self.current_index)[0][0]
current_edge = torch.tensor(current_edge).unsqueeze(0)
k_size = current_edge.size()[-1]
padding = torch.nn.ConstantPad1d((0, K_SIZE - k_size), 0)
current_edge = padding(current_edge)
current_edge = current_edge.unsqueeze(-1)
edge_padding_mask = torch.zeros((1, 1, k_size), dtype=torch.int16).to(self.device)
edge_padding_mask[0, 0, current_in_edge] = 1
padding = torch.nn.ConstantPad1d((0, K_SIZE - k_size), 1)
edge_padding_mask = padding(edge_padding_mask)
return [node_inputs, node_padding_mask, edge_mask, current_index, current_edge, edge_padding_mask]
def select_next_waypoint(self, observation):
_, _, _, _, current_edge, _ = observation
with torch.no_grad():
logp = self.policy_net(*observation)
action_index = torch.multinomial(logp.exp(), 1).long().squeeze(1)
next_node_index = current_edge[0, action_index.item(), 0].item()
next_position = self.node_coords[next_node_index]
return next_position, action_index
def plot_env(self):
plt.switch_backend('agg')
plt.figure(figsize=(18, 5))
plt.subplot(1, 3, 2)
nodes = get_cell_position_from_coords(self.node_coords, self.map_info)
if len(self.frontier) > 0:
frontiers = get_cell_position_from_coords(np.array(list(self.frontier)), self.map_info).reshape(-1, 2)
plt.scatter(frontiers[:, 0], frontiers[:, 1], c='r', s=2)
robot = get_cell_position_from_coords(self.location, self.map_info)
plt.imshow(self.map_info.map, cmap='gray')
plt.axis('off')
plt.scatter(nodes[:, 0], nodes[:, 1], c=self.utility, zorder=2)
for node, utility in zip(nodes, self.utility):
plt.text(node[0], node[1], str(utility), zorder=3)
plt.plot(robot[0], robot[1], 'mo', markersize=16, zorder=5)
for coords in self.node_coords:
node = self.node_manager.nodes_dict.find(coords.tolist()).data
for neighbor_coords in node.neighbor_set:
end = (np.array(neighbor_coords) - coords) / 2 + coords
plt.plot((np.array([coords[0], end[0]]) - self.map_info.map_origin_x) / self.cell_size,
(np.array([coords[1], end[1]]) - self.map_info.map_origin_y) / self.cell_size, 'tan', zorder=1)
plt.subplot(1, 3, 3)
plt.imshow(self.map_info.map, cmap='gray')
plt.axis('off')
plt.scatter(nodes[:, 0], nodes[:, 1], c=self.guidepost, zorder=2)
plt.plot(robot[0], robot[1], 'mo', markersize=16, zorder=5)