Esempio n. 1
0
 def kick_to_pos(self, pos: tuple, features: DiscFeatures1Teammate,
                 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)
Esempio n. 2
0
 def move_to_pos(self, pos: tuple, features: DiscFeatures1Teammate,
                 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)
Esempio n. 3
0
 def pass_ball(self, game_interface: HFOAttackingPlayer,
               features: DiscFeatures1Teammate):
     """ Tries to use the PASS action, if it fails, Kicks in the direction
     of the teammate"""
     attempts = 0
     while game_interface.in_game() and features.has_ball():
         if attempts > 2:
             break
         elif attempts == 2:
             # Failed to pass 2 times
             # print("Failed to PASS two times. WILL KICK")
             hfo_action = (KICK_TO, features.teammate_coord[0],
                           features.teammate_coord[1], 1.5)
         else:
             hfo_action = (PASS, 11)
         _, obs = game_interface.step(hfo_action, features.has_ball())
         features.update_features(obs)
         attempts += 1
     return game_interface.get_game_status(), \
         game_interface.get_observation_array()
Esempio n. 4
0
 def shoot_ball(self, game_interface: HFOAttackingPlayer,
                features: DiscFeatures1Teammate):
     """ Tries to shoot, if it fail, kicks to goal randomly """
     attempts = 0
     # while game_interface.in_game() and features.has_ball():
     #     if attempts > 3:
     #         break
     #     elif attempts == 3:
     #         # Failed to kick four times
     #         # print("Failed to SHOOT 3 times. WILL KICK")
     #         y = 0  # TODO random.choice([0.17, 0, -0.17])
     #         hfo_action = (KICK_TO, 0.9, y, 2)
     #     else:
     #         hfo_action = (SHOOT,)
     #     _, obs = game_interface.step(hfo_action, features.has_ball())
     #     features.update_features(obs)
     #     attempts += 1
     hfo_action = (KICK_TO, 0.9, 0, 2)
     _, obs = game_interface.step(hfo_action, features.has_ball())
     features.update_features(obs)
     return game_interface.get_game_status(), \
         game_interface.get_observation_array()
Esempio n. 5
0
    def move_agent(self, action_name, game_interface: HFOAttackingPlayer,
                   features: DiscFeatures1Teammate):
        """ Agent Moves/Dribbles in a specific direction """
        # print("move_agent!")
        if "SHORT" in action_name:
            num_repetitions = 10
        elif "LONG" in action_name:
            num_repetitions = 20
        else:
            raise ValueError("ACTION NAME is WRONG")

        # Get Movement type:
        if "MOVE" in action_name:
            action = MOVE_TO
        elif "DRIBBLE" in action_name:
            action = DRIBBLE_TO
        else:
            raise ValueError("ACTION NAME is WRONG")

        if "UP" in action_name:
            action = (action, features.agent_coord[0], -0.9)
        elif "DOWN" in action_name:
            action = (action, features.agent_coord[0], 0.9)
        elif "LEFT" in action_name:
            action = (action, -0.8, features.agent_coord[1])
        elif "RIGHT" in action_name:
            action = (action, 0.8, features.agent_coord[1])
        else:
            raise ValueError("ACTION NAME is WRONG")

        attempts = 0
        while game_interface.in_game() and attempts < num_repetitions:
            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()
Esempio n. 6
0
 def execute_action(self, action_idx: int,
                    game_interface: HFOAttackingPlayer,
                    features: DiscFeatures1Teammate):
     """ Receiving the idx of the action, the agent executes it and
     returns the game status """
     action_name = self.map_action_to_str(action_idx, features.has_ball())
     # KICK/SHOOT to goal
     if action_name == "KICK_TO_GOAL":
         status, observation = self.shoot_ball(game_interface, features)
     # PASS ball to teammate
     elif action_name == "PASS":
         status, observation = self.pass_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
Esempio n. 7
0
 def __init__(self, num_opponents: int, num_teammates: int,
              port: int = 6000):
     # Game Interface:
     self.game_interface = HFOAttackingPlayer(num_opponents=num_opponents,
                                              num_teammates=num_teammates,
                                              port=port)
     self.game_interface.connect_to_server()
     # Features Interface:
     self.features = DiscFeatures1Teammate(num_op=num_opponents,
                                           num_team=num_teammates)
     # Actions Interface:
     self.actions = Actions()
     # Agent instance:
     self.agent = QAgent(num_features=self.features.num_features,
                         num_actions=self.actions.get_num_actions(),
                         learning_rate=0.1, discount_factor=0.9, epsilon=0.8)
Esempio n. 8
0
 def no_ball_action(self, game_interface: HFOAttackingPlayer,
                    features: DiscFeatures1Teammate) -> int:
     action = (MOVE, )
     status, observation = game_interface.step(action, features.has_ball())
     features.update_features(observation)
     return status
Esempio n. 9
0
 def do_nothing(self, game_interface: HFOAttackingPlayer,
                features: DiscFeatures1Teammate):
     action = (NOOP, )
     status, observation = game_interface.step(action, features.has_ball())
     return status, observation
Esempio n. 10
0
    parser.add_argument('--port', type=int, default=6000)
    parser.add_argument('--num_games', type=int, default=1)

    args = parser.parse_args()
    port = args.port
    num_games = args.num_games
    
    NUM_TEAMMATES = 0
    NUM_OPPONENTS = 1
    
    # Game Interface:
    game_interface = HFOAttackingPlayer(num_opponents=NUM_OPPONENTS,
                                        num_teammates=NUM_TEAMMATES, port=port)
    game_interface.connect_to_server()
    # Features Interface:
    features = DiscFeatures1Teammate(num_op=NUM_OPPONENTS,
                                     num_team=NUM_TEAMMATES)
    # Actions Interface:
    actions = Actions()

    for ep in range(num_games):
        
        # Update features:
        features.update_features(game_interface.get_state())
        # Set up gaming conditions:
        actions.dribble_to_pos((-0.5, -0.7), features, game_interface)
        
        # Start learning loop
        status = IN_GAME
        prev_action_idx = None
        while game_interface.in_game():
            if features.has_ball():