Example #1
0
    def _get_new_instance(self, port=None, instance_id=None):
        """
        Gets a new instance and sets up a logger if need be. 
        """

        if not port is None:
            instance = InstanceManager.add_existing_instance(port)
        else:
            instance = InstanceManager.get_instance(os.getpid(), instance_id=instance_id)

        if InstanceManager.is_remote():
            launch_queue_logger_thread(instance, self.is_closed)

        instance.launch()
        return instance
Example #2
0
    def __init__(
        self,
        seeds: [int],
        file_name: Optional[str] = None,
        worker_id: int = 0,
        base_port: int = 5005,
        docker_training: bool = False,
        no_graphics: bool = False,
        # timeout_wait: int = 30,
        # args: Optional[List[str]] = None,
        num_envs: int = 1):
        """
        Starts a new unity environment and establishes a connection with the environment.
        Notice: Currently communication between Unity and Python takes place over an open socket without authentication.
        Ensure that the network where training takes place is secure.

        :string file_name: Name of Unity environment binary.
        :int base_port: Baseline port number to connect to Unity environment over. worker_id increments over this.
        :int worker_id: Number to add to communication port (5005) [0]. Used for asynchronous agent scenarios.
        :bool docker_training: Informs this class whether the process is being run within a container.
        :bool no_graphics: Whether to run the Unity simulator in no-graphics mode
        :int timeout_wait: Time (in seconds) to wait for connection from environment.
        :bool train_mode: Whether to run in training mode, speeding up the simulation, by default.
        """

        atexit.register(self._close)
        self.port = base_port + worker_id
        self._buffer_size = 12000
        self._version_ = "API-10"
        self._loaded = (
            False
        )  # If true, this means the environment was successfully loaded
        self.proc1 = (
            None
        )  # The process that is started. If None, no process was started
        # self.communicator = self.get_communicator(worker_id, base_port, timeout_wait)

        self._worker_id = worker_id
        self._is_first_message = True

        from minerl.env.malmo import InstanceManager
        self._envs: Dict[str, [Env]] = {}
        self._agent_ids: Dict[str, [str]] = {}
        self._n_agents: Dict[str, int] = {}
        self._academy_name = 'MineRLUnityAcademy'
        self._log_path = 'log_path'
        self._brains: Dict[str, BrainParameters] = {}
        self._brain_names: List[str] = []
        self._external_brain_names: List[str] = []
        InstanceManager.configure_malmo_base_port(base_port + worker_id)

        for i in range(num_envs):
            print('InstanceManager:', InstanceManager)
            try:
                print('.MAXINSTANCES', InstanceManager.MAXINSTANCES)
            except AttributeError:
                print('.MAXINSTANCES', 'None')
                pass
            print('.REMOTE', InstanceManager.is_remote())
            try:
                print('.ninstances', InstanceManager.ninstances)
            except AttributeError:
                print('.ninstances', 'None')
                pass
            try:
                print('.DEFAULT_IP', InstanceManager.DEFAULT_IP)
            except AttributeError:
                print('.DEFAULT_IP', 'None')
                pass
            env = gym.make(file_name)
            env = MineRLToMLAgentWrapper(env, seeds[i])
            env = RefineObservationsWrapper(env)
            env = NormalizeObservationsWrapper(env)

            VISUALIZE = bool(os.getenv('VISUALIZE', None))
            if VISUALIZE and self.worker_id is 0:
                env = KeyboardControlWrapper(env)

            # env = HardwireActionsWrapper(env)

            env = PruneActionsWrapper(
                env,
                [
                    # 'attack_jump'
                    # ,'camera_left_right'
                    # 'camera_up_down'
                    # ,'forward_back'
                    # 'left_right'
                    # ,'place'
                    # ,'sneak_sprint'
                ])
            env = PruneVisualObservationsWrapper(env, hack_ignor=True)
            # env = VisualObsAsFloatWrapper(env)

            env = FrameStackMono(env, 2, 10)

            EVALUATION_STAGE = os.getenv('EVALUATION_STAGE', '')
            if EVALUATION_STAGE != 'testing':
                env = EarlyExitWrapper(env)
                env = DingRewardOnDoneWrapper(env)

            # note: should be the last wrapper
            env = ResetOnDoneWrapper(env)

            MineRLToMLAgentWrapper.set_wrappers_for_pretraining(file_name, env)

            brain_name = env.brain_parameters.brain_name
            if brain_name not in self._agent_ids:
                self._envs[brain_name] = []
                self._agent_ids[brain_name] = []
                self._n_agents[brain_name] = 0
                self._brain_names.append(brain_name)
                brain = env.brain_parameters
                self._external_brain_names.append(brain_name)
                self._brains[brain_name] = brain
            self._envs[brain_name].append(env)
            self._agent_ids[brain_name].append(env.agent_id)
            self._n_agents[brain_name] += 1
        self._loaded = True

        self._num_brains = len(self._brain_names)
        self._num_external_brains = len(self._external_brain_names)
        # self._resetParameters = dict(aca_params.environment_parameters.float_parameters)
        self._resetParameters = dict()
        logger.info("\n'{0}' started successfully!\n{1}".format(
            self._academy_name, str(self)))
        if self._num_external_brains == 0:
            logger.warning(
                " No Learning Brains set to train found in the Unity Environment. "
                "You will not be able to pass actions to your agent(s).")