import os import random import sys import time import re import uuid from collections import namedtuple from operator import add EntityInfo = namedtuple( 'EntityInfo', 'x, y, z, yaw, pitch, name, colour, variation, quantity, life') EntityInfo.__new__.__defaults__ = (0, 0, 0, 0, 0, "", "", "", 1, "") # Create one agent host for parsing: agent_hosts = [MalmoPython.AgentHost()] # Parse the command-line options: agent_hosts[0].addOptionalFlag("debug,d", "Display debug information.") agent_hosts[0].addOptionalIntArgument( "agents,n", "Number of agents to use, including observer.", 4) try: agent_hosts[0].parse(sys.argv) except RuntimeError as e: print('ERROR:', e) print(agent_hosts[0].getUsage()) exit(1) if agent_hosts[0].receivedArgument("help"): print(agent_hosts[0].getUsage()) exit(0)
print "Bailing now." exit(1) time.sleep(0.1) print ".", if time.time() - start_time >= time_out: print "Timed out while waiting for mission to start - bailing." exit(1) print print "Mission has started." sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately # -- set up two agent hosts -- agent_host_simeon = MalmoPython.AgentHost() agent_host_fred = MalmoPython.AgentHost() try: agent_host_simeon.parse(sys.argv) except RuntimeError as e: print 'ERROR:', e print agent_host_simeon.getUsage() exit(1) if agent_host_simeon.receivedArgument("help"): print agent_host_simeon.getUsage() exit(0) # -- set up the mission -- xml = '''<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>MalmoTutorialBot</Name> <AgentStart> <Placement x="0.5" y="56.0" z="0.5"/> </AgentStart> <AgentHandlers> <AgentQuitFromTouchingBlockType> <Block type="redstone_block"/> </AgentQuitFromTouchingBlockType> </AgentHandlers> </AgentSection> </Mission>''' # Create default Malmo objects: agent_host = MalmoPython.AgentHost() try: agent_host.parse( sys.argv ) except RuntimeError as e: print('ERROR:',e) print(agent_host.getUsage()) exit(1) if agent_host.receivedArgument("help"): print(agent_host.getUsage()) exit(0) if agent_host.receivedArgument("test"): num_repeats = 1 else: num_repeats = 10
def start_default_mission(agent_host): my_mission = MalmoPython.MissionSpec() my_mission_record = MalmoPython.MissionRecordSpec() start_mission(agent_host, my_mission, my_mission_record)
def main(): sight = {'x': (-30, 30), 'z': (-30, 30), 'y': (-1, 1)} range_x = abs(sight['x'][1] - sight['x'][0]) + 1 range_y = abs(sight['y'][1] - sight['y'][0]) + 1 range_z = abs(sight['z'][1] - sight['z'][0]) + 1 malmoutils.fix_print() agent_host = MalmoPython.AgentHost() malmoutils.parse_command_line(agent_host) recordingsDirectory = malmoutils.get_recordings_directory(agent_host) recordingsDirectory = "../human_trajectories" if (not os.path.exists(recordingsDirectory)): os.mkdir(recordingsDirectory) logging.basicConfig(level=logging.INFO) # pdb.set_trace() logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # set to INFO if you want fewer messages video_width = 640 video_height = 480 sys.argv mission_xml_path = "../custom_xmls/usar.xml" validate = True # my_mission = MalmoPython.MissionSpec(missionXML, validate) my_mission = MalmoPython.MissionSpec(getMissionXML(mission_xml_path), validate) # ObservationFromGrid my_mission.observeGrid(sight['x'][0], sight['y'][0], sight['z'][0], sight['x'][1], sight['y'][1], sight['z'][1], 'relative_view') # agent_host.setObservationsPolicy(MalmoPython.ObservationsPolicy.LATEST_OBSERVATION_ONLY) agent_host.setVideoPolicy(MalmoPython.VideoPolicy.LATEST_FRAME_ONLY) if agent_host.receivedArgument("test"): num_reps = 1 else: num_reps = 30000 my_mission_record = MalmoPython.MissionRecordSpec() if recordingsDirectory: my_mission_record.recordRewards() my_mission_record.recordObservations() my_mission_record.recordCommands() # if agent_host.receivedArgument("record_video"): # my_mission_record.recordMP4(24,2000000) my_mission_record.recordMP4(24, 2000000) recording_name = datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p") for iRepeat in range(1): my_mission_record.setDestination( os.path.join(recordingsDirectory, recording_name + ".tgz")) max_retries = 3 for retry in range(max_retries): try: agent_host.startMission(my_mission, my_mission_record) break except RuntimeError as e: if retry == max_retries - 1: logger.error("Error starting mission: %s" % e) exit(1) else: time.sleep(2) logger.info('Mission %s', iRepeat) logger.info("Waiting for the mission to start") world_state = agent_host.getWorldState() while not world_state.has_mission_begun: print(".", end="") time.sleep(0.1) world_state = agent_host.getWorldState() print() img_counter = 0 # print('observations', world_state.observations) while world_state.is_mission_running: world_state = agent_host.getWorldState() # Observations # msg = observe(agent_host) # if msg is not None: # print('timestamp: ', msg['timestamp']) # NOTE : Nothing recorded in world state. Uncomment to test it out. # if world_state.number_of_observations_since_last_state > 0: # timestamp = world_state.observations[-1].timestamp # msg = world_state.observations[-1].text # obs = json.loads(msg) # print("{'timestamp': timestamp, 'observations': obs}") # Video Frames while world_state.number_of_video_frames_since_last_state < 1 and world_state.is_mission_running: logger.info("Waiting for frames...") time.sleep(0.05) world_state = agent_host.getWorldState() logger.info("Got frame!") # import ipdb; ipdb.set_trace # print('observations', world_state.observations) # world_state.observations if world_state.is_mission_running: # timestamp = world_state.observations[-1].timestamp # msg = world_state.observations[-1].text # print(timestamp) # print(msg) frame = world_state.video_frames[-1] img = Image.frombytes('RGB', (640, 480), bytes(frame.pixels)) # imageio.imsave("./tmp_imgs/{}.png".format(img_counter), img) img_counter += 1 logger.info("Mission has stopped.") time.sleep(1) # let the Mod recover
<AgentQuitFromReachingPosition> <Marker tolerance="0.1" x="0.5" y="227" z="''' + str( PATH_LENGTH + 0.5) + '''"/> </AgentQuitFromReachingPosition> </AgentHandlers> </AgentSection> </Mission>''' sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately validate = True agent_host = MalmoPython.AgentHost() agent_host.addOptionalIntArgument( "length,l", "Number of steps required to reach goal square.", 10) # Eg set the length to 0 or 1 to test https://github.com/Microsoft/malmo/issues/23 agent_host.addOptionalFlag("stop,s", "Stop after required number of steps.") # Eg if length is set to 10, will send 10 move commands and then wait for the mission to end. # This can be used to test that commands are all being acted on, regardless of the speed they are sent at, # and can give some indication of the latency between sending the final command and receiving the mission ended message. agent_host.addOptionalFloatArgument( "wait,w", "Number of seconds to wait between sending commands.", 0.1) # Setting this to something slow (eg 0.1) should show a clear cycle of commands/observations/rewards, # and a quit triggered after the correct number of commands. # Setting this to something faster (eg 0.05) should still show a clear cylce of commands/observations/rewards,
(world_x - 1 - curr_x + 0.5 - curr_radius) * scale, (world_y - 1 - curr_y + 0.5 - curr_radius) * scale, (world_x - 1 - curr_x + 0.5 + curr_radius) * scale, (world_y - 1 - curr_y + 0.5 + curr_radius) * scale, outline="#fff", fill="#fff") self.root.update() if sys.version_info[0] == 2: sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately else: import functools print = functools.partial(print, flush=True) agent_host = MalmoPython.AgentHost() # add some args agent_host.addOptionalStringArgument( 'mission_file', 'Path/to/file from which to load the mission.', './cliff_walking_1.xml') agent_host.addOptionalFloatArgument('alpha', 'Learning rate of the Q-learning agent.', 0.1) agent_host.addOptionalFloatArgument( 'epsilon', 'Exploration rate of the Q-learning agent.', 0.1) agent_host.addOptionalFloatArgument('gamma', 'Discount factor.', 1.0) agent_host.addOptionalFlag('load_model', 'Load initial model from model_file.') agent_host.addOptionalStringArgument('model_file', 'Path to the initial model file', '') agent_host.addOptionalFlag('debug', 'Turn on debugging.')
def __init__(self, mission_file): super(MinecraftEnvRLKit, self).__init__() self.agent_host = MalmoPython.AgentHost() assets_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../assets') mission_file = os.path.join(assets_dir, mission_file) xml = Path(mission_file).read_text() if not xml.startswith('<Mission'): i = xml.index("<Mission") xml = xml[i:] self.ns = '{http://ProjectMalmo.microsoft.com}' self.xml = etree.fromstring(xml) self.load_mission_file(mission_file) self.client_pool = None self.mc_process = None self.screen = None self.image_obs = None obs_from_grid = self.xml.findall( './/' + self.ns + 'ObservationFromGrid')[0].find(self.ns + 'Grid') low = dict(obs_from_grid[0].attrib) high = dict(obs_from_grid[1].attrib) self.x_lim = (int(low['x']), int(high['x'])) self.y_lim = (int(low['y']), int(high['y'])) self.z_lim = (int(low['z']), int(high['z'])) self.x = int(high['x']) - int(low['x']) + 1 self.y = int(float(high['y']) - float(low['y']) + 1) self.z = int(high['z']) - int(low['z']) + 1 self.full_dim = full_dim = self.z #self.x * self.y * self.z self.partial_dim = 6 #self.partial_obs_space = Box(low=0, high=10, shape=(6,), dtype=np.float32) self.obs_space = Box(low=0, high=10, shape=(full_dim + 4 + 6, ), dtype=np.float32) self.goal_space = Box(low=0, high=10, shape=(full_dim, ), dtype=np.float32) self.achieved_goal_space = Box(low=0, high=10, shape=(full_dim, ), dtype=np.float32) # self._state_goal = np.zeros((self.y, self.z, self.x)) # for y in range(1): # #self._state_goal[y, 0, :] = BLOCK_KEY['brick_block'] # #self._state_goal[y, -1, :] = BLOCK_KEY['brick_block'] # #self._state_goal[y, :, 0] = BLOCK_KEY['brick_block'] # #self._state_goal[y, 3, -1] = BLOCK_KEY['brick_block'] # self._state_goal[y, :, -1] = BLOCK_KEY['brick_block'] # self._state_goal = self._state_goal.flatten() self._state_goal = np.ones((full_dim, )) self.last_obs = None self.fix_goal = True self.init_state = None
recordingsDirectory = "EatingRecordings" try: os.makedirs(recordingsDirectory) except OSError as exception: if exception.errno != errno.EEXIST: # ignore error if already existed raise sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately validate = True # Create a pool of Minecraft Mod clients. # By default, mods will choose consecutive mission control ports, starting at 10000, # so running four mods locally should produce the following pool by default (assuming nothing else # is using these ports): my_client_pool = MalmoPython.ClientPool() my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10000)) my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10001)) my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10002)) my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10003)) agent_host = MalmoPython.AgentHost() try: agent_host.parse(sys.argv) except RuntimeError as e: print 'ERROR:', e print agent_host.getUsage() exit(1) if agent_host.receivedArgument("help"): print agent_host.getUsage() exit(0)
<ModifierList type="deny-list"> <!-- Example deny-list: prevent agent from strafing --> <command>strafe</command> </ModifierList> </ContinuousMovementCommands> <ChatCommands /> <AgentQuitFromTouchingBlockType> <Block type="redstone_block" /> </AgentQuitFromTouchingBlockType> </AgentHandlers> </AgentSection> </Mission>''' sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately agent_host = MalmoPython.AgentHost() agent_host.addOptionalIntArgument( "role,r", "For multi-agent missions, the role of this agent instance", 0) try: agent_host.parse(sys.argv) except RuntimeError as e: print 'ERROR:', e print agent_host.getUsage() exit(1) if agent_host.receivedArgument("help"): print agent_host.getUsage() exit(0) role = agent_host.getIntArgument("role") print "Will run as role", role
tau = t - self.n + 1 # self.update_q_table(tau, s, next_a, current_r, T) self.update_q_table(tau, s, next_a, current_r, T) print( "\n-------------------------------------- END ITERATION --------------------------------------\n" ) self.observation_space = ['stone'] * num_doors if __name__ == '__main__': print('Starting...', flush=True) expected_reward = 3390 my_client_pool = MalmoPython.ClientPool() my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10000)) agent_host = MalmoPython.AgentHost() try: agent_host.parse(sys.argv) except RuntimeError as e: print('ERROR:', e) print(agent_host.getUsage()) exit(1) if agent_host.receivedArgument("help"): print(agent_host.getUsage()) exit(0) retry = 0 epocs = 1000 monty = Monty()
def __init__(self): '''Initializes the class.''' self.agent_host = MalmoPython.AgentHost() self.root = Tk() self.root.wm_title("Human Action Component")
human_agent_host.parse(sys.argv) except RuntimeError as e: print('ERROR:', e) print(human_agent_host.getUsage()) exit(1) if human_agent_host.receivedArgument("help"): print(human_agent_host.getUsage()) exit(0) my_role = human_agent_host.getIntArgument("role") xml_filename = human_agent_host.getStringArgument("mission_xml") if not xml_filename == "": # load the mission from the specified XML file my_mission = MalmoPython.MissionSpec(open(xml_filename).read(), True) my_mission.requestVideo(640, 480) my_mission.timeLimitInSeconds(300) my_mission_record = MalmoPython.MissionRecordSpec( './hac_saved_mission.tgz') my_mission_record.recordCommands() my_mission_record.recordMP4(20, 400000) my_mission_record.recordRewards() my_mission_record.recordObservations() human_agent_host.runMission(my_mission, my_mission_record, role=my_role) else: # create some sample missions
def load_mission_xml(self, mission_xml): self.mission_spec = MalmoPython.MissionSpec(mission_xml, True) logger.info("Loaded mission: " + self.mission_spec.getSummary())
<MissionQuitCommands/> <RewardForCollectingItem> <Item type="emerald" reward="1"/> </RewardForCollectingItem> </AgentHandlers> </AgentSection> </Mission>''' if sys.version_info[0] == 2: sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately else: import functools print = functools.partial(print, flush=True) agent_host = MalmoPython.AgentHost() agent_host.addOptionalStringArgument("savesDir,s", "Location of Minecraft saves folder", "") try: agent_host.parse(sys.argv) except RuntimeError as e: print('ERROR:', e) print(agent_host.getUsage()) exit(1) if agent_host.receivedArgument("help"): print(agent_host.getUsage()) exit(0) saveFolders = [] savesDir = agent_host.getStringArgument("savesDir")
def init(self, client_pool=None, start_minecraft=None, continuous_discrete=True, add_noop_command=None, max_retries=90, retry_sleep=10, step_sleep=0.001, skip_steps=0, videoResolution=None, videoWithDepth=None, observeRecentCommands=None, observeHotBar=None, observeFullInventory=None, observeGrid=None, observeDistance=None, observeChat=None, allowContinuousMovement=None, allowDiscreteMovement=None, allowAbsoluteMovement=None, recordDestination=None, recordObservations=None, recordRewards=None, recordCommands=None, recordMP4=None, gameMode=None, forceWorldReset=None, image_obs=False): self.max_retries = max_retries self.retry_sleep = retry_sleep self.step_sleep = step_sleep self.skip_steps = skip_steps self.forceWorldReset = forceWorldReset self.continuous_discrete = continuous_discrete self.add_noop_command = add_noop_command self.image_obs = image_obs if videoResolution: if videoWithDepth: self.mission_spec.requestVideoWithDepth(*videoResolution) else: self.mission_spec.requestVideo(*videoResolution) if observeRecentCommands: self.mission_spec.observeRecentCommands() if observeHotBar: self.mission_spec.observeHotBar() if observeFullInventory: self.mission_spec.observeFullInventory() if observeGrid: self.mission_spec.observeGrid(*(observeGrid + ["grid"])) if observeDistance: self.mission_spec.observeDistance(*(observeDistance + ["dist"])) if observeChat: self.mission_spec.observeChat() if allowContinuousMovement or allowDiscreteMovement or allowAbsoluteMovement: # if there are any parameters, remove current command handlers first self.mission_spec.removeAllCommandHandlers() if allowContinuousMovement is True: self.mission_spec.allowAllContinuousMovementCommands() elif isinstance(allowContinuousMovement, list): for cmd in allowContinuousMovement: self.mission_spec.allowContinuousMovementCommand(cmd) if allowDiscreteMovement is True: self.mission_spec.allowAllDiscreteMovementCommands() elif isinstance(allowDiscreteMovement, list): for cmd in allowDiscreteMovement: self.mission_spec.allowDiscreteMovementCommand(cmd) if allowAbsoluteMovement is True: self.mission_spec.allowAllAbsoluteMovementCommands() elif isinstance(allowAbsoluteMovement, list): for cmd in allowAbsoluteMovement: self.mission_spec.allowAbsoluteMovementCommand(cmd) if start_minecraft: # start Minecraft process assigning port dynamically self.mc_process, port = minecraft_py.start() logger.info( "Started Minecraft on port %d, overriding client_pool.", port) client_pool = [('127.0.0.1', port)] if client_pool: if not isinstance(client_pool, list): raise ValueError( "client_pool must be list of tuples of (IP-address, port)") self.client_pool = MalmoPython.ClientPool() for client in client_pool: self.client_pool.add(MalmoPython.ClientInfo(*client)) # TODO: produce observation space dynamically based on requested features self.video_height = self.mission_spec.getVideoHeight(0) self.video_width = self.mission_spec.getVideoWidth(0) self.video_depth = self.mission_spec.getVideoChannels(0) #self.observation_space = spaces.Box(low=0, high=255, # shape=(self.video_height, self.video_width, self.video_depth)) self.observation_space = Dict([ ('observation', self.obs_space), ('desired_goal', self.goal_space), ('achieved_goal', self.achieved_goal_space), ('state_observation', self.obs_space), ('state_desired_goal', self.goal_space), ('state_achieved_goal', self.achieved_goal_space), ]) # dummy image just for the first observation self.last_image = np.zeros( (self.video_height, self.video_width, self.video_depth), dtype=np.uint8) self._create_action_space() # mission recording self.mission_record_spec = MalmoPython.MissionRecordSpec( ) # record nothing if recordDestination: self.mission_record_spec.setDestination(recordDestination) if recordRewards: self.mission_record_spec.recordRewards() if recordCommands: self.mission_record_spec.recordCommands() if recordMP4: self.mission_record_spec.recordMP4(*recordMP4) if gameMode: if gameMode == "spectator": self.mission_spec.setModeToSpectator() elif gameMode == "creative": self.mission_spec.setModeToCreative() elif gameMode == "survival": logger.warn( "Cannot force survival mode, assuming it is the default.") else: assert False, "Unknown game mode: " + gameMode
<Height>''' + str(video_height) + '''</Height> </ColourMapProducer> <ContinuousMovementCommands turnSpeedDegs="180" /> </AgentHandlers> </AgentSection> </Mission>''' if sys.version_info[0] == 2: sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately else: import functools print = functools.partial(print, flush=True) agent_host = MalmoPython.AgentHost() try: agent_host.parse(sys.argv) except RuntimeError as e: print('ERROR:', e) print(agent_host.getUsage()) exit(1) if agent_host.receivedArgument("help"): print(agent_host.getUsage()) exit(0) agent_host.setVideoPolicy(MalmoPython.VideoPolicy.LATEST_FRAME_ONLY) if agent_host.receivedArgument("test"): num_reps = 1 else:
</VideoProducer> <DiscreteMovementCommands /> <ChatCommands /> <RewardForSendingMatchingChatMessage> <ChatMatch reward="100.0" regex="''' + items[ obj_id] + '''" description="Anything that matches the object."/> </RewardForSendingMatchingChatMessage> <RewardForSendingCommand reward="-1"/> </AgentHandlers> </AgentSection> </Mission> ''' # Create default Malmo objects: agent_host = MalmoPython.AgentHost() try: agent_host.parse(sys.argv) except RuntimeError as e: print('ERROR:', e) print(agent_host.getUsage()) exit(1) if agent_host.receivedArgument("help"): print(agent_host.getUsage()) exit(0) my_mission = MalmoPython.MissionSpec(mission_xml, True) my_mission_record = MalmoPython.MissionRecordSpec("chat_reward.tgz") # Attempt to start a mission: max_retries = 3
</AgentHandlers> </AgentSection> </Mission>''' recordingsDirectory="MovingTargetRecordings" try: os.makedirs(recordingsDirectory) except OSError as exception: if exception.errno != errno.EEXIST: # ignore error if already existed raise sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately validate = True my_client_pool = MalmoPython.ClientPool() my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10000)) agent_host = MalmoPython.AgentHost() try: agent_host.parse( sys.argv ) except RuntimeError as e: print 'ERROR:',e print agent_host.getUsage() exit(1) if agent_host.receivedArgument("help"): print agent_host.getUsage() exit(0) if agent_host.receivedArgument("test"): num_reps = 1
import MalmoPython import time __author__ = "Chang Gao" __copyright__ = "Copyright (c) 2017 Malmactor" __license__ = "MIT" import src.movement from src.SuperMarioBros import * layout = layout_fromdefault() missionXML = layout_toxml( layout, {"template_path": "SuperMarioBros/mission_template.xml"}) # start mission agent_host = MalmoPython.AgentHost() agent_host.addOptionalIntArgument("speed,s", "Length of tick, in ms.", 1) my_mission = MalmoPython.MissionSpec(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.allowAllAbsoluteMovementCommands() my_mission.allowAllDiscreteMovementCommands() agent_host.startMission(my_mission, my_mission_record) # Loop until mission starts: world_state = agent_host.getWorldState() while not world_state.has_mission_begun: time.sleep(0.1) world_state = agent_host.getWorldState() for error in world_state.errors: print "Error:", error.text
def main(): global NUM_REPS, N, ALPHA, GAMMA, MSE, agent sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately debug = True if len( sys.argv) > 1 and sys.argv[1] == "debug=True" else False # Create default Malmo objects: agent_host = MalmoPython.AgentHost() try: agent_host.parse(sys.argv) except RuntimeError as e: print 'ERROR(Runtime):', e, "\n", agent_host.getUsage() exit(1) if agent_host.receivedArgument("help"): print agent_host.getUsage() exit(0) # Set the size of the matrix x, y = 21, 21 map_gen = environment_generator() visual = visualization(x, y, debug) for i in range(NUM_REPS): print "Survival Mode # " + str(i + 1) ws_interpre = world_state_interpreter(x, y) action_available = action(agent_host) # Attempt to start a mission: max_retries = 3 for retry in range(max_retries): try: missionXML = map_gen.generator(False) if i == 0: missionXML = map_gen.generator(True) my_mission = MalmoPython.MissionSpec(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() agent_host.startMission(my_mission, my_mission_record) time.sleep(3) break except RuntimeError as e: if retry == max_retries - 1: print "Error starting mission:", e exit(1) else: time.sleep(2) # Loop until mission starts: print "Waiting for the mission to start ", world_state = agent_host.getWorldState() while not world_state.has_mission_begun: sys.stdout.write(".") time.sleep(0.1) world_state = agent_host.getWorldState() for error in world_state.errors: print "Error:", error.text print print "Mission running " show_best = False if (i + 1) % 5 != 0 else True # Loop until mission ends: while world_state.is_mission_running: time.sleep(0.2) matrix = None ws_interpre.input(world_state) ent_matrix = ws_interpre.entities_to_matrix() env_matrix = ws_interpre.grid_matrix() if ent_matrix and env_matrix: visual.get_entities(ent_matrix) visual.get_environment(env_matrix) visual.draw() matrix = visual.get_matrix() if matrix: agent.run(agent_host, matrix, False) #time.sleep(0.1) # Use it for testing purpose world_state = agent_host.getWorldState() for error in world_state.errors: print "Error:", error.text MSE.append(agent.calculate_mse() * 100) agent.reset_mse() agent.replay(128) print print "Mission ended" with open('Weights.txt', 'wb') as f: pickle.dump(agent.get_weights(), f) # Mission has ended. time.sleep(1) if debug: visual.quit() plt.plot(np.arange(len(MSE)), MSE, 'o') m, b = np.polyfit(np.arange(len(MSE)), MSE, deg=1) plt.plot(np.arange(len(MSE)), m * np.arange(len(MSE)) + b, '-') plt.show()
canvas = tk.Canvas(root, width=Constants.CANVAS_WIDTH, height=Constants.CANVAS_HEIGHT, borderwidth=0, highlightthickness=0, bg="white") canvas.pack() root.update() validate = True # Create a pool of Minecraft Mod clients. # By default, mods will choose consecutive mission control ports, starting at 10000, # so running four mods locally should produce the following pool by default (assuming nothing else # is using these ports): my_client_pool = MalmoPython.ClientPool() my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10000)) my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10001)) my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10002)) my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10003)) Constants.agent_host = MalmoPython.AgentHost() try: Constants.agent_host.parse(sys.argv) except RuntimeError as e: print 'ERROR:', e print Constants.agent_host.getUsage() exit(1) if Constants.agent_host.receivedArgument("help"): print Constants.agent_host.getUsage() exit(0)
<Grid name="column"> <min x="0" y="-256" z="0" /> <max x="0" y="256" z="0" /> </Grid> </ObservationFromGrid> <VideoProducer want_depth="true"> <Width>''' + str(video_width) + '''</Width> <Height>''' + str(video_height) + '''</Height> </VideoProducer> <AbsoluteMovementCommands /> </AgentHandlers> </AgentSection> </Mission>''' validate = True my_mission = MalmoPython.MissionSpec( missionXML, validate ) agent_host = MalmoPython.AgentHost() try: agent_host.parse( sys.argv ) except RuntimeError as e: print 'ERROR:',e print agent_host.getUsage() exit(1) if agent_host.receivedArgument("help"): print agent_host.getUsage() exit(0) agent_host.setObservationsPolicy(MalmoPython.ObservationsPolicy.LATEST_OBSERVATION_ONLY) agent_host.setVideoPolicy(MalmoPython.VideoPolicy.LATEST_FRAME_ONLY)
def print_mission_spec(): my_mission = MalmoPython.MissionSpec() print(my_mission.getAsXML(True))
# A sample that demonstrates a two-agent mission with discrete actions to dig and place blocks import MalmoPython import json import logging import math import os import random import sys import time sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately # -- set up two agent hosts -- agent_host1 = MalmoPython.AgentHost() agent_host2 = MalmoPython.AgentHost() try: agent_host1.parse( sys.argv ) except RuntimeError as e: print 'ERROR:',e print agent_host1.getUsage() exit(1) if agent_host1.receivedArgument("help"): print agent_host1.getUsage() exit(0) # -- set up the mission -- xml = '''<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
def visualize(): # Create default Malmo objects: agent_host = MalmoPython.AgentHost() try: agent_host.parse(sys.argv) except RuntimeError as e: print('ERROR:', e) print(agent_host.getUsage()) exit(1) if agent_host.receivedArgument("help"): print(agent_host.getUsage()) exit(0) if agent_host.receivedArgument("test"): num_repeats = 1 else: num_repeats = 10 for i in range(len(MISSION_FILES)): # Attempt to start a mission: my_mission = MalmoPython.MissionSpec( open(MISSION_FILES[i + 3], 'r').read(), False) """planner = Agent(MISSION_START, MISSION_GOALS[i]) planner.create_world((MISSION_LOWER_BOUNDS[i][0], MISSION_UPPER_BOUNDS[i][0]), (MISSION_LOWER_BOUNDS[i][1], MISSION_UPPER_BOUNDS[i][1]), (MISSION_LOWER_BOUNDS[i][2], MISSION_UPPER_BOUNDS[i][2]), MISSION_TEXT_FILES[i]) path = planner.astar()""" if BOOL_DEBUG_PATHS: w = dict() for point in planner.world.walkable: if point[1] in w.keys(): w[point[1]].add((point[0], point[2])) else: w[point[1]] = set() w[point[1]].add([point[0], point[2]]) print(path) # Comment this out when actually testing with the Malmo platform #continue max_retries = 3 for retry in range(max_retries): try: agent_host.startMission(my_mission, MalmoPython.MissionRecordSpec()) break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission:", e) exit(1) else: time.sleep(2) # Loop until mission starts: print("Waiting for the mission to start ", ) world_state = agent_host.getWorldState() while not world_state.has_mission_begun: sys.stdout.write(".") time.sleep(.5) world_state = agent_host.getWorldState() for error in world_state.errors: print("Error:", error.text) print() print("Mission running ", ) # Loop until mission ends: while world_state.is_mission_running: sys.stdout.write(".") time.sleep(1.0) """world_state = agent_host.getWorldState() if path: position = path.pop() agent_host.sendCommand("tp {0} {1} {2}".format(*position))""" print() print("Mission ended")
outline=color_string, fill=color_string) if curr_x is not None and curr_y is not None: self.canvas.create_oval( (world_x - 1 - curr_x + 0.5 - curr_radius) * scale, (world_y - 1 - curr_y + 0.5 - curr_radius) * scale, (world_x - 1 - curr_x + 0.5 + curr_radius) * scale, (world_y - 1 - curr_y + 0.5 + curr_radius) * scale, outline="#fff", fill="#fff") self.root.update() agent_host = MalmoPython.AgentHost() # Find the default mission file by looking next to the schemas folder: schema_dir = None try: schema_dir = os.environ['MALMO_XSD_PATH'] except KeyError: print("MALMO_XSD_PATH not set? Check environment.") exit(1) mission_file = './random_map_3.xml' print('') print(os.path) # add some args agent_host.addOptionalStringArgument( 'mission_file', 'Path/to/file from which to load the mission.',
<Item reward="10" type="dirt"/> </RewardForCollectingItem> <RewardForDiscardingItem> <Item reward="100" type="dirt"/> </RewardForDiscardingItem> </AgentHandlers> </AgentSection> </Mission>''' my_mission = MalmoPython.MissionSpec(xml,True) client_pool = MalmoPython.ClientPool() client_pool.add( MalmoPython.ClientInfo('127.0.0.1',10000) ) client_pool.add( MalmoPython.ClientInfo('127.0.0.1',10001) ) MalmoPython.setLogging("", MalmoPython.LoggingSeverityLevel.LOG_OFF) def safeStartMission(agent_host, mission, client_pool, recording, role, experimentId): used_attempts = 0 max_attempts = 5 print("Calling startMission for role", role) while True: try: agent_host.startMission(mission, client_pool, recording, role, experimentId) break except MalmoPython.MissionException as e: errorCode = e.details.errorCode if errorCode == MalmoPython.MissionErrorCode.MISSION_SERVER_WARMING_UP: print("Server not quite ready yet - waiting...") time.sleep(2) elif errorCode == MalmoPython.MissionErrorCode.MISSION_INSUFFICIENT_CLIENTS_AVAILABLE:
import time import json import random import math import maze_gen2 from collections import defaultdict DEFAULT_SIZE = 4 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately # Create default Malmo objects: agent_host = MalmoPython.AgentHost() try: agent_host.parse(sys.argv) except RuntimeError as e: print 'ERROR:', e print agent_host.getUsage() exit(1) if agent_host.receivedArgument("help"): print agent_host.getUsage() exit(0) #loading mission file mission_file = "sarsaattempt1.xml" with open(mission_file, 'r') as f: print "Loading mission from %s" % mission_file missionXML = f.read()