Ejemplo n.º 1
0
 def _see_ghost(self, action):
     eye_pos = self.state.agent_pos + Moves.get_coord(action)
     while True:
         for g, ghost in enumerate(self.state.ghosts):
             if ghost.pos == eye_pos:
                 return g
         eye_pos += Moves.get_coord(action)
         if not (self.grid.is_inside(eye_pos)
                 and self.is_passable(eye_pos)):
             break
     return -1
Ejemplo n.º 2
0
 def _generate_legal(self):
     actions = []
     for action in self.action_space.n:
         if self.grid.is_inside(self.state.agent_pos +
                                Moves.get_coord(action.value)):
             actions.append(action.value)
     return actions
Ejemplo n.º 3
0
    def _next_pos(self, pos, direction):
        direction = Moves.get_coord(direction)
        if pos.x == 0 and pos.y == self.board['_passage_y'] and\
                direction == Moves.EAST:
            next_pos = Coord(self.grid.x_size - 1, pos.y)
        elif pos.x == self.grid.x_size - 1 and\
                pos.y == self.board['_passage_y'] and direction == Moves.WEST:
            next_pos = Coord(0, pos.y)
        else:
            next_pos = pos + direction

        if self.grid.is_inside(next_pos) and self.is_passable(next_pos):
            return next_pos
        else:
            return Coord(-1, -1)
Ejemplo n.º 4
0
 def _generate_preferred(self, history):
     actions = []
     if history.size == 0:
         return self._generate_legal()
     if history[-1].ob == self.grid.n_tiles and self.grid.is_corner(
             self.state.agent_pos):
         actions.append(Action.TAG.value)
         return actions
     for d in range(4):
         if history[-1].action != self.grid.opposite(
                 d) and self.grid.is_inside(self.state.agent_pos +
                                            Moves.get_coord(d)):
             actions.append(d)
     assert len(actions) > 0
     return actions
Ejemplo n.º 5
0
    def step(self, action):  # state
        assert self.action_space.contains(
            action)  # action are idx of movements
        assert self.done is False
        # assert self.done == False

        reward = 0.
        self.time += 1
        self.last_action = action
        assert self.grid.is_inside(self.state.agent_pos)
        assert self.grid.is_inside(self.state.opponent_pos[0])

        if action == 4:
            tagged = False
            for opp, opp_pos in enumerate(self.state.opponent_pos):
                if opp_pos == self.state.agent_pos:  # check if x==x_agent and y==y_agent
                    reward = 10.
                    tagged = True
                    # self.state.opponent_pos[opp] = Coord(4, 4)
                    self.state.num_opp -= 1
                    # self.state.opponent_pos.pop(opp)
                elif self.grid.is_inside(self.state.opponent_pos[opp]
                                         ) and self.state.num_opp > 0:
                    self.move_opponent(opp)
            if not tagged:
                reward = -10.

        else:
            reward = -1.
            next_pos = self.state.agent_pos + Moves.get_coord(action)
            if self.grid.is_inside(next_pos):
                self.state.agent_pos = next_pos

        ob = self._sample_ob(self.state, action)
        assert ob < self.grid.n_tiles + 1
        # p_ob = self._compute_prob(action, self.state, ob)
        self.done = self.state.num_opp == 0
        return ob, reward, self.done, {
            "state": self.state
        }  # self._encode_state(self.state)}