def parse_replay(replay_player_path, sampled_frame_path, reward): if os.path.isfile(os.path.join(FLAGS.parsed_replay_path, 'GlobalFeatures', replay_player_path)): return # Global Info with open(os.path.join(FLAGS.parsed_replay_path, 'GlobalInfos', replay_player_path)) as f: global_info = json.load(f) units_info = static_data.StaticData(Parse(global_info['data_raw'], sc_pb.ResponseData())).units feat = features.features_from_game_info(Parse(global_info['game_info'], sc_pb.ResponseGameInfo())) # Sampled Frames with open(sampled_frame_path) as f: sampled_frames = json.load(f) sampled_actions_idx = [frame // FLAGS.step_mul - 1 for frame in sampled_frames] # Create index to retrieve actions corresponding to sampled frames # Actions with open(os.path.join(FLAGS.parsed_replay_path, 'Actions', replay_player_path)) as f: actions = json.load(f) sampled_actions = [None if len(actions[idx]) == 0 else Parse(actions[idx][0], sc_pb.Action()) for idx in sampled_actions_idx] # Get first action executed after each sampled frame # Observations observations = [obs for obs in stream.parse(os.path.join(FLAGS.parsed_replay_path, 'SampledObservations', replay_player_path), sc_pb.ResponseObservation)] assert len(sampled_frames) == len(sampled_actions_idx) == len(sampled_actions) == len(observations) states = process_replay(sampled_frames, sampled_actions, observations, feat, units_info, reward) with open(os.path.join(FLAGS.parsed_replay_path, 'GlobalFeatures', replay_player_path), 'w') as f: json.dump(states, f)
def start(self): _features = features.features_from_game_info( self.controller.game_info()) i = 0 while i < self.info.game_duration_loops: i += self.skip self.controller.step(self.step_mul) obs = self.controller.observe() try: agent_obs = _features.transform_obs(obs) except: pass if obs.player_result: self._state = StepType.LAST discount = 0 else: discount = self.discount self._episode_steps += self.step_mul step = TimeStep(step_type=self._state, reward=0, discount=discount, observation=agent_obs) self.agent.step(step, obs.actions) if obs.player_result: break self._state = StepType.MID self.save_data()
def check_apm(self, name): """Set up a game, yield, then check the apm in the replay.""" interface = sc_pb.InterfaceOptions(raw=True, score=False) interface.feature_layer.width = 24 interface.feature_layer.resolution.x = 64 interface.feature_layer.resolution.y = 64 interface.feature_layer.minimap_resolution.x = 64 interface.feature_layer.minimap_resolution.y = 64 create = sc_pb.RequestCreateGame(random_seed=1, local_map=sc_pb.LocalMap( map_path=self._map_path, map_data=self._map_data)) create.player_setup.add(type=sc_pb.Participant) create.player_setup.add(type=sc_pb.Computer, race=sc_common.Protoss, difficulty=sc_pb.VeryEasy) join = sc_pb.RequestJoinGame(race=sc_common.Protoss, options=interface) self._controller.create_game(create) self._controller.join_game(join) self._info = self._controller.game_info() self._features = features.features_from_game_info( self._info, use_feature_units=True, use_raw_units=True) self._map_size = point.Point.build(self._info.start_raw.map_size) for i in range(60): yield i, self.step() data = self._controller.save_replay() replay_info = self._controller.replay_info(data) self._summary.append((name, replay_info))
def main(argv): if len(argv) > 1: raise app.UsageError("Too many command-line arguments.") stopwatch.sw.enable() interface = sc_pb.InterfaceOptions() interface.raw = FLAGS.use_feature_units or FLAGS.use_raw_units interface.score = True interface.feature_layer.width = 24 if FLAGS.feature_screen_size and FLAGS.feature_minimap_size: FLAGS.feature_screen_size.assign_to(interface.feature_layer.resolution) FLAGS.feature_minimap_size.assign_to( interface.feature_layer.minimap_resolution) if FLAGS.rgb_screen_size and FLAGS.rgb_minimap_size: FLAGS.rgb_screen_size.assign_to(interface.render.resolution) FLAGS.rgb_minimap_size.assign_to(interface.render.minimap_resolution) run_config = run_configs.get() replay_data = run_config.replay_data(FLAGS.replay) start_replay = sc_pb.RequestStartReplay( replay_data=replay_data, options=interface, observed_player_id=1) version = replay.get_replay_version(replay_data) run_config = run_configs.get(version=version) # Replace the run config. try: with run_config.start( want_rgb=interface.HasField("render")) as controller: info = controller.replay_info(replay_data) print(" Replay info ".center(60, "-")) print(info) print("-" * 60) map_path = FLAGS.map_path or info.local_map_path if map_path: start_replay.map_data = run_config.map_data(map_path) controller.start_replay(start_replay) feats = features.features_from_game_info( game_info=controller.game_info(), use_feature_units=FLAGS.use_feature_units, use_raw_units=FLAGS.use_raw_units, use_unit_counts=interface.raw, use_camera_position=False, action_space=actions.ActionSpace.FEATURES) while True: controller.step(FLAGS.step_mul) obs = controller.observe() feats.transform_obs(obs) if obs.player_result: break except KeyboardInterrupt: pass print(stopwatch.sw)
def _get_replay_data(self, controller, config): """Runs a replay to get the replay data.""" f = features.features_from_game_info(game_info=controller.game_info()) observations = {} last_actions = [] for _ in range(config.num_observations): raw_obs = controller.observe() o = raw_obs.observation obs = f.transform_obs(raw_obs) if raw_obs.action_errors: print('action errors:', raw_obs.action_errors) if o.game_loop == 2: # Center camera is initiated automatically by the game and reported # at frame 2. last_actions = [actions.FUNCTIONS.move_camera.id] self.assertEqual(last_actions, list(obs.last_actions)) unit_type = obs.feature_screen.unit_type observations[o.game_loop] = unit_type if o.game_loop in config.actions: func = config.actions[o.game_loop](obs) print((' loop: %s ' % o.game_loop).center(80, '-')) print(_obs_string(obs)) scv_y, scv_x = (units.Terran.SCV == unit_type).nonzero() print('scv locations: ', sorted(list(zip(scv_x, scv_y)))) print('available actions: ', list(sorted(obs.available_actions))) print('Making action: %s' % (func, )) # Ensure action is available. # If a build action is available, we have managed to target an SCV. self.assertIn(func.function, obs.available_actions) if (func.function in (actions.FUNCTIONS.Build_SupplyDepot_screen.id, actions.FUNCTIONS.Build_Barracks_screen.id)): # Ensure we can build on that position. x, y = func.arguments[1] self.assertEqual(_EMPTY, unit_type[y, x]) action = f.transform_action(o, func) last_actions = [func.function] controller.act(action) else: last_actions = [] controller.step() replay_data = controller.save_replay() return replay_data, observations
def process_replay(self, controller, replay_data, map_data, player_id): """Process a single replay, updating the stats.""" self._update_stage("start_replay") controller.start_replay( sc_pb.RequestStartReplay(replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=player_id)) feat = features.features_from_game_info(controller.game_info()) self.stats.replay_stats.replays += 1 self._update_stage("step") controller.step() while True: self.stats.replay_stats.steps += 1 self._update_stage("observe") obs = controller.observe() for action in obs.actions: act_fl = action.action_feature_layer if act_fl.HasField("unit_command"): self.stats.replay_stats.made_abilities[ act_fl.unit_command.ability_id] += 1 if act_fl.HasField("camera_move"): self.stats.replay_stats.camera_move += 1 if act_fl.HasField("unit_selection_point"): self.stats.replay_stats.select_pt += 1 if act_fl.HasField("unit_selection_rect"): self.stats.replay_stats.select_rect += 1 if action.action_ui.HasField("control_group"): self.stats.replay_stats.control_group += 1 try: func = feat.reverse_action(action).function except ValueError: func = -1 self.stats.replay_stats.made_actions[func] += 1 for valid in obs.observation.abilities: self.stats.replay_stats.valid_abilities[valid.ability_id] += 1 for u in obs.observation.raw_data.units: self.stats.replay_stats.unit_ids[u.unit_type] += 1 for ability_id in feat.available_actions(obs.observation): self.stats.replay_stats.valid_actions[ability_id] += 1 if obs.player_result: break self._update_stage("step") controller.step(FLAGS.step_mul)
def _launch_remote(self, host, config_port, race, name, interface, agent_interface_format): """Make sure this stays synced with bin/play_vs_agent.py.""" self._tcp_conn, settings = tcp_client(Addr(host, config_port)) self._map_name = settings["map_name"] if settings["remote"]: self._udp_sock = udp_server( Addr(host, settings["ports"]["server"]["game"])) daemon_thread(tcp_to_udp, (self._tcp_conn, self._udp_sock, Addr(host, settings["ports"]["client"]["game"]))) daemon_thread(udp_to_tcp, (self._udp_sock, self._tcp_conn)) extra_ports = [ settings["ports"]["server"]["game"], settings["ports"]["server"]["base"], settings["ports"]["client"]["game"], settings["ports"]["client"]["base"], ] self._run_config = run_configs.get(version=settings["game_version"]) self._sc2_procs = [ self._run_config.start(extra_ports=extra_ports, host=host, window_loc=(700, 50), want_rgb=interface.HasField("render")) ] self._controllers = [p.controller for p in self._sc2_procs] # Create the join request. join = sc_pb.RequestJoinGame(options=interface) join.race = race join.player_name = name join.shared_port = 0 # unused join.server_ports.game_port = settings["ports"]["server"]["game"] join.server_ports.base_port = settings["ports"]["server"]["base"] join.client_ports.add(game_port=settings["ports"]["client"]["game"], base_port=settings["ports"]["client"]["base"]) self._controllers[0].save_map(settings["map_path"], settings["map_data"]) self._controllers[0].join_game(join) self._game_info = [self._controllers[0].game_info()] self._features = [ features.features_from_game_info( game_info=self._game_info[0], agent_interface_format=agent_interface_format) ]
def _process_replay(self, controller, observations, config): f = features.features_from_game_info(game_info=controller.game_info()) while True: o = controller.observe() obs = f.transform_obs(o) if o.player_result: # end of game break unit_type = obs.feature_screen.unit_type self.assertEqual( tuple(observations[o.observation.game_loop].flatten()), tuple(unit_type.flatten())) self.assertIn(len(o.actions), (0, 1), 'Expected 0 or 1 action') if o.actions: func = f.reverse_action(o.actions[0]) # Action is reported one frame later. executed = config.actions.get(o.observation.game_loop - 1, None) executed_func = executed(obs) if executed else None print('%4d Sent: %s' % (o.observation.game_loop, executed_func)) print('%4d Returned: %s' % (o.observation.game_loop, func)) if o.observation.game_loop == 2: # Center camera is initiated automatically by the game and reported # at frame 2. self.assertEqual(actions.FUNCTIONS.move_camera.id, func.function) continue self.assertEqual(func.function, executed_func.function) if func.function != actions.FUNCTIONS.select_point.id: # select_point likes to return Toggle instead of Select. self.assertEqual(func.arguments, executed_func.arguments) self.assertEqual(func.function, obs.last_actions[0]) controller.step() return observations
def _finalize(self, agent_interface_formats, interfaces, visualize): game_info = self._parallel.run(c.game_info for c in self._controllers) if not self._map_name: self._map_name = game_info[0].map_name for g, interface in zip(game_info, interfaces): if g.options.render != interface.render: logging.warning( "Actual interface options don't match requested options:\n" "Requested:\n%s\n\nActual:\n%s", interface, g.options) self._features = [ features.features_from_game_info( game_info=g, use_feature_units=agent_interface_format.use_feature_units, use_raw_units=agent_interface_format.use_raw_units, use_unit_counts=agent_interface_format.use_unit_counts, use_camera_position=agent_interface_format.use_camera_position, action_space=agent_interface_format.action_space, hide_specific_actions=agent_interface_format. hide_specific_actions) for g, agent_interface_format in zip( game_info, agent_interface_formats) ] if visualize: static_data = self._controllers[0].data() self._renderer_human = renderer_human.RendererHuman() self._renderer_human.init(game_info[0], static_data) else: self._renderer_human = None self._metrics = metrics.Metrics(self._map_name) self._metrics.increment_instance() self._last_score = None self._total_steps = 0 self._episode_steps = 0 self._episode_count = 0 self._obs = [None] * len(interfaces) self._agent_obs = [None] * len(interfaces) self._state = environment.StepType.LAST # Want to jump to `reset`. logging.info("Environment is ready on map: %s", self._map_name)
def _connect_remote(self, host, host_port, lan_ports, race, name, map_inst, save_map, interface, agent_interface_format): """Make sure this stays synced with bin/agent_remote.py.""" # Connect! logging.info("Connecting...") self._controllers = [ remote_controller.RemoteController(host, host_port) ] logging.info("Connected") if map_inst and save_map: run_config = run_configs.get() self._controllers[0].save_map(map_inst.path, map_inst.data(run_config)) # Create the join request. join = sc_pb.RequestJoinGame(options=interface) join.race = race join.player_name = name join.shared_port = 0 # unused join.server_ports.game_port = lan_ports.pop(0) join.server_ports.base_port = lan_ports.pop(0) join.client_ports.add(game_port=lan_ports.pop(0), base_port=lan_ports.pop(0)) logging.info("Joining game.") self._controllers[0].join_game(join) self._game_info = [self._controllers[0].game_info()] if not self._map_name: self._map_name = self._game_info[0].map_name self._features = [ features.features_from_game_info( game_info=self._game_info[0], agent_interface_format=agent_interface_format) ] self._in_game = True logging.info("Game joined.")
def process_replay(self, controller, replay_path, replay_info, **kwargs): f = features.features_from_game_info(game_info=controller.game_info()) steps = 0 with tqdm.tqdm() as pbar: while True: pbar.update() steps += 1 o = controller.observe() if steps % self._step_mul == 0: obs = f.transform_obs(o) encode(self._exporter, obs, 1, replay_path, replay_info, steps) if o.player_result: # end of game break controller.step() self._exporter.flush() return
def process_replay(self, exporter, controller, replay_data, map_data, replay_path, replay_info, player_id): """Process a single replay, updating the stats.""" self._update_stage("start_replay") controller.start_replay( sc_pb.RequestStartReplay(replay_data=replay_data, map_data=map_data, options=self.interface(), observed_player_id=player_id)) feat = features.features_from_game_info( controller.game_info(), use_feature_units=False, action_space=actions.ActionSpace[FLAGS.action_space.upper()]) steps = 0 self.stats.replay_stats.replays += 1 self._update_stage("step") controller.step() while True: self.stats.replay_stats.steps += 1 steps += 1 self._update_stage("observe") o = controller.observe() try: obs = feat.transform_obs(o) encode(exporter, obs, player_id, replay_path, replay_info, steps) except ValueError: self.stats.replay_stats.invalid_states += 1 if o.player_result: break self._update_stage("step") controller.step(self.step_multplier)
def start(self): print("Hello we are in Start") step_mul = 1 trainingDataPath = 'C:\\Users\\Charlie\\training_data\\4101\\' _features = features.features_from_game_info( self.controller.game_info(), use_camera_position=True) #print("world_tl_to_world_camera_rel: {}\n\nworld_to_feature_screen_px: {}\n\nworld_to_world_tl: {}".format(_features._world_tl_to_world_camera_rel, # _features._world_to_feature_screen_px, # _features._world_to_world_tl)) # _features.init_camera(features.Dimensions(self.screen_size_px, self.minimap_size_px), # point.Point(*const.WorldSize()), # self.camera_width) packageCounter = 0 fileName = trainingDataPath + self.replay_file_name + "/" + str( packageCounter) + '.csv' npFileName = trainingDataPath + self.replay_file_name + "/" + str( packageCounter) + '.npy' npFileNameComp = trainingDataPath + self.replay_file_name + "/" + str( packageCounter) dirname = os.path.dirname(fileName) if not os.path.exists(dirname): os.makedirs(dirname) # keyboard = Controller() # time.sleep(1) # keyboard.press(str(self.player_id)) # time.sleep(0.5) # keyboard.release(str(self.player_id)) while True: #Takes one step through the replay self.controller.step(step_mul) #Converts visual data into abstract data obs = self.controller.observe() if obs.player_result: # Episide over. self._state = StepType.LAST print("Episode Over") break discount = 0 else: discount = self.discount if (len(obs.actions) == 0): continue agent_obs = _features.transform_obs(obs) step = TimeStep(step_type=self._state, reward=0, discount=discount, observation=agent_obs) for action in obs.actions: for num in self.agent.action_dict.keys(): # If action is worth recording if (int(_features.reverse_action(action).function) == num): # Check if the action is on a Micro Unit if (const.IsMicroUnit(agent_obs.single_select) or const.IsMicroUnit(agent_obs.multi_select)): # Record action #print(_features._world_tl_to_world_camera_rel.offset) #self.agent.states.append(self.agent.step(step, self.info, _features.reverse_action(action))) state = self.agent.step( step, self.info, _features.reverse_action(action)) if state != 0: npFileNameComp = trainingDataPath + self.replay_file_name + "/" + str( packageCounter) np.savez_compressed( npFileNameComp, action=translate_outputs_to_NN( state["action"][0]), feature_layers=np.moveaxis( (np.array(state["feature_layers"])), 0, 2)) packageCounter += 1 break #print("%s: %s" % (len(agent_obs.multi_select), units.Zerg(agent_obs.multi_select[0][0]))) #print(action) #print(units.Zerg(agent_obs.single_select[0][0])) #self.agent.step(step, self.info, acts) #print(_features.reverse_action(obs.actions[0])) #print ("+") #print(offset) #screenpoint = (84, 84) #screenpoint = point.Point(*screenpoint) if obs.player_result: os.remove(replay_file_path) print("Game Ended, File Removed") break self._state = StepType.MID
def _create_join(self): """Create the game, and join it.""" map_inst = random.choice(self._maps) self._map_name = map_inst.name self._step_mul = max(1, self._default_step_mul or map_inst.step_mul) self._score_index = get_default(self._default_score_index, map_inst.score_index) self._score_multiplier = get_default(self._default_score_multiplier, map_inst.score_multiplier) self._episode_length = get_default(self._default_episode_length, map_inst.game_steps_per_episode) if self._episode_length <= 0 or self._episode_length > MAX_STEP_COUNT: self._episode_length = MAX_STEP_COUNT # Create the game. Set the first instance as the host. create = sc_pb.RequestCreateGame(disable_fog=self._disable_fog, realtime=self._realtime) if self._battle_net_map: create.battlenet_map_name = map_inst.battle_net else: create.local_map.map_path = map_inst.path map_data = map_inst.data(self._run_config) if self._num_agents == 1: create.local_map.map_data = map_data else: # Save the maps so they can access it. Don't do it in parallel since SC2 # doesn't respect tmpdir on windows, which leads to a race condition: # https://github.com/Blizzard/s2client-proto/issues/102 for c in self._controllers: c.save_map(map_inst.path, map_data) if self._random_seed is not None: create.random_seed = self._random_seed for p in self._players: if isinstance(p, Agent): create.player_setup.add(type=sc_pb.Participant) else: create.player_setup.add(type=sc_pb.Computer, race=random.choice(p.race), difficulty=p.difficulty, ai_build=random.choice(p.build)) self._controllers[0].create_game(create) # Create the join requests. agent_players = [p for p in self._players if isinstance(p, Agent)] sanitized_names = crop_and_deduplicate_names(p.name for p in agent_players) join_reqs = [] for p, name, interface in zip(agent_players, sanitized_names, self._interface_options): join = sc_pb.RequestJoinGame(options=interface) join.race = random.choice(p.race) join.player_name = name if self._ports: join.shared_port = 0 # unused join.server_ports.game_port = self._ports[0] join.server_ports.base_port = self._ports[1] for i in range(self._num_agents - 1): join.client_ports.add(game_port=self._ports[i * 2 + 2], base_port=self._ports[i * 2 + 3]) join_reqs.append(join) # Join the game. This must be run in parallel because Join is a blocking # call to the game that waits until all clients have joined. self._parallel.run((c.join_game, join) for c, join in zip(self._controllers, join_reqs)) self._game_info = self._parallel.run(c.game_info for c in self._controllers) for g, interface in zip(self._game_info, self._interface_options): if g.options.render != interface.render: logging.warning( "Actual interface options don't match requested options:\n" "Requested:\n%s\n\nActual:\n%s", interface, g.options) self._features = [ features.features_from_game_info(game_info=g, agent_interface_format=aif, map_name=self._map_name) for g, aif in zip(self._game_info, self._interface_formats) ]
def start_game(self, show_cloaked=True, disable_fog=False, players=2): """Start a multiplayer game with options.""" self._disable_fog = disable_fog run_config = run_configs.get() self._parallel = run_parallel.RunParallel() # Needed for multiplayer. map_inst = maps.get("Flat64") self._map_data = map_inst.data(run_config) self._ports = portspicker.pick_unused_ports(4) if players == 2 else [] self._sc2_procs = [ run_config.start(extra_ports=self._ports, want_rgb=False) for _ in range(players) ] self._controllers = [p.controller for p in self._sc2_procs] if players == 2: for c in self._controllers: # Serial due to a race condition on Windows. c.save_map(map_inst.path, self._map_data) self._interface = sc_pb.InterfaceOptions() self._interface.raw = True self._interface.raw_crop_to_playable_area = True self._interface.show_cloaked = show_cloaked self._interface.score = False self._interface.feature_layer.width = 24 self._interface.feature_layer.resolution.x = 64 self._interface.feature_layer.resolution.y = 64 self._interface.feature_layer.minimap_resolution.x = 64 self._interface.feature_layer.minimap_resolution.y = 64 create = sc_pb.RequestCreateGame( random_seed=1, disable_fog=self._disable_fog, local_map=sc_pb.LocalMap(map_path=map_inst.path)) for _ in range(players): create.player_setup.add(type=sc_pb.Participant) if players == 1: create.local_map.map_data = self._map_data create.player_setup.add(type=sc_pb.Computer, race=sc_common.Random, difficulty=sc_pb.VeryEasy) join = sc_pb.RequestJoinGame(race=sc_common.Protoss, options=self._interface) if players == 2: join.shared_port = 0 # unused join.server_ports.game_port = self._ports[0] join.server_ports.base_port = self._ports[1] join.client_ports.add(game_port=self._ports[2], base_port=self._ports[3]) self._controllers[0].create_game(create) self._parallel.run((c.join_game, join) for c in self._controllers) self._info = self._controllers[0].game_info() self._features = features.features_from_game_info(self._info, use_raw_units=True) self._map_size = point.Point.build(self._info.start_raw.map_size) print("Map size:", self._map_size) self.in_game = True self.step() # Get into the game properly.
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 start(self): _features = features.features_from_game_info( self.controller.game_info()) _features.init_camera( features.Dimensions(self.screen_size_px, self.minimap_size_px), point.Point(*self.map_size), self.camera_width) while True: self.controller.step(self.step_mul) obs = self.controller.observe() try: agent_obs = _features.transform_obs(obs) except: pass #screenpoint = (42, 42) #screenpoint = point.Point(*screenpoint) if (len(obs.actions) == 0): continue #else: # print(obs.actions) #if obs.observation.game_loop in config.actions: #func = config.actions[o.game_loop](obs) #_features.reverse_action(obs.actions[1]) #action = _features.transform_action(obs.observation, actions.FUNCTIONS.move_camera([42,42])) #self.controller.act(action) #self.assertEqual(actions.FUNCTIONS.move_camera.id, func.function) #s2clientprotocol_dot_common__pb2._POINT2D #actions.FUNCTIONS.move_camera(screenpoint) #remote_controller.RemoteController.act(actions.move_camera(actions.FUNCTIONS.move_camera,['FEATURES'], screenpoint)) #action_observer_camera_move = (sc_pb.ActionObserverCameraMove(world_pos = screenpoint)) #sc_pb.RequestObserverAction #screenpoint.assign_to(action_observer_camera_move.world_pos) #self.controller.act(sc_pb.ActionObserverCameraMove(world_pos=screenpoint)) #sc_pb.RequestObserverAction(actions=[sc_pb.ObserverAction(player_perspective=sc_pb.ActionObserverPlayerPerspective(player_id=2))]) #obsAction = self.controller.act(sc_pb.RequestObserverAction(actions=[sc_pb.ObserverAction(player_perspective=sc_pb.ActionObserverPlayerPerspective(player_id=2))]))# [sc_pb.ActionObserverCameraMove(distance=50)])) #screenpoint.assign_to(obsAction.camera_move.world_pos) #remote_controller.RemoteController.actions if obs.player_result: # Episide over. self._state = StepType.LAST discount = 0 else: discount = self.discount #if (_features.reverse_action(obs.actions[0]).function == actions.FUNCTIONS.select_rect.id): agent_obs = _features.transform_obs(obs) #self._episode_steps += self.step_mul step = TimeStep(step_type=self._state, reward=0, discount=discount, observation=agent_obs) acts = [] for action in obs.actions: for num in self.agent.action_dict.keys(): if (format( _features.reverse_action(action).function) == num): acts.append(_features.reverse_action(action)) break data = self.controller.data() self.agent.step(step, self.info, acts) #offset = self.agent.step(step, self.info) #print(_features.reverse_action(obs.actions[0])) #print ("+") #print(offset) if obs.player_result: break self._state = StepType.MID
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
feature_screen_size = 128 feature_minimap_size = 64 rgb_screen_size = None rgb_minimap_size = None action_space = None use_feature_units = True aif = 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) print("controller.game_info(): " + str(controller.game_info())) _features = features.features_from_game_info( controller.game_info(), agent_interface_format=aif) build_name = [] build_info = [] replay_step = 0 while True: replay_step += 1 controller.step(step_mul) obs = controller.observe() agent_obs = _features.transform_obs(obs) print("replay_step: " + str(replay_step)) #print("agent_obs['feature_units']: " + str(agent_obs['feature_units'])) #print("len(obs.actions): " + str(len(obs.actions))) if (len(obs.actions) != 0):
def process_replay(self, controller, replay_data, map_data, player_id, out): """Process a single replay, updating the stats.""" self._update_stage("start_replay") controller.start_replay( sc_pb.RequestStartReplay(replay_data=replay_data, map_data=map_data, options=interface, observed_player_id=player_id)) if 'data' not in out: data = controller._client.send( data=sc_pb.RequestData(ability_id=True, unit_type_id=True, upgrade_id=True, buff_id=True, effect_id=True)) out['data'] = data.SerializeToString() game_info = controller.game_info() if 'game_info' not in out: out['game_info'] = game_info.SerializeToString() out['step_mul'] = FLAGS.step_mul observations = [] out['player' + str(player_id)] = observations feat = features.features_from_game_info(game_info) self.stats.replay_stats.replays += 1 self._update_stage("step") controller.step() while True: self.stats.replay_stats.steps += 1 self._update_stage("observe") obs = controller.observe() observations.append(obs.SerializeToString()) for action in obs.actions: act_fl = action.action_feature_layer if act_fl.HasField("unit_command"): self.stats.replay_stats.made_abilities[ act_fl.unit_command.ability_id] += 1 if act_fl.HasField("camera_move"): self.stats.replay_stats.camera_move += 1 if act_fl.HasField("unit_selection_point"): self.stats.replay_stats.select_pt += 1 if act_fl.HasField("unit_selection_rect"): self.stats.replay_stats.select_rect += 1 if action.action_ui.HasField("control_group"): self.stats.replay_stats.control_group += 1 try: func = feat.reverse_action(action).function except ValueError: func = -1 self.stats.replay_stats.made_actions[func] += 1 for valid in obs.observation.abilities: self.stats.replay_stats.valid_abilities[valid.ability_id] += 1 for u in obs.observation.raw_data.units: self.stats.replay_stats.unit_ids[u.unit_type] += 1 for ability_id in feat.available_actions(obs.observation): self.stats.replay_stats.valid_actions[ability_id] += 1 if obs.player_result: break self._update_stage("step") controller.step(FLAGS.step_mul)
def parse(replay): print("test") run_config = run_configs.get() interface = sc_pb.InterfaceOptions() interface.score = True interface.feature_layer.resolution.x = flags.feature_screen_size interface.feature_layer.resolution.y = flags.feature_screen_size interface.feature_layer.minimap_resolution.x = flags.feature_minimap interface.feature_layer.minimap_resolution.y = flags.feature_minimap replay_data = run_config.replay_data(replay) start_replay = sc_pb.RequestStartReplay( replay_data=replay_data, options=interface, disable_fog=not flags.fog_of_war, observed_player_id=flags.observed_player) game_version = get_game_version(replay_data) print('StarCraft II', game_version, 'Replay Parser') with run_config.start(game_version=game_version) as controller: info = controller.replay_info(replay_data) map_path = info.local_map_path if map_path: start_replay.map_data = run_config.map_data(map_path) controller.start_replay(start_replay) print('REPLAY'.center(60, '_')) print(info) print(map_path) print('_' * 60) try: print("******************") game_info = controller.game_info() print(game_info) print("******************") print(game_info.start_raw.map_size) print("******************") feat = features.features_from_game_info( controller.game_info(), use_feature_units=flags.use_feature_units) while True: beg = time.time() controller.step(flags.step_size) obs = controller.observe() obs_t = feat.transform_obs(obs) id = replay.split('\\')[-1].split( '.')[0] + '_player_{}'.format(flags.observed_player) datafold = os.path.join(flags.output_dir, id) if not os.path.exists(datafold): os.makedirs(datafold) # screen features for i in range(len(screen_features)): with open(os.path.join( datafold, 'screen_{}_.txt'.format(screen_features[i])), 'a+', newline='') as f: writer = csv.writer(f) writer.writerows( csr_matrix_to_list( sparse.csr_matrix(obs_t['feature_screen'][i]))) # minimap features for i in range(len(minimap_features)): with open(os.path.join( datafold, 'minimap_{}_.txt'.format(minimap_features[i])), 'a+', newline='') as f: writer = csv.writer(f) writer.writerows( csr_matrix_to_list( sparse.csr_matrix( obs_t['feature_minimap'][i]))) # non-spatial features for i in other_features: with open(os.path.join(datafold, '{}.txt'.format(i)), 'a+', newline='') as f: writer = csv.writer(f) writer.writerows([obs_t[i]]) # actions for action in obs.actions: try: func = feat.reverse_action(action).function args = feat.reverse_action(action).arguments with open(os.path.join(datafold, 'actions.txt'), 'a+', newline='') as f: writer = csv.writer(f) writer.writerows( [obs_t['game_loop'].tolist(), [func], [args]]) except ValueError: pass if obs.player_result: break time.sleep(max(0, beg + 1 / flags.fps - time.time())) except KeyboardInterrupt: pass print('score:', obs.observation.score.score) print('result:', obs.player_result) print()
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")