def test_corner(self, episode, factor, reverse): cfg = config.Config({ 'level': 'tests.corner_test', 'players': ['agent:left_players=1,right_players=1'], 'episode_number': episode, 'reverse_team_processing': reverse, }) env = football_env.FootballEnv(cfg) o = env.reset() done = False while not done: o, _, done, _ = env.step([ football_action_set.action_left, football_action_set.action_left ]) self.assertAlmostEqual(o[0]['ball'][0], -0.95 * factor, delta=0.1) self.assertAlmostEqual(o[0]['ball'][1], 0.4 * factor, delta=0.1) self.assertAlmostEqual(o[0]['right_team'][0][0], 1, delta=0.1) self.assertAlmostEqual(o[0]['right_team'][1][0], -0.95 * factor, delta=0.1) self.assertAlmostEqual(o[0]['left_team'][0][0], -0.95, delta=0.1) self.assertAlmostEqual(o[0]['left_team'][1][0], -0.9 * factor, delta=0.2) env.close()
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() except KeyboardInterrupt: logging.warning('Game stopped, writing dump...') env.write_dump('shutdown') exit(1)
def test_player_order_invariant(self): """Checks that environment behaves the same regardless of players order.""" players = ['agent:right_players=1', 'lazy:left_players=11'] cfg = config.Config({ 'level': 'tests.11_vs_11_hard_deterministic', 'players': players }) env = football_env.FootballEnv(cfg) actions = len(football_action_set.get_action_set(cfg)) hash_value1 = compute_hash(env, actions) players = [players[1], players[0]] cfg = config.Config({ 'level': 'tests.11_vs_11_hard_deterministic', 'players': players }) env = football_env.FootballEnv(cfg) hash_value2 = compute_hash(env, actions) self.assertEqual(hash_value1, hash_value2) env.close()
def test_different_action_formats(self): """Verify different action formats are accepted.""" cfg = config.Config() env = football_env.FootballEnv(cfg) env.reset() env.step(football_action_set.action_right) env.step([football_action_set.action_right]) env.step(np.array([football_action_set.action_right])) env.step(np.array(football_action_set.action_right)) env.close()
def test_setstate(self, seed): """Checks setState functionality.""" cfg1 = config.Config({ 'level': 'tests.symmetric', 'game_engine_random_seed': seed, 'reverse_team_processing': False }) cfg2 = config.Config({ 'level': 'tests.symmetric', 'game_engine_random_seed': seed + 10, 'reverse_team_processing': False }) env1 = football_env.FootballEnv(cfg1) env2 = football_env.FootballEnv(cfg2) initial_obs = env1.reset() env2.reset() initial_state = env1.get_state() env2.set_state(initial_state) random.seed(seed) actions = len(football_action_set.get_action_set(cfg1)) first_action = random.randint(0, actions - 1) first_obs, _, _, _ = env1.step(first_action) _, _, _, _ = env2.step(first_action) step = 0 limit = 10 if fast_run else 3000 while step < limit: step += 1 action = random.randint(0, actions - 1) if step % 10 == 0: env2.set_state(initial_state) self.compare_observations(initial_obs, env2.observation()) env2.step(first_action) self.compare_observations(first_obs, env2.observation()) env2.set_state(env1.get_state()) self.compare_observations(env1.observation(), env2.observation()) _, _, done1, _ = env1.step(action) _, _, done2, _ = env2.step(action) self.assertEqual(done1, done2) if done1: break env1.close() env2.close()
def test_multi_render(self): """Only one rendering instance allowed at a time.""" if 'UNITTEST_IN_DOCKER' in os.environ: # Rendering is not supported. return cfg = config.Config({}) env1 = football_env.FootballEnv(cfg) env1.render() env1.reset() env2 = football_env.FootballEnv(cfg) try: env2.render() except AssertionError: env1.close() env2.close() # It is still possible to render. env3 = football_env.FootballEnv(cfg) env3.reset() env3.close() return assert False, 'Exception expected'
def test_restore_after_reset(self): cfg = config.Config({ 'level': '11_vs_11_competition', }) env = football_env.FootballEnv(cfg) obs = env.reset() state = env.get_state() env.reset() env.set_state(state) obs_ = env.observation() state_ = env.get_state() env.step(0) # Test if can take step self.compare_observations(obs, obs_) self.assertEqual(state, state_)
def run_scenario(cfg, seed, queue, actions, render=False, validation=True): env = football_env.FootballEnv(cfg) if render: env.render() env.reset() if validation: env.tracker_setup(0, 999999999999999) done = False for action in actions: obs, _, done, _ = env.step([action, action]) queue.put(obs) if done: break queue.put(None) env.close()
def check_determinism(self, extensive=False): """Check that environment is deterministic.""" if 'UNITTEST_IN_DOCKER' in os.environ: return cfg = config.Config({'level': 'tests.11_vs_11_hard_deterministic'}) env = football_env.FootballEnv(cfg) actions = len(football_action_set.get_action_set(cfg)) for episode in range(1 if extensive else 2): hash_value = compute_hash(env, actions, extensive) if extensive: self.assertEqual(hash_value, 2258127135) elif episode % 2 == 0: self.assertEqual(hash_value, 716323440) else: self.assertEqual(hash_value, 1663893701) env.close()
def test__memory_usage(self): """Make sure memory usage is low when not recording videos.""" # This test has to go first, so that memory usage is not affected. if 'UNITTEST_IN_DOCKER' in os.environ: # Forge doesn't support rendering. return cfg = config.Config({'write_video': False}) env = football_env.FootballEnv(cfg) env.render() env.reset() initial_memory = self.memory_usage() for _ in range(100): _, _, _, _ = env.step(football_action_set.action_right) memory_usage = self.memory_usage() - initial_memory env.close() self.assertGreaterEqual(10000000, memory_usage)
def test_penalty(self): cfg = config.Config({ 'level': 'tests.penalty', 'players': ['agent:left_players=1'], }) env = football_env.FootballEnv(cfg) o = env.reset() done = False while not done: o, _, done, _ = env.step([football_action_set.action_sliding]) self.assertAlmostEqual(o[0]['ball'][0], -0.809, delta=0.01) self.assertAlmostEqual(o[0]['ball'][1], 0.0, delta=0.01) self.assertAlmostEqual(o[0]['right_team'][0][0], 1, delta=0.1) self.assertAlmostEqual(o[0]['right_team'][1][0], -0.75, delta=0.1) self.assertAlmostEqual(o[0]['left_team'][0][0], -0.95, delta=0.1) self.assertAlmostEqual(o[0]['left_team'][1][0], -0.70, delta=0.1) env.close()
def test_render(self): """Make sure rendering is not broken.""" if 'UNITTEST_IN_DOCKER' in os.environ: # Rendering is not supported. return cfg = config.Config({ 'level': 'tests.11_vs_11_hard_deterministic', }) env = football_env.FootballEnv(cfg) env.render() o = env.reset() hash = observation_hash(o) for _ in range(10): o, _, _, _ = env.step(football_action_set.action_right) hash = observation_hash(o, hash) self.assertEqual(hash, 845594868) env.close()
def test_goal(self, episode, reverse): cfg = config.Config({ 'level': 'tests.goal_test', 'players': ['agent:left_players=1,right_players=1'], 'episode_number': episode, 'reverse_team_processing': reverse, }) env = football_env.FootballEnv(cfg) o = env.reset() done = False while not done: o, _, done, _ = env.step([ football_action_set.action_right, football_action_set.action_right ]) self.assertAlmostEqual(o[0]['ball'][0], 0.0, delta=0.1) self.assertEqual(o[0]['score'][episode], 1) self.assertEqual(o[0]['score'][1 - episode], 0) env.close()
def offside_helper(self, episode, team2, reverse): cfg = config.Config({ 'level': 'tests.offside_test', 'players': ['agent:{}_players=1'.format(team2)], 'episode_number': episode, 'reverse_team_processing': reverse, }) env = football_env.FootballEnv(cfg) env.reset() o, _, done, _ = env.step(football_action_set.action_long_pass) done = False while not done and o[0]['right_team'][1][0] == 0: o, _, done, _ = env.step(football_action_set.action_idle) self.assertAlmostEqual(o[0]['ball'][0], 0.6, delta=0.4) self.assertAlmostEqual(o[0]['right_team'][0][0], 0.6, delta=0.4) self.assertAlmostEqual(o[0]['right_team'][1][0], 0.6, delta=0.4) self.assertAlmostEqual(o[0]['left_team'][0][0], -0.6, delta=0.4) self.assertAlmostEqual(o[0]['left_team'][1][0], -0.6, delta=0.4) env.close()
def create_environment( env_name='', stacked=False, representation='extracted', rewards='scoring', write_goal_dumps=False, write_full_episode_dumps=False, render=False, write_video=False, dump_frequency=1, logdir='', extra_players=None, number_of_left_players_agent_controls=1, number_of_right_players_agent_controls=0, channel_dimensions=(observation_preprocessing.SMM_WIDTH, observation_preprocessing.SMM_HEIGHT)): assert env_name players = [('agent:left_players=%d,right_players=%d' % (number_of_left_players_agent_controls, number_of_right_players_agent_controls))] if extra_players is not None: players.extend(extra_players) c = config.Config({ 'dump_full_episodes': write_full_episode_dumps, 'dump_scores': write_goal_dumps, 'players': players, 'level': env_name, 'tracesdir': logdir, 'write_video': write_video, }) env = football_env.FootballEnv(c) if render: env.render() 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 test_score_empty_goal(self): """Score on an empty goal.""" cfg = config.Config() env = football_env.FootballEnv(cfg) cfg['level'] = 'academy_empty_goal' last_o = env.reset()[0] for _ in range(120): o, reward, done, _ = env.step(football_action_set.action_right) o = o[0] if done: self.assertEqual(reward, 1) break self.assertFalse(done) self.assertGreaterEqual(o['ball'][0], last_o['ball'][0] - 0.01) self.assertGreaterEqual( o['left_team'][o['active']][0], last_o['left_team'][last_o['active']][0] - 0.01) last_o = o self.assertTrue(done) env.close()
def test_dynamic_render(self): """Verifies dynamic render support.""" if 'UNITTEST_IN_DOCKER' in os.environ: # Rendering is not supported. return cfg = config.Config({ 'level': 'tests.11_vs_11_hard_deterministic', }) env = football_env.FootballEnv(cfg) o = env.reset() for _ in range(10): o, _, _, _ = env.step(football_action_set.action_right) self.assertNotIn('frame', o[0]) env.render() self.assertIn('frame', env.observation()[0]) self.compare_observations(o, env.observation()) o, _, _, _ = env.step(football_action_set.action_right) self.assertIn('frame', env.observation()[0]) env.disable_render() self.compare_observations(o, env.observation()) env.close()