def setup_mission_record(self, params): """ Setups up the ``mission_record`` for the current environment. :param params: Marlo Game Parameters as described in :meth:`default_base_params` :type params: dict """ ############################################################ # Setup Mission Record ############################################################ self.mission_record_spec = MalmoPython.MissionRecordSpec() # empty if params.recordDestination: self.mission_record_spec.setDestination(params.recordDestination) if params.recordRewards: self.mission_record_spec.recordRewards() if params.recordCommands: self.mission_record_spec.recordCommands() if params.recordMP4: assert type(params.recordMP4) == list \ and len(params.recordMP4) == 2 self.mission_record_spec.recordMP4(*(params.recordMP4)) else: if params.recordRewards or params.recordCommands or params.recordMP4: raise Exception("recordRewards or recordCommands or recordMP4 " "provided without specifyin recordDestination")
def __init__(self, templates_folder): super(MarloEnvBuilderBase, self).__init__() self.templates_folder = templates_folder self.setup_templating() self._default_base_params = False self.agent_host = MalmoPython.AgentHost() self.mission_spec = None self.client_pool = None self.experiment_id = None self._turn = None
def setup_mission_spec(self, params): """ Generates and setups the first MissionSpec as generated by :meth:`render_mission_spec`. :param params: Marlo Game Parameters as described in :meth:`default_base_params` :type params: dict """ ############################################################ # Instantiate Mission Spec ############################################################ mission_xml = self.render_mission_spec() self.mission_spec = MalmoPython.MissionSpec(mission_xml, True)
def setup_client_pool(self, params): """ Setups up the ``client_pool`` for the current environment. :param params: Marlo Game Parameters as described in :meth:`default_base_params` :type params: dict """ ############################################################ # Setup Client Pool ############################################################ if not params.client_pool: logger.warn("No client pool provided, attempting to create " "a client_pool of the correct size") number_of_agents = self.mission_spec.getNumberOfAgents() params.client_pool = marlo.launch_clients(number_of_agents) self.client_pool = MalmoPython.ClientPool() for _client in params.client_pool: self.client_pool.add(MalmoPython.ClientInfo(*_client)) if not isinstance(params.client_pool, list): raise ValueError("params.client_pool must be a list of tuples" "of (ip_address, port)")
def setupEnv(mission='MarLo-FindTheGoal-v0', videoResolution = [800, 600], port=10000): # Sets up marlo environment client_pool = [('127.0.0.1', port)] # Step sleep at to 0.2 to handle lag between marlo and Malmo join_tokens = marlo.make(mission, params={ "client_pool": client_pool, 'suppress_info': False, 'videoResolution': videoResolution, 'tick_length': 50, 'step_sleep': 0.2}) # As this is a single agent scenario, # there will just be a single token assert len(join_tokens) == 1 join_token = join_tokens[0] env = marlo.init(join_token) # Change the spec of the mission by loading xml from file # This is necessary to use our custom XML files missionXML= loadMissionFile(mission+'.xml') env.mission_spec = MalmoPython.MissionSpec(missionXML, True) return env
Crouch Attack Use Rewards: nil """ def __init__(self, extra_params={}): super(MarloEnvBuilder, self).__init__( templates_folder=os.path.join(Path(__file__).parent, "templates")) self.params = self._default_params() # You can do something with the extra_params if you wish self.params.update(extra_params) print(self.params["maze_height"]) print(extra_params) def _default_params(self): _default_params = super(MarloEnvBuilder, self).default_base_params _default_params.update( dict(tick_length=50, agent_names=["MarLo-agent0"], maze_height=2)) return _default_params if __name__ == "__main__": env_builder = MarloEnvBuilder() print(env_builder.params) print(env_builder.params.experiment_id) mission_xml = env_builder.render_mission_spec() mission_spec = MalmoPython.MissionSpec(mission_xml, True) print(mission_spec.getSummary())
def _reset(self): self._rounds += 1 # Kill clients after configured number of rounds. if (self.params.kill_clients_after_num_rounds and self._rounds > self.params.kill_clients_after_num_rounds): self._kill_clients(False) self._rounds = 1 # If a mission is already running, try to quit it # Note : This assumes that <MissionQuitCommands/> is an allowed # command handler in the mission spec. world_state = self.agent_host.peekWorldState() if world_state.is_mission_running: if not self._turn or self._turn.can_play: self.send_command("quit") if self.params.forceWorldReset: # Force a World Reset on each reset self.mission_spec.forceWorldReset() # Attempt to start a mission for retry in range(self.params.max_retries + 1): logger.debug("RETRY : {}".format(retry)) # Role 0 (the server) could take some extra time to start if self.params.role != 0: time.sleep(1) else: time.sleep(0.1) if self.params.experiment_id: self.experiment_id = self.params.experiment_id try: if not self.client_pool: raise Exception("client_pool not specified.") self.agent_host.startMission(self.mission_spec, self.client_pool, self.mission_record_spec, self.params.role, self.experiment_id) logger.info("Waiting for mission to start...") world_state = self.agent_host.getWorldState() start_time = time.time() while not world_state.has_mission_begun: time.sleep(0.1) world_state = self.agent_host.getWorldState() for error in world_state.errors: logger.error("Mission start error: " + error.text) if any(world_state.errors): raise MalmoPython.MissionException( "Error while waiting for mission to start", world_state.errors[0]) if time.time() - start_time > 60: raise MalmoPython.MissionException( "Giving up on mission starting up") logger.info("Mission Running") frame = self._get_video_frame(world_state) # Notify Evaluation System, if applicable marlo.CrowdAiNotifier._env_reset() return frame except Exception as e: traceback.format_exc() if retry == self.params.max_retries: logger.error("Error Starting Mission : {}".format( traceback.format_exc())) raise e else: logger.warn( "Error on attempting to start mission : {}".format( str(e))) logger.warn("Will attempt again after {} seconds.".format( self.params.retry_sleep)) time.sleep(self.params.retry_sleep)