Beispiel #1
0
 def test_setstate(self, seed):
     """Checks setState functionality."""
     cfg = config.Config({
         'level': 'tests.symmetric',
         'game_engine_random_seed': seed
     })
     env1 = football_env.FootballEnv(cfg)
     env2 = football_env.FootballEnv(cfg)
     initial_obs = env1.reset()
     env2.reset()
     initial_state = env1.get_state()
     random.seed(seed)
     actions = len(football_action_set.get_action_set(cfg))
     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 generate_replay(self):
     """Generates replay of an episode."""
     cfg = config.Config()
     left_players = 2
     cfg.update({
         'action_set':
         'full',
         'level':
         'tests.corner_test',
         'dump_full_episodes':
         True,
         'players': [
             'agent:left_players={}'.format(left_players),
             'bot:right_players=1', 'lazy:right_players=1'
         ],
         'tracesdir':
         test_tmpdir
     })
     env = football_env.FootballEnv(cfg)
     env.reset()
     actions_cnt = len(football_action_set.get_action_set(cfg))
     done = False
     step = 0
     while not done:
         step += 1
         actions = [(step + x) % actions_cnt for x in range(left_players)]
         _, _, done, _ = env.step(actions)
     env.close()
Beispiel #3
0
 def reset(self):
     """Reset environment for a new episode using a given config."""
     self._episode_start = timeit.default_timer()
     self._action_set = football_action_set.get_action_set(self._config)
     self._trace = observation_processor.ObservationProcessor(self._config)
     self._cumulative_reward = 0
     self._step_count = 0
     self._env.reset(self._trace)
     self._env_state = 'game_started'
Beispiel #4
0
 def reset(self, inc=1):
     """Reset environment for a new episode using a given config."""
     self._episode_start = timeit.default_timer()
     self._action_set = football_action_set.get_action_set(self._config)
     trace = observation_processor.ObservationProcessor(self._config)
     self._cumulative_reward = 0
     self._step_count = 0
     self._trace = trace
     self._reset(self._env.game_config.render, inc=inc)
     while not self._retrieve_observation():
         self._env.step()
     return True
Beispiel #5
0
 def __init__(self, config):
   self._config = config
   player_config = {'index': 0}
   # There can be at most one agent at a time. We need to remember its
   # team and the index on the team to generate observations appropriately.
   self._agent = None
   self._agent_index = -1
   self._players = self._construct_players(config['players'], player_config)
   self._env = football_env_wrapper.FootballEnvWrapper(self._config)
   self._num_actions = len(football_action_set.get_action_set(self._config))
   self.last_observation = None
   self._last_swapped_sides = False
Beispiel #6
0
 def __init__(self, config):
     self._config = config
     player_config = {'index': 0}
     # There can be at most one agent at a time. We need to remember its
     # team and the index on the team to generate observations appropriately.
     self._agent = None
     self._agent_index = -1
     self._agent_left_position = -1
     self._agent_right_position = -1
     self._players = self._construct_players(config['players'],
                                             player_config)
     self._env = football_env_core.FootballEnvCore(self._config)
     self._num_actions = len(
         football_action_set.get_action_set(self._config))
     self._cached_observation = None
Beispiel #7
0
 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, 4203104251)
         elif episode % 2 == 0:
             self.assertEqual(hash_value, 716323440)
         else:
             self.assertEqual(hash_value, 1663893701)
     env.close()
Beispiel #8
0
 def test_render_state_equals_norender(self, seed):
     """Checks that rendering game state is the same as non-rendering."""
     if 'UNITTEST_IN_DOCKER' in os.environ:
         # Forge doesn't support rendering.
         return
     processes = []
     cfg1 = config.Config({
         'level':
         'tests.symmetric',
         'game_engine_random_seed':
         seed,
         'players': ['agent:left_players=1,right_players=1'],
         'reverse_team_processing':
         False,
     })
     cfg2 = config.Config({
         'level':
         'tests.symmetric',
         'game_engine_random_seed':
         seed,
         'players': ['agent:left_players=1,right_players=1'],
         'reverse_team_processing':
         False,
     })
     random.seed(seed)
     action_cnt = len(football_action_set.get_action_set(cfg1))
     actions = [random.randint(0, action_cnt - 1) for _ in range(50)]
     queue1 = Queue()
     thread1 = threading.Thread(target=run_scenario,
                                args=(cfg1, seed, queue1, actions, False,
                                      False))
     thread1.start()
     queue2 = Queue()
     thread2 = threading.Thread(target=run_scenario,
                                args=(cfg2, seed, queue2, actions, True,
                                      False))
     thread2.start()
     while True:
         o1 = queue1.get()
         o2 = queue2.get()
         if not o1 or not o2:
             self.assertEqual(o1, o2)
             break
         self.compare_observations(o1, o2)
     thread1.join()
     thread2.join()
