示例#1
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=True, 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()
        self.game_info = 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`.
def main(unused_argv):
    interface = sc_pb.InterfaceOptions()
    interface.raw = True
    interface.score = True
    interface.feature_layer.width = 24
    interface.feature_layer.resolution.x = 84
    interface.feature_layer.resolution.y = 84
    interface.feature_layer.minimap_resolution.x = 64
    interface.feature_layer.minimap_resolution.y = 64

    timeline = []

    start = time.time()
    run_config = run_configs.get()
    proc = run_config.start()
    process = psutil.Process(proc.pid)
    episode = 0

    def add(s):
        cpu = process.cpu_times().user
        mem = process.memory_info().rss / 2**20  # In Mb
        step = Timestep(episode, time.time() - start, cpu, mem, s)
        print(step)
        timeline.append(step)
        if mem > FLAGS.mem_limit:
            raise MemoryException("%s Mb mem limit exceeded" % FLAGS.mem_limit)

    try:
        add("Started process")

        controller = proc.controller
        map_inst = maps.get("Simple64")
        create = sc_pb.RequestCreateGame(
            realtime=False,
            disable_fog=False,
            local_map=sc_pb.LocalMap(map_path=map_inst.path,
                                     map_data=map_inst.data(run_config)))
        create.player_setup.add(type=sc_pb.Participant)
        create.player_setup.add(type=sc_pb.Computer,
                                race=sc_common.Random,
                                difficulty=sc_pb.CheatInsane)
        join = sc_pb.RequestJoinGame(race=sc_common.Random, options=interface)
        controller.create_game(create)

        add("Created game")

        controller.join_game(join)

        episode += 1
        add("Joined game")

        for _ in range(FLAGS.episodes):
            for i in range(2000):
                controller.step(16)
                obs = controller.observe()
                if obs.player_result:
                    add("Lost on step %s" % i)
                    break
                if i > 0 and i % 100 == 0:
                    add("Step %s" % i)
            controller.restart()
            episode += 1
            add("Restarted")
        add("Done")
    except KeyboardInterrupt:
        pass
    except (MemoryException, protocol.ConnectionError) as e:
        print(e)
    finally:
        proc.close()

    print("Timeline:")
    for t in timeline:
        print(t)
示例#3
0
文件: play.py 项目: Hotpotfish/pysc2
def main(unused_argv):
  """Run SC2 to play a game or a replay."""
  if FLAGS.trace:
    stopwatch.sw.trace()
  elif FLAGS.profile:
    stopwatch.sw.enable()

  if (FLAGS.map and FLAGS.replay) or (not FLAGS.map and not FLAGS.replay):
    sys.exit("Must supply either a map or replay.")

  if FLAGS.replay and not FLAGS.replay.lower().endswith("sc2replay"):
    sys.exit("Replay must end in .SC2Replay.")

  if FLAGS.realtime and FLAGS.replay:
    # TODO(tewalds): Support realtime in replays once the game supports it.
    sys.exit("realtime isn't possible for replays yet.")

  if FLAGS.render and (FLAGS.realtime or FLAGS.full_screen):
    sys.exit("disable pygame rendering if you want realtime or full_screen.")

  if platform.system() == "Linux" and (FLAGS.realtime or FLAGS.full_screen):
    sys.exit("realtime and full_screen only make sense on Windows/MacOS.")

  if not FLAGS.render and FLAGS.render_sync:
    sys.exit("render_sync only makes sense with pygame rendering on.")

  run_config = run_configs.get()

  interface = sc_pb.InterfaceOptions()
  interface.raw = FLAGS.render
  interface.raw_affects_selection = True
  interface.raw_crop_to_playable_area = True
  interface.score = True
  interface.show_cloaked = True
  interface.show_burrowed_shadows = True
  interface.show_placeholders = True
  if FLAGS.feature_screen_size and FLAGS.feature_minimap_size:
    interface.feature_layer.width = FLAGS.feature_camera_width
    FLAGS.feature_screen_size.assign_to(interface.feature_layer.resolution)
    FLAGS.feature_minimap_size.assign_to(
        interface.feature_layer.minimap_resolution)
    interface.feature_layer.crop_to_playable_area = True
    interface.feature_layer.allow_cheating_layers = True
  if FLAGS.render and 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)

  max_episode_steps = FLAGS.max_episode_steps

  if FLAGS.map:
    create = sc_pb.RequestCreateGame(
        realtime=FLAGS.realtime,
        disable_fog=FLAGS.disable_fog)
    try:
      map_inst = maps.get(FLAGS.map)
    except maps.lib.NoMapError:
      if FLAGS.battle_net_map:
        create.battlenet_map_name = FLAGS.map
      else:
        raise
    else:
      if map_inst.game_steps_per_episode:
        max_episode_steps = map_inst.game_steps_per_episode
      if FLAGS.battle_net_map:
        create.battlenet_map_name = map_inst.battle_net
      else:
        create.local_map.map_path = map_inst.path
        create.local_map.map_data = map_inst.data(run_config)

    create.player_setup.add(type=sc_pb.Participant)
    create.player_setup.add(type=sc_pb.Computer,
                            race=sc2_env.Race[FLAGS.bot_race],
                            difficulty=sc2_env.Difficulty[FLAGS.difficulty],
                            ai_build=sc2_env.BotBuild[FLAGS.bot_build])
    join = sc_pb.RequestJoinGame(
        options=interface, race=sc2_env.Race[FLAGS.user_race],
        player_name=FLAGS.user_name)
    version = None
  else:
    replay_data = run_config.replay_data(FLAGS.replay)
    start_replay = sc_pb.RequestStartReplay(
        replay_data=replay_data,
        options=interface,
        disable_fog=FLAGS.disable_fog,
        observed_player_id=FLAGS.observed_player)
    version = replay.get_replay_version(replay_data)
    run_config = run_configs.get(version=version)  # Replace the run config.

  with run_config.start(
      full_screen=FLAGS.full_screen,
      window_size=FLAGS.window_size,
      want_rgb=interface.HasField("render")) as controller:
    if FLAGS.map:
      controller.create_game(create)
      controller.join_game(join)
    else:
      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,
                                                    len(info.player_info))
      controller.start_replay(start_replay)

    if FLAGS.render:
      renderer = renderer_human.RendererHuman(
          fps=FLAGS.fps, step_mul=FLAGS.step_mul,
          render_sync=FLAGS.render_sync, video=FLAGS.video)
      renderer.run(
          run_config, controller, max_game_steps=FLAGS.max_game_steps,
          game_steps_per_episode=max_episode_steps,
          save_replay=FLAGS.save_replay)
    else:  # Still step forward so the Mac/Windows renderer works.
      try:
        while True:
          frame_start_time = time.time()
          if not FLAGS.realtime:
            controller.step(FLAGS.step_mul)
          obs = controller.observe()

          if obs.player_result:
            break
          time.sleep(max(0, frame_start_time + 1 / FLAGS.fps - time.time()))
      except KeyboardInterrupt:
        pass
      print("Score: ", obs.observation.score.score)
      print("Result: ", obs.player_result)
      if FLAGS.map and FLAGS.save_replay:
        replay_save_loc = run_config.save_replay(
            controller.save_replay(), "local", FLAGS.map)
        print("Replay saved to:", replay_save_loc)
        # Save scores so we know how the human player did.
        with open(replay_save_loc.replace("SC2Replay", "txt"), "w") as f:
          f.write("{}\n".format(obs.observation.score.score))

  if FLAGS.profile:
    print(stopwatch.sw)
示例#4
0
from pysc2.lib import protocol
from pysc2.lib import remote_controller

from pysc2.lib import app
import gflags as flags
from s2clientprotocol import sc2api_pb2 as sc_pb

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(sys.argv)

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
        self.camera_move = 0
示例#5
0
    def __init__(self,
                 map_name,
                 screen_size_px=(64, 64),
                 minimap_size_px=(64, 64),
                 camera_width_world_units=None,
                 discount=1.,
                 visualize=False,
                 agent_race=None,
                 bot_race=None,
                 difficulty=None,
                 step_mul=None,
                 save_replay_steps=0,
                 replay_dir=None,
                 game_steps_per_episode=None,
                 score_index=None,
                 score_multiplier=None):
        """Create a SC2 Env.

    Args:
      map_name: Name of a SC2 map. Run bin/map_list to get the full list of
          known maps. Alternatively, pass a Map instance. Take a look at the
          docs in maps/README.md for more information on available maps.
      screen_size_px: The size of your screen output in pixels.
      minimap_size_px: The size of your minimap output in pixels.
      camera_width_world_units: The width of your screen in world units. If your
          screen_size_px=(64, 48) and camera_width is 24, then each px
          represents 24 / 64 = 0.375 world units in each of x and y. It'll then
          represent a camera of size (24, 0.375 * 48) = (24, 18) world units.
      discount: Returned as part of the observation.
      visualize: Whether to pop up a window showing the camera and feature
          layers. This won't work without access to a window manager.
      agent_race: One of P,T,Z,R default random. This is the race you control.
      bot_race: One of P,T,Z,R default random. This is the race controlled by
          the built-in bot.
      difficulty: One of 1-9,A. How strong should the bot be?
      step_mul: How many game steps per agent step (action/observation). None
          means use the map default.
      save_replay_steps: How many game steps to wait before saving a replay.
          Default of 0 means don't save replays.
      replay_dir: Directory to save replays to. Required with save_replay_steps.
      game_steps_per_episode: Game steps per episode, independent of the
          step_mul. 0 means no limit. None means use the map default.
      score_index: -1 means use the win/loss reward, >=0 is the index into the
          score_cumulative with 0 being the curriculum score. None means use
          the map default.
      score_multiplier: How much to multiply the score by. Useful for negating.

    Raises:
      ValueError: if the agent_race, bot_race or difficulty are invalid.
    """
        # Make the destructor happy.
        self._renderer_human = None
        self._controller = None
        self._sc2_proc = None

        if save_replay_steps and not replay_dir:
            raise ValueError("Missing replay_dir")
        if agent_race and agent_race not in races:
            raise ValueError("Bad agent_race args")
        if bot_race and bot_race not in races:
            raise ValueError("Bad bot_race args")
        if difficulty and str(difficulty) not in difficulties:
            raise ValueError("Bad difficulty")
        self._map = maps.get(map_name)
        self._discount = discount
        self._step_mul = step_mul or self._map.step_mul
        self._save_replay_steps = save_replay_steps
        self._replay_dir = replay_dir
        self._total_steps = 0

        if score_index is None:
            self._score_index = self._map.score_index
        else:
            self._score_index = score_index
        if score_multiplier is None:
            self._score_multiplier = self._map.score_multiplier
        else:
            self._score_multiplier = score_multiplier
        self._last_score = None

        self._episode_length = (game_steps_per_episode
                                or self._map.game_steps_per_episode)
        self._episode_steps = 0

        self._run_config = run_configs.get()
        self._sc2_proc = self._run_config.start()
        self._controller = self._sc2_proc.controller

        screen_size_px = point.Point(*screen_size_px)
        minimap_size_px = point.Point(*minimap_size_px)
        interface = sc_pb.InterfaceOptions(
            raw=visualize,
            score=True,
            feature_layer=sc_pb.SpatialCameraSetup(
                width=camera_width_world_units or 24))
        screen_size_px.assign_to(interface.feature_layer.resolution)
        minimap_size_px.assign_to(interface.feature_layer.minimap_resolution)

        create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap(
            map_path=self._map.path, map_data=self._map.data(
                self._run_config)))
        create.player_setup.add(type=sc_pb.Participant)
        create.player_setup.add(type=sc_pb.Computer,
                                race=races[bot_race or "R"],
                                difficulty=difficulties[difficulty or "1"])
        join = sc_pb.RequestJoinGame(race=races[agent_race or "R"],
                                     options=interface)

        self._controller.create_game(create)
        self._controller.join_game(join)

        game_info = self._controller.game_info()
        static_data = self._controller.data()

        self._features = features.Features(game_info)
        if visualize:
            self._renderer_human = renderer_human.RendererHuman()
            self._renderer_human.init(game_info, static_data)

        self._episode_count = 0
        self._obs = None
        self._state = environment.StepType.LAST  # Want to jump to `reset`.
        logging.info("Environment is ready.")
    def test_multi_player(self):
        players = 2
        run_config = run_configs.get()
        parallel = run_parallel.RunParallel()
        map_inst = maps.get("Simple64")

        screen_size_px = point.Point(64, 64)
        minimap_size_px = point.Point(32, 32)
        interface = sc_pb.InterfaceOptions()
        screen_size_px.assign_to(interface.feature_layer.resolution)
        minimap_size_px.assign_to(interface.feature_layer.minimap_resolution)

        # Reserve a whole bunch of ports for the weird multiplayer implementation.
        ports = [portpicker.pick_unused_port() for _ in range(players * 2)]
        logging.info("Valid Ports: %s", ports)

        # Actually launch the game processes.
        print_stage("start")
        sc2_procs = [
            run_config.start(extra_ports=ports) for _ in range(players)
        ]
        controllers = [p.controller for p in sc2_procs]

        try:
            # Save the maps so they can access it.
            map_path = os.path.basename(map_inst.path)
            print_stage("save_map")
            parallel.run((c.save_map, map_path, map_inst.data(run_config))
                         for c in controllers)

            # Create the create request.
            create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap(
                map_path=map_path))
            for _ in range(players):
                create.player_setup.add(type=sc_pb.Participant)

            # Create the join request.
            join = sc_pb.RequestJoinGame(race=sc_common.Random,
                                         options=interface)
            join.shared_port = 0  # unused
            join.server_ports.game_port = ports.pop(0)
            join.server_ports.base_port = ports.pop(0)
            for _ in range(players - 1):
                join.client_ports.add(game_port=ports.pop(0),
                                      base_port=ports.pop(0))

            # Play a few short games.
            for _ in range(2):  # 2 episodes
                # Create and Join
                print_stage("create")
                controllers[0].create_game(create)
                print_stage("join")
                parallel.run((c.join_game, join) for c in controllers)

                print_stage("run")
                for game_loop in range(1, 10):  # steps per episode
                    # Step the game
                    parallel.run(c.step for c in controllers)

                    # Observe
                    obs = parallel.run(c.observe for c in controllers)
                    for p_id, o in enumerate(obs):
                        self.assertEqual(o.observation.game_loop, game_loop)
                        self.assertEqual(o.observation.player_common.player_id,
                                         p_id + 1)

                    # Act
                    actions = [sc_pb.Action() for _ in range(players)]
                    for action in actions:
                        pt = (point.Point.unit_rand() *
                              minimap_size_px).floor()
                        pt.assign_to(action.action_feature_layer.camera_move.
                                     center_minimap)
                    parallel.run(
                        (c.act, a) for c, a in zip(controllers, actions))

                # Done this game.
                print_stage("leave")
                parallel.run(c.leave for c in controllers)
        finally:
            print_stage("quit")
            # Done, shut down. Don't depend on parallel since it might be broken.
            for c in controllers:
                c.quit()
            for p in sc2_procs:
                p.close()
示例#7
0
	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
				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)

				_features = features.features_from_game_info(controller.game_info(), agent_interface_format=aif)

				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)[0])
								#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)
					self.home_trajectory.append(agent_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

					#print("obs.player_result: " + str(obs.player_result))
					if obs.player_result:
						break

					_state = StepType.MID

				self.home_BO = build_info
				self.away_BU = score_cumulative_dict
				break
			except:
				continue
def main(unused_argv):
    """Run SC2 to play a game or a replay."""
    stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace
    stopwatch.sw.trace = FLAGS.trace

    if FLAGS.replay and not FLAGS.replay.lower().endswith("sc2replay"):
        sys.exit("Replay must end in .SC2Replay.")

    run_config = run_configs.get()

    interface = sc_pb.InterfaceOptions()
    interface.score = True
    interface.feature_layer.width = 24
    interface.feature_layer.resolution.x = FLAGS.screen_resolution
    interface.feature_layer.resolution.y = FLAGS.screen_resolution
    interface.feature_layer.minimap_resolution.x = FLAGS.minimap_resolution
    interface.feature_layer.minimap_resolution.y = FLAGS.minimap_resolution

    replay_data = run_config.replay_data(FLAGS.replay)
    start_replay = sc_pb.RequestStartReplay(
        replay_data=replay_data,
        options=interface,
        disable_fog=FLAGS.disable_fog,
        observed_player_id=FLAGS.observed_player)
    game_version = get_game_version(replay_data)
    print(game_version)

    with run_config.start(game_version=game_version) 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
        print(map_path)
        if map_path:
            start_replay.map_data = run_config.map_data(map_path)
        controller.start_replay(start_replay)

        try:
            feat = features.Features(controller.game_info())
            while True:
                frame_start_time = time.time()
                controller.step(FLAGS.step_mul)
                obs = controller.observe()
                obs_t = feat.transform_obs(obs.observation)

                # Screen Features
                screen_features = ['height_map', 'visibility_map', 'creep', 'power', 'player_id', 'player_relative',
                                 'unit_type', 'selected', 'unit_hit_point', 'unit_hit_point_ratio', 'unit_energy',
                                 'unit_energy_ratio', 'unit_shield', 'unit_shield_ratio', 'unit_density',
                                 'unit_density_ratio', 'effects']
                # Minimap Features
                minimap_features = ['height_map', 'visibility_map', 'creep', 'camera', 'player_id', 'player_relative',
                                  'selected']
                # Other features
                other_features = ['player', 'game_loop', 'score_cumulative', 'available_actions', 'single_select',
                                'multi_select', 'cargo', 'cargo_slots_available', 'build_queue', 'control_groups']

                # Create replay data foldr
                data_folder = './replay_data/' + FLAGS.replay.split('\\')[-1][:10] + \
                            '_player_{}'.format(FLAGS.observed_player)
                if not os.path.exists(data_folder):
                    os.makedirs(data_folder)

                # Write screen features
                for i in range(len(screen_features)):
                    data_path = data_folder + "/" + 'screen_' + screen_features[i] + '.txt'
                    mode = 'a+' if os.path.exists(data_path) else 'w+'
                    with open(data_path, mode, newline='') as f:
                        writer = csv.writer(f)
                        writer.writerows(csr_matrix_to_list(sparse.csr_matrix(obs_t['screen'][i])))

                # Write minimap features
                for i in range(len(minimap_features)):
                    data_path = data_folder + "/" + 'minimap_' + minimap_features[i] + '.txt'
                    mode = 'a+' if os.path.exists(data_path) else 'w+'
                    with open(data_path, mode, newline='') as f:
                        writer = csv.writer(f)
                        writer.writerows(csr_matrix_to_list(sparse.csr_matrix(obs_t['minimap'][i])))

                # Write other features
                for i in other_features:
                    data_path = data_folder + "/" + i + '.txt'
                    mode = 'a+' if os.path.exists(data_path) else 'w+'
                    with open(data_path, mode, newline='') as f:
                        writer = csv.writer(f)
                        writer.writerows([obs_t[i]])

                # Write actions
                for action in obs.actions:
                    try:
                        func = feat.reverse_action(action).function
                        args = feat.reverse_action(action).arguments
                        try:
                            print(func, args)
                        except OSError:
                            pass

                        data_path = data_folder + "/" + 'action' + '.txt'
                        mode = 'a+' if os.path.exists(data_path) else 'w+'
                        with open(data_path, mode, 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, frame_start_time + 1 / FLAGS.fps - time.time()))
        except KeyboardInterrupt:
            pass
        print("Score: ", obs.observation.score.score)
        print("Result: ", obs.player_result)

    if FLAGS.profile:
        print(stopwatch.sw)
示例#9
0
def test_multi_player(agents, disable_fog):
    players = 2
    if len(agents) == 2:
        agent1, agent2 = agents
    run_config = run_configs.get()
    parallel = run_parallel.RunParallel()
    map_inst = maps.get(FLAGS.map)

    screen_size_px = point.Point(64, 64)
    minimap_size_px = point.Point(32, 32)
    interface = sc_pb.InterfaceOptions(raw=True, score=True)
    screen_size_px.assign_to(interface.feature_layer.resolution)
    minimap_size_px.assign_to(interface.feature_layer.minimap_resolution)

    # Reserve a whole bunch of ports for the weird multiplayer implementation.
    ports = [portpicker.pick_unused_port() for _ in range(1 + players * 2)]
    print("Valid Ports: %s", ports)

    # Actually launch the game processes.
    print("start")
    sc2_procs = [run_config.start(extra_ports=ports) for _ in range(players)]
    controllers = [p.controller for p in sc2_procs]

    try:
        # Save the maps so they can access it.
        map_path = os.path.basename(map_inst.path)
        print("save_map")
        parallel.run((c.save_map, map_path, run_config.map_data(map_inst.path))
                     for c in controllers)

        # Create the create request.
        real_time = True
        create = sc_pb.RequestCreateGame(
            local_map=sc_pb.LocalMap(map_path=map_path), realtime=real_time)
        for _ in range(players):
            create.player_setup.add(type=sc_pb.Participant)

        # Create the join request.
        join1 = sc_pb.RequestJoinGame(race=races[FLAGS.agent1_race],
                                      options=interface)
        join1.shared_port = ports.pop()
        join1.server_ports.game_port = ports.pop()
        join1.server_ports.base_port = ports.pop()
        join1.client_ports.add(game_port=ports.pop(), base_port=ports.pop())

        join2 = copy.copy(join1)
        join2.race = races[FLAGS.agent2_race]

        # This is where actually game plays
        # Create and Join
        print("create")
        controllers[0].create_game(create)
        print("join")
        parallel.run((c.join_game, join)
                     for c, join in zip(controllers, [join1, join2]))

        controllers[0]._client.send(debug=sc_pb.RequestDebug(
            debug=[debug_pb2.DebugCommand(game_state=1)]))
        if disable_fog[0]:
            controllers[0].disable_fog()
        if disable_fog[1]:
            controllers[1].disable_fog()

        print("run")
        game_info = controllers[0].game_info()
        extractors = features.Features(game_info)
        for game_loop in range(1, 100000):  # steps per episode
            # Step the game
            step_mul = FLAGS.step_mul
            if not real_time:
                parallel.run((c.step, step_mul) for c in controllers)
            else:
                time.sleep(FLAGS.sleep_time)

            # Observe
            obs = parallel.run(c.observe for c in controllers)
            agent_obs = [extractors.transform_obs(o.observation) for o in obs]
            game_info = [None for c in controllers]

            if not any(o.player_result for o in obs):  # Episode over.
                game_info = parallel.run(c.game_info for c in controllers)
            timesteps = tuple(
                environment.TimeStep(step_type=0,
                                     reward=0,
                                     discount=0,
                                     observation=o,
                                     game_info=info)
                for o, info in zip(agent_obs, game_info))

            # Act
            if agent1 is not None:
                actions1 = agent1.step(timesteps[0])
            else:
                actions1 = []
            actions2 = agent2.step(timesteps[1])
            actions = [actions1, actions2]
            funcs_with_args = [(c.acts, a)
                               for c, a in zip(controllers, actions)]
            parallel.run(funcs_with_args)

        # Done with the game.
        print("leave")
        parallel.run(c.leave for c in controllers)
    finally:
        print("quit")
        # Done, shut down. Don't depend on parallel since it might be broken.
        for c in controllers:
            c.quit()
        for p in sc2_procs:
            p.close()
示例#10
0
def run(debug=False):
    """PURPOSE: start a starcraft2 process using the defined the config parameters"""
    FLAGS(sys.argv)
    config = gameConfig.Config(
        version=
        None,  # vers is None... unless a specific game version is desired
        themap=selectMap(ladder=True),  # pick a random ladder map
        players=["defaulthuman", "blizzbot2_easy"],
    )
    createReq = sc_pb.RequestCreateGame(  # used to advance to "Init Game" state, when hosting
        realtime=config.realtime,
        disable_fog=config.fogDisabled,
        random_seed=int(now(
        )),  # a game is created using the current second timestamp as the seed
        local_map=sc_pb.LocalMap(map_path=config.mapLocalPath,
                                 map_data=config.mapData))
    joinRace = None
    for player in config.players:
        reqPlayer = createReq.player_setup.add(
        )  # add new player; get link to settings
        playerObj = PlayerPreGame(player)
        if playerObj.isComputer:
            reqPlayer.difficulty = playerObj.difficulty.gameValue()
            pType = playerObj.type.type
        else:
            pType = c.PARTICIPANT
        reqPlayer.type = t.PlayerControls(pType).gameValue()
        reqPlayer.race = playerObj.selectedRace.gameValue()
        if not playerObj.isComputer:
            joinRace = reqPlayer.race
    interface = sc_pb.InterfaceOptions()
    raw, score, feature, rendered = config.interfaces
    interface.raw = raw  # whether raw data is reported in observations
    interface.score = score  # whether score data is reported in observations
    interface.feature_layer.width = 24
    joinReq = sc_pb.RequestJoinGame(
        options=interface)  # SC2APIProtocol.RequestJoinGame
    joinReq.race = joinRace  # update joinGame request as necessary
    if debug: print("Starcraft2 game process is launching.")
    controller = None
    with config.launchApp() as controller:
        try:
            if debug:
                print("Starcraft2 application is live. (%s)" %
                      (controller.status))  # status: launched
            controller.create_game(createReq)
            if debug:
                print(
                    "Starcraft2 is waiting for %d player(s) to join. (%s)" %
                    (config.numAgents, controller.status))  # status: init_game
            playerID = controller.join_game(
                joinReq).player_id  # SC2APIProtocol.RequestJoinGame
            print("[GET IN-GAME DATA] player#%d %s" % (playerID, config))
            return (controller.ping(), controller.data())
        except (protocol.ConnectionError, protocol.ProtocolError,
                remote_controller.RequestError) as e:
            if debug:
                print("%s Connection to game closed (NOT a bug)%s%s" %
                      (type(e), os.linesep, e))
            else:
                print("Connection to game closed.")
        except KeyboardInterrupt:
            print(
                "caught command to forcibly shutdown Starcraft2 host server.")
        finally:
            config.disable(
            )  # if the saved cfg file still exists, always remove it
            controller.quit()  # force the sc2 application to close
示例#11
0
文件: sc2_env.py 项目: NoxiouSi/pysc2
    def __init__(
            self,  # pylint: disable=invalid-name
            _only_use_kwargs=None,
            map_name=None,
            players=None,
            agent_race=None,  # deprecated
            bot_race=None,  # deprecated
            difficulty=None,  # deprecated
            screen_size_px=None,  # deprecated
            minimap_size_px=None,  # deprecated
            feature_screen_size=None,
            feature_screen_width=None,
            feature_screen_height=None,
            feature_minimap_size=None,
            feature_minimap_width=None,
            feature_minimap_height=None,
            rgb_screen_size=None,
            rgb_screen_width=None,
            rgb_screen_height=None,
            rgb_minimap_size=None,
            rgb_minimap_width=None,
            rgb_minimap_height=None,
            action_space=None,
            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,
            use_feature_units=False,
            random_seed=None):
        """Create a SC2 Env.

    You must pass a resolution that you want to play at. You can send either
    feature layer resolution or rgb resolution or both. If you send both you
    must also choose which to use as your action space. Regardless of which you
    choose you must send both the screen and minimap resolutions.

    For each of the 4 resolutions, either specify size or both width and
    height. If you specify size then both width and height will take that value.

    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.
      players: A list of Agent and Bot instances that specify who will play.
      agent_race: Deprecated. Use players instead.
      bot_race: Deprecated. Use players instead.
      difficulty: Deprecated. Use players instead.
      screen_size_px: Deprecated. Use feature_screen_... instead.
      minimap_size_px: Deprecated. Use feature_minimap_... instead.
      feature_screen_size: Sets feature_screen_width and feature_screen_width.
      feature_screen_width: The width of the feature layer screen observation.
      feature_screen_height: The height of the feature layer screen observation.
      feature_minimap_size: Sets feature_minimap_width and
          feature_minimap_height.
      feature_minimap_width: The width of the feature layer minimap observation.
      feature_minimap_height: The height of the feature layer minimap
          observation.
      rgb_screen_size: Sets rgb_screen_width and rgb_screen_height.
      rgb_screen_width: The width of the rgb screen observation.
      rgb_screen_height: The height of the rgb screen observation.
      rgb_minimap_size: Sets rgb_minimap_width and rgb_minimap_height.
      rgb_minimap_width: The width of the rgb minimap observation.
      rgb_minimap_height: The height of the rgb minimap observation.
      action_space: If you pass both feature and rgb sizes, then you must also
          specify which you want to use for your actions as an ActionSpace enum.
      camera_width_world_units: The width of your screen in world units. If your
          feature_screen=(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.
      step_mul: How many game steps per agent step (action/observation). None
          means use the map default.
      save_replay_episodes: Save a replay after this many episodes. Default of 0
          means don't save replays.
      replay_dir: Directory to save replays. Required with save_replay_episodes.
      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.
      use_feature_units: Whether to include feature unit data in observations.
      random_seed: Random number seed to use when initializing the game. This
          lets you run repeatable games/tests.

    Raises:
      ValueError: if the agent_race, bot_race or difficulty are invalid.
      ValueError: if too many players are requested for a map.
      ValueError: if the resolutions aren't specified correctly.
      DeprecationWarning: if screen_size_px or minimap_size_px are sent.
      DeprecationWarning: if agent_race, bot_race or difficulty are sent.
    """
        if _only_use_kwargs:
            raise ValueError(
                "All arguments must be passed as keyword arguments.")

        if screen_size_px or minimap_size_px:
            raise DeprecationWarning(
                "screen_size_px and minimap_size_px are deprecated. Use the feature "
                "or rgb variants instead. Make sure to check your observations too "
                "since they also switched from screen/minimap to feature and rgb "
                "variants.")

        if agent_race or bot_race or difficulty:
            raise DeprecationWarning(
                "Explicit agent and bot races are deprecated. Pass an array of "
                "sc2_env.Bot and sc2_env.Agent instances instead.")

        if not players:
            players = [
                Agent(Race.random),
                Bot(Race.random, Difficulty.very_easy)
            ]

        for p in players:
            if not isinstance(p, (Agent, Bot)):
                raise ValueError(
                    "Expected players to be of type Agent or Bot. Got: %s." %
                    p)

        self._num_players = sum(1 for p in players if isinstance(p, Agent))
        self._players = players

        if not 1 <= len(players) <= 2 or not 1 <= self._num_players <= 2:
            raise ValueError("Only 1 or 2 players is supported at the moment.")

        feature_screen_px = features.point_from_size_width_height(
            feature_screen_size, feature_screen_width, feature_screen_height)
        feature_minimap_px = features.point_from_size_width_height(
            feature_minimap_size, feature_minimap_width,
            feature_minimap_height)
        rgb_screen_px = features.point_from_size_width_height(
            rgb_screen_size, rgb_screen_width, rgb_screen_height)
        rgb_minimap_px = features.point_from_size_width_height(
            rgb_minimap_size, rgb_minimap_width, rgb_minimap_height)

        if bool(feature_screen_px) != bool(feature_minimap_px):
            raise ValueError("Must set all the feature layer sizes.")
        if bool(rgb_screen_px) != bool(rgb_minimap_px):
            raise ValueError("Must set all the rgb sizes.")
        if not feature_screen_px and not rgb_screen_px:
            raise ValueError("Must set either the feature layer or rgb sizes.")

        if rgb_screen_px and (rgb_screen_px.x < rgb_minimap_px.x
                              or rgb_screen_px.y < rgb_minimap_px.y):
            raise ValueError(
                "Screen (%s) can't be smaller than the minimap (%s)." %
                (rgb_screen_px, rgb_minimap_px))

        if feature_screen_px and rgb_screen_px and not action_space:
            raise ValueError(
                "You must specify the action space if you have both observations."
            )

        if save_replay_episodes and not replay_dir:
            raise ValueError("Missing replay_dir")

        self._map = maps.get(map_name)

        if self._map.players and self._num_players > self._map.players:
            raise ValueError(
                "Map only supports %s players, but trying to join with %s" %
                (self._map.players, self._num_players))

        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
        self._random_seed = random_seed

        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
        if self._episode_length is None:
            self._episode_length = self._map.game_steps_per_episode
        self._episode_steps = 0

        self._run_config = run_configs.get()
        self._parallel = run_parallel.RunParallel()  # Needed for multiplayer.

        interface = sc_pb.InterfaceOptions(raw=(visualize
                                                or use_feature_units),
                                           score=True)
        if feature_screen_px:
            interface.feature_layer.width = camera_width_world_units or 24
            feature_screen_px.assign_to(interface.feature_layer.resolution)
            feature_minimap_px.assign_to(
                interface.feature_layer.minimap_resolution)
        if rgb_screen_px:
            rgb_screen_px.assign_to(interface.render.resolution)
            rgb_minimap_px.assign_to(interface.render.minimap_resolution)

        if self._num_players == 1:
            self._launch_sp(interface)
        else:
            self._launch_mp(interface)

        game_info = self._controllers[0].game_info()
        static_data = self._controllers[0].data()

        if game_info.options.render != interface.render:
            logging.warning(
                "Actual interface options don't match requested options:\n"
                "Requested:\n%s\n\nActual:\n%s", interface, game_info.options)

        self._features = features.Features(game_info=game_info,
                                           action_space=action_space,
                                           use_feature_units=use_feature_units)
        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.")
示例#12
0
    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)
        self.in_game = True
        self.step()  # Get into the game properly.
示例#13
0
    def test_replay_a_replay(self):
        run_config = run_configs.get()
        with run_config.start(want_rgb=False) as controller:
            map_inst = maps.get("Flat64")
            map_data = map_inst.data(run_config)
            interface = sc_pb.InterfaceOptions(raw=True)

            # Play a quick game to generate a replay.
            create = sc_pb.RequestCreateGame(local_map=sc_pb.LocalMap(
                map_path=map_inst.path, map_data=map_data))
            create.player_setup.add(type=sc_pb.Participant)
            create.player_setup.add(type=sc_pb.Computer,
                                    race=sc_common.Terran,
                                    difficulty=sc_pb.VeryEasy)
            join = sc_pb.RequestJoinGame(race=sc_common.Terran,
                                         options=interface)

            controller.create_game(create)
            controller.join_game(join)
            controller.step(100)
            obs = controller.observe()
            replay_data = controller.save_replay()

            # Run through the replay the first time, verifying that it finishes, but
            # wasn't recording a replay.
            start_replay = sc_pb.RequestStartReplay(replay_data=replay_data,
                                                    map_data=map_data,
                                                    options=interface,
                                                    observed_player_id=1)

            controller.start_replay(start_replay)
            controller.step(1000)
            obs2 = controller.observe()
            self.assertEqual(obs.observation.game_loop,
                             obs2.observation.game_loop)
            with self.assertRaises(protocol.ProtocolError):
                controller.save_replay()

            # Run through the replay a second time, verifying that it finishes, and
            # *was* recording a replay.
            start_replay.record_replay = True
            controller.start_replay(start_replay)
            controller.step(1000)
            obs2 = controller.observe()
            self.assertEqual(obs.observation.game_loop,
                             obs2.observation.game_loop)
            replay_data2 = controller.save_replay()

            # Make sure the replay isn't too small. Variance is fine but empty is not.
            self.assertGreater(len(replay_data2), len(replay_data) * 0.8)

            # Run through the replay a third time, verifying that it finishes, but
            # still wasn't recording a replay.
            start_replay.record_replay = False
            controller.start_replay(start_replay)
            controller.step(1000)
            obs3 = controller.observe()
            self.assertEqual(obs.observation.game_loop,
                             obs3.observation.game_loop)
            with self.assertRaises(protocol.ProtocolError):
                controller.save_replay()
示例#14
0
flags.DEFINE_string(name='save_path', default='../parsed_replays',
                    help='Path for saving results')

flags.DEFINE_integer(name='n_instance', default=64, #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.
        while True:
示例#15
0
def main(unused_argv):
    """Run SC2 to play a game or a replay."""
    stopwatch.sw.enabled = FLAGS.profile or FLAGS.trace
    stopwatch.sw.trace = FLAGS.trace

    if (FLAGS.map and FLAGS.replay) or (not FLAGS.map and not FLAGS.replay):
        sys.exit("Must supply either a map or replay.")

    if FLAGS.replay and not FLAGS.replay.lower().endswith("sc2replay"):
        sys.exit("Replay must end in .SC2Replay.")

    if FLAGS.realtime and FLAGS.replay:
        # TODO(tewalds): Support realtime in replays once the game supports it.
        sys.exit("realtime isn't possible for replays yet.")

    if FLAGS.render and (FLAGS.realtime or FLAGS.full_screen):
        sys.exit(
            "disable pygame rendering if you want realtime or full_screen.")

    if platform.system() == "Linux" and (FLAGS.realtime or FLAGS.full_screen):
        sys.exit("realtime and full_screen only make sense on Windows/MacOS.")

    if not FLAGS.render and FLAGS.render_sync:
        sys.exit("render_sync only makes sense with pygame rendering on.")

    run_config = run_configs.get()

    interface = sc_pb.InterfaceOptions()
    interface.raw = FLAGS.render
    interface.score = True
    interface.feature_layer.width = 24
    interface.feature_layer.resolution.x = FLAGS.screen_resolution
    interface.feature_layer.resolution.y = FLAGS.screen_resolution
    interface.feature_layer.minimap_resolution.x = FLAGS.minimap_resolution
    interface.feature_layer.minimap_resolution.y = FLAGS.minimap_resolution

    max_episode_steps = FLAGS.max_episode_steps

    if FLAGS.map:
        map_inst = maps.get(FLAGS.map)
        if map_inst.game_steps_per_episode:
            max_episode_steps = map_inst.game_steps_per_episode
        create = sc_pb.RequestCreateGame(
            realtime=FLAGS.realtime,
            disable_fog=FLAGS.disable_fog,
            local_map=sc_pb.LocalMap(map_path=map_inst.path,
                                     map_data=map_inst.data(run_config)))
        create.player_setup.add(type=sc_pb.Participant)
        create.player_setup.add(
            type=sc_pb.Computer,
            race=sc2_env.races[FLAGS.bot_race],
            difficulty=sc2_env.difficulties[FLAGS.difficulty])
        join = sc_pb.RequestJoinGame(race=sc2_env.races[FLAGS.user_race],
                                     options=interface)
        game_version = None
    else:
        replay_data = run_config.replay_data(FLAGS.replay)
        start_replay = sc_pb.RequestStartReplay(
            replay_data=replay_data,
            options=interface,
            disable_fog=FLAGS.disable_fog,
            observed_player_id=FLAGS.observed_player)
        game_version = get_game_version(replay_data)

    with run_config.start(game_version=game_version,
                          full_screen=FLAGS.full_screen) as controller:
        if FLAGS.map:
            controller.create_game(create)
            controller.join_game(join)
        else:
            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)

        if FLAGS.render:
            renderer = renderer_human.RendererHuman(
                fps=FLAGS.fps,
                step_mul=FLAGS.step_mul,
                render_sync=FLAGS.render_sync)
            renderer.run(run_config,
                         controller,
                         max_game_steps=FLAGS.max_game_steps,
                         game_steps_per_episode=max_episode_steps,
                         save_replay=FLAGS.save_replay)
        else:  # Still step forward so the Mac/Windows renderer works.
            try:
                while True:
                    frame_start_time = time.time()
                    if not FLAGS.realtime:
                        controller.step(FLAGS.step_mul)
                    obs = controller.observe()

                    if obs.player_result:
                        break
                    time.sleep(
                        max(0, frame_start_time + 1 / FLAGS.fps - time.time()))
            except KeyboardInterrupt:
                pass
            print("Score: ", obs.observation.score.score)
            print("Result: ", obs.player_result)
            if FLAGS.map and FLAGS.save_replay:
                replay_save_loc = run_config.save_replay(
                    controller.save_replay(), "local", FLAGS.map)
                print("Replay saved to:", replay_save_loc)
                # Save scores so we know how the human player did.
                with open(replay_save_loc.replace("SC2Replay", "txt"),
                          "w") as f:
                    f.write("{}\n".format(obs.observation.score.score))

    if FLAGS.profile:
        print(stopwatch.sw)
示例#16
0
def main(argv):
    import a3c.common.a3c
    scenarios.load_scenarios()
    run_config = run_configs.get()

    interface = sc_pb.InterfaceOptions()
    interface.raw = False
    interface.score = True
    interface.feature_layer.width = 24
    interface.feature_layer.resolution.x = FLAGS.screen_resolution
    interface.feature_layer.resolution.y = FLAGS.screen_resolution
    interface.feature_layer.minimap_resolution.x = 64
    interface.feature_layer.minimap_resolution.y = 64

    queue = FakeQueue()
    #
    shared.gamma_n = FLAGS.gamma ** FLAGS.n_step_return
    env = helpers.get_env_wrapper(False)
    s_space = env.observation_space.shape

    none_state = np.zeros(s_space)
    none_state = none_state.reshape(s_space)
    replay_agent = Agent(env.action_space.n, t_queue=queue, none_state=none_state)

    for fname in glob.glob(os.path.join(FLAGS.dir, '*.SC2Replay')):
        replay_data = run_config.replay_data(fname)
        start_replay = sc_pb.RequestStartReplay(
            replay_data=replay_data,
            options=interface,
            disable_fog=True,
            observed_player_id=1)
        game_version = get_game_version(replay_data)
        with run_config.start(game_version=game_version,
                              full_screen=False) as controller:


            controller.start_replay(start_replay)
            feat = features.Features(controller.game_info())

            obs = controller.observe()
            s = get_obs(env._input_layers, obs)
            results = 0
            last_reward = 0
            while True:
                actions = []
                for a in obs.actions:
                    try:
                        temp = feat.reverse_action(a)
                        x = 0
                        y = 0
                        if temp[0] not in [0, 7]:
                            x = temp.arguments[1][0]
                            y = temp.arguments[1][1]
                        actions.append([env._actions.index(temp[0]), x ,y])
                    except ValueError:
                        pass

                if len(actions) < 1:
                    try:
                        controller.step(FLAGS.step_mul)
                    except ProtocolError:
                        break;

                    obs = controller.observe()
                    s = get_obs(env._input_layers, obs)
                    continue

                r = obs.observation.score.score

                controller.step(FLAGS.step_mul)
                obs = controller.observe()

                s_ = get_obs(env._input_layers, obs)

                if r == 0 and last_reward != 0:
                    s_ = None
                    print('Episode end')

                results += 1

                if not FLAGS.raw_rewards:
                    replay_agent.train(s, actions[0][0], actions[0][1], actions[0][2], r, s_)
                else:
                    queue.put([s, actions[0][0], actions[0][1], actions[0][2], r, s_])

                if obs.player_result:
                    break

                if r == 0 and last_reward != 0:
                    last_reward = 0
                else:
                    s = s_
                    last_reward = r

    with gzip.open('./replay_info/info.gz', 'wb+') as outfile:
        print('pushed: {}'.format(results))
        #json.dump(queue.get(), outfile)
        pickle.dump(queue.get(), outfile)
示例#17
0
def main(unused_argv):
    """Run SC2 to play a game or a replay."""

    if not FLAGS.replay:
        sys.exit("Must supply a replay.")

    if not FLAGS.save_path:
        sys.exit("Must supply a replays_save path.")

    if not os.path.exists(FLAGS.save_path) and FLAGS.save_data:
        os.makedirs(FLAGS.save_path)

    if FLAGS.replay and not FLAGS.replay.lower().endswith("sc2replay"):
        sys.exit("Replay must end in .SC2Replay.")

    if FLAGS.realtime and FLAGS.replay:
        # TODO(tewalds): Support realtime in replays once the game supports it.
        sys.exit("realtime isn't possible for replays yet.")

    if FLAGS.render and (FLAGS.realtime or FLAGS.full_screen):
        sys.exit("disable pygame rendering if you want realtime or full_screen.")

    if platform.system() == "Linux" and (FLAGS.realtime or FLAGS.full_screen):
        sys.exit("realtime and full_screen only make sense on Windows/MacOS.")

    if not FLAGS.render and FLAGS.render_sync:
        sys.exit("render_sync only makes sense with pygame rendering on.")

    run_config = run_configs.get()

    interface = sc_pb.InterfaceOptions()
    interface.raw = True
    interface.score = True
    interface.feature_layer.width = 24
    interface.feature_layer.resolution.x = FLAGS.screen_resolution
    interface.feature_layer.resolution.y = FLAGS.screen_resolution
    interface.feature_layer.minimap_resolution.x = FLAGS.minimap_resolution
    interface.feature_layer.minimap_resolution.y = FLAGS.minimap_resolution

    replay_data = run_config.replay_data(FLAGS.replay)
    start_replay = sc_pb.RequestStartReplay(
        replay_data=replay_data,
        options=interface,
        disable_fog=FLAGS.disable_fog,
        observed_player_id=FLAGS.observed_player)

    with run_config.start(full_screen=FLAGS.full_screen) as controller:

        info = controller.replay_info(replay_data)
        print(" Replay info ".center(60, "-"))
        print(info)
        print("-" * 60)

        controller.start_replay(start_replay)

        feature_layer = features.Features(controller.game_info())

        frame_num = info.game_duration_loops
        step_num = frame_num // FLAGS.step_mul

        path = FLAGS.save_path

        # init data
        player_data = np.zeros((step_num, 1 + 11))
        unit_data = np.zeros((step_num, 1 + 7))
        score_data = np.zeros((step_num, 1 + 13))

        frame_array = [(x+1)*FLAGS.step_mul for x in range(step_num)]
        player_data[:, 0] = unit_data[:, 0] = score_data[:, 0] = frame_array

        order_data = np.array([])
        obs = controller.observe()
        for i in range(step_num):
            # to play the game in the normal speed

            controller.step(FLAGS.step_mul)

            obs = controller.observe()
            obs_data = feature_layer.transform_obs(obs.observation)

            player_data[i, 1:] = obs_data["player"]
            unit_data[i, 1:] = obs_data["single_select"]
            score_data[i, 1:] = obs_data["score_cumulative"]

            if FLAGS.save_data:
                np.save(path + "minimap_%d.npy" % obs_data["game_loop"], obs_data["minimap"])
                np.save(path + "screen_%d.npy" % obs_data["game_loop"], obs_data["screen"])

            # [game_loop, action_type, x, y]
            # action_type: 0 : move, 1 : build_pylon, 2 : build_forge, 3: build_cannon, 4: move_camera
            action_ability_list = [16, 881, 884, 887]

            unit_set = obs.observation.raw_data.units
            for u in unit_set:
                if u.orders:
                    if u.orders[0].ability_id in action_ability_list:
                        order_temp = np.zeros(4)
                        order_temp[0] = obs.observation.game_loop
                        order_temp[1] = action_ability_list.index(u.orders[0].ability_id)
                        order_temp[2:] = [u.orders[0].target_world_space_pos.x, u.orders[0].target_world_space_pos.y]

                        order_data = np.append(order_data, order_temp)

                        for unit in unit_set:
                            if unit.build_progress < 1:
                                print(1)

        print("Score: ", obs.observation.score.score)
        print("Result: ", obs.player_result)

        if FLAGS.save_data:
            np.savetxt(path + "unit.txt", unit_data)
            np.savetxt(path + "top.txt", player_data)
            np.savetxt(path + "score.txt", score_data)
            np.savetxt(path + "order.txt", order_data.reshape(-1, 4))