Exemplo n.º 1
0
    def close(self):
        """Shutdown and free all resources."""
        for controller in self._controllers:
            controller.quit()
        self._controllers = []

        for process in self._processes:
            process.close()
        self._processes = []

        portspicker.return_ports(self._lan_ports)
        self._lan_ports = []
Exemplo n.º 2
0
    def close(self):  # Instead of tearDown.
        """Shut down the SC2 instances."""
        # Don't use parallel since it might be broken by an exception.
        if hasattr(self, "_controllers") and self._controllers:
            for c in self._controllers:
                c.quit()
            self._controllers = None
        if hasattr(self, "_sc2_procs") and self._sc2_procs:
            for p in self._sc2_procs:
                p.close()
            self._sc2_procs = None

        if hasattr(self, "_ports") and self._ports:
            portspicker.return_ports(self._ports)
            self._ports = None
        self._parallel = None
Exemplo n.º 3
0
    def close(self):
        """Shutdown and free all resources."""
        try:
            self._reconnect(timeout_seconds=1)
            for controller in self._controllers:
                controller.quit()
        except (remote_controller.ConnectError, protocol.ConnectionError):
            pass
        self._controllers = []

        for process in self._processes:
            process.close()
        self._processes = []

        portspicker.return_ports(self._lan_ports)
        self._lan_ports = []
Exemplo n.º 4
0
    def close(self):
        logging.info("Environment Close")
        if hasattr(self, "_metrics") and self._metrics:
            self._metrics.close()
            self._metrics = None
        if hasattr(self, "_renderer_human") and self._renderer_human:
            self._renderer_human.close()
            self._renderer_human = None

        # Don't use parallel since it might be broken by an exception.
        if hasattr(self, "_controllers") and self._controllers:
            for c in self._controllers:
                c.quit()
            self._controllers = None
        if hasattr(self, "_sc2_procs") and self._sc2_procs:
            for p in self._sc2_procs:
                p.close()
            self._sc2_procs = None

        if hasattr(self, "_ports") and self._ports:
            portspicker.return_ports(self._ports)
            self._ports = None
Exemplo n.º 5
0
  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 = portspicker.pick_unused_ports(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()
      portspicker.return_ports(ports)
Exemplo n.º 6
0
 def testContiguousReservation(self, num_ports):
     reserved = portspicker.pick_contiguous_unused_ports(num_ports)
     self.assertLen(reserved, num_ports)
     portspicker.return_ports(reserved)
Exemplo n.º 7
0
  def test_observe_players(self):
    players = 2  # Can be 1.
    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(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 = portspicker.pick_unused_ports((players + 2) * 2)
    logging.info("Valid Ports: %s", ports)

    # Actually launch the game processes.
    print_stage("start")
    sc2_procs = [run_config.start(extra_ports=ports, want_rgb=False)
                 for _ in range(players + 1)]
    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))
      create.player_setup.add(type=sc_pb.Participant)
      if players == 1:
        create.player_setup.add(type=sc_pb.Computer, race=sc_common.Random,
                                difficulty=sc_pb.VeryEasy)
      else:
        create.player_setup.add(type=sc_pb.Participant)
      create.player_setup.add(type=sc_pb.Observer)

      # Create the join request.
      joins = []
      portIdx = 2
      for i in range(players + 1):
        join = sc_pb.RequestJoinGame(options=interface)
        if i < players:
          join.race = sc_common.Random
        else:
          join.observed_player_id = 0
        join.host_ip = sc2_procs[0].host
        join.shared_port = 0  # unused
        join.server_ports.game_port = ports[0]
        join.server_ports.base_port = ports[1]
        join.client_ports.add(game_port=ports[portIdx], base_port=ports[portIdx + 1])
        portIdx = portIdx + 2
        joins.append(join)

      # Create and Join
      print_stage("create")
      controllers[0].create_game(create)
      print_stage("join")
      parallel.run((c.join_game, join) for c, join in zip(controllers, joins))

      print_stage("run")
      for game_loop in range(1, 10):  # steps per episode
        # Step the game
        parallel.run((c.step, 16) 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 * 16)
          if p_id == players:  # ie the observer
            self.assertEqual(o.observation.player_common.player_id, 0)
          else:
            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[:players], 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()
      portspicker.return_ports(ports)
Exemplo n.º 8
0
def human():
    """Run a host which expects one player to connect remotely."""
    run_config = run_configs.get()

    map_inst = maps.get(FLAGS.map)

    if not FLAGS.rgb_screen_size or not FLAGS.rgb_minimap_size:
        logging.info(
            "Use --rgb_screen_size and --rgb_minimap_size if you want rgb "
            "observations.")

    ports = portspicker.pick_contiguous_unused_ports(4)  # 2 * num_players
    host_proc = run_config.start(extra_ports=ports,
                                 host=FLAGS.host,
                                 timeout_seconds=300,
                                 window_loc=(50, 50))
    client_proc = run_config.start(extra_ports=ports,
                                   host=FLAGS.host,
                                   connect=False,
                                   window_loc=(700, 50))

    create = sc_pb.RequestCreateGame(
        realtime=FLAGS.realtime,
        local_map=sc_pb.LocalMap(map_path=map_inst.path))
    create.player_setup.add(type=sc_pb.Participant)
    create.player_setup.add(type=sc_pb.Participant)

    controller = host_proc.controller
    controller.save_map(map_inst.path, map_inst.data(run_config))
    controller.create_game(create)

    print("-" * 80)
    print("Join host: agent_remote --map %s --host %s --host_port %s "
          "--lan_port %s" %
          (FLAGS.map, FLAGS.host, client_proc.port, ports[0]))
    print("-" * 80)

    join = sc_pb.RequestJoinGame()
    join.shared_port = 0  # unused
    join.server_ports.game_port = ports.pop(0)
    join.server_ports.base_port = ports.pop(0)
    join.client_ports.add(game_port=ports.pop(0), base_port=ports.pop(0))

    join.race = sc2_env.Race[FLAGS.user_race]
    if FLAGS.render:
        join.options.raw = True
        join.options.score = True
        if FLAGS.feature_screen_size and FLAGS.feature_minimap_size:
            fl = join.options.feature_layer
            fl.width = 24
            FLAGS.feature_screen_size.assign_to(fl.resolution)
            FLAGS.feature_minimap_size.assign_to(fl.minimap_resolution)
        if FLAGS.rgb_screen_size and FLAGS.rgb_minimap_size:
            FLAGS.rgb_screen_size.assign_to(join.options.render.resolution)
            FLAGS.rgb_minimap_size.assign_to(
                join.options.render.minimap_resolution)
    controller.join_game(join)

    if FLAGS.render:
        renderer = renderer_human.RendererHuman(fps=FLAGS.fps,
                                                render_feature_grid=False)
        renderer.run(run_configs.get(), controller, max_episodes=1)
    else:  # Still step forward so the Mac/Windows renderer works.
        try:
            while True:
                frame_start_time = time.time()
                if not FLAGS.realtime:
                    controller.step()
                obs = controller.observe()

                if obs.player_result:
                    break
                time.sleep(
                    max(0, frame_start_time - time.time() + 1 / FLAGS.fps))
        except KeyboardInterrupt:
            pass

    for p in [host_proc, client_proc]:
        p.close()

    portspicker.return_ports(ports)
Exemplo n.º 9
0
 def testNonContiguousReservation(self, num_ports):
   reserved = portspicker.pick_unused_ports(num_ports)
   self.assertEqual(len(reserved), num_ports)
   portspicker.return_ports(reserved)