Beispiel #9
0
 def test_symmetry(self, seed):
     """Checks game symmetry."""
     processes = []
     cfg1 = config.Config({
         'level':
         'tests.symmetric',
         'game_engine_random_seed':
         seed,
         'players': ['agent:left_players=1,right_players=1'],
         'reverse_team_processing':
         False,
     })
     cfg2 = config.Config({
         'level':
         'tests.symmetric',
         'game_engine_random_seed':
         seed,
         'players': ['agent:left_players=1,right_players=1'],
         'reverse_team_processing':
         True,
     })
     random.seed(seed)
     action_cnt = len(football_action_set.get_action_set(cfg1))
     actions = [
         random.randint(0, action_cnt - 1)
         for _ in range(10 if fast_run else 3000)
     ]
     queue1 = Queue()
     thread1 = threading.Thread(target=run_scenario,
                                args=(cfg1, queue1, actions))
     thread1.start()
     queue2 = Queue()
     thread2 = threading.Thread(target=run_scenario,
                                args=(cfg2, queue2, actions))
     thread2.start()
     while True:
         o1 = queue1.get()
         o2 = queue2.get()
         if not o1 or not o2:
             self.assertEqual(o1, o2)
             break
         self.compare_observations(o1[:1], o2[1:])
         self.compare_observations(o2[:1], o1[1:])
     thread1.join()
     thread2.join()
Beispiel #10
0
 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_action_from_basic_action_set(self):
        action_set = football_action_set.get_action_set(
            {'action_set': 'default'})
        self.assertEqual(named_action_from_action_set(action_set, 1),
                         football_action_set.action_left)
        self.assertEqual(named_action_from_action_set(action_set, 12),
                         football_action_set.action_shot)

        self.assertEqual(named_action_from_action_set(action_set, np.int32(1)),
                         football_action_set.action_left)
        self.assertRaises(Exception, named_action_from_action_set, action_set,
                          np.int32(100))

        self.assertEqual(
            named_action_from_action_set(action_set,
                                         football_action_set.action_left),
            football_action_set.action_left)
        self.assertRaises(Exception, named_action_from_action_set, action_set,
                          100)
def flip_single_action(action, config):
    """Actions corresponding to the field rotated by 180 degrees."""
    action = football_action_set.named_action_from_action_set(
        football_action_set.get_action_set(config), action)
    if action == football_action_set.action_left:
        return football_action_set.action_right
    if action == football_action_set.action_top_left:
        return football_action_set.action_bottom_right
    if action == football_action_set.action_top:
        return football_action_set.action_bottom
    if action == football_action_set.action_top_right:
        return football_action_set.action_bottom_left
    if action == football_action_set.action_right:
        return football_action_set.action_left
    if action == football_action_set.action_bottom_right:
        return football_action_set.action_top_left
    if action == football_action_set.action_bottom:
        return football_action_set.action_top
    if action == football_action_set.action_bottom_left:
        return football_action_set.action_top_right
    return action
Beispiel #13
0
    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:

                if hash_value != 1174966789:
                    self.assertEqual(hash_value, 2245893576)
            elif episode % 2 == 0:

                if hash_value != 2275067030:
                    self.assertEqual(hash_value, 4024823270)
            else:

                if hash_value != 2045063811:
                    self.assertEqual(hash_value, 1264083657)
        env.close()
