/
node.py
executable file
·91 lines (80 loc) · 3.22 KB
/
node.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
import numpy as np
class Node():
def __init__(self, coords, frontiers, robot_belief):
self.coords = coords
self.observable_frontiers = []
self.sensor_range = 80
self.initialize_observable_frontiers(frontiers, robot_belief)
self.utility = self.get_node_utility()
if self.utility == 0:
self.zero_utility_node = True
else:
self.zero_utility_node = False
def initialize_observable_frontiers(self, frontiers, robot_belief):
dist_list = np.linalg.norm(frontiers - self.coords, axis=-1)
frontiers_in_range = frontiers[dist_list < self.sensor_range - 10]
for point in frontiers_in_range:
collision = self.check_collision(self.coords, point, robot_belief)
if not collision:
self.observable_frontiers.append(point)
def get_node_utility(self):
return len(self.observable_frontiers)
def update_observable_frontiers(self, observed_frontiers, new_frontiers, robot_belief):
# remove observed frontiers in the observable frontiers
if observed_frontiers != []:
observed_index = []
for i, point in enumerate(self.observable_frontiers):
if point[0] + point[1] * 1j in observed_frontiers[:, 0] + observed_frontiers[:, 1] * 1j:
observed_index.append(i)
for index in reversed(observed_index):
self.observable_frontiers.pop(index)
# add new frontiers in the observable frontiers
if new_frontiers != []:
dist_list = np.linalg.norm(new_frontiers - self.coords, axis=-1)
new_frontiers_in_range = new_frontiers[dist_list < self.sensor_range - 10]
for point in new_frontiers_in_range:
collision = self.check_collision(self.coords, point, robot_belief)
if not collision:
self.observable_frontiers.append(point)
self.utility = self.get_node_utility()
if self.utility < 5:
self.utility = 0
if self.utility == 0:
self.zero_utility_node = True
else:
self.zero_utility_node = False
def set_visited(self):
self.observable_frontiers = []
self.utility = 0
self.zero_utility_node = True
def check_collision(self, start, end, robot_belief):
# Bresenham line algorithm checking
collision = False
x0 = start[0].round()
y0 = start[1].round()
x1 = end[0].round()
y1 = end[1].round()
dx, dy = abs(x1 - x0), abs(y1 - y0)
x, y = x0, y0
error = dx - dy
x_inc = 1 if x1 > x0 else -1
y_inc = 1 if y1 > y0 else -1
dx *= 2
dy *= 2
while 0 <= x < robot_belief.shape[1] and 0 <= y < robot_belief.shape[0]:
k = robot_belief.item(int(y), int(x))
if x == x1 and y == y1:
break
if k == 1:
collision = True
break
if k == 127:
collision = True
break
if error > 0:
x += x_inc
error -= dy
else:
y += y_inc
error += dx
return collision