(self, task, player)
Add observables corresponding to game statistics.

    Args:
      task: A `soccer.Task` instance.
      player: A `Walker` instance to which observables will be added.
Add observables corresponding to game statistics.
Add observables corresponding to game statistics .
def _add_player_stats_observables(self, task, player):
    """Add observables corresponding to game statistics.

    Args:
      task: A `soccer.Task` instance.
      player: A `Walker` instance to which observables will be added.
    """

    def _stats_vel_to_ball(physics):
      dir_ = (
          physics.bind(task.ball.geom).xpos -
          physics.bind(player.walker.root_body).xpos)
      vel_to_ball = np.dot(dir_[:2] / (np.linalg.norm(dir_[:2]) + 1e-7),
                           physics.bind(player.walker.root_body).cvel[3:5])
      return np.sum(vel_to_ball)

    player.walker.observables.add_observable(
        'stats_vel_to_ball', base_observable.Generic(_stats_vel_to_ball))

    def _stats_closest_vel_to_ball(physics):
      """Velocity to the ball if this walker is the team's closest."""
      closest = None
      min_team_dist_to_ball = np.inf
      for player_ in task.players:
        if player_.team == player.team:
          dist_to_ball = np.linalg.norm(
              physics.bind(task.ball.geom).xpos -
              physics.bind(player_.walker.root_body).xpos)
          if dist_to_ball < min_team_dist_to_ball:
            min_team_dist_to_ball = dist_to_ball
            closest = player_
      if closest is player:
        return _stats_vel_to_ball(physics)
      return 0.

    player.walker.observables.add_observable(
        'stats_closest_vel_to_ball',
        base_observable.Generic(_stats_closest_vel_to_ball))

    def _stats_veloc_forward(physics):
      """Player's forward velocity."""
      return player.walker.observables.veloc_forward(physics)

    player.walker.observables.add_observable(
        'stats_veloc_forward', base_observable.Generic(_stats_veloc_forward))

    def _stats_vel_ball_to_goal(physics):
      """Ball velocity towards opponents' goal."""
      if player.team == team_lib.Team.HOME:
        goal = task.arena.away_goal
      else:
        goal = task.arena.home_goal

      goal_center = (goal.upper + goal.lower) / 2.
      direction = goal_center - physics.bind(task.ball.geom).xpos
      ball_vel_observable = task.ball.observables.linear_velocity
      ball_vel = ball_vel_observable.observation_callable(physics)()

      norm_dir = np.linalg.norm(direction)
      normalized_dir = direction / norm_dir if norm_dir else direction
      return np.sum(np.dot(normalized_dir, ball_vel))

    player.walker.observables.add_observable(
        'stats_vel_ball_to_goal',
        base_observable.Generic(_stats_vel_ball_to_goal))

    def _stats_avg_teammate_dist(physics):
      """Compute average distance from `walker` to its teammates."""
      teammate_dists = []
      for other in task.players:
        if player is other:
          continue
        if other.team != player.team:
          continue
        dist = np.linalg.norm(
            physics.bind(player.walker.root_body).xpos -
            physics.bind(other.walker.root_body).xpos)
        teammate_dists.append(dist)
      return np.mean(teammate_dists) if teammate_dists else 0.

    player.walker.observables.add_observable(
        'stats_home_avg_teammate_dist',
        base_observable.Generic(_stats_avg_teammate_dist))

    def _stats_teammate_spread_out(physics):
      """Compute average distance from `walker` to its teammates."""
      return _stats_avg_teammate_dist(physics) > 5.

    player.walker.observables.add_observable(
        'stats_teammate_spread_out',
        base_observable.Generic(_stats_teammate_spread_out))

    def _stats_home_score(unused_physics):
      if (task.arena.detected_goal() and
          task.arena.detected_goal() == player.team):
        return 1.
      return 0.

    player.walker.observables.add_observable(
        'stats_home_score', base_observable.Generic(_stats_home_score))

    has_opponent = any([p.team != player.team for p in task.players])

    def _stats_away_score(unused_physics):
      if (has_opponent and task.arena.detected_goal() and
          task.arena.detected_goal() != player.team):
        return 1.
      return 0.

    player.walker.observables.add_observable(
        'stats_away_score', base_observable.Generic(_stats_away_score))
