Ejemplo n.º 1
0
 def __init__(self,
              config_dir=settings.CONFIG_DIR, agent_id=0, port=6000,
              server_addr='localhost', num_opponents=0, num_teammates=0):
     self.hfo = HFOEnvironment()
     self.config_dir = config_dir
     self.port = port
     self.server_addr = server_addr
     self.status = IN_GAME
Ejemplo n.º 2
0
class HFOGoalkeeperPlayer(object):
    def __init__(self,
                 config_dir=settings.CONFIG_DIR, agent_id=0, port=6000,
                 server_addr='localhost', num_opponents=0, num_teammates=0):
        self.hfo = HFOEnvironment()
        self.config_dir = config_dir
        self.port = port
        self.server_addr = server_addr
        self.status = IN_GAME
    
    def reset(self) -> list:
        self.status = IN_GAME
        return self.hfo.getState()
    
    def connect_to_server(self):
        """ Establish connection with HFO server """
        self.hfo.connectToServer(
            HIGH_LEVEL_FEATURE_SET,
            self.config_dir,
            self.port,
            self.server_addr,
            team_name='base_right',
            play_goalie=True)
    
    def step(self, hfo_action: int, *args) -> (int, list):
        """
        Method that serves as an interface between a script controlling the
        agent and the environement_features. Method returns the current status
        of the episode and nextState
        """
        self.hfo.act(hfo_action, *args)
        self.status = self.hfo.step()
        return self.status, self.hfo.getState()
    
    def quit_game(self):
        self.hfo.act(QUIT)
    
    def get_game_status(self) -> int:
        return self.status
    
    def in_game(self) -> bool:
        if self.status == IN_GAME:
            return True
        else:
            return False
Ejemplo n.º 3
0
    reward = 0
    if can_kick and action in [DRIBBLE, SHOOT]:
        reward += 2
    elif not can_kick and action == MOVE:
        reward += 2

    if s == GOAL:
        return 10000 + reward
    elif s in [CAPTURED_BY_DEFENSE, OUT_OF_TIME, OUT_OF_BOUNDS]:
        return -10000 + reward
    else:
        return -3 + reward


if __name__ == '__main__':
    hfo = HFOEnvironment()
    hfo.connectToServer(feature_set=HIGH_LEVEL_FEATURE_SET, server_port=6000)
    num_teammates = 0
    num_opponents = 1
    lr = 0.0005
    n_games = 5
    agent = DeepQAgent(gamma=0.99,
                       epsilon=0,
                       alpha=lr,
                       input_dims=4,
                       n_actions=3,
                       mem_size=100000,
                       batch_size=64,
                       epsilon_end=0.0)
    agent.load_model()
    scores = []
Ejemplo n.º 4
0
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--numTeammates', type=int, default=0)
    parser.add_argument('--numOpponents', type=int, default=1)
    parser.add_argument('--numEpisodes', type=int, default=1)
    parser.add_argument('--saveFile', type=str, default="q_agent.model")
    args = parser.parse_args()
    num_teammates = args.numTeammates
    num_opponents = args.numOpponents
    n_games = args.numEpisodes
    save_file = args.saveFile
    lr = 0.01
    epsilon = 1
    discount_factor = 0.9
    # Useful Instances:
    hfo = HFOEnvironment()
    hfo.connectToServer(feature_set=HIGH_LEVEL_FEATURE_SET, server_port=6000)
    env = DiscreteHighLevelFeatures(hfo.getState(), num_teammates,
                                    num_opponents)
    actions = Action()
    agent = QLearner(num_states=env.get_num_states(),
                     num_actions=actions.get_num_actions(),
                     epsilon=epsilon,
                     learning_rate=lr,
                     discount_factor=discount_factor,
                     save_file=save_file)
    # Saving lists
    scores = []
    eps_history = []
    for i in range(n_games):
        print("\n<< {}/{} Game >> eps={}".format(i, n_games, agent.epsilon))
Ejemplo n.º 5
0
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--numTeammates', type=int, default=0)
    parser.add_argument('--numOpponents', type=int, default=1)
    parser.add_argument('--numEpisodes', type=int, default=1)
    parser.add_argument('--loadFile', type=str)
    args = parser.parse_args()
    num_teammates = args.numTeammates
    num_opponents = args.numOpponents
    n_games = args.numEpisodes
    load_file = args.loadFile
    if load_file is None:
        raise FileNotFoundError()
    # Useful Instances:
    hfo = HFOEnvironment()
    hfo.connectToServer(feature_set=HIGH_LEVEL_FEATURE_SET, server_port=6000)
    env = DiscreteHighLevelFeatures(hfo.getState(), num_teammates,
                                    num_opponents)
    actions = Action()
    agent = QLearner(num_states=env.get_num_states(),
                     num_actions=actions.get_num_actions())
    agent.load_q_table(load_file)
    # Saving lists
    scores = []
    eps_history = []
    for i in range(n_games):
        print("\n<< {}/{} Game >> eps={}".format(i, n_games, agent.epsilon))
        game_status = IN_GAME
        score = 0
        while game_status == IN_GAME:
