in interaction_exploration/tools/calculate_max_interactions.py [0:0]
def compute_graph(self):
G = nx.DiGraph()
queue = [self.agent_pose(self.state)] # random reachable pose
seen = set(queue)
while True:
if len(queue)==0:
break
node = queue.pop(0)
x0, y0, z0, rot0, hor0 = node
nbhs = []
# turn
nbhs.append({'act':'tright', 'pos':node[:3]+((rot0+30)%360, hor0)})
nbhs.append({'act':'tleft', 'pos':node[:3]+((rot0-30)%360, hor0)})
# look
nbhs.append({'act':'up', 'pos':node[:3]+(rot0, max(-30, hor0-15))})
nbhs.append({'act':'down', 'pos':node[:3]+(rot0, min(60, hor0+15))})
# move -- only allow if facing right angles
if rot0%90 == 0:
x = x0 + 0.25*np.sin(np.radians(rot0))
z = z0 + 0.25*np.cos(np.radians(-rot0))
y = y0
x_snap = min(self.reachable_x, key=lambda item: np.abs(item-x))
z_snap = min(self.reachable_z, key=lambda item: np.abs(item-z))
dist = np.sqrt((x-x_snap)**2 + (z-z_snap)**2)
if (x_snap, y, z_snap) in self.reachable_positions and dist<1e-12:
nbhs.append({'act':'forward', 'pos':(x_snap, y, z_snap, rot0, hor0)})
for nbh in nbhs:
if nbh['pos'] not in seen:
queue.append(nbh['pos'])
seen.add(nbh['pos'])
G.add_edge(node, nbh['pos'], action=nbh['act'])
print (f'Graph constructed with {len(G.nodes)} nodes |{len(self.reachable_positions)} reachable positions')
# checking if all positions are in graph
success = True
for (x, y, z), rot, hor in itertools.product(self.reachable_positions, [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330], [-30, -15, 0, 15, 30, 45, 60]):
if (x, y, z, rot, hor) not in G:
print (x, y, z, rot, hor, '<-- MISSING')
success = False
break
if not nx.is_strongly_connected(G):
cc = nx.strongly_connected_components(G)
print ('NOT STRONGLY CONNECTED:', len(cc), 'components')
return None
else:
print ('IS CONNECTED!!')
if not success:
G = None
return G