code: def main():
      env = gym.make("CartPole-v0")
      # env = env.unwrapped # Cancel the minimum score limit
      obs_dim = env.observation_space.shape[0]
      act_dim = env.action_space.n
      logger.info("obs_dim {}, act_dim {}".format(obs_dim, act_dim))
  
      # 根据parl框架构建agent
      model = Model(obs_dim=obs_dim, act_dim=act_dim)
      alg = PolicyGradient(model, lr=LEARNING_RATE)
      agent = Agent(alg)
  
      # 加载模型并评估
      # if os.path.exists('./model.ckpt'):
      #     agent.restore('./model.ckpt')
      #     run_evaluate_episodes(agent, env, render=True)
      #     exit()
  
      for i in range(1000):
          obs_list, action_list, reward_list = run_train_episode(
              agent, env
          )
          if i % 10 == 0:
              logger.info(
                  "Episode {}, Reward Sum {}.".format(
                      i, sum(reward_list)
                  )
              )
  
          batch_obs = np.array(obs_list)
          batch_action = np.array(action_list)
          batch_reward = calc_reward_to_go(reward_list)
  
          agent.learn(batch_obs, batch_action, batch_reward)
          if (i + 1) % 100 == 0:
              # render=True 查看显示效果
              total_reward = run_evaluate_episodes(
                  agent, env, render=False
              )
              logger.info("Test reward: {}".format(total_reward))
  
      # save the parameters to ./model.ckpt
      agent.save("./model.ckpt")
  
file_path: PARL/examples/tutorials/parl2_dygraph/lesson4/policy_gradient/train.py
imports: import gym
  import parl
  import paddle
  import os
  import gym
  import numpy
  import parl
  from agent import Agent
  from model import Model
  from algorithm import PolicyGradient
  from parl.utils import logger
label_desc: Use image node_id 4 for calling a global function with example usage: main() without return types