Ejemplo n.º 6
0
from hfo import HFOEnvironment, HIGH_LEVEL_FEATURE_SET, IN_GAME, MOVE, SHOOT, \
    DRIBBLE, SERVER_DOWN, QUIT, MOVE_TO, INTERCEPT, DRIBBLE_TO, KICK_TO, NOOP

from environement_features.discrete_features_v2 import DiscreteFeaturesV2
from settings import CONFIG_DIR


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--port', type=int, default=6000)

    args = parser.parse_args()
    port = args.port
    
    hfo = HFOEnvironment()
    hfo.connectToServer(feature_set=HIGH_LEVEL_FEATURE_SET, server_port=port,
                        config_dir=CONFIG_DIR)
    for i in range(1):
        status = IN_GAME
        score = 0
        NUM_TEAMMATES = 0
        NUM_OPPONENTS = 1
        observation = hfo.getState()
        env = DiscreteFeaturesV2(num_team=NUM_TEAMMATES, num_op=NUM_OPPONENTS)
        went_to_the_corner = False
        ep = 0
        print("NEW GAME:")
        for i in range(4):
            print("New game:")
            print("Status: ",hfo.step())
Ejemplo n.º 7
0
class HFOAttackingPlayer(object):
    def __init__(self, config_dir=settings.CONFIG_DIR, agent_id=0, port=6000,
                 server_addr='localhost', num_opponents=0, num_teammates=0):
        self.hfo = HFOEnvironment()
        self.config_dir = config_dir
        self.port = port
        self.server_addr = server_addr
        self.num_teammates = num_teammates
        self.num_opponents = num_opponents
        self.agent_id = agent_id
        self.episode = 0
        self.num_steps = 0
        self.status = IN_GAME
    
    def reset(self) -> list:
        self.status = IN_GAME
        self.episode += 1
        self.num_steps = 0
        return self.hfo.getState()
    
    def get_state(self):
        # TODO deprecated
        return self.hfo.getState()
    
    def get_observation_array(self):
        return self.hfo.getState()

    def connect_to_server(self):
        """ Establish connection with HFO server """
        self.hfo.connectToServer(
            HIGH_LEVEL_FEATURE_SET,
            self.config_dir,
            self.port,
            self.server_addr,
            team_name='base_left',
            play_goalie=False)

    def step(self, hfo_action, has_ball: bool, filter=True) -> (int, list):
        """
        Method that serves as an interface between a script controlling the
        agent and the environment_features. Method returns the current status
        of the episode and nextState
        @param hfo_action: [int, tuple]
        @param has_ball:
        @param filter:
        """
        if filter:
            if type(hfo_action) is tuple:
                action = hfo_action[0]
                params = hfo_action[1:]
            else:
                action = hfo_action
                params = ()
            action_args = BaseActions.ActionManager.valid_action(
                action, has_ball, params)
        else:
            action_args = hfo_action
        self.hfo.act(*action_args)
        self.num_steps += 1
        self.status = self.hfo.step()
        return self.status, self.hfo.getState()

    def quit_game(self):
        self.hfo.act(QUIT)
    
    def get_game_status(self) -> int:
        return self.status
    
    def in_game(self) -> bool:
        if self.status == IN_GAME:
            return True
        else:
            return False
    
    def scored_goal(self) -> bool:
        return self.status == GOAL
    
    def check_server_is_up(self):
        if self.hfo.step() == SERVER_DOWN:
            raise ServerDownError("Server is Down!!")
Ejemplo n.º 8
0
from hfo import HFOEnvironment, HIGH_LEVEL_FEATURE_SET, IN_GAME, SERVER_DOWN, \
    QUIT, DRIBBLE_TO, DRIBBLE

from environement_features.discrete_features import \
    DiscreteHighLevelFeatures
from settings import CONFIG_DIR

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--port', type=int, default=6000)

    args = parser.parse_args()
    port = args.port

    hfo = HFOEnvironment()
    hfo.connectToServer(feature_set=HIGH_LEVEL_FEATURE_SET,
                        server_port=port,
                        config_dir=CONFIG_DIR)
    print("Connected")
    for i in range(1):
        status = IN_GAME
        score = 0
        NUM_TEAMMATES = 0
        NUM_OPPONENTS = 2
        observation = hfo.getState()
        env = DiscreteHighLevelFeatures(num_team=NUM_TEAMMATES,
                                        num_op=NUM_OPPONENTS)
        ep_counter = 0
        while status == IN_GAME:
            hfo.act(DRIBBLE)