def make_agent_walk_along_nx_graph(self, nx_skeleton, source_node, target_node): if isinstance(nx_skeleton, nx.Graph): nx_skeleton = networkx_utils.NxSkeleton(nx_graph=nx_skeleton) self.relative_coordinate_system = True nx_skeleton.add_direction_vector_to_nodes(check_for_circles_branches=False) nx_graph = nx_skeleton.nx_graph walk = nx.shortest_path(nx_graph, source_node, target_node) # Let the hero walk along the given graph for ii in walk: first_pos = nx_graph.node[ii]['position'] dir_vector = nx_graph.node[ii]['dir_vector'] # print dir_vector dir_vector = helpers.discretize_vector_to_voxel(dir_vector) rel_vector = self.abs_dir_to_rel_dir(dir_vector) rel_vector = helpers.discretize_vector_to_voxel(rel_vector) action_id = self.conv_dir_to_action_id(rel_vector) # print "----" # print 'dir vector', dir_vector # print 'rel_vector', rel_vector # print 'direction vector of agent', self.hero.get_current_direction() # print 'action id', action_id # print self.directions[action_id] self.hero.jump(first_pos) self.hero.observed_observations.append(self.observation_matrix[first_pos[0], first_pos[1], first_pos[2]]) self.hero.visited_positions.append(tuple(nx_graph.node[ii]['position'])) self.hero.taken_actions.append(action_id)
def relative_dir_to_abs_dir(self, direction): direction = np.array(direction) direction_of_agent = self.hero.get_current_direction() if direction_of_agent is None: direction_of_agent = direction.copy() direction_of_agent = helpers.np_array_to_vector3(direction_of_agent) direction_of_agent.normalize() direction_of_agent = helpers.discretize_vector_to_voxel(direction_of_agent) rotation_matrix = helpers.get_rot_matrix_from_vec1_to_vec2([0, 1, 0], direction_of_agent) absolute_direction = np.matmul(rotation_matrix, direction) absolute_direction = helpers.discretize_vector_to_voxel(absolute_direction) check_direction = np.matmul(rotation_matrix, np.array([0, 1, 0])) check_direction = helpers.discretize_vector_to_voxel(check_direction) assert ((check_direction == direction_of_agent).all()), "rotating the system to agent's perspective is broken" return absolute_direction
def abs_dir_to_rel_dir(self, direction, direction_of_agent=None): if direction_of_agent is None: direction_of_agent = self.hero.get_current_direction() if direction_of_agent is None: direction_of_agent = direction.copy() rotation_matrix = helpers.get_rot_matrix_from_vec1_to_vec2(direction_of_agent, [0, 1, 0]) relative_direction = np.matmul(rotation_matrix, direction) relative_direction = helpers.discretize_vector_to_voxel(relative_direction) return relative_direction