def openReplay(self, replay_file_path, player_id=1, screen_size_px=(84, 84), minimap_size_px=(64, 64)): replay_data = self.run_config.replay_data(replay_file_path) ping = self.controller.ping() info = self.controller.replay_info(replay_data) if not self._valid_replay(info, ping): raise Exception( "{} is not a valid replay file!".format(replay_file_path)) screen_size_px = point.Point(*screen_size_px) minimap_size_px = point.Point(*minimap_size_px) interface = sc_pb.InterfaceOptions( raw=False, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=24)) screen_size_px.assign_to(interface.feature_layer.resolution) minimap_size_px.assign_to(interface.feature_layer.minimap_resolution) map_data = None if info.local_map_path: map_data = self.run_config.map_data(info.local_map_path) self._episode_length = info.game_duration_loops self._episode_steps = 0 self.controller.start_replay( sc_pb.RequestStartReplay(replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=player_id)) self._state = StepType.FIRST
def __init__(self, replay_file_path, agent, player_id=1, screen_size_px=(64, 64), # (60, 60) minimap_size_px=(64, 64), # (60, 60) discount=1., frames_per_game=1): print("Parsing " + replay_file_path) self.replay_file_name = replay_file_path.split("/")[-1].split(".")[0] self.agent = agent self.discount = discount self.frames_per_game = frames_per_game self.run_config = run_configs.get() self.sc2_proc = self.run_config.start() self.controller = self.sc2_proc.controller replay_data = self.run_config.replay_data(self.replay_file_name + '.SC2Replay') ping = self.controller.ping() self.info = self.controller.replay_info(replay_data) # print(self.info) if not self._valid_replay(self.info, ping): self.sc2_proc.close() # print(self.info) raise Exception("{} is not a valid replay file!".format(self.replay_file_name + '.SC2Replay')) # global FILE_OP # FILE_OP.write(self.replay_file_name + '.SC2Replay') # self.replay_file_name = self.info.map_name+'_'+self.replay_file_name # for player_info in self.info.player_info: # race = sc_common.Race.Name(player_info.player_info.race_actual) # self.replay_file_name = race + '_' + self.replay_file_name screen_size_px = point.Point(*screen_size_px) minimap_size_px = point.Point(*minimap_size_px) interface = sc_pb.InterfaceOptions( raw=False, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=24)) screen_size_px.assign_to(interface.feature_layer.resolution) minimap_size_px.assign_to(interface.feature_layer.minimap_resolution) map_data = None if self.info.local_map_path: map_data = self.run_config.map_data(self.info.local_map_path) self._episode_length = self.info.game_duration_loops self._episode_steps = 0 self.controller.start_replay(sc_pb.RequestStartReplay( replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=player_id)) self._state = StepType.FIRST
def get_dft_sc2_interface(): """get default sc2 interface when starting the game. It uses a small enough size (16, 16) for the irrelevant feature layers""" size = point.Point(16, 16) sc_interface = sc_pb.InterfaceOptions( raw=True, score=False, feature_layer=sc_pb.SpatialCameraSetup(width=24)) size.assign_to(sc_interface.feature_layer.resolution) size.assign_to(sc_interface.feature_layer.minimap_resolution) return sc_interface
def __init__(self, replay_file_path, agent, discount=1.): #frames_per_game=1): print("Parsing " + replay_file_path) self.replay_file_name = replay_file_path.split("\\")[-1].split(".")[0] self.agent = agent self.discount = discount #self.frames_per_game = frames_per_game self.run_config = run_configs.get() versions = self.run_config.get_versions() self.sc2_proc = self.run_config.start(version=versions['4.10.1']) self.controller = self.sc2_proc.controller ping = self.controller.ping() replay_data = self.run_config.replay_data(replay_file_path) try: self.info = self.controller.replay_info(replay_data) except Exception as e: raise Exception(e) if not self._valid_replay(self, self.info, ping): # #os.remove(replay_file_path) raise Exception("{} Was a loser".format(replay_file_path)) _screen_size_px = point.Point(*self.screen_size_px) _minimap_size_px = point.Point(*self.minimap_size_px) interface = sc_pb.InterfaceOptions( feature_layer=sc_pb.SpatialCameraSetup( width=self.camera_width), # crop_to_playable_area=True), show_cloaked=True, raw=True ) #, raw_affects_selection=True,raw_crop_to_playable_area=True) _screen_size_px.assign_to(interface.feature_layer.resolution) _minimap_size_px.assign_to(interface.feature_layer.minimap_resolution) map_data = None if self.info.local_map_path: map_data = self.run_config.map_data(self.info.local_map_path) self._episode_length = self.info.game_duration_loops self._episode_steps = 0 self.controller.start_replay( sc_pb.RequestStartReplay(replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=self.player_id)) self._state = StepType.FIRST
def get_replay_actor_interface(map_name, crop=False): map_name = map_name_transform(map_name) map_size = MAP_PLAYABLE_SIZE_DICT[map_name] if crop else MAP_ORI_SIZE_DICT[ map_name] screen_size = point.Point(16, 16) minimap_size = point.Point(int(map_size[0]), int(map_size[1])) interface = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=24)) screen_size.assign_to(interface.feature_layer.resolution) minimap_size.assign_to(interface.feature_layer.minimap_resolution) if crop: interface.feature_layer.crop_to_playable_area = True interface.raw_crop_to_playable_area = True return interface
def __init__(self, replay_file_path, agent, player_id=1, screen_size_px=(64, 64), minimap_size_px=(64, 64), discount=1., step_mul=1): self.agent = agent self.discount = discount self.step_mul = step_mul self.skip = 10 self.replay_file_name = replay_file_path.split("/")[-1].split(".")[0] self.run_config = run_configs.get() self.sc2_proc = self.run_config.start() self.controller = self.sc2_proc.controller replay_data = self.run_config.replay_data(replay_file_path) ping = self.controller.ping() self.info = self.controller.replay_info(replay_data) if not self._valid_replay(self.info, ping): raise Exception( "{} is not a valid replay file!".format(replay_file_path)) screen_size_px = point.Point(*screen_size_px) minimap_size_px = point.Point(*minimap_size_px) interface = sc_pb.InterfaceOptions( raw=False, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=24)) screen_size_px.assign_to(interface.feature_layer.resolution) minimap_size_px.assign_to(interface.feature_layer.minimap_resolution) map_data = None if self.info.local_map_path: map_data = self.run_config.map_data(self.info.local_map_path) self._episode_length = self.info.game_duration_loops self._episode_steps = 0 self.controller.start_replay( sc_pb.RequestStartReplay(replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=player_id)) self._state = StepType.FIRST
def __init__(self, replay_file_path, agent, player_id=1, discount=1., step_mul=1): self.agent = agent self.discount = discount self.step_mul = step_mul # lib.version_dict self.run_config = run_configs.get() # self.run_config.lib. self.sc2_proc = self.run_config.start() self.controller = self.sc2_proc.controller # self.sc2_proc.version = sc_pb.RequestReplayInfo.download_data replay_data = self.run_config.replay_data(replay_file_path) ping = self.controller.ping() # sc_process. self.info = self.controller.replay_info(replay_data) if not self._valid_replay(self.info, ping): raise Exception( "{} is not a valid replay file!".format(replay_file_path)) _screen_size_px = point.Point(*self.screen_size_px) _minimap_size_px = point.Point(*self.minimap_size_px) interface = sc_pb.InterfaceOptions( raw=False, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=self.camera_width, crop_to_playable_area=True)) _screen_size_px.assign_to(interface.feature_layer.resolution) _minimap_size_px.assign_to(interface.feature_layer.minimap_resolution) map_data = None if self.info.local_map_path: map_data = self.run_config.map_data(self.info.local_map_path) self._episode_length = self.info.game_duration_loops self._episode_steps = 0 self.controller.start_replay( sc_pb.RequestStartReplay(replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=player_id)) self._state = StepType.FIRST
def __init__(self): # Environment. self.map_name = 'NewkirkPrecinct' self.screen_size_px = (32, 32) self.minimap_size_px = (32, 32) self.camera_width = 24 self.random_seed = 42 self.interface = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=self.camera_width)) resolution = point.Point(*self.screen_size_px) resolution.assign_to(self.interface.feature_layer.resolution) minimap_resolution = point.Point(*self.minimap_size_px) minimap_resolution.assign_to( self.interface.feature_layer.minimap_resolution) # Magic unit ids. self.scv_unit_id = 45 self.command_center_id = 18 # Feature layer with the unit types. self.unit_type_id = features.SCREEN_FEATURES.unit_type.index self.num_observations = 3000 self.player_id = 1 # Hard code an action sequence. self.action_sequence = { 507: ('select_point', [[1], [9, 15]]), # Target an SCV. 963: ('Build_SupplyDepot_screen', [[0], [4, 19]]), 1152: ('select_point', [[0], [15, 10]]), # Select the Command Center. 1320: ('Train_SCV_quick', [[0]]), 1350: ('Train_SCV_quick', [[0]]), 1393: ('Train_SCV_quick', [[0]]), 1437: ('Train_SCV_quick', [[0]]), 1564: ('Train_SCV_quick', [[0]]), 1602: ('Train_SCV_quick', [[0]]), 1822: ('select_idle_worker', [[2]]), 2848: ('Build_Barracks_screen', [[0], [22, 22]]) } self.actions = { frame: self.action_to_function_call(*action) for frame, action in six.iteritems(self.action_sequence) }
def __init__(self): # Environment. self.map_name = 'NewkirkPrecinct' self.screen_size_px = (32, 32) self.minimap_size_px = (32, 32) self.camera_width = 24 self.random_seed = 42 self.interface = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=self.camera_width)) resolution = point.Point(*self.screen_size_px) resolution.assign_to(self.interface.feature_layer.resolution) minimap_resolution = point.Point(*self.minimap_size_px) minimap_resolution.assign_to( self.interface.feature_layer.minimap_resolution) # Feature layer with the unit types. self.unit_type_id = features.SCREEN_FEATURES.unit_type.index self.num_observations = 3000 self.player_id = 1 # Hard code an action sequence. # TODO(petkoig): This is very brittle. A random seed reduces flakiness, but # isn't guaranteed to give the same actions between game versions. The pixel # coords should be computed at run-time, maybe with a trigger type system in # case build times also change. self.action_sequence = { 507: ('select_point', [[1], [9, 18]]), # Target an SCV. 963: ('Build_SupplyDepot_screen', [[0], [4, 19]]), 1152: ('select_point', [[0], [15, 13]]), # Select the Command Center. 1320: ('Train_SCV_quick', [[0]]), 1350: ('Train_SCV_quick', [[0]]), 1393: ('Train_SCV_quick', [[0]]), 1437: ('Train_SCV_quick', [[0]]), 1564: ('Train_SCV_quick', [[0]]), 1602: ('Train_SCV_quick', [[0]]), 1822: ('select_idle_worker', [[2]]), 2848: ('Build_Barracks_screen', [[0], [22, 22]]) } self.actions = { frame: self.action_to_function_call(*action) for frame, action in six.iteritems(self.action_sequence) }
def __init__(self, aif, samples_queue, step_mul=8, player_id=1): """ Creates a new SC2 environment replayer. :param AgentInterfaceFormat aif: the SC2 agent interface options. :param mp.Queue samples_queue: the queue to put data in. :param int step_mul: the environment's step multiplier. :param int player_id: the id of the player considered as the agent in the replay file. """ self._aif = aif self._interface_options = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=self._aif.camera_width_world_units)) self._aif.feature_dimensions.screen.assign_to(self._interface_options.feature_layer.resolution) self._aif.feature_dimensions.minimap.assign_to(self._interface_options.feature_layer.minimap_resolution) if self._aif.rgb_dimensions is not None: self._aif.rgb_dimensions.screen.assign_to(self._interface_options.render.resolution) self._aif.rgb_dimensions.minimap.assign_to(self._interface_options.render.minimap_resolution) self._listener = _ReplayCollectorListener(samples_queue, player_id) self._step_mul = step_mul
def __init__(self): # Environment. self.map_name = 'Flat64' screen_resolution = point.Point(32, 32) minimap_resolution = point.Point(32, 32) self.camera_width = 24 self.random_seed = 42 self.interface = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=self.camera_width)) screen_resolution.assign_to(self.interface.feature_layer.resolution) minimap_resolution.assign_to( self.interface.feature_layer.minimap_resolution) # Hard code an action sequence. # TODO(petkoig): Consider whether the Barracks and Supply Depot positions # need to be dynamically determined. self.actions = { 507: select(any_point, units.Terran.SCV), 963: identity_function('Build_SupplyDepot_screen', ['now', [25, 15]]), 1152: select(avg_point, units.Terran.CommandCenter), 1320: identity_function('Train_SCV_quick', ['now']), 1350: identity_function('Train_SCV_quick', ['now']), 1393: identity_function('Train_SCV_quick', ['now']), 1437: identity_function('Train_SCV_quick', ['now']), 1522: select(any_point, units.Terran.SCV), 1548: identity_function('Build_Barracks_screen', ['now', [25, 25]]), 1752: select(avg_point, units.Terran.CommandCenter), 1937: identity_function('Train_SCV_quick', ['now']), 2400: select(avg_point, units.Terran.Barracks), 2700: identity_function('Train_Marine_quick', ['now']), 3300: select(any_point, units.Terran.Marine), } self.num_observations = max(self.actions.keys()) + 2 self.player_id = 1
async def join_game(self, race=None, observed_player_id=None, portconfig=None): # Note modified by Davey for pysc2 screen_size_px = (64, 64) minimap_size_px = (64, 64) screen_size_px = point.Point(*screen_size_px) minimap_size_px = point.Point(*minimap_size_px) ifopts = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=24)) screen_size_px.assign_to(ifopts.feature_layer.resolution) minimap_size_px.assign_to(ifopts.feature_layer.minimap_resolution) if race is None: assert isinstance(observed_player_id, int) # join as observer req = sc_pb.RequestJoinGame(observed_player_id=observed_player_id, options=ifopts) else: assert isinstance(race, Race) req = sc_pb.RequestJoinGame(race=race.value, options=ifopts) if portconfig: req.shared_port = portconfig.shared req.server_ports.game_port = portconfig.server[0] req.server_ports.base_port = portconfig.server[1] for ppc in portconfig.players: p = req.client_ports.add() p.game_port = ppc[0] p.base_port = ppc[1] result = await self._execute(join_game=req) self._game_result = None self._player_id = result.join_game.player_id return result.join_game.player_id
def _get_interface(map_name, game_core_config={}): crop = (game_core_config['crop_to_playable_area'] if 'crop_to_playable_area' in game_core_config else False) map_name = map_name_transform(map_name) map_size = MAP_PLAYABLE_SIZE_DICT[map_name] if crop else MAP_ORI_SIZE_DICT[ map_name] screen_size = point.Point(16, 16) minimap_size = point.Point(int(map_size[0]), int(map_size[1])) interface = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=24)) screen_size.assign_to(interface.feature_layer.resolution) minimap_size.assign_to(interface.feature_layer.minimap_resolution) if crop: interface.feature_layer.crop_to_playable_area = True interface.raw_crop_to_playable_area = True if 'show_cloaked' in game_core_config: interface.show_cloaked = game_core_config['show_cloaked'] if 'show_burrowed_shadows' in game_core_config: interface.show_burrowed_shadows = game_core_config[ 'show_burrowed_shadows'] if 'show_placeholders' in game_core_config: interface.show_placeholders = game_core_config['show_placeholders'] return interface
async def join_game(self, race=None, observed_player_id=None, portconfig=None): ifopts = sc_pb.InterfaceOptions(raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup ( width=24, resolution=common_pb.Size2DI(x=50,y=50), minimap_resolution=common_pb.Size2DI(x=50,y=50) ) ) # 임시로 설정해둔 값 if race is None: assert isinstance(observed_player_id, int) # join as observer req = sc_pb.RequestJoinGame( observed_player_id=observed_player_id, options=ifopts ) else: assert isinstance(race, Race) req = sc_pb.RequestJoinGame( race=race.value, options=ifopts ) if portconfig: req.shared_port = portconfig.shared req.server_ports.game_port = portconfig.server[0] req.server_ports.base_port = portconfig.server[1] for ppc in portconfig.players: p = req.client_ports.add() p.game_port = ppc[0] p.base_port = ppc[1] result = await self._execute(join_game=req) self._game_result = None self._player_id = result.join_game.player_id return result.join_game.player_id
def __init__(self): # Environment. self.map_name = 'NewkirkPrecinct' screen_resolution = point.Point(32, 32) minimap_resolution = point.Point(32, 32) self.camera_width = 24 self.random_seed = 42 self.interface = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=self.camera_width)) screen_resolution.assign_to(self.interface.feature_layer.resolution) minimap_resolution.assign_to( self.interface.feature_layer.minimap_resolution) self.num_observations = 3000 self.player_id = 1 # Hard code an action sequence. # TODO(petkoig): Consider whether the Barracks and Supply Depot positions # need to be dynamically determined. self.actions = { 507: select_scv, 963: identity_function('Build_SupplyDepot_screen', ['now', [4, 19]]), 1152: select_command_center, 1320: identity_function('Train_SCV_quick', ['now']), 1350: identity_function('Train_SCV_quick', ['now']), 1393: identity_function('Train_SCV_quick', ['now']), 1437: identity_function('Train_SCV_quick', ['now']), 1564: identity_function('Train_SCV_quick', ['now']), 1602: identity_function('Train_SCV_quick', ['now']), 1822: select_scv, 2848: identity_function('Build_Barracks_screen', ['now', [22, 22]]) }
import numpy as np import json as json PROJ_DIR = os.path.dirname(os.path.abspath(__file__)) FLAGS = flags.FLAGS flags.DEFINE_integer("parallel", 1, "How many instances to run in parallel.") flags.DEFINE_integer("step_mul", 8, "How many game steps per observation.") flags.DEFINE_string("replays", "%s/replays/mineral1.SC2Replay" % PROJ_DIR, "Path to a directory of replays.") flags.mark_flag_as_required("replays") size = point.Point(32, 32) interface = sc_pb.InterfaceOptions( raw=True, score=False, feature_layer=sc_pb.SpatialCameraSetup(width=24)) size.assign_to(interface.feature_layer.resolution) size.assign_to(interface.feature_layer.minimap_resolution) def sorted_dict_str(d): return "{%s}" % ", ".join("%s: %s" % (k, d[k]) for k in sorted(d, key=d.get, reverse=True)) class ReplayStats(object): """Summary stats of the replays seen so far.""" def __init__(self): self.replays = 0 self.steps = 0 self.camera_move = 0
def _setup(self, player_setup, map_name, screen_size_px=(64, 64), minimap_size_px=(64, 64), camera_width_world_units=None, discount=1., visualize=False, step_mul=None, save_replay_episodes=0, replay_dir=None, game_steps_per_episode=None, score_index=None, score_multiplier=None): if save_replay_episodes and not replay_dir: raise ValueError("Missing replay_dir") self._map = maps.get(map_name) self._discount = discount self._step_mul = step_mul or self._map.step_mul self._save_replay_episodes = save_replay_episodes 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._parallel = run_parallel.RunParallel() # Needed for multiplayer. 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) self._launch(interface, player_setup) game_info = self._controllers[0].game_info() static_data = self._controllers[0].data() self._features = features.Features(game_info) if visualize: self._renderer_human = renderer_human.RendererHuman() self._renderer_human.init(game_info, static_data) else: self._renderer_human = None self._episode_count = 0 self._obs = None self._state = environment.StepType.LAST # Want to jump to `reset`. logging.info("Environment is ready.")
flags.DEFINE_integer(name='n_instance', default=16, help='# of processes to run') flags.DEFINE_integer(name='batch_size', default=10, help='# of replays to process in one iter') flags.DEFINE_integer(name='width', default=24, help='World width') flags.DEFINE_integer(name='map_size', default=64, help='Map size') FLAGS(sys.argv) size = point.Point(FLAGS.map_size, FLAGS.map_size) interface = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=FLAGS.width)) size.assign_to(interface.feature_layer.resolution) size.assign_to(interface.feature_layer.minimap_resolution) class ReplayProcessor(multiprocessing.Process): """A Process that pulls replays and processes them.""" def __init__(self, run_config, replay_queue, counter, total_num): super(ReplayProcessor, self).__init__() self.run_config = run_config self.replay_queue = replay_queue self.counter = counter self.total_num = total_num def run(self): signal.signal(signal.SIGTERM, lambda a, b: sys.exit()) # Exit quietly.
FLAGS = flags.FLAGS flags.DEFINE_integer("parallel", 1, "How many instances to run in parallel.") flags.DEFINE_integer("step_mul", 8, "How many game steps per observation.") flags.DEFINE_string("replays", None, "Path to a directory of replays.") flags.mark_flag_as_required("replays") flags.DEFINE_string("game_data_destination", None, "Path to a destination directory for the serialized replay data") flags.mark_flag_as_required("game_data_destination") flags.DEFINE_integer("print_time", 300, "Interval between stat prints and data saves in seconds") size = point.Point(16, 16) interface = sc_pb.InterfaceOptions( raw=True, score=False, feature_layer=sc_pb.SpatialCameraSetup(width=24)) size.assign_to(interface.feature_layer.resolution) size.assign_to(interface.feature_layer.minimap_resolution) def sorted_dict_str(d): return "{%s}" % ", ".join("%s: %s" % (k, d[k]) for k in sorted(d, key=d.get, reverse=True)) class ReplayStats(object): """Summary stats of the replays seen so far.""" def __init__(self): self.replays = 0 self.steps = 0
def __init__(self, # pylint: disable=invalid-name _only_use_kwargs=None, map_name=None, 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: _only_use_kwargs: Don't pass args, only kwargs. 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 _only_use_kwargs: raise ValueError("All arguments must be passed as keyword arguments.") 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.")
def __init__(self, replay_file_path, parser_objects, player_id=1, screen_size=(64, 64), minimap_size=(64, 64), discount=1., step_mul=1, override=False): self.replay_file_path = os.path.abspath(replay_file_path) self.replay_name = os.path.split(replay_file_path)[-1].replace( '.SC2Replay', '') self.write_dir = os.path.join(FLAGS.result_dir, FLAGS.race_matchup, self.replay_name) if isinstance(parser_objects, list): self.parsers = [p_obj(self.write_dir) for p_obj in parser_objects] elif issubclass(parser_objects, ParserBase): self.parsers = [parser_objects(self.write_dir)] else: raise ValueError( "Argument 'parsers' expects a single or list of Parser objects." ) self.player_id = player_id self.discount = discount self.step_mul = step_mul self.override = override # Configure screen size if isinstance(screen_size, tuple): self.screen_size = screen_size elif isinstance(screen_size, int): self.screen_size = (screen_size, screen_size) else: raise ValueError( "Argument 'screen_size' requires a tuple of size 2 or a single integer." ) # Configure minimap size if isinstance(minimap_size, tuple): self.minimap_size = minimap_size elif isinstance(minimap_size, int): self.minimap_size = (minimap_size, minimap_size) else: raise ValueError( "Argument 'minimap_size' requires a tuple of size 2 or a single integer." ) assert len(self.screen_size) == 2 assert len(self.minimap_size) == 2 # Arguments for 'sc_process.StarCraftProcess'. Check the following: # https://github.com/deepmind/pysc2/blob/master/pysc2/lib/sc_process.py try: sc2_process_configs = { "full_screen": False, 'timeout_seconds': 300 } self.run_config = run_configs.get() self.sc2_process = self.run_config.start(**sc2_process_configs) self.controller = self.sc2_process.controller except websocket.WebSocketTimeoutException as e: raise ConnectionRefusedError( f'Connection to SC2 process unavailable. ({e})') except protocol.ConnectionError as e: raise ConnectionRefusedError( f'Connection to SC2 process unavailable. ({e})') # Check the following links for usage of run_config and controller. # https://github.com/deepmind/pysc2/blob/master/pysc2/run_configs/platforms.py # https://github.com/deepmind/pysc2/blob/master/pysc2/lib/sc_process.py # https://github.com/deepmind/pysc2/blob/master/pysc2/lib/remote_controller.py # Load replay information & check validity. replay_data = self.run_config.replay_data(self.replay_file_path) info = self.controller.replay_info(replay_data) if not self.check_valid_replay(info, self.controller.ping()): self.safe_escape() raise ValueError('Invalid replay.') # Filter replay by race matchup if FLAGS.race_matchup is not None: if not self.check_valid_matchup(info, matchup=FLAGS.race_matchup): self.safe_escape() raise ValueError('Invalid matchup.') # Map name self.map_name = info.map_name print('...') # 'raw=True' returns enables the use of 'feature_units' # https://github.com/Blizzard/s2client-proto/blob/master/docs/protocol.md#interfaces interface = sc_pb.InterfaceOptions( raw=False, score=True, show_cloaked=False, feature_layer=sc_pb.SpatialCameraSetup(width=24, allow_cheating_layers=True)) self.screen_size = point.Point(*self.screen_size) self.minimap_size = point.Point(*self.minimap_size) self.screen_size.assign_to(interface.feature_layer.resolution) self.minimap_size.assign_to(interface.feature_layer.minimap_resolution) map_data = None if info.local_map_path: map_data = self.run_config.map_data(info.local_map_path) self._episode_length = info.game_duration_loops self._episode_steps = 0 # Request replay self.controller.start_replay(req_start_replay=sc_pb.RequestStartReplay( replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=self.player_id, disable_fog=True, )) self._state = environment.StepType.FIRST self.info = info
def get_random_trajectory(self): function_dict = {} for _FUNCTION in actions._FUNCTIONS: #print(_FUNCTION) function_dict[_FUNCTION.ability_id] = _FUNCTION.name race_list = ['Terran', 'Zerg', 'Protoss'] """How many agent steps the agent has been trained for.""" run_config = run_configs.get() sc2_proc = run_config.start() controller = sc2_proc.controller #print ("source: {}".format(source)) #root_path = '/media/kimbring2/Steam/StarCraftII/Replays/4.8.2.71663-20190123_035823-1' root_path = self.source file_list = glob.glob(root_path + '*.*') #print ("file_list: {}".format(file_list)) for i in range(0, 500): #print("i: " + str(i)) replay_file_path = random.choice(file_list) #print ("replay_file_path: {}".format(replay_file_path)) #replay_file_path = root_path + '0a0f62052fe4311368910ad38c662bf979e292b86ad02b49b41a87013e58c432.SC2Replay' #replay_file_path = root_path + '/0a1b09abc9e98f4e0c3921ae0a427c27e97c2bbdcf34f50df18dc41cea3f3249.SC2Replay' #replay_file_path_2 = root_path + '/0a01d32e9a98e1596b88bc2cdec7752249b22aca774e3305dae2e93efef34be3.SC2Replay' #replay_file_path_0 = human_data #print ("replay_file_path: {}".format(replay_file_path)) try: replay_data = run_config.replay_data(replay_file_path) ping = controller.ping() info = controller.replay_info(replay_data) print("ping: " + str(ping)) print("replay_info: " + str(info)) player0_race = info.player_info[0].player_info.race_actual player0_mmr = info.player_info[0].player_mmr player0_apm = info.player_info[0].player_apm player0_result = info.player_info[0].player_result.result print("player0_race: " + str(player0_race)) print("player0_mmr: " + str(player0_mmr)) print("player0_apm: " + str(player0_apm)) print("player0_result: " + str(player0_result)) home_race = race_list.index(self.home_race_name) + 1 if (home_race == player0_race): print("player0_race pass") else: print("player0_race fail") continue if (player0_mmr >= self.replay_filter): print("player0_mmr pass ") else: print("player0_mmr fail") continue player1_race = info.player_info[0].player_info.race_actual player1_mmr = info.player_info[0].player_mmr player1_apm = info.player_info[0].player_apm player1_result = info.player_info[0].player_result.result print("player1_race: " + str(player1_race)) print("player1_mmr: " + str(player1_mmr)) print("player1_apm: " + str(player1_apm)) print("player1_result: " + str(player1_result)) away_race = race_list.index(self.away_race_name) + 1 if (away_race == player1_race): print("player1_race pass ") else: print("player1_race fail ") continue if (player1_mmr >= self.replay_filter): print("player1_mmr pass ") else: print("player1_mmr fail") continue screen_size_px = (128, 128) minimap_size_px = (64, 64) player_id = 1 discount = 1. step_mul = 8 screen_size_px = point.Point(*screen_size_px) minimap_size_px = point.Point(*minimap_size_px) interface = sc_pb.InterfaceOptions( raw=False, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=24)) screen_size_px.assign_to(interface.feature_layer.resolution) minimap_size_px.assign_to( interface.feature_layer.minimap_resolution) map_data = None if info.local_map_path: map_data = run_config.map_data(info.local_map_path) _episode_length = info.game_duration_loops _episode_steps = 0 controller.start_replay( sc_pb.RequestStartReplay(replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=player_id)) _state = StepType.FIRST if (info.HasField("error") or info.base_build != ping.base_build or # different game version info.game_duration_loops < 1000 or len(info.player_info) != 2): # Probably corrupt, or just not interesting. print("error") continue feature_screen_size = 128 feature_minimap_size = 64 rgb_screen_size = None rgb_minimap_size = None action_space = None use_feature_units = True agent_interface_format = sc2_env.parse_agent_interface_format( feature_screen=feature_screen_size, feature_minimap=feature_minimap_size, rgb_screen=rgb_screen_size, rgb_minimap=rgb_minimap_size, action_space=action_space, use_feature_units=use_feature_units) _features = features.features_from_game_info( controller.game_info()) build_info = [] build_name = [] replay_step = 0 print("True loop") while True: replay_step += 1 print("replay_step: " + str(replay_step)) controller.step(step_mul) obs = controller.observe() self.home_trajectory.append(obs) if (len(obs.actions) != 0): action = (obs.actions)[0] action_spatial = action.action_feature_layer unit_command = action_spatial.unit_command ability_id = unit_command.ability_id function_name = function_dict[ability_id] if (function_name != 'build_queue'): function_name_parse = function_name.split('_') function_name_first = function_name_parse[0] #print("function_name_first: " + str(function_name_first)) if (function_name_first == 'Build' or function_name_first == 'Train'): unit_name = function_name_parse[1] unit_info = int( units_new.get_unit_type( self.home_race_name, unit_name)) #print("unit_name: " + str(unit_name)) #print("unit_info: " + str(unit_info)) #print("function_name_parse[1]: " + str(function_name_parse[1])) build_name.append(unit_name) build_info.append(unit_info) if obs.player_result: # Episide over. _state = StepType.LAST discount = 0 else: discount = discount _episode_steps += step_mul agent_obs = _features.transform_obs(obs) step = TimeStep(step_type=_state, reward=0, discount=discount, observation=agent_obs) score_cumulative = agent_obs['score_cumulative'] score_cumulative_dict = {} score_cumulative_dict['score'] = score_cumulative.score score_cumulative_dict[ 'idle_production_time'] = score_cumulative.idle_production_time score_cumulative_dict[ 'idle_worker_time'] = score_cumulative.idle_worker_time score_cumulative_dict[ 'total_value_units'] = score_cumulative.total_value_units score_cumulative_dict[ 'total_value_structures'] = score_cumulative.total_value_structures score_cumulative_dict[ 'killed_value_units'] = score_cumulative.killed_value_units score_cumulative_dict[ 'killed_value_structures'] = score_cumulative.killed_value_structures score_cumulative_dict[ 'collected_minerals'] = score_cumulative.collected_minerals score_cumulative_dict[ 'collected_vespene'] = score_cumulative.collected_vespene score_cumulative_dict[ 'collection_rate_minerals'] = score_cumulative.collection_rate_minerals score_cumulative_dict[ 'collection_rate_vespene'] = score_cumulative.collection_rate_vespene score_cumulative_dict[ 'spent_minerals'] = score_cumulative.spent_minerals score_cumulative_dict[ 'spent_vespene'] = score_cumulative.spent_vespene if obs.player_result: break _state = StepType.MID self.home_BO = build_info self.away_BU = score_cumulative_dict break except: continue
def run(self): try: self.is_running = True """A run loop to have agents and an environment interact.""" total_frames = 0 total_episodes = 0 results = [0, 0, 0] start_time = time() print("start_time before training:", strftime("%Y-%m-%d %H:%M:%S", localtime(start_time))) while time() - start_time < self.max_time_for_training: self.opponent, _ = self.player.get_match() agents = [self.player, self.opponent] # if self.use_replay_expert_reward: run_config = run_configs.get( version=self.replay_version ) # the replays released by blizzard are all 3.16.1 version with self.create_env(self.player, self.opponent) as env: # set the obs and action spec observation_spec = env.observation_spec() action_spec = env.action_spec() for agent, obs_spec, act_spec in zip( agents, observation_spec, action_spec): agent.setup(obs_spec, act_spec) self.teacher.setup(self.player.agent.obs_spec, self.player.agent.action_spec) print('player:', self.player) if debug else None print('opponent:', self.opponent) if debug else None print('teacher:', self.teacher) if debug else None trajectory = [] start_time = time() # in seconds. print("start_time before reset:", strftime("%Y-%m-%d %H:%M:%S", localtime(start_time))) # one opponent match (may include several games) defaultly lasts for no more than 2 hour while time() - start_time < self.max_time_per_one_opponent: # Note: the pysc2 environment don't return z # AlphaStar: home_observation, away_observation, is_final, z = env.reset() total_episodes += 1 print("total_episodes:", total_episodes) timesteps = env.reset() for a in agents: a.reset() # check the condition that the replay is over but the game is not with run_config.start(full_screen=False) as controller: # here we must use the with ... as ... statement, or it will cause an error #controller = run_config.start(full_screen=False) # start replay reward raw_affects_selection = False raw_crop_to_playable_area = False screen_resolution = point.Point(64, 64) minimap_resolution = point.Point(64, 64) camera_width = 24 interface = sc_pb.InterfaceOptions( raw=True, score=True, # Omit to disable. feature_layer=sc_pb.SpatialCameraSetup( width=camera_width), # Omit to disable. render=None, # By default cloaked units are completely hidden. This shows some details. show_cloaked=False, # By default burrowed units are completely hidden. This shows some details for those that produce a shadow. show_burrowed_shadows=False, # Return placeholder units (buildings to be constructed), both for raw and feature layers. show_placeholders=False, # see below raw_affects_selection=raw_affects_selection, # see below raw_crop_to_playable_area= raw_crop_to_playable_area) screen_resolution.assign_to( interface.feature_layer.resolution) minimap_resolution.assign_to( interface.feature_layer.minimap_resolution) replay_files = os.listdir(self.replay_path) # random select a replay file from the candidate replays random.shuffle(replay_files) replay_path = self.replay_path + replay_files[0] print('replay_path:', replay_path) replay_data = run_config.replay_data(replay_path) replay_info = controller.replay_info(replay_data) infos = replay_info.player_info observe_id_list = [] observe_result_list = [] for info in infos: print('info:', info) if debug else None player_info = info.player_info result = info.player_result.result print('player_info', player_info) if debug else None if player_info.race_actual == com_pb.Protoss: observe_id_list.append( player_info.player_id) observe_result_list.append(result) win_observe_id = 0 for i, result in enumerate(observe_result_list): if result == sc_pb.Victory: win_observe_id = observe_id_list[i] break start_replay = sc_pb.RequestStartReplay( replay_data=replay_data, options=interface, disable_fog=False, # FLAGS.disable_fog observed_player_id= win_observe_id, # FLAGS.observed_player map_data=None, realtime=False) controller.start_replay(start_replay) feat = F.features_from_game_info( game_info=controller.game_info(), raw_resolution=AAIFP.raw_resolution, hide_specific_actions=AAIFP. hide_specific_actions, use_feature_units=True, use_raw_units=True, use_unit_counts=True, use_raw_actions=True, show_cloaked=True, show_burrowed_shadows=True, show_placeholders=True) replay_obs = None replay_bo = [] replay_o = controller.observe() replay_obs = feat.transform_obs(replay_o) # end replay reward [home_obs, away_obs] = timesteps is_final = home_obs.last() player_memory = self.player.agent.initial_state() opponent_memory = self.opponent.agent.initial_state( ) teacher_memory = self.teacher.initial_state() # initial build order player_bo = [] episode_frames = 0 # default outcome is 0 (means draw) outcome = 0 # in one episode (game) # start_episode_time = time() # in seconds. print( "start_episode_time before is_final:", strftime("%Y-%m-%d %H:%M:%S", localtime(start_episode_time))) while not is_final: total_frames += 1 episode_frames += 1 state = self.player.agent.agent_nn.preprocess_state_all( home_obs.observation, build_order=player_bo) state_op = self.player.agent.agent_nn.preprocess_state_all( away_obs.observation) # baseline_state = self.player.agent.agent_nn.get_scalar_list(home_obs.observation, build_order=player_bo) # baseline_state_op = self.player.agent.agent_nn.get_scalar_list(away_obs.observation) baseline_state = self.player.agent.agent_nn.get_baseline_state_from_multi_source_state( state) baseline_state_op = self.player.agent.agent_nn.get_baseline_state_from_multi_source_state( state_op) player_step = self.player.agent.step_from_state( state, player_memory) player_function_call, player_action, player_logits, player_new_memory = player_step print("player_function_call:", player_function_call) if debug else None opponent_step = self.opponent.agent.step_from_state( state_op, opponent_memory) opponent_function_call, opponent_action, opponent_logits, opponent_new_memory = opponent_step # Q: how to do it ? # teacher_logits = self.teacher(home_obs, player_action, teacher_memory) # may change implemention of teacher_logits teacher_step = self.teacher.step_from_state( state, teacher_memory) teacher_function_call, teacher_action, teacher_logits, teacher_new_memory = teacher_step print("teacher_function_call:", teacher_function_call) if debug else None env_actions = [ player_function_call, opponent_function_call ] player_action_spec = action_spec[0] action_masks = U.get_mask( player_action, player_action_spec) z = None timesteps = env.step(env_actions) [home_next_obs, away_next_obs] = timesteps # print the observation of the agent # print("home_obs.observation:", home_obs.observation) reward = home_next_obs.reward print("reward: ", reward) if debug else None is_final = home_next_obs.last() # calculate the build order player_bo = L.calculate_build_order( player_bo, home_obs.observation, home_next_obs.observation) print("player build order:", player_bo) if debug else None # calculate the unit counts of bag player_ucb = L.calculate_unit_counts_bow( home_obs.observation).reshape( -1).numpy().tolist() print("player unit count of bow:", sum(player_ucb)) if debug else None # start replay_reward # note the controller should step the same steps as with the rl actor (keep the time as the same) controller.step(STEP_MUL) replay_next_o = controller.observe() replay_next_obs = feat.transform_obs( replay_next_o) # calculate the build order for replay replay_bo = L.calculate_build_order( replay_bo, replay_obs, replay_next_obs) print("replay build order:", player_bo) if debug else None # calculate the unit counts of bag for replay replay_ucb = L.calculate_unit_counts_bow( replay_obs).reshape(-1).numpy().tolist() print("replay unit count of bow:", sum(replay_ucb)) if debug else None # end replay_reward game_loop = home_obs.observation.game_loop[0] print("game_loop", game_loop) if debug else None # note, original AlphaStar pseudo-code has some mistakes, we modified # them here traj_step = Trajectory( state=state, baseline_state=baseline_state, baseline_state_op=baseline_state_op, memory=player_memory, z=z, masks=action_masks, action=player_action, behavior_logits=player_logits, teacher_logits=teacher_logits, is_final=is_final, reward=reward, build_order=player_bo, z_build_order= replay_bo, # we change it to the sampled build order unit_counts=player_ucb, z_unit_counts= replay_ucb, # we change it to the sampled unit counts game_loop=game_loop, ) trajectory.append(traj_step) player_memory = tuple( h.detach() for h in player_new_memory) opponent_memory = tuple( h.detach() for h in opponent_new_memory) teacher_memory = tuple( h.detach() for h in teacher_new_memory) home_obs = home_next_obs away_obs = away_next_obs # for replay reward replay_obs = replay_next_obs replay_o = replay_next_o if is_final: outcome = reward print("outcome: ", outcome) if debug else None results[outcome + 1] += 1 if len(trajectory) >= AHP.sequence_length: trajectories = U.stack_namedtuple( trajectory) if self.player.learner is not None: if self.player.learner.is_running: print("Learner send_trajectory!") self.player.learner.send_trajectory( trajectories) trajectory = [] else: print("Learner stops!") print("Actor also stops!") return # use max_frames to end the loop # whether to stop the run if self.max_frames and total_frames >= self.max_frames: print("Beyond the max_frames, return!") return # use max_frames_per_episode to end the episode if self.max_frames_per_episode and episode_frames >= self.max_frames_per_episode: print( "Beyond the max_frames_per_episode, break!" ) break # end of replay if replay_o.player_result: print(replay_o.player_result) break self.coordinator.send_outcome( self.player, self.opponent, outcome) # use max_frames_per_episode to end the episode if self.max_episodes and total_episodes >= self.max_episodes: print("Beyond the max_episodes, return!") print("results: ", results) if debug else None print("win rate: ", results[2] / (1e-8 + sum(results))) if debug else None return # close the replays except Exception as e: print( "ActorLoop.run() Exception cause return, Detials of the Exception:", e) print(traceback.format_exc()) finally: self.is_running = False
flags.DEFINE_integer(name='step_size', default=72, help='# of frames to step') flags.DEFINE_integer(name='width', default=24, help='World width of rendered area in screen') flags.DEFINE_integer(name='map_size', default=64, help='Spatial observation size in pixels') FLAGS(sys.argv) size = point.Point(FLAGS.map_size, FLAGS.map_size) interface = sc_pb.InterfaceOptions( raw=True, score=True, feature_layer=sc_pb.SpatialCameraSetup(width=FLAGS.width, allow_cheating_layers=True), ) size.assign_to(interface.feature_layer.resolution) size.assign_to(interface.feature_layer.minimap_resolution) class ReplayProcessor(multiprocessing.Process): """A Process that pulls replays and processes them.""" def __init__(self, run_config, replay_queue): super(ReplayProcessor, self).__init__() self.run_config = run_config self.replay_queue = replay_queue def run(self): signal.signal( signal.SIGTERM,
def run_alphastar_replay(on_server=False, race_name='Protoss'): if on_server: REPLAY_PATH = "/home/liuruoze/mini-AlphaStar/data/filtered_replays_1/" COPY_PATH = None SAVE_PATH = "./result.csv" max_steps_of_replay = FLAGS.max_steps_of_replay max_replays = FLAGS.max_replays else: REPLAY_PATH = "data/Replays/replays_paper_ready/Final/" + race_name + "/" COPY_PATH = None SAVE_PATH = "./result.csv" max_steps_of_replay = FLAGS.max_steps_of_replay max_replays = 10 run_config = run_configs.get(version=FLAGS.replay_version) print('REPLAY_PATH:', REPLAY_PATH) replay_files = os.listdir(REPLAY_PATH) replay_files.sort(reverse=False) screen_resolution = point.Point(FLAGS.screen_resolution, FLAGS.screen_resolution) minimap_resolution = point.Point(FLAGS.minimap_resolution, FLAGS.minimap_resolution) camera_width = 24 # By default raw actions select, act and revert the selection. This is useful # if you're playing simultaneously with the agent so it doesn't steal your # selection. This inflates APM (due to deselect) and makes the actions hard # to follow in a replay. Setting this to true will cause raw actions to do # select, act, but not revert the selection. raw_affects_selection = False # Changes the coordinates in raw.proto to be relative to the playable area. # The map_size and playable_area will be the diagonal of the real playable area. raw_crop_to_playable_area = False interface = sc_pb.InterfaceOptions( raw=True, score=True, # Omit to disable. feature_layer=sc_pb.SpatialCameraSetup(width=camera_width), # Omit to disable. render=None, # By default cloaked units are completely hidden. This shows some details. show_cloaked=False, # By default burrowed units are completely hidden. This shows some details for those that produce a shadow. show_burrowed_shadows=False, # Return placeholder units (buildings to be constructed), both for raw and feature layers. show_placeholders=False, # see below raw_affects_selection=raw_affects_selection, # see below raw_crop_to_playable_area=raw_crop_to_playable_area) screen_resolution.assign_to(interface.feature_layer.resolution) minimap_resolution.assign_to(interface.feature_layer.minimap_resolution) agent = Agent() replay_length_list = [] noop_length_list = [] with open("cameras.txt", "w") as f: pass with run_config.start(full_screen=False) as controller: for idx in range(2): j = 0 for replay_file in replay_files: j += 1 if j > max_replays: # test the first n frames print("max replays test, break out!") break try: replay_path = REPLAY_PATH + replay_file print('replay_path:', replay_path) replay_data = run_config.replay_data(replay_path) replay_info = controller.replay_info(replay_data) print('replay_info:', replay_info) base = os.path.basename(replay_path) replay_name = os.path.splitext(base)[0] print('replay_name:', replay_name) info = replay_info.player_info as_id = -1 hp_id = -1 for x in range(2): player_name = info[x].player_info.player_name print('player_name:', player_name) the_id = x + 1 if player_name == "AlphaStar": print('Find AlphaStar!') print('AlphaStar ID is', the_id) as_id = the_id else: hp_id = the_id print("as_id", as_id) print("hp_id", hp_id) if idx == 0: player_id = as_id player_name = info[player_id - 1].player_info.player_name assert player_name == "AlphaStar" player_name = "AS" else: player_id = hp_id player_name = "HP" start_replay = sc_pb.RequestStartReplay( replay_data=replay_data, options=interface, disable_fog=False, # FLAGS.disable_fog observed_player_id=player_id, map_data=None, realtime=False) #print('stop', stop) #print("-" * 60) if debug else None controller.start_replay(start_replay) # The below several arguments are default set to False, so we shall enable them. # use_feature_units: Whether to include feature_unit observations. # use_raw_units: Whether to include raw unit data in observations. This # differs from feature_units because it includes units outside the # screen and hidden units, and because unit positions are given in # terms of world units instead of screen units. # use_raw_actions: [bool] Whether to use raw actions as the interface. # Same as specifying action_space=ActionSpace.RAW. # use_unit_counts: Whether to include unit_counts observation. Disabled by # default since it gives information outside the visible area. ''' show_cloaked: Whether to show limited information for cloaked units. show_burrowed_shadows: Whether to show limited information for burrowed units that leave a shadow on the ground (ie widow mines and moving roaches and infestors). show_placeholders: Whether to show buildings that are queued for construction. ''' feat = F.features_from_game_info( game_info=controller.game_info(), use_feature_units=True, use_raw_units=True, use_unit_counts=True, use_raw_actions=True, show_cloaked=True, show_burrowed_shadows=True, show_placeholders=True) #print("feat obs spec:", feat.observation_spec()) if debug else None #print("feat action spec:", feat.action_spec()) if debug else None prev_obs = None i = 0 save_steps = 0 noop_count = 0 camera_count = 0 all_op_count = 0 feature_list, label_list = [], [] step_dict = {} # set the obs and action spec obs_spec = feat.observation_spec() act_spec = feat.action_spec() while True: o = controller.observe() try: obs = feat.transform_obs(o) try: func_call = None no_op = False if o.actions and prev_obs: func_call = getFuncCall(o, feat, prev_obs) if func_call.function.value == 168: camera_count += 1 if func_call.function.value == 0: no_op = True func_call = None else: no_op = True if no_op: pass else: all_op_count += 1 except Exception as e: traceback.print_exc() if i >= max_steps_of_replay: # test the first n frames print("max frames test, break out!") break if o.player_result: # end of game # print(o.player_result) break except Exception as inst: traceback.print_exc() controller.step() prev_obs = obs i += 1 # print("player_id", player_id, "player_name", player_name, # "camera_count", camera_count, "all_op_count", all_op_count, # "no_camera_op_rate", 1.0 - camera_count / (all_op_count + 1e-9)) print("player_id", player_id, "player_name", player_name, ",", camera_count, all_op_count, 1.0 - camera_count / (all_op_count + 1e-9)) print(" ") with open("cameras.txt", "a") as f: print(replay_name, ",", player_id, ",", player_name, ",", camera_count, ",", all_op_count, ",", 1.0 - camera_count / (all_op_count + 1e-9), file=f) replay_length_list.append(save_steps) noop_length_list.append(noop_count) # We only test the first one replay except Exception as inst: traceback.print_exc() print("end")