def main(argv): logger = logging.getLogger('sc2rl') logger.setLevel(logging.INFO) if not os.path.isdir('logs'): os.mkdir('logs') fh = logging.FileHandler('logs/' + time.strftime("%Y%m%d-%H%M%S") + '.log') ch = logging.StreamHandler() fh.setLevel(logging.INFO) ch.setLevel(logging.INFO) logger.addHandler(fh) logger.addHandler(ch) np.set_printoptions(threshold=np.nan) #stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace #stopwatch.sw.trace = FLAGS.trace scenarios.load_scenarios() maps.get(FLAGS.map) # Assert the map exists. with helpers.get_env_wrapper(render=False) as env: a_space = env.action_space.n s_space = env.observation_space.shape algo_module, algo_name = FLAGS.algorithm.rsplit(".", 1) algo_cls = getattr(importlib.import_module(algo_module), algo_name) algo = algo_cls(FLAGS.run_time, a_space, s_space) algo.run() print('sys exit') sys.exit()
def _main(unused_argv): """Run an agent.""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace # Map Name mapName = FLAGS.map globals()[mapName] = type(mapName, (maps.mini_games.MiniGame, ), dict(filename=mapName)) maps.get(FLAGS.map) # Assert the map exists. agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) threads = [] for _ in range(FLAGS.parallel - 1): t = threading.Thread(target=run_thread, args=(agent_cls, FLAGS.map, False)) threads.append(t) t.start() run_thread(agent_cls, FLAGS.map, FLAGS.render) for t in threads: t.join() if FLAGS.profile: print(stopwatch.sw)
def main(): FLAGS(sys.argv) maps.get(map_name) # Assert the map exists. tf.reset_default_graph() mainQN = Qnetwork(h_size) targetQN = Qnetwork(h_size) init = tf.global_variables_initializer() saver = tf.train.Saver() trainables = tf.trainable_variables() targetOps = updateTargetGraph(trainables, tau) myBuffer = experience_buffer() with sc2_env.SC2Env( map_name, step_mul=step_mul, game_steps_per_episode=0, # maxEpLength? screen_size_px=(screen_xy, screen_xy), minimap_size_px=(64, 64), visualize=True) as env: saver.restore(path + '/model-5000.ckpt') print("Restored") action_spec = env.action_spec() # Reset environment and get first new observation timestep = env.reset()[0] s = processState(timestep.observation["screen"][_UNIT_TYPE_VIEW])
def main(unused_argv): """Run an agent.""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. players = [] agents = [] bot_difficulty = difficulties[FLAGS.difficulty] if FLAGS.agent1 == 'Bot': players.append(sc2_env.Bot(races['Z'], bot_difficulty)) else: players.append(sc2_env.Agent(races[FLAGS.agent1_race])) agents.append(get_agent(FLAGS.agent1, FLAGS.agent1_config)) if FLAGS.agent2 is None: pass elif FLAGS.agent2 == 'Bot': players.append(sc2_env.Bot(races['Z'], bot_difficulty)) else: players.append(sc2_env.Agent(races[FLAGS.agent2_race])) agents.append(get_agent(FLAGS.agent2, FLAGS.agent2_config)) run_thread(players, agents, FLAGS.map, FLAGS.render) if FLAGS.profile: print(stopwatch.sw)
def _main(unused_argv): """Run agents""" maps.get(FLAGS.map) # Assert the map exists. # create distribute tf cluster start_port = FLAGS.port_num SERVER_DICT["ps"].append("localhost:%d" % start_port) for i in range(PARALLEL): SERVER_DICT["worker"].append("localhost:%d" % (start_port + 1 + i)) Cluster = tf.train.ClusterSpec(SERVER_DICT) global LOG if FLAGS.training: now = datetime.now() LOG = "./logs/" + now.strftime("%Y%m%d-%H%M%S") + "/" model_path = "./model/" + now.strftime("%Y%m%d-%H%M%S") + "/" if not os.path.exists(model_path): os.makedirs(model_path) dynamic_path = "./model/" + now.strftime("%Y%m%d-%H%M%S") + "_dynamic/" if not os.path.exists(dynamic_path): os.makedirs(dynamic_path) C._SAVE_MODEL_PATH = model_path C._SAVE_DYNAMIC_PATH = dynamic_path if FLAGS.restore_model: C._LOAD_MODEL_PATH = FLAGS.restore_model_path P.restore_model = FLAGS.restore_model if FLAGS.restore_dynamic: C._LOAD_DYNAMIC_PATH = FLAGS.restore_dynamic_path print('C._LOAD_DYNAMIC_PATH ', C._LOAD_DYNAMIC_PATH) P.restore_dynamic = FLAGS.restore_dynamic UPDATE_GAME_NUM = NUM_FOR_UPDATE per_update_num = np.ceil(UPDATE_GAME_NUM / PARALLEL) Synchronizer = mp.Barrier(PARALLEL + 1) # Run parallel process procs = [] for index in range(PARALLEL): p = mp.Process(name="Worker_%d" % index, target=Worker, args=(index, per_update_num, Synchronizer, Cluster)) procs.append(p) p.daemon = True p.start() time.sleep(1) Parameter_Server(Synchronizer, Cluster, LOG) for p in procs: p.join() if FLAGS.profile: print(stopwatch.sw)
def main(unused_argv): """Run an agent.""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. global identifier, map_name, agent_module, agent_name if identifier == False: map_name = FLAGS.map agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) threads = [] for _ in range(FLAGS.parallel - 1): t = threading.Thread(target=run_thread, args=(agent_cls, FLAGS.map, False)) threads.append(t) t.start() run_thread(agent_cls, map_name, FLAGS.render) for t in threads: t.join() if FLAGS.profile: print(stopwatch.sw)
def main_runner(unused_argv): with sc2_env.SC2Env( map_name = "LongRangeKill", step_mul=8, visualize=True) as env: maps.get(FLAGS.map) # Assert the map exists. agent_000 = New_ScriptedAgent_ResourceCollection() action_spec = env.action_spec() observation_spec = env.observation_spec() agent_000.setup(observation_spec, action_spec) agent_000.reset() try: obs = env.reset() #maybe have the first action to select all marines first for step_idx in range(1000): #could use packaged python agents #print(obs[0].observation["available_actions"]) print(obs[0].observation["score_cumulative"]) print(obs[0].reward) print(obs[0].discount) one_step = agent_000.step(obs[0]) obs = env.step([one_step]) except KeyboardInterrupt: pass finally: print("simulation done")
def main(arg): maps.get(FLAGS.map) agent_classes = [] agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) agent_classes.append(agent_cls) player = sc2_env.Agent(sc2_env.Race[FLAGS.agent_race], agent_name) agent = agent_cls(FLAGS.training, FLAGS.minimap_resolution, FLAGS.screen_resolution, FLAGS.max_episode, FLAGS.learning_rate, FLAGS.discount, [FLAGS.epsilon_non_spatial, FLAGS.epsilon_spatial], FLAGS.random_range) agent.build_model(DEVICE) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) summary_writer = tf.summary.FileWriter(LOG) agent.setup(sess, summary_writer) agent.initialize() if not FLAGS.training or FLAGS.continuation: global COUNTER COUNTER = agent.load_model(SNAPSHOT) run_thread(agent, player, FLAGS.map, FLAGS.render)
def main(unused_argv): """Run an agent.""" # stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace # stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. run_thread(FLAGS.map, FLAGS.render)
def main(unused_argv): """Run an agent.""" pool = Pool(processes=FLAGS.parallel) stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. agent_cls = getattr(sys.modules[__name__], FLAGS.agent_name) async_results = [ pool.apply_async(run_thread, (agent_cls, FLAGS.map, False)) for which in range(FLAGS.parallel) ] # Can do anything here return_vals = [r.get() for r in async_results] all_scores = reduce(list.__add__, return_vals) print_stastic(all_scores) # After all threads done if FLAGS.profile: print(stopwatch.sw)
def _main(unused_argv): """Run agents""" if FLAGS.trace: stopwatch.sw.trace() elif FLAGS.profile: stopwatch.sw.enable() maps.get(FLAGS.map) # Assert the map exists. # Setup agents agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) print(agent_module, agent_name, agent_cls) agents = [] players = [] players.append( sc2_env.Agent(sc2_env.Race[FLAGS.agent_race], FLAGS.agent_name or agent_name)) for i in range(PARALLEL): agent = agent_cls(FLAGS.training, FLAGS.feature_minimap_size[0], FLAGS.feature_screen_size[0]) agent.build_model(i > 0, DEVICE[i % len(DEVICE)], FLAGS.net) agents.append(agent) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) summary_writer = tf.summary.FileWriter(LOG) for i in range(PARALLEL): agents[i].setup(sess, summary_writer) agent.initialize() if not FLAGS.training or FLAGS.continuation: global COUNTER COUNTER = agent.load_model(SNAPSHOT) # Run threads # wrapper_func = run_train_and_eval wrapper_func = run_thread threads = [] for i in range(PARALLEL - 1): t = threading.Thread(target=wrapper_func, args=(agents[i], players, FLAGS.map, False)) threads.append(t) t.daemon = True t.start() time.sleep(5) wrapper_func(agents[-1], players, FLAGS.map, FLAGS.render) for t in threads: t.join() if FLAGS.profile: print(stopwatch.sw)
def main(unused_argument): map_names = [ 'DefeatRoachesAntiSuicide', 'DefeatRoachesAntiSuicideMarineDeath0', 'DefeatRoaches' ] for map_name in map_names: get = lib.get(map_name) maps.get(map_name) run_thread(map_name, FLAGS.render)
def main(unused_argv): #____________________HYPERPARAMETERS_________________ RUN_TIME = 999999 THREADS = 8 OPTIMIZERS = 2 THREAD_DELAY = 0.001 EPS_START = 0.02 EPS_STOP = .01 EPS_STEPS = 3000 MIN_BATCH = 32 """Run an agent.""" #____________________HYPERPARAMETERS_END_________________ stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) # threads = [] # for _ in range(FLAGS.parallel - 1): # t = threading.Thread(target=run_thread, args=(agent_cls, FLAGS.map, False)) # threads.append(t) # t.start() envs = [Environment(agent_cls, FLAGS.map, False) for i in range(THREADS)] opts = [Optimizer() for i in range(OPTIMIZERS)] for o in opts: o.start() for e in envs: e.start() time.sleep(RUN_TIME) for e in envs: e.stop() for e in envs: e.join() for o in opts: o.stop() for o in opts: o.join() # run_thread(agent_cls, FLAGS.map, FLAGS.render) # for t in threads: # t.join() if FLAGS.profile: print(stopwatch.sw)
def _main(unused_argv): """Run an agent.""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) # my code tf.reset_default_graph() if not os.path.exists(model_path): os.makedirs(model_path) global_episodes = tf.Variable(0, dtype=tf.int32, name='global_episodes', trainable=False) trainer = tf.train.AdamOptimizer(learning_rate=1e-4) master_network = acNetwork.AC_Network(s_size, a_size, 'global', None) # Generate global network workers = [] # Create worker classes for i in range(FLAGS.parallel): workers.append( run_loop.Worker(i, s_size, a_size, trainer, model_path, global_episodes)) saver = tf.train.Saver(max_to_keep=5) with tf.Session() as sess: coord = tf.train.Coordinator() if load_model == True: print('Loading Model...') ckpt = tf.train.get_checkpoint_state(model_path) saver.restore(sess, ckpt.model_checkpoint_path) else: sess.run(tf.global_variables_initializer()) #This is where the asynchronous magic happens. #Start the "work" process for each worker in a separate threat. worker_threads = [] for worker in workers: t = threading.Thread(target=run_thread, args=(worker, max_episode_length, gamma, master_network, sess, coord, saver, agent_cls, FLAGS.map, FLAGS.render and i == 0)) worker_threads.append(t) t.start() coord.join(worker_threads) if FLAGS.profile: print(stopwatch.sw)
def main(unused_argv): """Run an agent.""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) # Set up parameters params = { "verbosity": FLAGS.output, "learning_rate": FLAGS.learning_rate, "beta": FLAGS.beta, "eta": FLAGS.eta, "use_advantage": FLAGS.use_advantage, "t_max": FLAGS.t_max } # Set up A3C global network graph = tf.get_default_graph() session = tf.Session(graph=graph) optimizer = tf.train.AdamOptimizer(params["learning_rate"]) with graph.as_default(), tf.device('/cpu:0'): model.sc2network(optimizer, beta=params["beta"], eta=params["eta"], advantage=params["use_advantage"], scope="global") session.run( tf.variables_initializer( tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, "global"))) id = 1 # Set up lock lock = threading.Lock() threads = [] for _ in range(FLAGS.parallel - 1): t = threading.Thread(target=run_thread, args=(agent_cls, FLAGS.map, False, id, params, lock, session, graph, optimizer)) threads.append(t) t.start() id += 1 run_thread(agent_cls, FLAGS.map, FLAGS.render, id, params, lock, session, graph, optimizer) for t in threads: t.join() if FLAGS.profile: print(stopwatch.sw)
def main(unused_argv): """Run agents""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map_name) # Assert the map exists. # Setup agents agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) agents = [] for i in range(PARALLEL): agent = agent_cls(FLAGS.training, FLAGS.minimap_res, FLAGS.screen_res) print(f'agent {i} created') # first is fine, rest will raise error agent.build(i > 0, DEVICE[i % len(DEVICE)]) # reuse, dev agents.append(agent) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) for i in range(PARALLEL): summary_dir = LOG + str(i) summary_writer = tf.summary.FileWriter( summary_dir) # why just one? when is the summary being saved? agents[i].setup(sess, summary_writer) print(f'To view Tensorboard, run tensorboard --logdir={summary_dir}') agent.initialize() # is this correct if FLAGS.continuation and os.path.exists( SNAPSHOT): # should the training flag be here global COUNTER COUNTER = agent.load_model(SNAPSHOT) print("MODEL LOADED.") # Run threads threads = [] for i in range(PARALLEL - 1): t = threading.Thread(target=run_thread, args=(agents[i], FLAGS.map_name)) threads.append(t) t.daemon = True t.start() time.sleep(5) run_thread(agents[-1], FLAGS.map_name) for t in threads: t.join() if FLAGS.profile: print(stopwatch.sw)
def train(map_name, num_timesteps, batch_steps, seed, network, ar, lr, lrschedule, screen_size, minimap_size, step_mul, num_cpu, optimizer, ent_coef, vl_coef, max_grad_norm): maps.get(map_name) # Assert the map exists. log_path = './experiments/%s/' % (time.strftime("%m%d_%H%M_") + map_name) make_path(log_path) make_path("%sreplay" % log_path) def make_env(rank): def _thunk(): agent_interface = features.parse_agent_interface_format( feature_screen=64, feature_minimap=64) env = sc2_env.SC2Env( map_name=map_name, step_mul=step_mul, agent_interface_format=agent_interface, # screen_size_px=(screen_size, screen_size), # minimap_size_px=(minimap_size, minimap_size), visualize=False) return env return _thunk set_global_seeds(seed) log_file = open("%sconfig.log" % log_path, "a+") log_file.write("Map Name: %s\n" % map_name) log_file.write("Optimizer: %s\n" % optimizer) log_file.write("Network: %s\n" % network) log_file.write("Learning Rate: %f\n" % lr) log_file.write("Entropy Coefficient: %f\n" % ent_coef) log_file.write("Value Function Coefficient: %f\n" % vl_coef) log_file.write("Maximum Gradient Norm: %f\n" % max_grad_norm) log_file.write("Screen Size: %d\n" % screen_size) log_file.write("Minimap Size: %d\n" % minimap_size) log_file.write("Batch Steps: %d\n" % batch_steps) log_file.close() learn(network, log_path, make_env, total_timesteps=num_timesteps, nsteps=batch_steps, ent_coef=ent_coef, max_grad_norm=max_grad_norm, optimizer=optimizer, vl_coef=vl_coef, ar=ar, lr=lr, num_cpu=num_cpu)
def _main(unused_argv): """Run agents""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. # Setup agents agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) agents = [] for i in range(PARALLEL): agent = agent_cls(FLAGS.training, FLAGS.minimap_resolution, FLAGS.screen_resolution) agent.build_model(i > 0, DEVICE[i % len(DEVICE)], FLAGS.net) agents.append(agent) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) summary_writer = tf.summary.FileWriter(LOG) for i in range(PARALLEL): agents[i].setup(sess, summary_writer) agent.initialize() if not FLAGS.training or FLAGS.continuation: print("training: " + str(FLAGS.training)) print("continuation: " + str(FLAGS.training)) global COUNTER COUNTER = agent.load_model(SNAPSHOT) # Run threads threads = [] for i in range(PARALLEL - 1): t = threading.Thread(target=run_thread, args=(agents[i], FLAGS.map, False)) threads.append(t) t.daemon = True t.start() time.sleep(5) run_thread(agents[-1], FLAGS.map, FLAGS.render) for t in threads: t.join() if FLAGS.profile: print(stopwatch.sw)
def test_list_all_maps(self, map_name): """Make sure all maps can be read.""" run_config = run_configs.get() map_inst = maps.get(map_name) logging.info("map: %s", map_inst.name) self.assertTrue(map_inst.data(run_config), msg="Failed on %s" % map_inst)
def main(unused_argvs): map_name = "MoveToBeacon" map_inst = maps.get(map_name) players = [] players.append(sc2_env.Agent(sc2_env.Race["random"], "TestAgent")) with sc2_env.SC2Env( map_name=map_name, players=players, agent_interface_format=sc2_env.parse_agent_interface_format( feature_screen=84, feature_minimap=64, rgb_screen=None, rgb_minimap=None, action_space=None, use_feature_units=True, use_raw_units=True), step_mul=8, game_steps_per_episode=None, disable_fog=False, visualize=False) as env: env = available_actions_printer.AvailableActionsPrinter(env) for function in env.action_spec()[0].functions: print(function) print(env.action_spec()[0].types) print(env.observation_spec()[0])
def _launch(self): self._run_config = run_configs.get() self._map = maps.get(self.map_name) # Setting up the interface self.interface = sc_pb.InterfaceOptions( raw=True, # raw, feature-level data score=True) self._sc2_proc = self._run_config.start(game_version=self.game_version, window_size=self.window_size) self.controller = self._sc2_proc.controller # Create the game. create = sc_pb.RequestCreateGame( realtime=False, random_seed=self.seed, local_map=sc_pb.LocalMap(map_path=self._map.path, map_data=self._run_config.map_data( self._map.path))) create.player_setup.add(type=sc_pb.Participant) create.player_setup.add(type=sc_pb.Computer, race=races[self._bot_race], difficulty=difficulties[self.difficulty]) self.controller.create_game(create) join = sc_pb.RequestJoinGame(race=races[self._agent_race], options=self.interface) self.controller.join_game(join)
def test_observe_bots(self): run_config = run_configs.get() map_inst = maps.get("Simple64") with run_config.start(want_rgb=False) as controller: create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap( map_path=map_inst.path, map_data=map_inst.data(run_config))) create.player_setup.add( type=sc_pb.Computer, race=sc_common.Random, difficulty=sc_pb.VeryEasy) create.player_setup.add( type=sc_pb.Computer, race=sc_common.Random, difficulty=sc_pb.VeryHard) create.player_setup.add(type=sc_pb.Observer) controller.create_game(create) join = sc_pb.RequestJoinGame( options=sc_pb.InterfaceOptions(), # cheap observations observed_player_id=0) controller.join_game(join) outcome = False for _ in range(60 * 60): # 60 minutes should be plenty. controller.step(16) obs = controller.observe() if obs.player_result: print("Outcome after %s steps (%0.1f game minutes):" % ( obs.observation.game_loop, obs.observation.game_loop / (16 * 60))) for r in obs.player_result: print("Player %s: %s" % (r.player_id, sc_pb.Result.Name(r.result))) outcome = True break self.assertTrue(outcome)
def get_static_data(): """Retrieve static data from the game.""" try: with open(STATIC_DATA_PICKLE_PATH, 'rb') as f: static_data = pickle.load(f) return static_data except FileNotFoundError: pass run_config = run_configs.get() with run_config.start() as controller: m = maps.get("Sequencer") # Arbitrary ladder map. create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap( map_path=m.path, map_data=m.data(run_config))) create.player_setup.add(type=sc_pb.Participant) create.player_setup.add(type=sc_pb.Computer, race=common_pb.Random, difficulty=sc_pb.VeryEasy) join = sc_pb.RequestJoinGame(race=common_pb.Terran, options=sc_pb.InterfaceOptions(raw=True)) controller.create_game(create) controller.join_game(join) static_data = controller.data_raw() with open(STATIC_DATA_PICKLE_PATH, 'wb') as f: static_data = pickle.dump(static_data, f, protocol=pickle.HIGHEST_PROTOCOL) return static_data
def get_map_size(map_name: str) -> tuple: """Get the map size. If this info hasn't already been extracted by the agent before, a game will be started in order to get it. The information will then be pickled and further calls to this function will look for the info in the pickled file. :param map_name: the map name :type map_name: str :return: a tuple :math:`(x, y)` containing the dimensions of the map :rtype: tuple """ if map_name in __data: map_size = __data[map_name] else: run_config = run_configs.get() map_inst = maps.get(map_name) with run_config.start(want_rgb=False) as controller: create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap( map_path=map_inst.path, map_data=map_inst.data(run_config))) create.player_setup.add(type=sc_pb.Participant) join = sc_pb.RequestJoinGame( race=sc_common.Terran, options=sc_pb.InterfaceOptions(raw=True)) controller.create_game(create) controller.join_game(join) info = controller.game_info() map_size = info.start_raw.map_size __data[map_name] = (map_size.x, map_size.y) with open(__pickle, 'wb') as fp: pickle.dump(__data, fp, protocol=pickle.HIGHEST_PROTOCOL) return map_size
def main(unused_argv): """Run SC2 to play a game or a replay.""" run_config = run_configs.get() map_inst = maps.get(FLAGS.map) ports = [portpicker.pick_unused_port() for _ in range(5)] sc2_procs = [run_config.start(extra_ports=ports) for _ in range(2)] controllers = [p.controller for p in sc2_procs] for c in controllers: c.save_map(map_inst.path, map_inst.data(run_config)) create = sc_pb.RequestCreateGame( realtime=FLAGS.realtime, local_map=sc_pb.LocalMap(map_path=map_inst.path)) create.player_setup.add(type=sc_pb.Participant) create.player_setup.add(type=sc_pb.Participant) controllers[0].create_game(create) join = sc_pb.RequestJoinGame() join.shared_port = ports.pop() join.server_ports.game_port = ports.pop() join.server_ports.base_port = ports.pop() join.client_ports.add(game_port=ports.pop(), base_port=ports.pop()) threads = [ threading.Thread(target=human_runner, args=(controllers[0], join)), threading.Thread(target=agent_runner, args=(controllers[1], join)), ] for t in threads: t.start() for t in threads: t.join()
def test_versions_create_game(self, game_version): log_center("starting create game: %s", game_version) run_config = run_configs.get() with run_config.start(version=game_version) as controller: interface = sc_pb.InterfaceOptions() interface.raw = True interface.score = True interface.feature_layer.width = 24 interface.feature_layer.resolution.x = 84 interface.feature_layer.resolution.y = 84 interface.feature_layer.minimap_resolution.x = 64 interface.feature_layer.minimap_resolution.y = 64 map_inst = maps.get("Simple64") create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap( map_path=map_inst.path, map_data=map_inst.data(run_config))) create.player_setup.add(type=sc_pb.Participant) create.player_setup.add(type=sc_pb.Computer, race=sc_common.Terran, difficulty=sc_pb.VeryEasy) join = sc_pb.RequestJoinGame(race=sc_common.Terran, options=interface) controller.create_game(create) controller.join_game(join) for _ in range(5): controller.step(16) controller.observe() log_center("success: %s", game_version)
def main(unused_argv): """Run an agent.""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace map_inst = maps.get(FLAGS.map) agent_classes = [] players = [] agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) agent_classes.append(agent_cls) players.append(sc2_env.Agent(sc2_env.Race[FLAGS.agent_race], FLAGS.agent_name or agent_name)) if map_inst.players >= 2: if FLAGS.agent2 == "Bot": players.append(sc2_env.Bot(sc2_env.Race[FLAGS.agent2_race], sc2_env.Difficulty[FLAGS.difficulty])) else: agent_module, agent_name = FLAGS.agent2.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) agent_classes.append(agent_cls) players.append(sc2_env.Agent(sc2_env.Race[FLAGS.agent2_race], FLAGS.agent2_name or agent_name)) # threads = [] # for _ in range(FLAGS.parallel - 1): # t = threading.Thread(target=run_thread, # args=(agent_classes, players, FLAGS.map, False)) # threads.append(t) # t.start() run_thread(agent_classes, players, FLAGS.map, FLAGS.render)
def test_load_random_map(self, controller, map_name): """Test loading a few random maps.""" m = maps.get(map_name) run_config = run_configs.get() logging.info("Loading map: %s", m.name) create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap( map_path=m.path, map_data=m.data(run_config))) create.player_setup.add(type=sc_pb.Participant) create.player_setup.add(type=sc_pb.Computer, race=sc_common.Random, difficulty=sc_pb.VeryEasy) join = sc_pb.RequestJoinGame(race=sc_common.Random, options=sc_pb.InterfaceOptions(raw=True)) controller.create_game(create) controller.join_game(join) # Verify it has the right mods and isn't running into licensing issues. info = controller.game_info() logging.info("Mods for %s: %s", m.name, info.mod_names) self.assertIn("Mods/Void.SC2Mod", info.mod_names) self.assertIn("Mods/VoidMulti.SC2Mod", info.mod_names) # Verify it can be played without making actions. for _ in range(3): controller.step() controller.observe()
def create_game(self, map_name): """Create a game for the agents to join. Args: map_name: The map to use. """ self._reconnect() map_inst = maps.get(map_name) map_data = map_inst.data(self._run_config) if map_name not in self._saved_maps: for controller in self._controllers: controller.save_map(map_inst.path, map_data) self._saved_maps.add(map_name) # Form the create game message. create = sc_pb.RequestCreateGame( local_map=sc_pb.LocalMap(map_path=map_inst.path), disable_fog=False) # Set up for two agents. for _ in range(self._num_agents): create.player_setup.add(type=sc_pb.Participant) # Create the game. self._controllers[0].create_game(create) self._disconnect()
def _launch(self): """Launch the StarCraft II game.""" self._run_config = run_configs.get() _map = maps.get(self.map_name) # Setting up the interface interface_options = sc_pb.InterfaceOptions(raw=True, score=False) self._sc2_proc = self._run_config.start(game_version=self.game_version, window_size=self.window_size) self.controller = self._sc2_proc.controller # Request to create the game create = sc_pb.RequestCreateGame( local_map=sc_pb.LocalMap( map_path=_map.path, map_data=self._run_config.map_data(_map.path)), realtime=False, random_seed=self.seed) create.player_setup.add(type=sc_pb.Participant) create.player_setup.add(type=sc_pb.Computer, race=races[self._bot_race], difficulty=difficulties[self.difficulty]) self.controller.create_game(create) join = sc_pb.RequestJoinGame(race=races[self._agent_race], options=interface_options) self.controller.join_game(join)
def _main(unused_argv): """Run agents""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. # Setup agents agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) agents = [] for i in range(PARALLEL): agent = agent_cls(FLAGS.training, FLAGS.minimap_resolution, FLAGS.screen_resolution) agent.build_model(i > 0, DEVICE[i % len(DEVICE)], FLAGS.net) agents.append(agent) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) summary_writer = tf.summary.FileWriter(LOG) for i in range(PARALLEL): agents[i].setup(sess, summary_writer) agent.initialize() if not FLAGS.training or FLAGS.continuation: global COUNTER COUNTER = agent.load_model(SNAPSHOT) # Run threads threads = [] for i in range(PARALLEL - 1): t = threading.Thread(target=run_thread, args=(agents[i], FLAGS.map, False)) threads.append(t) t.daemon = True t.start() time.sleep(5) run_thread(agents[-1], FLAGS.map, FLAGS.render) for t in threads: t.join() if FLAGS.profile: print(stopwatch.sw)
def _initialize(self): """Initialize play/replay connection.""" run_config = run_configs.get() self._map_inst = maps.get(self._config.map_name) self._map_data = self._map_inst.data(run_config) self._sc2_proc = run_config.start() self._controller = self._sc2_proc.controller
def main(unused_argv): """Run an agent.""" stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace stopwatch.sw.trace = FLAGS.trace maps.get(FLAGS.map) # Assert the map exists. agent_module, agent_name = FLAGS.agent.rsplit(".", 1) agent_cls = getattr(importlib.import_module(agent_module), agent_name) threads = [] for _ in range(FLAGS.parallel - 1): t = threading.Thread(target=run_thread, args=(agent_cls, FLAGS.map, False)) threads.append(t) t.start() run_thread(agent_cls, FLAGS.map, FLAGS.render) for t in threads: t.join() if FLAGS.profile: print(stopwatch.sw)
def __init__(self, map_name, screen_size_px=(64, 64), minimap_size_px=(64, 64), camera_width_world_units=None, discount=1., visualize=False, agent_race=None, bot_race=None, difficulty=None, step_mul=None, save_replay_steps=0, replay_dir=None, game_steps_per_episode=None, score_index=None, score_multiplier=None): """Create a SC2 Env. Args: map_name: Name of a SC2 map. Run bin/map_list to get the full list of known maps. Alternatively, pass a Map instance. Take a look at the docs in maps/README.md for more information on available maps. screen_size_px: The size of your screen output in pixels. minimap_size_px: The size of your minimap output in pixels. camera_width_world_units: The width of your screen in world units. If your screen_size_px=(64, 48) and camera_width is 24, then each px represents 24 / 64 = 0.375 world units in each of x and y. It'll then represent a camera of size (24, 0.375 * 48) = (24, 18) world units. discount: Returned as part of the observation. visualize: Whether to pop up a window showing the camera and feature layers. This won't work without access to a window manager. agent_race: One of P,T,Z,R default random. This is the race you control. bot_race: One of P,T,Z,R default random. This is the race controlled by the built-in bot. difficulty: One of 1-9,A. How strong should the bot be? step_mul: How many game steps per agent step (action/observation). None means use the map default. save_replay_steps: How many game steps to wait before saving a replay. Default of 0 means don't save replays. replay_dir: Directory to save replays to. Required with save_replay_steps. game_steps_per_episode: Game steps per episode, independent of the step_mul. 0 means no limit. None means use the map default. score_index: -1 means use the win/loss reward, >=0 is the index into the score_cumulative with 0 being the curriculum score. None means use the map default. score_multiplier: How much to multiply the score by. Useful for negating. Raises: ValueError: if the agent_race, bot_race or difficulty are invalid. """ # Make the destructor happy. self._renderer_human = None self._controller = None self._sc2_proc = None if save_replay_steps and not replay_dir: raise ValueError("Missing replay_dir") if agent_race and agent_race not in races: raise ValueError("Bad agent_race args") if bot_race and bot_race not in races: raise ValueError("Bad bot_race args") if difficulty and str(difficulty) not in difficulties: raise ValueError("Bad difficulty") self._map = maps.get(map_name) self._discount = discount self._step_mul = step_mul or self._map.step_mul self._save_replay_steps = save_replay_steps self._replay_dir = replay_dir self._total_steps = 0 if score_index is None: self._score_index = self._map.score_index else: self._score_index = score_index if score_multiplier is None: self._score_multiplier = self._map.score_multiplier else: self._score_multiplier = score_multiplier self._last_score = None self._episode_length = (game_steps_per_episode or self._map.game_steps_per_episode) self._episode_steps = 0 self._run_config = run_configs.get() self._sc2_proc = self._run_config.start() self._controller = self._sc2_proc.controller screen_size_px = point.Point(*screen_size_px) minimap_size_px = point.Point(*minimap_size_px) interface = sc_pb.InterfaceOptions( raw=visualize, score=True, feature_layer=sc_pb.SpatialCameraSetup( width=camera_width_world_units or 24)) screen_size_px.assign_to(interface.feature_layer.resolution) minimap_size_px.assign_to(interface.feature_layer.minimap_resolution) create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap( map_path=self._map.path, map_data=self._map.data(self._run_config))) create.player_setup.add(type=sc_pb.Participant) create.player_setup.add(type=sc_pb.Computer, race=races[bot_race or "R"], difficulty=difficulties[difficulty or "1"]) join = sc_pb.RequestJoinGame(race=races[agent_race or "R"], options=interface) self._controller.create_game(create) self._controller.join_game(join) game_info = self._controller.game_info() static_data = self._controller.data() self._features = features.Features(game_info) if visualize: self._renderer_human = renderer_human.RendererHuman() self._renderer_human.init(game_info, static_data) self._episode_count = 0 self._obs = None self._state = environment.StepType.LAST # Want to jump to `reset`. logging.info("Environment is ready.")