Beispiel #14
0
  def add_step(self, o):
    # Write video if requested.
    if self._video_writer:
      frame = get_frame(o)
      frame = frame[..., ::-1]
      frame = cv2.resize(frame, self._frame_dim, interpolation=cv2.INTER_AREA)
      writer = TextWriter(frame, self._frame_dim[0] - 300)
      if self._config['custom_display_stats']:
        for line in self._config['custom_display_stats']:
          writer.write(line)
      if self._config['display_game_stats']:
        writer.write('SCORE: %d - %d' % (o['score'][0], o['score'][1]))
        writer.write('BALL OWNED TEAM: %d' % (o['ball_owned_team']))
        writer.write('BALL OWNED PLAYER: %d' % (o['ball_owned_player']))
        writer.write('REWARD %.4f' % (o['reward']))
        writer.write('CUM. REWARD: %.4f' % (o['cumulative_reward']))
        writer = TextWriter(frame, 0)
        writer.write('FRAME: %d' % self._step_cnt)
        writer.write('TIME: %f' % (o._time - self._last_frame_time))
        sticky_actions = football_action_set.get_sticky_actions(self._config)

        players_info = {}
        for team in ['left', 'right']:
          sticky_actions_field = '%s_agent_sticky_actions' % team
          for player in range(len(o[sticky_actions_field])):
            assert len(sticky_actions) == len(o[sticky_actions_field][player])
            player_idx = o['%s_agent_controlled_player' % team][player]
            players_info[(team, player_idx)] = {}
            players_info[(team, player_idx)]['color'] = (
                0, 255, 0) if team == 'left' else (0, 255, 255)
            players_info[(team, player_idx)]['id'] = 'G' if o[
                '%s_team_roles' %
                team][player_idx] == e_PlayerRole_GK else str(player_idx)
            active_direction = None
            for i in range(len(sticky_actions)):
              if sticky_actions[i]._directional:
                if o[sticky_actions_field][player][i]:
                  active_direction = sticky_actions[i]
              else:
                players_info[(team, player_idx)][sticky_actions[i]._name] = \
                    o[sticky_actions_field][player][i]

            # Info about direction
            players_info[(team, player_idx)]['DIRECTION'] = \
                'O' if active_direction is None else active_direction._name
            if 'action' in o._trace['debug']:
              # Info about action
              players_info[(team, player_idx)]['ACTION'] = \
                  o['action'][player]._name

        write_players_state(writer, players_info)

        if 'baseline' in o._trace['debug']:
          writer.write('BASELINE: %.5f' % o._trace['debug']['baseline'])
        if 'logits' in o._trace['debug']:
          probs = softmax(o._trace['debug']['logits'])
          action_set = football_action_set.get_action_set(self._config)
          for action, prob in zip(action_set, probs):
            writer.write('%s: %.5f' % (action.name, prob), scale_factor=0.5)
        for d in o._debugs:
          writer.write(d)
      self._video_writer.write(frame)
    # Write the dump.
    temp_frame = None
    if 'frame' in o._trace['observation']:
      temp_frame = o._trace['observation']['frame']
      del o._trace['observation']['frame']

    # Add config to the first frame for our replay tools to use.
    if self._step_cnt == 0:
      o['debug']['config'] = self._config.get_dictionary()

    six.moves.cPickle.dump(o._trace, self._dump_file)
    if temp_frame is not None:
      o._trace['observation']['frame'] = temp_frame
    self._last_frame_time = o._time
    self._step_cnt += 1