def _add_player_stats_observables ( self , task , player ) : def _stats_vel_to_ball ( physics ) : dir_ = ( physics . bind ( task . ball . geom ) . xpos - physics . bind ( player . walker . root_body ) . xpos ) vel_to_ball = np . dot ( dir_ [ : 2 ] / ( np . linalg . norm ( dir_ [ : 2 ] ) + 1e-7 ) , physics . bind ( player . walker . root_body ) . cvel [ 3 : 5 ] ) return np . sum ( vel_to_ball ) player . walker . observables . add_observable ( 'stats_vel_to_ball' , base_observable . Generic ( _stats_vel_to_ball ) ) def _stats_closest_vel_to_ball ( physics ) : """Velocity to the ball if this walker is the team's closest.""" closest = None min_team_dist_to_ball = np . inf for player_ in task . players : if player_ . team == player . team : dist_to_ball = np . linalg . norm ( physics . bind ( task . ball . geom ) . xpos - physics . bind ( player_ . walker . root_body ) . xpos ) if dist_to_ball < min_team_dist_to_ball : min_team_dist_to_ball = dist_to_ball closest = player_ if closest is player : return _stats_vel_to_ball ( physics ) return 0. player . walker . observables . add_observable ( 'stats_closest_vel_to_ball' , base_observable . Generic ( _stats_closest_vel_to_ball ) ) def _stats_veloc_forward ( physics ) : """Player's forward velocity.""" return player . walker . observables . veloc_forward ( physics ) player . walker . observables . add_observable ( 'stats_veloc_forward' , base_observable . Generic ( _stats_veloc_forward ) ) def _stats_vel_ball_to_goal ( physics ) : """Ball velocity towards opponents' goal.""" if player . team == team_lib . Team . HOME : goal = task . arena . away_goal else : goal = task . arena . home_goal goal_center = ( goal . upper + goal . lower ) / 2. direction = goal_center - physics . bind ( task . ball . geom ) . xpos ball_vel_observable = task . ball . observables . linear_velocity ball_vel = ball_vel_observable . observation_callable ( physics ) ( ) norm_dir = np . linalg . norm ( direction ) normalized_dir = direction / norm_dir if norm_dir else direction return np . sum ( np . dot ( normalized_dir , ball_vel ) ) player . walker . observables . add_observable ( 'stats_vel_ball_to_goal' , base_observable . Generic ( _stats_vel_ball_to_goal ) ) def _stats_avg_teammate_dist ( physics ) : """Compute average distance from `walker` to its teammates.""" teammate_dists = [ ] for other in task . players : if player is other : continue if other . team != player . team : continue dist = np . linalg . norm ( physics . bind ( player . walker . root_body ) . xpos - physics . bind ( other . walker . root_body ) . xpos ) teammate_dists . append ( dist ) return np . mean ( teammate_dists ) if teammate_dists else 0. player . walker . observables . add_observable ( 'stats_home_avg_teammate_dist' , base_observable . Generic ( _stats_avg_teammate_dist ) ) def _stats_teammate_spread_out ( physics ) : """Compute average distance from `walker` to its teammates.""" return _stats_avg_teammate_dist ( physics ) > 5. player . walker . observables . add_observable ( 'stats_teammate_spread_out' , base_observable . Generic ( _stats_teammate_spread_out ) ) def _stats_home_score ( unused_physics ) : if ( task . arena . detected_goal ( ) and task . arena . detected_goal ( ) == player . team ) : return 1. return 0. player . walker . observables . add_observable ( 'stats_home_score' , base_observable . Generic ( _stats_home_score ) ) has_opponent = any ( [ p . team != player . team for p in task . players ] ) def _stats_away_score ( unused_physics ) : if ( has_opponent and task . arena . detected_goal ( ) and task . arena . detected_goal ( ) != player . team ) : return 1. return 0. player . walker . observables . add_observable ( 'stats_away_score' , base_observable . Generic ( _stats_away_score ) )