def init_malmo(self): """ Initialize new malmo mission. """ my_mission = MalmoPython.MissionSpec(self.GetMissionXML(), True) my_mission.forceWorldReset() #so it doesn't collect any past objects my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(1) self.num_entities = self.num_entities_copy max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo( '127.0.0.1', 10000)) # add Minecraft machines here as available for retry in range(max_retries): try: self.agent_host.startMission(my_mission, my_clients, my_mission_record, 0, "ZombieKiller") break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission:", e) exit(1) else: time.sleep(2) self.cobblestone_wall = 0 return self.agent_host
def create_mission(ind,agent_host,start, dropoff, pickup): my_mission = MalmoPython.MissionSpec(GetMissionXML(start), True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(1) # Attempt to start a mission: max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo('127.0.0.1', 10000)) # add Minecraft machines here as available my_mission.drawBlock(int(pickup[0]), 1, int(pickup[1]), "redstone_block") my_mission.drawBlock(int(dropoff[0]), 1, int(dropoff[1]), "diamond_block") for retry in range(max_retries): try: agent_host.startMission( my_mission, my_clients, my_mission_record, 0, "%s-%d" % ('Herobrine', ind) ) break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission", ":",e) exit(1) else: time.sleep(2) world_state = agent_host.peekWorldState() while not world_state.has_mission_begun: time.sleep(0.1) world_state = agent_host.peekWorldState() for error in world_state.errors: print("Error:",error.text) time.sleep(1) return my_mission, world_state
def init_malmo(agent_host): """ Initialize new malmo mission. """ my_mission = MalmoPython.MissionSpec(GetMissionXML(), True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(1200, 700) my_mission.setViewpoint(0) max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo( '127.0.0.1', 10000)) # add Minecraft machines here as available for retry in range(max_retries): try: agent_host.startMission(my_mission, my_clients, my_mission_record, 0, "Dropper") break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission:", e) exit(1) else: time.sleep(2) return agent_host
def init_malmo(self): """ Initialize new malmo mission. """ my_mission = MalmoPython.MissionSpec(self.get_mission_xml(), True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(1) max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo( '127.0.0.1', 10000)) # add Minecraft machines here as available for retry in range(max_retries): try: self.agent_host.startMission(my_mission, my_clients, my_mission_record, 0, 'DiamondCollector') break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission:", e) exit(1) else: time.sleep(2) world_state = self.agent_host.getWorldState() while not world_state.has_mission_begun: time.sleep(0.1) world_state = self.agent_host.getWorldState() for error in world_state.errors: print("\nError:", error.text) return world_state
def init_malmo(agent_host): my_mission = MalmoPython.MissionSpec( GetMissionXML(Hyperparameters.SIZE, Hyperparameters.OBS_SIZE, Hyperparameters.MAX_EPISODE_STEPS), True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(1) max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo( '127.0.0.1', 10000)) # add Minecraft machines here as available my_clients.add(MalmoPython.ClientInfo('127.0.0.1', 10001)) for retry in range(max_retries): try: agent_host.startMission(my_mission, my_clients, my_mission_record, 0, "MineGuyz") break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission:", e) exit(1) else: time.sleep(2) return agent_host
def record_malmo_video(self, instructions, xml_file, seed): ''' Replays a set of instructions through Malmo using two players. The first player will navigate the specified mission based on the given instructions. The second player observes the first player's moves, which is captured in a video. ''' if not self.is_malmo_initialized: raise Exception('Malmo not initialized. Call init_malmo() first.') # Set up the mission my_mission = MalmoPython.MissionSpec(self._get_xml(xml_file, seed), True) bot_recording_spec = MalmoPython.MissionRecordSpec() camera_recording_spec = MalmoPython.MissionRecordSpec() recordingsDirectory = \ malmoutils.get_recordings_directory(self.agent_host_bot) if recordingsDirectory: camera_recording_spec.setDestination(recordingsDirectory + "//rollout_" + str(seed) + ".tgz") camera_recording_spec.recordMP4(MalmoPython.FrameType.VIDEO, 36, 2000000, False) # Start the agents self._start_mission(self.agent_host_bot, my_mission, bot_recording_spec, 0) self._start_mission(self.agent_host_camera, my_mission, camera_recording_spec, 1) self._wait_for_start([self.agent_host_camera, self.agent_host_bot]) # Teleport the camera agent to the required position self.agent_host_camera.sendCommand('tp -29 72 -6.7') instruction_index = 0 while self._is_mission_running(): command = instructions[instruction_index] instruction_index += 1 self.agent_host_bot.sendCommand(command) # Pause for half a second - change this for faster/slower videos time.sleep(0.5) if instruction_index == len(instructions): self.agent_host_bot.sendCommand("jump 1") time.sleep(2) self.agent_host_bot.sendCommand("quit") # Wait a little for Malmo to reset before the # next mission is started time.sleep(2) print("Video recorded.")
def __init__(self, missionXML, validate, setup_mission=None): super() self.agent_host = MalmoPython.AgentHost() try: self.agent_host.parse(sys.argv) except RuntimeError as e: print('ERROR:', e) print(self.agent_host.getUsage()) exit(1) if self.agent_host.receivedArgument("help"): print(self.agent_host.getUsage()) exit(0) self.mission = MalmoPython.MissionSpec(missionXML, validate) self.mission_record = MalmoPython.MissionRecordSpec() if (setup_mission is not None): setup_mission(self.mission)
def init_malmo(agent_host, recordingsDirectory, video_width, video_height): my_mission = MalmoPython.MissionSpec( GetMissionXML(Hyperparameters.SIZE, Hyperparameters.OBS_SIZE, Hyperparameters.MAX_EPISODE_STEPS, video_width, video_height), True) my_mission.setViewpoint(0) agent_host.setObservationsPolicy( MalmoPython.ObservationsPolicy.LATEST_OBSERVATION_ONLY) agent_host.setVideoPolicy(MalmoPython.VideoPolicy.LATEST_FRAME_ONLY) 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) if recordingsDirectory: my_mission_record.setDestination(recordingsDirectory + "//" + "Mission_" + str(test + 1) + ".tgz") max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo( '127.0.0.1', 10000)) # add Minecraft machines here as available my_clients.add(MalmoPython.ClientInfo('127.0.0.1', 10001)) for retry in range(max_retries): try: agent_host.startMission(my_mission, my_clients, my_mission_record, 0, "MineGuyz") break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission:", e) exit(1) else: time.sleep(2) return agent_host
def __init__( self, missionXML, validate, setup_mission=None, ip="127.0.0.1", port=10000 ): super() self.agent_host = MalmoPython.AgentHost() self.clientPool = MalmoPython.ClientPool() self.clientPool.add(MalmoPython.ClientInfo(ip, port)) try: self.agent_host.parse(sys.argv) except RuntimeError as e: print("ERROR:", e) print(self.agent_host.getUsage()) exit(1) if self.agent_host.receivedArgument("help"): print(self.agent_host.getUsage()) exit(0) self.mission = MalmoPython.MissionSpec(missionXML, validate) self.mission_record = MalmoPython.MissionRecordSpec() if setup_mission is not None: setup_mission(self.mission)
def create_mission(ind, agent_host): my_mission = MalmoPython.MissionSpec(GetMissionXML(), True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(1) # Attempt to start a mission: max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo( '127.0.0.1', 10000)) # add Minecraft machines here as available for i in range(len(legalPosList)): if i == itemPosId: my_mission.drawBlock(legalPosList[itemPosId][0], 1, legalPosList[itemPosId][1], "emerald_block") elif i == destPosId: my_mission.drawBlock(legalPosList[destPosId][0], 1, legalPosList[destPosId][1], "diamond_block") else: my_mission.drawBlock(legalPosList[i][0], 1, legalPosList[i][1], "stone") for retry in range(max_retries): try: agent_host.startMission(my_mission, my_clients, my_mission_record, 0, "%s-%d" % ('Herobrine', ind)) break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission", ":", e) exit(1) else: time.sleep(2) world_state = agent_host.peekWorldState() while not world_state.has_mission_begun: time.sleep(0.1) world_state = agent_host.peekWorldState() for error in world_state.errors: print("Error:", error.text) time.sleep(1) return my_mission
def reset_world(agent_host, mission_xml_path, my_clients, agentID, expID, logger): reseted = False while not reseted: my_mission_record = MalmoPython.MissionRecordSpec() with open(mission_xml_path, 'r') as f: logger.info("Loading mission from %s" % mission_xml_path) mission_xml = f.read() my_mission = MalmoPython.MissionSpec(mission_xml, True) my_mission.removeAllCommandHandlers() my_mission.allowAllDiscreteMovementCommands() my_mission.setViewpoint(2) for retry in range(MAX_RETRIES): try: agent_host.startMission(my_mission, my_clients, my_mission_record, agentID, "%s" % (expID)) break except RuntimeError as e: if retry == MAX_RETRIES - 1: logger.info(f"Error starting mission: {e}") exit(1) else: time.sleep(2.5) world_state = agent_host.getWorldState() while not world_state.has_mission_begun: time.sleep(0.1) world_state = agent_host.getWorldState() agent_host.sendCommand("look -1") agent_host.sendCommand("look -1") while world_state.is_mission_running and all( e.text == '{}' for e in world_state.observations): world_state = agent_host.peekWorldState() reseted = len(world_state.observations) > 0 return world_state
def main(): # Start mission # Create default Malmo objects: global agent_host global matrix2dOriginal global maze_map global actionHistCounter 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) # The following one line is for setting how many times you want the agent to repeat num_repeats = 50 esFile = open("Eval_Stats.txt", "w+") esFile.write("\n") esFile.close() esFile = open("Eval_Stats.txt", "a") trFile = open("training_result.txt", "w+") trFile.write("\n") trFile.close() trFile = open("training_result.txt", "a") for i in range(num_repeats): esFile.write("Run #" + str(i + 1) + "\n") actionHistCounter = i + 1 # size = int(6 + 0.5*i) print("Size of maze:", size_of_maze) #my_mission = MalmoPython.MissionSpec(get_mission_xml("0", 0.4 + float(i/20.0), size_of_maze, 0), True) randomDif = random.uniform(-0.2, 0.2) print("Parameters of the mission:", str(i), "next:", 0.4 + randomDif, "size:", size_of_maze) my_mission = MalmoPython.MissionSpec( get_mission_xml(str(i), 0.4 + randomDif, size_of_maze, 0), True) # my_mission = MalmoPython.MissionSpec(get_mission_xml(), True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(1) # Attempt to start a mission: max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo( '127.0.0.1', 10000)) # add Minecraft machines here as available for retry in range(max_retries): try: agent_host.startMission(my_mission, my_clients, my_mission_record, 0, "%s-%d" % ('Moshe', i)) break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission", (i + 1), ":", e) exit(1) else: time.sleep(2) # Loop until mission starts: print( "Waiting for the mission", (i + 1), "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", (i + 1), "running.") grid = load_grid(world_state, agent_host) # print("World State Grid:", grid) print("Size of actual map:", len(grid)) maze_map = get_maze_map(grid) print("maze map:", len(maze_map)) #print(maze_map[244]) #The maze construction matrix2dOriginal = maze_to_2dMatrix(maze_map, size_of_maze) matrix2d = maze_to_2dMatrix_reversed(maze_map, size_of_maze) print("the matrix 2d: ", matrix2d) matrixArray = matrix2d.flatten() start_and_end_positions_in_actual_map = find_start_end(grid) print("size of maze map:", len(maze_map)) print("first position in actual map:", first_block_index_in_actual_map) print("last position in actual map:", last_block_index_in_actual_map) global agent_current_position_xy_in_maze, agent_current_position_index_in_grid agent_current_position_xy_in_maze = get_xy_position_of_maze_map_by_position_of_actual_map(\ start_and_end_positions_in_actual_map[0], \ grid) print("Started: agent current position(xy in maze):", agent_current_position_xy_in_maze) agent_current_position_index_in_grid = get_position_of_actual_map_by_xy_position_of_maze_map(\ agent_current_position_xy_in_maze, grid) print("Started: agent current position(index in grid):", agent_current_position_index_in_grid \ , "compared with real position:", start_and_end_positions_in_actual_map[0]) index_of_yaw = my_mission.getAsXML(True).index("yaw") yaw_of_agent = int( re.compile("(\d+)").match( my_mission.getAsXML(True)[index_of_yaw + 5:index_of_yaw + 8]).group(1)) sync_agent_direction_with_yaw(yaw_of_agent) print("Started: agent current yaw(face to where):", yaw_of_agent) # go_to_goal_and_finish_mission(grid, start_and_end_positions_in_actual_map[0], \ # start_and_end_positions_in_actual_map[1], world_state, agent_host, i) print("Started: How many walkable blocks in front of agent's direction:", agent_current_direction, "is walk able? Answer:", \ get_num_of_walkable_blocks_in_front_of_agent(agent_current_position_xy_in_maze, size_of_maze, grid)) # test_moving(agent_host, [3, 3, 0, 3, 3, 0, 3]) positionTransition(grid, matrixArray, yaw_of_agent, size_of_maze) trainingStart = time.time() trainingProcess = Process(target=missionTrainingStart, args=(actionHistCounter, )) trainingProcess.start() stringList = [] is_complete_action_history = False curr_action_counter = 0 while True: if not is_complete_action_history: actionHistFile = None while True: try: actionHistFile = open( "action_history_" + str(actionHistCounter) + "_.txt", "r") stringList = actionHistFile.readlines() if len(stringList) != 0: break except: continue #actionHistFile = open("action_history_"+str(actionHistCounter)+"_.txt", "r") #stringList = actionHistFile.readlines() print("Reading action history file, get string: ", stringList) curr_action_list = stringList[0].split(' ') actionHistFile.close() print("Here is the list length:", len(curr_action_list), curr_action_counter + 1) try: if (len(curr_action_list) >= curr_action_counter + 1): action = curr_action_list[curr_action_counter] convertAction = directionConvert(int(action[0])) test_moving(agent_host, [convertAction], grid) curr_action_counter += 1 except ValueError: # The last index of action is a newline character break if (stringList[len(stringList) - 1] == "END"): is_complete_action_history = True if (is_complete_action_history and len(curr_action_list) == curr_action_counter - 1): break trainingProcess.join() trainingEnd = time.time() trainingElapsed = trainingEnd - trainingStart esFile.write("Training Time: " + str(trainingElapsed) + " ") #actionHistFile.close() ''' print(stringList) actionCollection = [] positionCollection = [] for n in range(0, len(stringList)-1): tmp = stringList[n].split(' ') for m in range(0,len(tmp)-1): L = tmp[m].split(',') actionCollection.append(L[0]) positionCollection.append([L[1],L[2]]) print('The original: ',actionCollection) print(positionCollection) ''' """ del stringList[-1] for string in stringList: actionCollection = string.split(' ') del actionCollection[-1] for aindex in range(len(actionCollection)): converted = directionConvert(int(actionCollection[aindex])) actionCollection[aindex] = converted actionList.append(actionCollection) """ """ for testingset in actionList: #check if it's reachable test_moving(agent_host, testingset) """ ''' actionList = [] for index in range(len(actionCollection)): row,col = positionCollection[index][0], positionCollection[index][1] action = actionCollection[index] print(matrix2d[int(row)][int(col)]) if matrix2d[int(row)][int(col)] == 0: convertAction = directionConvert(int(action)) actionList.append(convertAction) #print('THIS IS THE ACTION: ',len(actionList), actionList) print('The list:', actionList) #raise('STOP HERE') test_moving(agent_host, actionList, grid) ''' print( "Training complete. Training result can be found in training_result.txt." ) travelStart = time.time() go_to_goal_and_finish_mission(grid, agent_current_position_index_in_grid, \ start_and_end_positions_in_actual_map[1], world_state, agent_host, i) travelEnd = time.time() travelElapsed = travelEnd - travelStart esFile.write("Agent Travel Time: " + str(travelElapsed) + "\n\n") print("Aiku did it!") trFile.close() esFile.close()
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) # -- set up the mission -- # mission_file = './find_the_goal.xml' with open(mission_file, 'r') as f: print("Loading mission from %s" % mission_file) mission_xml = f.read() my_mission = MalmoPython.MissionSpec(mission_xml, True) # Python code alterations to the environment # my_mission.drawBlock(0, 110, 0, "stone") my_mission_record = MalmoPython.MissionRecordSpec() # Attempt to start a mission: max_retries = 3 try: path = "./Models/Torch/my_model.pth" brain.load_model(path) print("Model loaded from path:", path) except Exception: print('Could not load model, continue with random initialision (y/n):')
# Set up a client pool. # IMPORTANT: If ANY of the clients will be on a different machine, then you MUST # make sure that any client which can be the server has an IP address that is # reachable from other machines - ie DO NOT SIMPLY USE 127.0.0.1!!!! # The IP address used in the client pool will be broadcast to other agents who # are attempting to find the server - so this will fail for any agents on a # different machine. client_pool = MalmoPython.ClientPool() for x in range(10000, 10000 + NUM_AGENTS + 1): client_pool.add(MalmoPython.ClientInfo('127.0.0.1', x)) num_missions = 5 if INTEGRATION_TEST_MODE else 30000 for mission_no in range(1, num_missions + 1): print("Running mission #" + str(mission_no)) # Create mission xml - use forcereset if this is the first mission. my_mission = MalmoPython.MissionSpec( getXML("true" if mission_no == 1 else "false"), True) # Generate an experiment ID for this mission. # This is used to make sure the right clients join the right servers - # if the experiment IDs don't match, the startMission request will be rejected. # In practice, if the client pool is only being used by one researcher, there # should be little danger of clients joining the wrong experiments, so a static # ID would probably suffice, though changing the ID on each mission also catches # potential problems with clients and servers getting out of step. # Note that, in this sample, the same process is responsible for all calls to startMission, # so passing the experiment ID like this is a simple matter. If the agentHosts are distributed # across different threads, processes, or machines, a different approach will be required. # (Eg generate the IDs procedurally, in a way that is guaranteed to produce the same results # for each agentHost independently.) experimentID = str(uuid.uuid4())
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 = 1 for i in range(num_repeats): my_mission = MalmoPython.MissionSpec(GetMissionXML(), True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(1) # Attempt to start a mission: max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo('127.0.0.1', 10000)) # add Minecraft machines here as available for retry in range(max_retries): try: agent_host.startMission( my_mission, my_clients, my_mission_record, 0, "%s-%d" % ('Moshe', i) ) break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission", (i+1), ":",e)
if command == 5: return -1 if __name__ == '__main__': # Create default Malmo objects: agent_host = MalmoPython.AgentHost() #scout_ai = ScoutAI(agent_host) scout_ai = MalmoPython.AgentHost() malmoutils.parse_command_line(agent_host) commandQueue = CommandQueue() prev_command = 0 counter = 0 my_mission = MalmoPython.MissionSpec(buildEnvironment(), True) my_mission.allowAllChatCommands() client_pool = MalmoPython.ClientPool() client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10000)) # client_pool.add(MalmoPython.ClientInfo( "127.0.0.1", 10001) ) agent_host_record = MalmoPython.MissionRecordSpec() # scout_record = MalmoPython.MissionRecordSpec() safeStartMission(agent_host, my_mission, client_pool, agent_host_record, 0, '') # safeStartMission(scout_ai, my_mission, client_pool, scout_record,1,'') print("start mission success") safeWaitForStart([agent_host]) # safeWaitForStart([agent_host,scout_ai])
def load_mission_xml(self, mission_xml): """ load mission file into game """ self.mission_spec = MalmoPython.MissionSpec(mission_xml, True) logger.info("Loaded mission: " + self.mission_spec.getSummary())
hori_step_size = 0.5 #Load model from file data_set = FileIO.get_data_set() shoot_agent = MalmoAgent("Slayer", agents[0], 0, 0, vert_step_size, hori_step_size, data_set) move_agent = MalmoAgent("Mover", agents[1], 0, 0, vert_step_size, hori_step_size, data_set) mission_accuracies = [] try: for i in range(iterations): time.sleep(1) params = (random.randint(10, 30) * random.randrange(-1, 2, 2), random.randint(10, 30) * random.randrange(-1, 2, 2), random.randint(10, 30)) mission = MalmoPython.MissionSpec(my_mission.get_mission_xml(params), True) my_mission.load_duo_mission(mission, agents) shoot_agent.reset() shoot_agent.reset_shoot_loop() move_agent.reset() # Loop until mission ends: record_cycle = 86 total_time = 0 keeper = TimeKeeper() my_mission.chat_command_init(shoot_agent, move_agent, params) shoot_agent.agent.sendCommand("use 1") world_state = shoot_agent.agent.peekWorldState() shoot_agent.reset_shoot_loop()
def _validate_mission(self): self.my_mission = MalmoPython.MissionSpec( self.mission_generator.getXML(), True)
def doXML(area): missionXML = '''<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <About> <Summary>Hello world!</Summary> </About> <ServerSection> <ServerHandlers> <FlatWorldGenerator generatorString="3;7,0,5*3,2;3;,biome_1" forceReset="true"/> <DrawingDecorator> ''' + placeBottom(area) + ''' </DrawingDecorator> </ServerHandlers> </ServerSection> <AgentSection mode="Creative"> <Name>SketchyAI</Name> <AgentStart> ''' + '<Placement x="{0}" y="{1}" z="{2}" yaw="0"/>'.format(.5, 30, .5) + ''' </AgentStart> <AgentHandlers> <ObservationFromFullStats/> <ContinuousMovementCommands turnSpeedDegs="180"/> </AgentHandlers> </AgentSection> </Mission>''' 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(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.setModeToSpectator() # Attempt to start a mission: 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: 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 ") # Loop until mission ends: while world_state.is_mission_running: 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 ended")
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) num_reps = 30000 n = 1 odie = Odie(n=n) print("n=", n) odie.clear_inventory() for iRepeat in range(num_reps): my_mission = MalmoPython.MissionSpec( GetMissionXML("Fetch boy #" + str(iRepeat)), True) my_mission_record = MalmoPython.MissionRecordSpec( ) # Records nothing by default my_mission.requestVideo(800, 500) my_mission.setViewpoint(0) max_retries = 3 for retry in range(max_retries): try: # Attempt to start the mission: agent_host.startMission(my_mission, my_client_pool, my_mission_record, 0, "Odie") break except RuntimeError as e: if retry == max_retries - 1: print("Error starting mission", e) print("Is the game running?")
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(num_repeats): size = int(6 + 0.5 * i) print("Size of maze:", size) my_mission = MalmoPython.MissionSpec( GetMissionXML("0", 0.4 + float(i / 20.0), size), True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(1) # Attempt to start a mission: max_retries = 3 my_clients = MalmoPython.ClientPool() my_clients.add(MalmoPython.ClientInfo( '127.0.0.1', 10000)) # add Minecraft machines here as available for retry in range(max_retries): try: agent_host.startMission(my_mission, my_clients, my_mission_record, 0, "%s-%d" % ('Moshe', i)) break except RuntimeError as e:
def load_mission_xml(self): mission_xml = getMissionXML(MOB_TYPE + " Apocalypse #", self.agent_host) self.mission_spec = MalmoPython.MissionSpec(mission_xml, True)
def setupMinecraft(): ''' Setup the Minecraft environment NOTE: action space relies heavily on the coordinate system and minecraft has a weird coord system ''' # 0: up, 1: up, 2: down, 3: left, 4: right action_space = ["move 0", "move 1", "move -1", "strafe -1", "strafe 1"] # 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) # Set up the mission mission_file = './mission.xml' with open(mission_file, 'r') as f: print("Loading mission from %s" % mission_file) mission_xml = f.read() my_mission = MalmoPython.MissionSpec(mission_xml, True) # Force reset of the environment, generate a brand new world every episode my_mission.forceWorldReset() # Python code for alterations to the environment my_mission.drawCuboid(-1, 106, -1, GRID_SIZE, 106, GRID_SIZE, "lava") my_mission.drawCuboid(-1, 107, -1, GRID_SIZE, 107, GRID_SIZE, "obsidian") # my_mission.drawCuboid(0, 108, 0, GRID_SIZE-1, 110, GRID_SIZE-1, "air") # makes steve move # Generating the map gridmap = reset_map(GRID_SIZE, MAP_PATH) for j in range(GRID_SIZE): for i in range(GRID_SIZE): if gridmap[j][i] == '1': my_mission.drawBlock(i, 108, j, "stone") my_mission.drawBlock(i, 109, j, "stone") for j in range(GRID_SIZE): for i in range(GRID_SIZE): if gridmap[j][i] == '2': my_mission.drawBlock(i, 107, j, "lava") pass # Placing diamonds on map diamond_spots = [(4, 6), (0, 0), (5, 1), (9, 2), (7, 8), (0, 9), (7, 4), (8, 0), (1, 6), (8, 6)] for d in diamond_spots: my_mission.drawItem(d[0], 109, d[1], "diamond") # Extra aesthetics my_mission.drawBlock(-1, 111, -1, "torch") my_mission.drawBlock(-1, 111, GRID_SIZE, "torch") my_mission.drawBlock(GRID_SIZE, 111, -1, "torch") my_mission.drawBlock(GRID_SIZE, 111, GRID_SIZE, "torch") # Idea for another mission # my_mission.drawLine(0, 107, 8, 15, 107, 8, "netherrack") # my_mission.drawBlock(8, 108, 8, "fire") # Can't add a door properly, only adding half a door? # my_mission.drawBlock(11, 108, 6, "wooden_door") # my_mission.drawBlock(11, 109, 6, "wooden_door") # Placing Steve in the map x = np.random.randint(0, 9) + 0.5 z = np.random.randint(0, 9) + 0.5 # my_mission.startAt(x, 108, z) my_mission.startAt(4.5, 108, 3.5) my_mission_record = MalmoPython.MissionRecordSpec() print(my_mission.getSummary()) return agent_host, my_mission, my_mission_record, action_space
def _config_malmo(mission_file: Tuple[str, str]) -> MalmoPython.MissionSpec: xml = get_data(mission_file[0], mission_file[1]) mission = MalmoPython.MissionSpec(xml, True) return mission
</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(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() # Attempt to start a mission: 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: print("Error starting mission:", e) exit(1) else: time.sleep(2)
def main(model=None, mode='train', start_episode=0): my_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"> <About> <Summary>Hill Descent.</Summary> </About> <ModSettings> <MsPerTick>20</MsPerTick> </ModSettings> <ServerSection> <ServerInitialConditions> <Time><StartTime>1</StartTime></Time> </ServerInitialConditions> <ServerHandlers> <DefaultWorldGenerator seed="-999595225643433963" forceReset="false" destroyAfterUse="false" /> <ServerQuitFromTimeUp timeLimitMs="100000000"/> <ServerQuitWhenAnyAgentFinishes/> </ServerHandlers> </ServerSection> <AgentSection mode="Survival"> <Name>Bob</Name> <AgentStart> <Placement x="28.5" y="87" z="330.5" pitch="-90" yaw="0"/> </AgentStart> <AgentHandlers> <DiscreteMovementCommands/> <MissionQuitCommands quitDescription="done"/> <ChatCommands/> <ObservationFromFullStats/> <ObservationFromGrid> <Grid name="sight"> <min x="{}" y="{}" z="{}"/> <max x="{}" y="{}" z="{}"/> </Grid> <Grid name="feet"> <min x="0" y="-1" z="0"/> <max x="0" y="-1" z="0"/> </Grid> </ObservationsationFromGrid> <AgentQuitFromTouchingBlockType> <Block type="cobblestone" /> </AgentQuitFromTouchingBlockType> </AgentHandlers> </AgentSection> </Mission> '''.format(-(grid_width - 1) // 2, -grid_height, -(grid_width - 1) // 2, (grid_width - 1) // 2, grid_height, (grid_width - 1) // 2) batch_size = 100 agent = DQNAgent(state_size, action_size, learning_rate, discount_rate, epsilon, epsilon_min, epsilon_decay) if model != None: agent.load(model) if mode == 'test': agent.epsilon = 0.0 print('loaded model: {}'.format(model)) else: clear_csv('./data/results.csv') clear_csv('./data/moves.csv') my_client_pool = MalmoPython.ClientPool() my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10001)) agent_host = MalmoPython.AgentHost() for e in range(start_episode + 1, episodes + 1): my_mission = MalmoPython.MissionSpec(my_xml, True) my_mission_record = MalmoPython.MissionRecordSpec() my_mission.requestVideo(800, 500) my_mission.setViewpoint(2) print("Waiting for the mission to start", end=' ') agent_host.startMission( my_mission, my_mission_record, ) world_state = agent_host.getWorldState() while not world_state.has_mission_begun: print(".", end="") time.sleep(0.1) world_state = agent_host.getWorldState() for error in world_state.errors: print("Error:", error.text) print() agent_host.sendCommand('chat /kill @e[type=Chicken]') agent_host.sendCommand('chat /kill @e[type=Pig]') agent_host.sendCommand('chat /kill @e[type=Cow]') moves = 0 episode_reward = 0 while world_state.is_mission_running: world_state = agent_host.getWorldState() if world_state.number_of_observations_since_last_state > 0: try: obvsText = world_state.observations[-1].text data = json.loads(obvsText) except: print("Error when getting state") continue state = get_state(data) prev_x = data.get(u'XPos', 0) prev_y = data.get(u'YPos', 0) prev_z = data.get(u'ZPos', 0) useful_state = [state[2], state[6], state[7], state[8], \ state[10], state[11], state[13], \ state[14], state[16], state[17], \ state[18], state[22]] action = agent.act(useful_state) if ((action == 0 and state[grid_center - grid_width] == 0) or (action == 1 and state[grid_center + 1] == 0) or (action == 2 and state[grid_center + grid_width] == 0) or (action == 3 and state[grid_center - 1] == 0)): agent_host.sendCommand(jump_directions[action]) else: agent_host.sendCommand(directions[action]) time.sleep(0.25) #print("North:", state[grid_center - grid_width], \ # " East:", state[grid_center + 1], \ # " South:", state[grid_center + grid_width], \ # " West:", state[grid_center - 1]) try: world_state = wait_world_state(agent_host, world_state) obvsText = world_state.observations[-1].text data = json.loads(obvsText) except: print("Error when getting state") continue current_x = data.get(u'XPos', 0) current_y = data.get(u'YPos', 0) current_z = data.get(u'ZPos', 0) damage_taken = calculate_damage(prev_y, current_y) next_state = get_state(data) useful_next_state = [state[2], state[6], state[7], state[8], \ state[10], state[11], state[13], \ state[14], state[16], state[17], \ state[18], state[22]] # print("previous and current y", prev_y, current_y) # print("damage taken", damage_taken) #print("X:", prev_x, current_x, "\n", \ # "Y:", prev_y, current_y, "\n", \ # "Z:", prev_z, current_z, "\n") reward = 2 * ( prev_y - current_y ) - 50 * damage_taken - 1 if prev_x != current_x or prev_y != current_y or prev_z != current_z else -1000 episode_reward += reward done = True if current_y <= goal_height or not world_state.is_mission_running or data[ 'Life'] <= 0 else False agent.remember(useful_state, action, reward, useful_next_state, done) if ((action == 0 and state[grid_center - grid_width] == 0) or (action == 1 and state[grid_center + 1] == 0) or (action == 2 and state[grid_center + grid_width] == 0) or (action == 3 and state[grid_center - 1] == 0)): print( 'episode {}/{}, action: {}, reward: {}, e: {:.2}, move: {}, done: {}' .format(e, episodes, jump_directions[action], reward, agent.epsilon, moves, done)) else: print( 'episode {}/{}, action: {}, reward: {}, e: {:.2}, move: {}, done: {}' .format(e, episodes, directions[action], reward, agent.epsilon, moves, done)) moves += 1 if mode == 'train' or model == None: write_to_csv('./data/moves.csv', [e, current_x, current_y, current_z, reward]) if e > batch_size: agent.replay(batch_size) if done or moves > max_moves: agent_host.sendCommand("quit") if (mode == 'train' or model == None) and (e in checkpoints or agent.epsilon <= epsilon_min): print('saving model at episode {}'.format(e)) agent.save('./models/model_{}'.format(e)) if agent.epsilon <= epsilon_min: break time.sleep(1) # my_mission.forceWorldReset() if mode == 'train' or model == None: write_to_csv('./data/results.csv', [e, episode_reward, moves, int(episode_reward > 0)])