Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main(_):
players = FLAGS.players.split(';') if FLAGS.players else ''
assert not (any(['agent' in player for player in players])
), ('Player type \'agent\' can not be used with play_game.')
cfg = config.Config({
'action_set': FLAGS.action_set,
'dump_full_episodes': True,
'players': players,
'real_time': FLAGS.real_time,
})
if FLAGS.level:
cfg['level'] = FLAGS.level
env = football_env.FootballEnv(cfg)
if FLAGS.render:
env.render()
env.reset()
try:
while True:
_, _, done, _ = env.step([])
if done:
env.reset()
Each player is defined by a string like:
"$player_name:left_players=?,right_players=?,$param1=?,$param2=?...."
number_of_left_players_agent_controls: Number of left players an agent
controls.
number_of_right_players_agent_controls: Number of right players an agent
controls.
channel_dimensions: (width, height) tuple that represents the dimensions of
SMM or pixels representation.
other_config_options: dict that allows directly setting other options in
the Config
Returns:
Google Research Football environment.
"""
assert env_name
scenario_config = config.Config({'level': env_name}).ScenarioConfig()
players = [('agent:left_players=%d,right_players=%d' % (
number_of_left_players_agent_controls,
number_of_right_players_agent_controls))]
# Enable MultiAgentToSingleAgent wrapper?
multiagent_to_singleagent = False
if scenario_config.control_all_players:
if (number_of_left_players_agent_controls in [0, 1] and
number_of_right_players_agent_controls in [0, 1]):
multiagent_to_singleagent = True
players = [('agent:left_players=%d,right_players=%d' %
(scenario_config.controllable_left_players
if number_of_left_players_agent_controls else 0,
scenario_config.controllable_right_players
if number_of_right_players_agent_controls else 0))]
if number_of_left_players_agent_controls else 0,
scenario_config.controllable_right_players
if number_of_right_players_agent_controls else 0))]
if extra_players is not None:
players.extend(extra_players)
config_values = {
'dump_full_episodes': write_full_episode_dumps,
'dump_scores': write_goal_dumps,
'players': players,
'level': env_name,
'tracesdir': logdir,
'write_video': write_video,
}
config_values.update(other_config_options)
c = config.Config(config_values)
env = football_env.FootballEnv(c)
if render:
env.render()
if multiagent_to_singleagent:
env = wrappers.MultiAgentToSingleAgent(
env, number_of_left_players_agent_controls,
number_of_right_players_agent_controls)
if dump_frequency > 1:
env = wrappers.PeriodicDumpWriter(env, dump_frequency)
env = _apply_output_wrappers(
env, rewards, representation, channel_dimensions,
(number_of_left_players_agent_controls +
number_of_right_players_agent_controls == 1), stacked)
return env
def replay(self, dump, fps=10, config_update={}, directory=None, render=True):
replay = self.load_dump(dump)
trace = self.__modify_trace(replay, fps)
fd, temp_path = tempfile.mkstemp(suffix='.dump')
with open(temp_path, 'wb') as f:
for step in trace:
six.moves.cPickle.dump(step, f)
assert replay[0]['debug']['frame_cnt'] == 0, (
'Trace does not start from the beginning of the episode, can not replay')
cfg = config.Config(replay[0]['debug']['config'])
cfg['players'] = self.__build_players(temp_path, cfg['players'])
config_update['physics_steps_per_frame'] = int(100 / fps)
config_update['real_time'] = False
if directory:
config_update['tracesdir'] = directory
config_update['write_video'] = True
cfg.update(config_update)
env = football_env.FootballEnv(cfg)
if render:
env.render()
env.reset()
done = False
try:
while not done:
_, _, done, _ = env.step([])
except KeyboardInterrupt:
def dump_to_video(self, dump_file):
dump = self.load_dump(dump_file)
cfg = config.Config(dump[0]['debug']['config'])
cfg['dump_full_episodes'] = True
cfg['write_video'] = True
cfg['display_game_stats'] = True
processor = observation_processor.ObservationProcessor(cfg)
processor.write_dump('episode_done')
for frame in dump:
processor.update(frame)