Exemplo n.º 1
0
 def _construct_players(self, definitions, config):
   result = []
   left_position = 0
   right_position = 0
   for definition in definitions:
     (name, d) = cfg.parse_player_definition(definition)
     config_name = 'player_{}'.format(name)
     if config_name in config:
       config[config_name] += 1
     else:
       config[config_name] = 0
     try:
       player_factory = importlib.import_module(
           'gfootball.env.players.{}'.format(name))
     except ImportError as e:
       logging.warning('Failed loading player "%s"', name)
       logging.warning(e)
       exit(1)
     player_config = copy.deepcopy(config)
     player_config.update(d)
     player = player_factory.Player(player_config, self._config)
     if name == 'agent':
       assert not self._agent, 'Only one \'agent\' player allowed'
       self._agent = player
       self._agent_index = len(result)
       self._agent_left_position = left_position
       self._agent_right_position = right_position
     result.append(player)
     left_position += player.num_controlled_left_players()
     right_position += player.num_controlled_right_players()
     config['index'] += 1
   return result
Exemplo n.º 2
0
 def _construct_players(self, definitions, base_player_config):
     # assert 0, definitions
     result = []
     left_position = 0
     right_position = 0
     for definition in definitions:
         (name, d) = cfg.parse_player_definition(definition)
         # assert 0, (name, d)
         config_name = 'player_{}'.format(name)
         if config_name in base_player_config:
             base_player_config[config_name] += 1
         else:
             base_player_config[config_name] = 0
         if name in PLAYERS_BY_NAME:
             player_class = PLAYERS_BY_NAME[name]
         else:
             assert 0, 'Unknown player class: %s. Choose from: %s' % (
                 name, sorted(PLAYERS_BY_NAME))
         player_config = copy.deepcopy(base_player_config)
         player_config.update(d)
         if ('checkpoint' in player_config) and (player_config['checkpoint']
                                                 is not None):
             player_config['checkpoint'] = expanduser(
                 player_config['checkpoint'])
         player = player_class(player_config=player_config,
                               env_config=self._config)
         if name.startswith('agent'):
             assert not self._agent, 'Only one \'agent\' player allowed'
             assert player is not None
             self._agent = player
             self._agent_index = len(result)
             self._agent_left_position = left_position
             self._agent_right_position = right_position
         result.append(player)
         left_position += player.num_controlled_left_players()
         right_position += player.num_controlled_right_players()
         base_player_config['index'] += 1
     assert self._agent is not None
     return result
def main(_):

    left_player = 'ppo2_cnn:left_players=1,policy=gfootball_impala_cnn,checkpoint=/Users/stephen/Documents/football/checkpoints/11_vs_11_easy_stochastic_v2'
    right_player = 'ppo2_cnn:right_players=1,policy=gfootball_impala_cnn,checkpoint=/Users/stephen/Documents/football/checkpoints/11_vs_11_easy_stochastic_v2'
    players = [left_player, right_player]

    env_config_values = {
        'dump_full_episodes': False,
        'dump_scores': False,
        'players': players,
        'level': '11_vs_11_easy_stochastic',
        'tracesdir': '/Users/stephen/Documents/football/logs',  # logdir
        'write_video': False
    }

    env_config = config.Config(env_config_values)
    env = football_env.FootballEnv(env_config)
    env.reset()

    player_config = {'index': 2}
    name, definition = config.parse_player_definition(left_player)
    config_name = 'player_{}'.format(name)
    if config_name in player_config:
        player_config[config_name] += 1
    else:
        player_config[config_name] = 0
    player_config.update(definition)
    player_config['stacked'] = True
    player = Player(player_config, env_config)
    stacker = ObservationStacker(4)

    n_timesteps = 30000  # 10 games
    game_i = 0
    observations = []
    actions = []

    for i in range(n_timesteps):
        obs, _, done, _ = env.step([])
        obs_processed = observation_preprocessing.generate_smm([obs])
        obs_processed = stacker.get(obs_processed)
        observations.append(obs_processed)
        act = player.take_action([obs])[0]
        actions.append(full_action_set.index(act))
        if done:
            env.reset()
            stacker.reset()
            observations = np.squeeze(np.vstack(
                observations))  # should not be shape (3000, 72, 96, 16)
            actions = np.array(actions)  # should be shape (n_samples,)
            with open(
                    f'/Users/stephen/Documents/football/data/observations{game_i}.pkl',
                    'wb') as f:
                pickle.dump(observations, f)
            with open(
                    f'/Users/stephen/Documents/football/data/actions{game_i}.pkl',
                    'wb') as f:
                pickle.dump(actions, f)
            game_i += 1
            observations = []
            actions = []

    print('Done :)')