AGENT_JUST_ATTACKED[agent_index] = 0 AGENT_COOLDOWNS[agent_index] = ATTACK_COOLDOWNS[AGENT_WEAPONS[agent_index]] def agent_distance(): return math.sqrt((AGENT_LOCATIONS[0][0] - AGENT_LOCATIONS[1][0]) ** 2 + (AGENT_LOCATIONS[0][1] - AGENT_LOCATIONS[1][1]) ** 2 + (AGENT_LOCATIONS[0][2] - AGENT_LOCATIONS[1][2]) ** 2) if __name__ == "__main__": # Flush immediately print = functools.partial(print, flush=True) # Create agent host agent_hosts = [Malmo.AgentHost() for _ in range(2)] # Create client pool client_pool = Malmo.ClientPool() client_pool.add(Malmo.ClientInfo("127.0.0.1", 10000)) client_pool.add(Malmo.ClientInfo("127.0.0.1", 10002)) for a in range(MISSION_COUNT): print(f"Running mission #{a}...") # Create missions mission = Malmo.MissionSpec(get_mission_xml(), True) mission_id = str(uuid.uuid4()) # Start mission for a in range(2): start_mission(agent_hosts[a], mission, client_pool, Malmo.MissionRecordSpec(), a, mission_id)
from __future__ import print_function from collections import deque from builtins import range import MalmoPython import os import sys import time import json import sys import math import BFS import DFS import A_Star agent_host = MalmoPython.AgentHost() agent_host.addOptionalStringArgument("map,m", "Name of map to be used", "smallMaze") 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 sys.version_info[0] == 2: sys.stdout = os.fdopen(sys.stdout.fileno(), 'w',
import time import malmoutils import expectimax import minimax import reflex import hiddenMarkov #algorithms = {"reflex": reflex.reflex, "hiddenMarkov": hiddenMarkov.hiddenMarkov, "minimax":minimax.minimax, "expectimax": expectimax.expectimax} algorithms = {"reflex": reflex.reflex} assert len( sys.argv ) == 4, "Wrong number of arguments, the form is: mapSize, agent algorithm, enemy alogrithm" malmoutils.fix_print() # -- set up two agent hosts -- agent_host1 = MalmoPython.AgentHost() agent_host2 = MalmoPython.AgentHost() map_size = str(sys.argv[1]) agentAlgo = algorithms[sys.argv[2]] enemyAlgo = algorithms[sys.argv[3]] # Use agent_host1 for parsing the command-line options. # (This is why agent_host1 is passed in to all the subsequent malmoutils calls, even for # agent 2's setup.) malmoutils.parse_command_line(agent_host1) 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>
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 = functools.partial(print, flush=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)) my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10004)) my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10005)) # Create one agent host for parsing: parser = MalmoPython.AgentHost() options = [("nn", "n", RouteGenerators.NearestNeighbour, True), ("gen-al", "g", RouteGenerators.Genetic, False), ("div-and-conq", "d", RouteGenerators.DivideAndConquer, False), ("mst", "m", RouteGenerators.MinSpanTree, True), ("conv-hull", "c", RouteGenerators.Spiral, False), ("sa", "s", RouteGenerators.Annealing, True)] for opt in options: parser.addOptionalFlag( opt[0] + "," + opt[1], "Add " + RouteGenerators.DisplayNames[opt[2]] + " agent") parser.addOptionalIntArgument("points,p", "Number of points to use", 50) try:
print("Errors waiting for mission start:") for e in errors: print(e.text) print("Bailing now.") exit(1) time.sleep(0.1) print(".", end=' ') if time.time() - start_time >= time_out: print("Timed out while waiting for mission to start - bailing.") exit(1) print() print("Mission has started.") # -- set up two agent hosts -- agent_host_simeon = MalmoPython.AgentHost() agent_host_fred = MalmoPython.AgentHost() # Use simeon's agenthost to hold the command-line options: malmoutils.parse_command_line(agent_host_simeon) # -- 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"> <About> <Summary/> </About> <ModSettings> <MsPerTick>10</MsPerTick> <!-- Because it's pretty boring watching Fred build steps for five minutes... --> </ModSettings> <ServerSection>
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")
if used_attempts < max_attempts: print("Will wait and retry.", max_attempts - used_attempts, "attempts left.") time.sleep(2) else: print("Other error: ", str(e)) print("Waiting will not help here - bailing immediately.") exit(1) if used_attempts == max_attempts: print("All chances used up - bailing now.") exit(1) print("startMission called okay.") # a = world() agent_host = MalmoPython.AgentHost() agent_host_obervser = MalmoPython.AgentHost() a = world(agent_host=[agent_host]) 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:
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) # if Constants.agent_host.receivedArgument("test"): if 'test' in sys.argv: num_reps = 1 current_yaw = 0
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
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
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()
def __init__(self): '''Initializes the class.''' self.agent_host = MalmoPython.AgentHost() self.root = Tk() self.root.wm_title("Human Action Component")