code: def main():
      logger.set_dir(
          "./train_log/{}_{}".format(args.env, args.continuous_actions)
      )
      env = MAenv(args.env, args.continuous_actions)
      if args.continuous_actions:
          assert isinstance(env.action_space[0], spaces.Box)
  
      critic_in_dim = sum(env.obs_shape_n) + sum(env.act_shape_n)
  
      # build agents
      agents = []
      for i in range(env.n):
          model = MAModel(
              env.obs_shape_n[i],
              env.act_shape_n[i],
              critic_in_dim,
              args.continuous_actions,
          )
          algorithm = MADDPG(
              model,
              agent_index=i,
              act_space=env.action_space,
              gamma=GAMMA,
              tau=TAU,
              critic_lr=CRITIC_LR,
              actor_lr=ACTOR_LR,
          )
          agent = MAAgent(
              algorithm,
              agent_index=i,
              obs_dim_n=env.obs_shape_n,
              act_dim_n=env.act_shape_n,
              batch_size=BATCH_SIZE,
          )
          agents.append(agent)
  
      if args.restore:
          # restore model
          for i in range(len(agents)):
              model_file = args.model_dir + "/agent_" + str(i)
              if not os.path.exists(model_file):
                  raise Exception(
                      "model file {} does not exits".format(model_file)
                  )
              agents[i].restore(model_file)
  
      total_steps = 0
      total_episodes = 0
      while total_episodes <= args.max_episodes:
          # run an episode
          ep_reward, ep_agent_rewards, steps = run_episode(env, agents)
          summary.add_scalar(
              "train/episode_reward_wrt_episode",
              ep_reward,
              total_episodes,
          )
          summary.add_scalar(
              "train/episode_reward_wrt_step", ep_reward, total_steps
          )
          logger.info(
              "total_steps {}, episode {}, reward {}, agents rewards {}, episode steps {}".format(
                  total_steps,
                  total_episodes,
                  ep_reward,
                  ep_agent_rewards,
                  steps,
              )
          )
  
          total_steps += steps
          total_episodes += 1
  
          # evaluste agents
          if total_episodes % args.test_every_episodes == 0:
              (
                  eval_episode_rewards,
                  eval_episode_steps,
              ) = run_evaluate_episodes(env, agents, EVAL_EPISODES)
              summary.add_scalar(
                  "eval/episode_reward",
                  np.mean(eval_episode_rewards),
                  total_episodes,
              )
              logger.info(
                  "Evaluation over: {} episodes, Reward: {}".format(
                      EVAL_EPISODES, np.mean(eval_episode_rewards)
                  )
              )
  
              # save model
              if not args.restore:
                  model_dir = args.model_dir
                  os.makedirs(os.path.dirname(model_dir), exist_ok=True)
                  for i in range(len(agents)):
                      model_name = "/agent_" + str(i)
                      agents[i].save(model_dir + model_name)
  
file_path: PARL/examples/MADDPG/train.py
imports: import os
  import time
  import argparse
  import numpy
  from simple_model import MAModel
  from simple_agent import MAAgent
  from parl.algorithms import MADDPG
  from parl.env.multiagent_env import MAenv
  from parl.utils import logger, summary
  from gym import spaces
label_desc: Use image node_id 3 for calling a global function with example usage: main() without return types