code: def main():
      env = gym.make(
          "MountainCar-v0"
      )  # MountainCar-v0:expected reward > -120
      action_dim = env.action_space.n  # CartPole-v0: 2
      obs_shape = env.observation_space.shape  # CartPole-v0: (4,)
  
      rpm = ReplayMemory(MEMORY_SIZE)  # DQN的经验回放池
  
      # 根据parl框架构建agent
      model = Model(act_dim=action_dim)
      algorithm = DQN(
          model, act_dim=action_dim, gamma=GAMMA, lr=LEARNING_RATE
      )
      agent = Agent(
          algorithm,
          obs_dim=obs_shape[0],
          act_dim=action_dim,
          e_greed=0.1,  # 有一定概率随机选取动作，探索
          e_greed_decrement=1e-6,
      )  # 随着训练逐步收敛，探索的程度慢慢降低
  
      # 加载模型
      # save_path = './dqn_model.ckpt'
      # agent.restore(save_path)
  
      # 先往经验池里存一些数据，避免最开始训练的时候样本丰富度不够
      while len(rpm) < MEMORY_WARMUP_SIZE:
          run_episode(env, agent, rpm)
  
      max_episode = 2000
  
      # start train
      episode = 0
      while (
          episode < max_episode
      ):  # 训练max_episode个回合，test部分不计算入episode数量
          # train part
          for i in range(0, 50):
              total_reward = run_episode(env, agent, rpm)
              episode += 1
  
          # test part
          eval_reward = evaluate(
              env, agent, render=False
          )  # render=True 查看显示效果
          logger.info(
              "episode:{}    e_greed:{}   test_reward:{}".format(
                  episode, agent.e_greed, eval_reward
              )
          )
  
      # 训练结束，保存模型
      save_path = "./dqn_model.ckpt"
      agent.save(save_path)
  
file_path: PARL/examples/tutorials/homework/lesson3/dqn_mountaincar/train.py
imports: import gym
  import parl
  import paddle
  import os
  import gym
  import numpy
  import parl
  from parl.utils import logger
  from model import Model
  from agent import Agent
  from parl.algorithms import DQN
  from replay_memory import ReplayMemory
label_desc: Use image node_id 3 for calling a global function with example usage: main() without return types