parameters: (env, screen, x_max, y_max, subsampling, font)
docstring: Main loop of the oracle agent.
docstring_summary: Main loop of the oracle agent.
docstring_tokens: Main loop of the oracle agent .
function: def loop(env, screen, x_max, y_max, subsampling, font):
    """Main loop of the oracle agent."""
    screen_buffer = np.zeros((x_max, y_max, 3), np.uint8)
    action = np.array([0, 0, 0, 0])
    action_spec = env.action_spec()
    sum_rewards = 0
    sum_rewards_at_goal = 0
    previous_goal_id = None
    while True:
  
      # Take a step given the previous action and record the reward.
      observation, reward, done, info = env.step(action)
      sum_rewards += reward
      if (reward > 0) and (info['current_goal_id'] is not previous_goal_id):
        sum_rewards_at_goal += reward
      previous_goal_id = info['current_goal_id']
      if done:
        print('Episode reward: {}'.format(sum_rewards))
        if FLAGS.stats_path:
          with open(FLAGS.stats_path, 'a') as f:
            f.write(str(sum_rewards) + '\t' + str(sum_rewards_at_goal) + '\n')
        sum_rewards = 0
        sum_rewards_at_goal = 0
  
      # Determine the next pano and bearing to that pano.
      current_pano_id = info['current_pano_id']
      next_pano_id = info['next_pano_id']
      bearing_info = info['bearing_to_next_pano']
      bearing = observation['ground_truth_direction']
      logging.info('Current pano: %s, next pano %s at %f (%f)',
                   current_pano_id, next_pano_id, bearing, bearing_info)
      current_step = info.get('current_step', -1)
  
      # Bearing-based navigation.
      if bearing > FLAGS.horizontal_rot:
        if bearing > FLAGS.horizontal_rot + 2 * FLAGS.horizontal_rot:
          action = 3 * FLAGS.horizontal_rot * action_spec['horizontal_rotation']
        else:
          action = FLAGS.horizontal_rot * action_spec['horizontal_rotation']
      elif bearing < -FLAGS.horizontal_rot:
        if bearing < -FLAGS.horizontal_rot - 2 * FLAGS.horizontal_rot:
          action = -3 * FLAGS.horizontal_rot * action_spec['horizontal_rotation']
        else:
          action = -FLAGS.horizontal_rot * action_spec['horizontal_rotation']
      else:
        action = action_spec['move_forward']
  
      # Draw the observations (view, graph, thumbnails, instructions).
      view_image = observation['view_image_hwc']
      graph_image = observation['graph_image_hwc']
      screen_buffer[:FLAGS.width, :FLAGS.height, :] = view_image.swapaxes(0, 1)
      screen_buffer[:FLAGS.width, FLAGS.height:(FLAGS.height*2), :] = (
          graph_image.swapaxes(0, 1))
      thumb_image = np.copy(observation['thumbnails'])
      for k in range(FLAGS.max_instructions+1):
        if k != current_step:
          thumb_image[k, :, :, :] = thumb_image[k, :, :, :] / 2
      thumb_image = thumb_image.reshape(
          FLAGS.height * (FLAGS.max_instructions + 1), FLAGS.width, 3)
      thumb_image = thumb_image.swapaxes(0, 1)
      thumb_image = thumb_image[::subsampling, ::subsampling, :]
      screen_buffer[FLAGS.width:(FLAGS.width+thumb_image.shape[0]),
                    0:thumb_image.shape[1],
                    :] = thumb_image
      pygame.surfarray.blit_array(screen, screen_buffer)
      instructions = observation['instructions'].decode('utf-8')
      instructions = instructions.split('|')
      instructions.append('[goal]')
      x_min = x_max - FLAGS.width_text + 10
      y = 10
      for k in range(len(instructions)):
        instruction = instructions[k]
        if k == current_step:
          color = COLOR_WAYPOINT
        elif k == len(instructions) - 1:
          color = COLOR_GOAL
        else:
          color = COLOR_INSTRUCTION
        blit_instruction(screen, instruction, font, color, x_min, y, x_max)
        y += int(FLAGS.height / subsampling)
      pygame.display.update()
  
      for event in pygame.event.get():
        if (event.type == pygame.QUIT or
            (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE)):
          return
        if event.type == pygame.KEYDOWN:
          if event.key == pygame.K_p:
            filename = time.strftime('/tmp/oracle_agent_%Y%m%d_%H%M%S.bmp')
            pygame.image.save(screen, filename)
