Example #1
0
    def move_agent(self, action_name, game_interface: HFOAttackingPlayer,
                   features: BaseHighLevelState):
        """ Agent Moves/Dribbles in a specific direction """

        # Get Movement type:
        action = DRIBBLE_TO

        if "UP" in action_name:
            action = (action, features.agent.x_pos, -0.9)
        elif "DOWN" in action_name:
            action = (action, features.agent.x_pos, 0.9)
        elif "LEFT" in action_name:
            action = (action, -0.8, features.agent.y_pos)
        elif "RIGHT" in action_name:
            action = (action, 0.8, features.agent.y_pos)
        else:
            raise ValueError("ACTION NAME is WRONG")

        attempts = 0
        while game_interface.in_game() and attempts < self.action_num_episodes:
            status, observation = game_interface.step(action,
                                                      features.has_ball())
            features.update_features(observation)
            attempts += 1
        return game_interface.get_game_status(), \
            game_interface.get_observation_array()
Example #2
0
def set_start_game_conditions(features_manager: BaseHighLevelState,
                              hfo_interface: HFOAttackingPlayer,
                              wait: bool,
                              fixed_position: bool = False):
    ball_pos = [features_manager.agent.ball_x, features_manager.agent.ball_y]
    starting_corners = get_vertices_around_ball(ball_pos)
    start_pos = random.choice(starting_corners)

    aux_counter = 0
    if wait:
        while hfo_interface.in_game():
            msg = hfo_interface.hfo.hear()
            if msg == settings.PLAYER_READY_MSG:
                # print("\n[HELIOS] HEARD MESSAGE!! Start Playing\n")
                break
            else:
                if fixed_position:
                    hfo_action = (MOVE_TO, start_pos[0], start_pos[1])
                    _, observation = hfo_interface.step(hfo_action)
                else:
                    _, observation = hfo_interface.step(NOOP)
                features_manager.update_features(observation)
            aux_counter += 1
            if aux_counter == 120:
                # print("\n[HELIOS] STILL WAITING!! Start Playing\n")
                break
    return
Example #3
0
 def kick_to_pos(self, pos: tuple, features: BaseHighLevelState,
                 game_interface: HFOAttackingPlayer):
     """ The agent kicks to position expected """
     hfo_action = (KICK_TO, pos[0], pos[1], 2)
     status, observation = game_interface.step(hfo_action,
                                               features.has_ball())
     # Update features:
     features.update_features(observation)
Example #4
0
 def move_to_pos(self, pos: tuple, features: BaseHighLevelState,
                 game_interface: HFOAttackingPlayer):
     """ The agent keeps moving until reach the position expected """
     curr_pos = features.get_pos_tuple(round_ndigits=1)
     while pos != curr_pos:
         hfo_action = (MOVE_TO, pos[0], pos[1])
         status, observation = game_interface.step(hfo_action,
                                                   features.has_ball())
         # Update features:
         features.update_features(observation)
         curr_pos = features.get_pos_tuple(round_ndigits=1)
Example #5
0
 def execute_action(self, action_idx: int,
                    game_interface: HFOAttackingPlayer,
                    features: BaseHighLevelState):
     """ Receiving the idx of the action, the agent executes it and
     returns the game status """
     action_name = self.map_action_to_str(action_idx)
     # KICK/SHOOT to goal
     if action_name == "KICK_TO_GOAL":
         status, observation = self.shoot_ball(game_interface, features)
     # MOVE/DRIBBLE
     elif "MOVE" in action_name or "DRIBBLE" in action_name:
         status, observation = self.move_agent(action_name, game_interface,
                                               features)
     # DO NOTHING
     elif action_name == "NOOP":
         status, observation = self.do_nothing(game_interface, features)
     else:
         raise ValueError("Action Wrong name")
     # Update Features:
     features.update_features(observation)
     return status
Example #6
0
 def shoot_ball(self, game_interface: HFOAttackingPlayer,
                features: BaseHighLevelState):
     """ Tries to shoot, if it fail, kicks to goal randomly """
     # Get best shoot angle:
     angles = []
     goalie_coord = np.array([features.opponents[0].x_pos,
                              features.opponents[0].y_pos])
     player_coord = np.array(features.get_pos_tuple())
     for goal_pos in self.shoot_possible_coord:
         angles.append(get_angle(goalie=goalie_coord, player=player_coord,
                                 point=goal_pos))
     idx = int(np.argmax(np.array(angles)))
     best_shoot_coord = self.shoot_possible_coord[idx]
     # Action parameters:
     hfo_action = (KICK_TO, best_shoot_coord[0], best_shoot_coord[1], 2.5)
     # Step game:
     _, obs = game_interface.step(hfo_action, features.has_ball())
     # Update features:
     features.update_features(obs)
     return game_interface.get_game_status(), \
         game_interface.get_observation_array()
Example #7
0
    def dribble_to_pos(self, pos: tuple, features: BaseHighLevelState,
                       game_interface: HFOAttackingPlayer):
        """ The agent keeps dribbling until reach the position expected """
        def check_valid_pos(pos_tuple: tuple):
            for pos_aux in pos_tuple:
                try:
                    num_digits = len(str(pos_aux).split(".")[1])
                    if num_digits >= 2:
                        return False
                except IndexError:
                    pass
            return True

        if check_valid_pos(pos) is False:
            raise Exception("Initial positions invalid. Initial positions "
                            "should be a float with 1 digit or less")
        curr_pos = features.get_pos_tuple(round_ndigits=1)
        while pos != curr_pos:
            hfo_action = (DRIBBLE_TO, pos[0], pos[1])
            status, observation = game_interface.step(hfo_action,
                                                      features.has_ball())
            # Update features:
            features.update_features(observation)
            curr_pos = features.get_pos_tuple(round_ndigits=1)
Example #8
0
 def no_ball_action(self, game_interface: HFOAttackingPlayer,
                    features: BaseHighLevelState) -> int:
     action = (MOVE, )
     status, observation = game_interface.step(action, features.has_ball())
     features.update_features(observation)
     return status
Example #9
0
    # Initialize connection with the HFO server
    hfo_interface = HFOAttackingPlayer(num_opponents=num_op,
                                       port=port,
                                       num_teammates=num_team)
    hfo_interface.connect_to_server()
    uniform_id = hfo_interface.hfo.getUnum()
    teammate_id = 7 if uniform_id == 11 else 11
    print("<< Start HELIOS AGENT ID {} >> wait_for_teammate={}; "
          "teammate_id={}".format(uniform_id, wait_for_teammate, teammate_id))

    # Get number of features and actions
    features_manager = BaseHighLevelState(num_op=num_op, num_team=num_team)

    for i in range(num_episodes):
        observation = hfo_interface.reset()
        features_manager.update_features(observation)

        # Set start game conditions:
        set_start_game_conditions(features_manager=features_manager,
                                  hfo_interface=hfo_interface,
                                  wait=wait_for_teammate,
                                  fixed_position=starts_fixed_position)

        # Update environment features:
        features_manager.update_features(hfo_interface.get_observation())

        last_action = -1
        attempts_to_shoot = 0
        last_action_failed = False
        while hfo_interface.in_game():
            # Features: