parser.add_argument("--cfg_path", type=str, default='config.json.dist') parser.add_argument("--restrict", type=bool, default=False) parser.add_argument("--imitation", type=bool, default=False) parser.add_argument("--test", type=bool, nargs='?', const=True, default=False) parser.add_argument("--restore", type=bool, nargs='?', const=True, default=False) parser.add_argument('--save_replay', type=bool, nargs='?', const=True, default=False) args = parser.parse_args() os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu) #environ是一个字符串所对应环境的映像对象,输入为要使用的GPU number tf.reset_default_graph() sess = tf.Session() # config = Config(args.sz, args.map, lambda _: 1) config = Config(args.sz, args.map, args.run_id, restrict=args.restrict, imitation=args.imitation) # 进行参数的设置 os.makedirs('weights/' + config.full_id(), exist_ok=True) cfg_path = 'weights/%s/config.json' % config.full_id() # 保存参数的位置 config.build(cfg_path if args.restore else args.cfg_path) # 建立和设置参数 if not args.restore and not args.test: config.save(cfg_path) # 保存参数 envs = EnvWrapper(make_envs(args), config) # 创建环境,封装一层 agent = A2CAgent(sess, fully_conv, config, args.restore, args.discount, args.lr, args.vf_coef, args.ent_coef, args.clip_grads) # 创建agent runner = Runner(envs, agent, args.steps) # 创建进程 runner.run(args.updates, not args.test) # 开始运行 if args.save_replay: #是否保存回放 envs.save_replay() envs.close() # 关闭环境
# Allow gpu growth tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True config = Config(args.sz, '_'.join(args.maps), args.run_id) weight_dir = os.path.join(args.work_dir, 'weights', config.full_id()) log_dir = os.path.join(args.work_dir, 'logs', config.full_id()) print('weights are saved at ', weight_dir) os.makedirs(weight_dir, exist_ok=True) cfg_path = os.path.join(weight_dir, 'config.json') if args.restore and (not os.path.isfile(os.path.join(weight_dir, 'checkpoint'))) and args.remote_restore != '': args.restore_path = args.remote_restore config.build(os.path.join(args.restore_path, 'config.json')) config.save(cfg_path) assert os.path.isfile(os.path.join(args.restore_path, 'checkpoint')) elif args.restore: if os.path.isfile(cfg_path): config.build(cfg_path) else: config.build(args.cfg_path) args.restore_path = weight_dir assert os.path.isfile(os.path.join(args.restore_path, 'checkpoint')) elif args.remote_restore != '': args.restore_path = args.remote_restore config.build(os.path.join(args.restore_path, 'config.json')) assert os.path.isfile(os.path.join(args.restore_path, 'checkpoint')) else: args.restore_path = None config.build(args.cfg_path)