def __init__(self, cube): super(Return, self).__init__() self.ACTION = action(cube) self.makeTwoBlocks() self.makeThreeBLocks()
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 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: if (retry == 0): # The Zombie Does Not Exist On the First Try Caused by Drawing Error new_gen = False if i >= 2 else True map_gen = auto_env() missionXML = map_gen.mob_XML_generator(new_gen) my_mission = MalmoPython.MissionSpec(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() agent_host.startMission(my_mission, my_mission_record) time.sleep(3) missionXML = mob_XML_generator(False) my_mission = MalmoPython.MissionSpec(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() agent_host.startMission(my_mission, my_mission_record) else: missionXML = mob_XML_generator(False) my_mission = MalmoPython.MissionSpec(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() 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 " 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()
def main(): 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) # Attempt to start a mission: max_retries = 2 for retry in range(max_retries): try: if (retry == 0): # The Zombie Does Not Exist On the First Try Caused by Drawing Error missionXML = mob_XML_generator(True) my_mission = MalmoPython.MissionSpec(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() agent_host.startMission(my_mission, my_mission_record) time.sleep(3) missionXML = mob_XML_generator(False) my_mission = MalmoPython.MissionSpec(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() agent_host.startMission(my_mission, my_mission_record) else: missionXML = mob_XML_generator(False) my_mission = MalmoPython.MissionSpec(missionXML, True) my_mission_record = MalmoPython.MissionRecordSpec() 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 " x = 21 y = 21 agent = zombies_fighter() ws_interpre = world_state_interpreter(x, y) visual = visualization(x, y) action_available = action(agent_host) # Loop until mission ends: while world_state.is_mission_running: matrix = None ws_interpre.input(world_state) ent_matrix = ws_interpre.entities_to_matrix() env_matrix = ws_interpre.grid_matrix() if ent_matrix != False and env_matrix != False: visual.get_entities(ent_matrix) visual.get_environment(env_matrix) visual.draw() matrix = visual.get_matrix() if matrix != None: action_available.get_ws(ws_interpre) agent.act(agent_host, matrix, action_available) #time.sleep(0.1) world_state = agent_host.getWorldState() for error in world_state.errors: print "Error:", error.text print print "Mission ended"