def kick_to_pos(self, pos: tuple, features: DiscreteFeatures1Teammate, 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)
def move_to_pos(self, pos: tuple, features: DiscreteFeatures1Teammate, 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)
def shoot_ball(self, game_interface: HFOAttackingPlayer, features: DiscreteFeatures1Teammate): """ 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 = 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 return game_interface.get_game_status(), \ game_interface.get_observation_array()
def pass_ball(self, game_interface: HFOAttackingPlayer, features: DiscreteFeatures1Teammate): """ 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") y = random.choice([0.17, 0, -0.17]) hfo_action = (KICK_TO, 0.9, y, 2) 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()
def move_agent(self, action_name, game_interface: HFOAttackingPlayer, features: DiscreteFeatures1Teammate): """ 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()
def execute_action(self, action_idx: int, game_interface: HFOAttackingPlayer, features: DiscreteFeatures1Teammate): """ 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
def __init__(self, num_opponents: int, num_teammates: int): # Game Interface: self.game_interface = HFOAttackingPlayer(num_opponents=num_opponents, num_teammates=num_teammates) self.game_interface.connect_to_server() # Features Interface: self.features = DiscreteFeatures1Teammate(num_op=num_opponents, num_team=num_teammates) # Actions Interface: self.actions = DiscreteActionsModule() # 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=1, final_epsilon=0.3)
def do_nothing(self, game_interface: HFOAttackingPlayer, features: DiscreteFeatures1Teammate): action = (NOOP,) status, observation = game_interface.step(action, features.has_ball()) return status, observation