def write_dump(name, trace, skip_visuals=False, config={}):
  if not skip_visuals:
    fd, temp_path = tempfile.mkstemp(suffix='.avi')
    if HIGH_RES:
      frame_dim = (1280, 720)
      fcc = cv2.VideoWriter_fourcc('p', 'n', 'g', ' ')
    else:
      fcc = cv2.VideoWriter_fourcc(*'XVID')
      frame_dim = (800, 600)
    video = cv2.VideoWriter(
        temp_path, fcc,
        constants.PHYSICS_STEPS_PER_SECOND / config['physics_steps_per_frame'],
        frame_dim)
    frame_cnt = 0
    if len(trace) > 0:
      time = trace[0]._time
    for o in trace:
      frame_cnt += 1
      frame = get_frame(o)
      frame = frame[..., ::-1]
      frame = cv2.resize(frame, frame_dim, interpolation=cv2.INTER_AREA)
      if config['display_game_stats']:
        writer = TextWriter(frame, 950 if HIGH_RES else 500)
        writer.write('SCORE: %d - %d' % (o['score'][0], o['score'][1]))
        writer.write('BALL OWNED TEAM: %d' % (o['ball_owned_team']))
        writer.write('BALL OWNED PLAYER: %d' % (o['ball_owned_player']))
        writer.write('REWARD %.4f' % (o['reward']))
        writer.write('CUM. REWARD: %.4f' % (o['cumulative_reward']))
        writer = TextWriter(frame, 0)
        writer.write('FRAME: %d' % frame_cnt)
        writer.write('TIME: %f' % (o._time - time))
        sticky_actions = football_action_set.get_sticky_actions(config)
        sticky_actions_field = 'left_agent_sticky_actions'
        if len(o[sticky_actions_field]) == 0:
          sticky_actions_field = 'right_agent_sticky_actions'
        assert len(sticky_actions) == len(o[sticky_actions_field][0])
        active_direction = None
        for i in range(len(sticky_actions)):
          if sticky_actions[i]._directional:
            if o[sticky_actions_field][0][i]:
              active_direction = sticky_actions[i]
          else:
            writer.write('%s: %d' % (sticky_actions[i]._name,
                                     o[sticky_actions_field][0][i]))
        writer.write('DIRECTION: %s' % ('NONE' if active_direction is None
                                        else active_direction._name))
        if 'action' in o._trace['debug']:
          writer.write('ACTION: %s' % (o['action'][0]._name))
        if 'baseline' in o._trace['debug']:
          writer.write('BASELINE: %.5f' % o._trace['debug']['baseline'])
        if 'logits' in o._trace['debug']:
          probs = softmax(o._trace['debug']['logits'])
          action_set = football_action_set.get_action_set(config)
          for action, prob in zip(action_set, probs):
            writer.write('%s: %.5f' % (action.name, prob), scale_factor=0.5)
        for d in o._debugs:
          writer.write(d)
      video.write(frame)
      for frame in o._additional_frames:
        frame = frame[..., ::-1]
        frame = cv2.resize(frame, frame_dim, interpolation=cv2.INTER_AREA)
        video.write(frame)
    video.release()
    os.close(fd)
    try:
      # For some reason sometimes the file is missing, so the code fails.
      if WRITE_FILES:
        shutil.copy2(temp_path, name + '.avi')
      os.remove(temp_path)
    except:
      logging.info(traceback.format_exc())
  to_pickle = []
  temp_frames = []
  for o in trace:
    if 'frame' in o._trace['observation']:
      temp_frames.append(o._trace['observation']['frame'])
      o._trace['observation']['frame'] = REMOVED_FRAME
    to_pickle.append(o._trace)
  if WRITE_FILES:
    with open(name + '.dump', 'wb') as f:
      six.moves.cPickle.dump(to_pickle, f)
  for o in trace:
    if 'frame' in o._trace['observation']:
      o._trace['observation']['frame'] = temp_frames.pop(0)
  logging.info('Dump written to %s.dump', name)
  if not skip_visuals:
    logging.info('Video written to %s.avi', name)
  return True
Beispiel #16
0
    def add_step(self, o):
        # Write video if requested.
        if self._video_writer:
            frame = get_frame(o)
            frame = frame[..., ::-1]
            frame = cv2.resize(frame,
                               self._frame_dim,
                               interpolation=cv2.INTER_AREA)
            writer = TextWriter(frame, self._frame_dim[0] - 300)
            if self._config['custom_display_stats']:
                for line in self._config['custom_display_stats']:
                    writer.write(line)
            if self._config['display_game_stats']:
                writer.write('SCORE: %d - %d' % (o['score'][0], o['score'][1]))
                writer.write('BALL OWNED TEAM: %d' % (o['ball_owned_team']))
                writer.write('BALL OWNED PLAYER: %d' %
                             (o['ball_owned_player']))
                writer.write('REWARD %.4f' % (o['reward']))
                writer.write('CUM. REWARD: %.4f' % (o['cumulative_reward']))
                writer = TextWriter(frame, 0)
                writer.write('FRAME: %d' % self._step_cnt)
                writer.write('TIME: %f' % (o._time - self._last_frame_time))
                sticky_actions = football_action_set.get_sticky_actions(
                    self._config)
                sticky_actions_field = 'left_agent_sticky_actions'
                if len(o[sticky_actions_field]) == 0:
                    sticky_actions_field = 'right_agent_sticky_actions'
                assert len(sticky_actions) == len(o[sticky_actions_field][0])
                active_direction = None
                for i in range(len(sticky_actions)):
                    if sticky_actions[i]._directional:
                        if o[sticky_actions_field][0][i]:
                            active_direction = sticky_actions[i]
                    else:
                        writer.write('%s: %d' %
                                     (sticky_actions[i]._name,
                                      o[sticky_actions_field][0][i]))
                writer.write('DIRECTION: %s' %
                             ('NONE' if active_direction is None else
                              active_direction._name))
                if 'action' in o._trace['debug']:
                    writer.write('ACTION: %s' % (o['action'][0]._name))
                if 'baseline' in o._trace['debug']:
                    writer.write('BASELINE: %.5f' %
                                 o._trace['debug']['baseline'])
                if 'logits' in o._trace['debug']:
                    probs = softmax(o._trace['debug']['logits'])
                    action_set = football_action_set.get_action_set(
                        self._config)
                    for action, prob in zip(action_set, probs):
                        writer.write('%s: %.5f' % (action.name, prob),
                                     scale_factor=0.5)
                for d in o._debugs:
                    writer.write(d)
            self._video_writer.write(frame)
        # Write the dump.
        temp_frame = None
        if 'frame' in o._trace['observation']:
            temp_frame = o._trace['observation']['frame']
            del o._trace['observation']['frame']

        # Add config to the first frame for our replay tools to use.
        if self._step_cnt == 0:
            o['debug']['config'] = self._config.get_dictionary()

        six.moves.cPickle.dump(o._trace, self._dump_file)
        if temp_frame is not None:
            o._trace['observation']['frame'] = temp_frame
        self._last_frame_time = o._time
        self._step_cnt += 1