function_tokens: def loop ( env , screen , x_max , y_max , subsampling , font ) : screen_buffer = np . zeros ( ( x_max , y_max , 3 ) , np . uint8 ) action = np . array ( [ 0 , 0 , 0 , 0 ] ) action_spec = env . action_spec ( ) sum_rewards = 0 sum_rewards_at_goal = 0 previous_goal_id = None while True : # Take a step given the previous action and record the reward. observation , reward , done , info = env . step ( action ) sum_rewards += reward if ( reward > 0 ) and ( info [ 'current_goal_id' ] is not previous_goal_id ) : sum_rewards_at_goal += reward previous_goal_id = info [ 'current_goal_id' ] if done : print ( 'Episode reward: {}' . format ( sum_rewards ) ) if FLAGS . stats_path : with open ( FLAGS . stats_path , 'a' ) as f : f . write ( str ( sum_rewards ) + '\t' + str ( sum_rewards_at_goal ) + '\n' ) sum_rewards = 0 sum_rewards_at_goal = 0 # Determine the next pano and bearing to that pano. current_pano_id = info [ 'current_pano_id' ] next_pano_id = info [ 'next_pano_id' ] bearing_info = info [ 'bearing_to_next_pano' ] bearing = observation [ 'ground_truth_direction' ] logging . info ( 'Current pano: %s, next pano %s at %f (%f)' , current_pano_id , next_pano_id , bearing , bearing_info ) current_step = info . get ( 'current_step' , - 1 ) # Bearing-based navigation. if bearing > FLAGS . horizontal_rot : if bearing > FLAGS . horizontal_rot + 2 * FLAGS . horizontal_rot : action = 3 * FLAGS . horizontal_rot * action_spec [ 'horizontal_rotation' ] else : action = FLAGS . horizontal_rot * action_spec [ 'horizontal_rotation' ] elif bearing < - FLAGS . horizontal_rot : if bearing < - FLAGS . horizontal_rot - 2 * FLAGS . horizontal_rot : action = - 3 * FLAGS . horizontal_rot * action_spec [ 'horizontal_rotation' ] else : action = - FLAGS . horizontal_rot * action_spec [ 'horizontal_rotation' ] else : action = action_spec [ 'move_forward' ] # Draw the observations (view, graph, thumbnails, instructions). view_image = observation [ 'view_image_hwc' ] graph_image = observation [ 'graph_image_hwc' ] screen_buffer [ : FLAGS . width , : FLAGS . height , : ] = view_image . swapaxes ( 0 , 1 ) screen_buffer [ : FLAGS . width , FLAGS . height : ( FLAGS . height * 2 ) , : ] = ( graph_image . swapaxes ( 0 , 1 ) ) thumb_image = np . copy ( observation [ 'thumbnails' ] ) for k in range ( FLAGS . max_instructions + 1 ) : if k != current_step : thumb_image [ k , : , : , : ] = thumb_image [ k , : , : , : ] / 2 thumb_image = thumb_image . reshape ( FLAGS . height * ( FLAGS . max_instructions + 1 ) , FLAGS . width , 3 ) thumb_image = thumb_image . swapaxes ( 0 , 1 ) thumb_image = thumb_image [ : : subsampling , : : subsampling , : ] screen_buffer [ FLAGS . width : ( FLAGS . width + thumb_image . shape [ 0 ] ) , 0 : thumb_image . shape [ 1 ] , : ] = thumb_image pygame . surfarray . blit_array ( screen , screen_buffer ) instructions = observation [ 'instructions' ] . decode ( 'utf-8' ) instructions = instructions . split ( '|' ) instructions . append ( '[goal]' ) x_min = x_max - FLAGS . width_text + 10 y = 10 for k in range ( len ( instructions ) ) : instruction = instructions [ k ] if k == current_step : color = COLOR_WAYPOINT elif k == len ( instructions ) - 1 : color = COLOR_GOAL else : color = COLOR_INSTRUCTION blit_instruction ( screen , instruction , font , color , x_min , y , x_max ) y += int ( FLAGS . height / subsampling ) pygame . display . update ( ) for event in pygame . event . get ( ) : if ( event . type == pygame . QUIT or ( event . type == pygame . KEYDOWN and event . key == pygame . K_ESCAPE ) ) : return if event . type == pygame . KEYDOWN : if event . key == pygame . K_p : filename = time . strftime ( '/tmp/oracle_agent_%Y%m%d_%H%M%S.bmp' ) pygame . image . save ( screen , filename )