code: def rl_loop():
    
  
    state = State()
  
    if FLAGS.checkpoint_dir:
      # Start from a partially trained model.
      initialize_from_checkpoint(state)
    else:
      # Play the first round of selfplay games with a fake model that returns
      # random noise. We do this instead of playing multiple games using a single
      # model bootstrapped with random noise to avoid any initial bias.
      wait(selfplay(state, 'bootstrap'))
  
      # Train a real model from the random selfplay games.
      tf_records = get_golden_chunk_records()
      state.iter_num += 1
      wait(train(state, tf_records))
  
      # Select the newly trained model as the best.
      state.best_model_name = state.train_model_name
      state.gen_num += 1
  
      # Run selfplay using the new model.
      wait(selfplay(state))
  
    # Now start the full training loop.
    while state.iter_num <= FLAGS.iterations:
      # Build holdout glob before incrementing the iteration number because we
      # want to run validation on the previous generation.
      holdout_glob = os.path.join(fsdb.holdout_dir(), '%06d-*' % state.iter_num,
                                  '*')
  
      # Train on shuffled game data from recent selfplay rounds.
      tf_records = get_golden_chunk_records()
      state.iter_num += 1
      wait(train(state, tf_records))
  
      if FLAGS.parallel_post_train:
        # Run eval, validation & selfplay in parallel.
        model_win_rate, _, _ = wait([
            evaluate_trained_model(state),
            validate(state, holdout_glob),
            selfplay(state)])
      else:
        # Run eval, validation & selfplay sequentially.
        model_win_rate = wait(evaluate_trained_model(state))
        wait(validate(state, holdout_glob))
        wait(selfplay(state))
  
      if model_win_rate >= FLAGS.gating_win_rate:
        # Promote the trained model to the best model and increment the generation
        # number.
        state.best_model_name = state.train_model_name
        state.gen_num += 1
docs: The main reinforcement learning (RL) loop.