Beispiel #17
0
def write_dump(name, trace, skip_visuals=False, config={}):
    if not skip_visuals:
        fd, temp_path = tempfile.mkstemp(suffix='.avi')
        if HIGH_RES:
            frame_dim = (1280, 720)
            fcc = cv2.VideoWriter_fourcc('p', 'n', 'g', ' ')
        else:
            fcc = cv2.VideoWriter_fourcc(*'XVID')
            frame_dim = (800, 600)
        video = cv2.VideoWriter(
            temp_path, fcc, constants.PHYSICS_STEPS_PER_SECOND /
            config['physics_steps_per_frame'], frame_dim)
        frame_cnt = 0
        if len(trace) > 0:
            time = trace[0]._time
        for o in trace:
            frame_cnt += 1
            frame = get_frame(o)
            frame = frame[..., ::-1]
            frame = cv2.resize(frame, frame_dim, interpolation=cv2.INTER_AREA)
            if config['display_game_stats']:
                writer = TextWriter(frame, 950 if HIGH_RES else 500)
                writer.write('SCORE: %d - %d' % (o._score[0], o._score[1]))
                writer.write('BALL OWNED TEAM: %d' % (o['ball_owned_team']))
                writer.write('BALL OWNED PLAYER: %d' %
                             (o['ball_owned_player']))
                writer.write('REWARD %.4f' % (o['reward']))
                writer.write('CUM. REWARD: %.4f' % (o['cumulative_reward']))
                writer = TextWriter(frame, 0)
                writer.write('FRAME: %d' % frame_cnt)
                writer.write('TIME: %f' % (o._time - time))
                time = o._time
                writer.write('Pressure: %d' % o._pressed_pressure)
                writer.write('Keeper pressure: %d' % o._pressed_keeper_rush)
                writer.write('Team pressure: %d' % o._pressed_team_pressure)
                writer.write('Sprint: %d' % o._pressed_sprint)
                writer.write('Dribble: %d' % o._pressed_dribble)
                writer.write('DIRECTION: %s' %
                             ('NONE' if o._pressed_direction is None else
                              o._pressed_direction.name))
                if 'action' in o._trace['debug']:
                    writer.write('ACTION: %s' % (o['action'][0].name))
                if 'baseline' in o._trace['debug']:
                    writer.write('BASELINE: %.5f' %
                                 o._trace['debug']['baseline'])
                if 'logits' in o._trace['debug']:
                    probs = softmax(o._trace['debug']['logits'])
                    action_set = football_action_set.get_action_set(config)
                    for action, prob in zip(action_set, probs):
                        writer.write('%s: %.5f' % (action.name, prob),
                                     scale_factor=0.5)
                for d in o._debugs:
                    writer.write(d)
            video.write(frame)
            for frame in o._additional_frames:
                frame = frame[..., ::-1]
                frame = cv2.resize(frame,
                                   frame_dim,
                                   interpolation=cv2.INTER_AREA)
                video.write(frame)
        video.release()
        os.close(fd)
        try:
            # For some reason sometimes the file is missing, so the code fails.
            tf.gfile.Copy(temp_path, name + '.avi', overwrite=True)
            os.remove(temp_path)
        except:
            logging.info(traceback.format_exc())
    to_pickle = []
    temp_frames = []
    for o in trace:
        if 'frame' in o._trace['observation']:
            temp_frames.append(o._trace['observation']['frame'])
            o._trace['observation']['frame'] = 'removed'
        to_pickle.append(o._trace)
    with tf.gfile.Open(name + '.dump', 'wb') as f:
        six.moves.cPickle.dump(to_pickle, f)
    for o in trace:
        if 'frame' in o._trace['observation']:
            o._trace['observation']['frame'] = temp_frames.pop(0)
    logging.info('Dump written to %s.dump', name)
    if not skip_visuals:
        logging.info('Video written to %s.avi', name)
    return True