Exemplo n.º 1
0
 def valid_moves_feature(mp: Map) -> torch.Tensor:
     neighbours: Set[Point] = set(mp.get_neighbours(mp.agent.position))
     res: torch.Tensor = torch.zeros(8)
     for idx, mv in enumerate(mp.EIGHT_POINTS_MOVE_VECTOR):
         next_point: Point = Map.apply_move(mv, mp.agent.position)
         if next_point in neighbours:
             res[idx] = 1
     return res
Exemplo n.º 2
0
 def get_sequential_features(mp: Map, feature_extractors: List[str]) -> List[Dict[str, torch.Tensor]]:
     """
     Replays the map trace and extracts the features at each time step
     :return: A list, each cell represents a time step and contains a dictionary of features
     """
     if not mp.trace:
         # fake movement
         mp.move_agent(mp.agent.position)
     return mp.replay_trace(lambda m: MapProcessing.extract_features(m, feature_extractors))
 def get_movement_cost_from_index(self, idx: int, frm: Point) -> float:
     if not self.__cached_move_costs:
         zeros = Point(*[0 for i in range(self.size.n_dim)])
         self.__cached_move_costs = list(
             map(lambda p: Map.get_movement_cost(self, zeros, p),
                 self.ALL_POINTS_MOVE_VECTOR))
     to = frm + self.ALL_POINTS_MOVE_VECTOR[idx]
     return self.__cached_move_costs[idx] + self.weight_grid[to.values]
Exemplo n.º 4
0
        def random_sample_pts(m: Map, nr_of_pts) -> List[Point]:
            all_valid_pts = []
            for y in range(m.size.height):
                for x in range(m.size.width):
                    new_agent_pos = Point(x, y)
                    if m.is_agent_valid_pos(new_agent_pos) and \
                                not (m.goal.position.x == new_agent_pos.x and m.goal.position.y == new_agent_pos.y):
                        all_valid_pts.append(new_agent_pos)

            random.shuffle(all_valid_pts)
            return all_valid_pts[:nr_of_pts]
Exemplo n.º 5
0
    def next_position_index_label(mp: Map) -> List[torch.Tensor]:
        move_indexes: List[int] = list(map(
            lambda el: mp.get_move_index(
                MapProcessing.__get_pos(el[1]) - MapProcessing.__get_pos(el[0])),
            zip(mp.trace, mp.trace[1:] + [Trace(mp.goal.position)]))
        )

        res: List[torch.Tensor] = []
        for move_idx in move_indexes:
            res.append(torch.tensor(float(move_idx)))
        return res
Exemplo n.º 6
0
    def gradient_descent(self, current: Point, grid: Map) -> List[Point]:
        """
        Search backward from the given point the next lowest number until we reach the agent position
        :param current: The position from witch to start the descent
        :param grid: the map
        :return: The trace
        """
        trace: List[Point] = [current]
        # find the trace of the path by looking at neighbours at each point and moving the current towards the agent position. (number to next lowest numb(-1))
        while self.step_grid[current.values] != 1:
            for n in grid.get_next_positions(current):
                if self.step_grid[n.values] == self.step_grid[current.values] - 1:
                    trace.append(n)
                    current = n
                    break

        return trace[1:]
Exemplo n.º 7
0
    def gradient_descent(self, current: Point, grid: Map) -> List[Point]:
        """
        Search backward from the given point the next lowest number until we reach the agent position
        :param current: The position from witch to start the descent
        :param grid: the map
        :return: The trace
        """
        trace: List[Point] = [current]

        while self.step_grid[current.y][current.x] != 1:
            for n in grid.get_next_positions(current):
                if self.step_grid[n.y][
                        n.x] == self.step_grid[current.y][current.x] - 1:
                    trace.append(n)
                    current = n
                    break

        return trace[1:]
Exemplo n.º 8
0
    def local_map_normalized_feature(mp: Map) -> torch.Tensor:
        extents: int = 4

        if not isinstance(mp, DenseMap):
            raise Exception("mp has to be of type DenseMap")

        local_map: torch.Tensor = torch.zeros((extents * 2 + 1, extents * 2 + 1))
        local_map.fill_(DenseMap.WALL_ID)

        xx: int = 0
        for x in range(mp.agent.position.x - extents, mp.agent.position.x + extents + 1):
            yy: int = 0
            for y in range(mp.agent.position.y - extents, mp.agent.position.y + extents + 1):
                if not mp.is_out_of_bounds_pos(Point(x, y)):
                    local_map[yy][xx] = mp.grid[y][x]
                yy += 1
            xx += 1

        return MapProcessing.__convert_grid(local_map)
Exemplo n.º 9
0
 def test_copy(self) -> None:
     map1: Map = Map(Size(2, 3))
     with self.assertRaises(Exception):
         copy.copy(map1)
Exemplo n.º 10
0
 def test_ne(self) -> None:
     self.assertNotEqual(Map(Size(3, 2)), 5)
Exemplo n.º 11
0
 def __get_hit_point_along_dir(mp: Map, move: Point) -> Point:
     pos: Point = mp.agent.position
     pos = mp.apply_move(pos, move)
     while mp.is_agent_valid_pos(pos):
         pos = mp.apply_move(pos, move)
     return pos
Exemplo n.º 12
0
 def next_position_label(mp: Map) -> List[torch.Tensor]:
     return list(map(
         lambda el: mp.get_move_along_dir(
             MapProcessing.__get_pos(el[1]) - MapProcessing.__get_pos(el[0])).to_tensor(),
         zip(mp.trace, mp.trace[1:] + [Trace(mp.goal.position)]))
     )