def main(unused_argv): config = Config(FLAGS.sz, FLAGS.map, -1) # 进行参数的设置 os.makedirs('weights/' + config.full_id(), exist_ok=True) cfg_path = 'weights/%s/config.json' % config.full_id() # 保存参数的位置 config.build(cfg_path) # 建立和设置参数 if FLAGS.human: human() else: agent(config)
parser.add_argument("--map", type=str, default='MoveToBeacon') 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() # 关闭环境
# preprocess args if args.maps == '': args.maps = '{"' + args.map + '": ' + str(args.envs) + '}' args.maps = json.loads(args.maps, object_pairs_hook=OrderedDict) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu) # 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.action_dim = opt_dim # max_step from float --> int if len(args.map) > 5 and (args.map.startswith('Build')): args.max_step = int(args.num_timesteps / args.step_mul) else: args.max_step = int(args.num_timesteps / args.step_mul) args.game_steps_per_episode = args.max_step * args.step_mul # pytorch device setup processor = 'cpu' if args.eval or args.gpu == -1 else 'cuda:%s'%args.gpu device = torch.device(processor) data_format = "NHWC" if args.nhwc else "NCHW" config = Config(args, device, data_format) os.makedirs('weights/' + config.full_id(), exist_ok=True) with open('weights/' + config.full_id() + '/arguments.txt', 'w') as f: for key,value in vars(args).items(): string = str(key) + ' : ' + str(value) + '\n' f.write( string ) # make/clean directory for save mode dirname = os.path.join('results', args.map, args.meta, 'run_%d'%(args.run_id)) if args.save_ilp or (args.meta in ['random', 'hrl'] and not args.eval): if os.path.exists(dirname): print("The savepath already exists. Deleting {}".format(dirname)) shutil.rmtree(dirname) os.makedirs(dirname, exist_ok=True) # set up agent & ilp ilp = None