Beispiel #1
0
def main():
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w',
                           0)  # flush print output immediately
    SB = SpecialistBot(fname="sb_qtable.p")
    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_record = MalmoPython.MissionRecordSpec()

    ##########################################################
    ## Modify the below code in order to change the encounters
    ##########################################################
    encounters = len(Arena.ENTITY_LIST) * 3
    for n in range(encounters):
        i = n % len(Arena.ENTITY_LIST)
        enemy = Arena.malmoName(
            Arena.ENTITY_LIST[i])  #"Zombie" if you want to run it exclusively
        # against Zombies
        print
        print 'Mission %d of %d: %s' % (n + 1, encounters, enemy)

        # Create the mission using the preset XML function fromarena_gen
        missxml = Arena.create_mission(enemy)
        my_mission = MalmoPython.MissionSpec(missxml, True)
        my_mission.forceWorldReset()  # RESET THE WORLD IN BETWEEN ENCOUNTERS

        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)

        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

        # -- run the agent in the world -- #
        SB.run(agent_host)
        print "Mission has stopped.\n"
        # -- clean up -- #
        time.sleep(2)  # (let the Mod reset)
    print "Done."
    SB.log_Q()
    SB.log_results('sb_results_base.txt')
Beispiel #2
0
    def reset(self):
        log = logging.getLogger('SimpleMalmoEnvironment.reset')

        obstacle_locations = [[l[0], l[1]] for l in self.obstacles]
        landmark_locations = [[l[0], l[1]] for l in self.landmarks]

        del self.mission  # just to be sure, i create a new mission every episode
        # mission related objects
        self.mission_xml = self.generate_malmo_environment_xml()
        log.debug("Obtained mission XML: \n %s", self.mission_xml)
        self.mission_record = MalmoPython.MissionRecordSpec()
        self.mission = MalmoPython.MissionSpec(self.mission_xml, True)
        log.info("Loaded mission XML")

        # select a random start location such that is is not one of the wall cells and not one of landmarks
        # x, y = random.randint(0, self.size[0] - 1), random.randint(0, self.size[1] - 1)
        # while [x, y] in obstacle_locations or [x, y] in landmark_locations:
        #     x, y = random.randint(0, 6), random.randint(0, 6)

        self.mission.setViewpoint(1)

        # set mission variables - landmarks, source and destination
        landmarks = copy.deepcopy(self.landmarks)
        source_loc = random.choice(landmarks)  # first select the source to pick up from
        remaining_landmarks = [lm for lm in landmarks if lm != source_loc]  # tentative destinations are other landmarks
        destination = random.choice(remaining_landmarks)  # now randomly choose the destination from above list
        agent_start_loc = random.choice(remaining_landmarks)  # start locations for agent; start loc != pick up source
        x, y = agent_start_loc[0], agent_start_loc[1]
        self.current_agent_location = [x, y]
        # malmo needs locations to be 0.5 to be in the middle of the square, else, it is at the edge
        self.mission.startAt(x + 0.5, 46, y + 0.5)

        self.item_location = landmarks.index(source_loc)
        self.destination = landmarks.index(destination)

        self.mission.drawItem(source_loc[0], 47, source_loc[1], self.landmark_types[self.destination])

        retries = 3
        log.debug("Final Mission XML sent to Malmo: \n %s", self.mission.getAsXML(True))
        for retry in range(retries):
            try:
                malmo_env.startMission(self.mission, self.mission_record)
                time.sleep(10)

                world_state = malmo_env.getWorldState()
                if world_state.has_mission_begun:
                    break
            except RuntimeError as e:
                if retry == retries - 1:
                    log.error("Error starting mission. Max retries elapsed. Closing! %s", e.message)
                    exit(1)
                else:
                    time.sleep(10)

        world_state = malmo_env.getWorldState()

        while not world_state.has_mission_begun:
            log.debug("Waiting for mission to begin")
            time.sleep(0.1)
            world_state = malmo_env.getWorldState()
            for error in world_state.errors:
                log.error("Error: %s", error.text)
Beispiel #3
0
    # 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())

    for i in range(len(agent_hosts)):
        startMission(agent_hosts[i], my_mission, client_pool,
                     MalmoPython.MissionRecordSpec(), i, experimentID)

    # Wait for mission to start - complicated by having multiple agent hosts, and the potential
    # for multiple errors to occur in the start-up process.
    print "Waiting for the mission to start ",
    hasBegun = False
    hadErrors = False
    while not hasBegun and not hadErrors:
        sys.stdout.write(".")
        time.sleep(0.1)
        for ah in agent_hosts:
            world_state = ah.getWorldState()
            if world_state.has_mission_begun:
                hasBegun = True
            if len(world_state.errors):
                hadErrors = True
    def __init__(self, xmlfile):

        self.agent_host = MalmoPython.AgentHost()
        self.my_mission = MalmoPython.MissionSpec(getMissionXML(xmlfile), True)
        self.my_mission_record = MalmoPython.MissionRecordSpec()

        self.objects_of_interest = ['stone_button', 'wooden_door', 'lever']

        # 4 represents anything in the env that is walkable (excluding wool)
        self.object_to_index = {
            'air': 9,
            'player': 8,
            'wooden_door': 2,
            'wool': 3,
            'stained_hardened_clay': 4,
            'clay': 4,
            'iron_block': 4,
            'quartz_block': 4,
            'fire': 5,
            'lever': 6,
            'stone_button': 7,
            'gravel': 10,
            'redstone_wire': 4
        }

        self.index_to_object = {
            255: 'unknown',
            9: 'frontier',
            8: 'player',
            2: 'wooden_door',
            3: 'wool',
            4: 'wall',
            5: 'fire',
            6: 'lever',
            7: 'stone_button',
            10: 'gravel'
        }

        self.non_opaque_objects = [9, 8, 1, 2, 5, 6,
                                   7]  #state of the door to be recorded
        self.passable_objects = ['air', 'wooden_door']  #, 'lever', 'gravel']
        self.passable_objects_with_cost = {
            'air': 1,
            'lever': 1,
            'wooden_door': 2,
            'gravel': 5
        }
        self.floor_objects_types = [
            'redstone_wire', 'wool', 'iron_block', 'quartz_block'
        ]
        self.envsize = 50
        # Env specific variables; (modify them wrt xmlfile)
        # self.sight= {'x': (-3, 3), 'z': (-3, 3), 'y':(-1, 1)}
        self.sight = {'x': (-21, 21), 'z': (-21, 21), 'y': (-1, 1)}
        self.angle = 50
        self.range_x = abs(self.sight['x'][1] - self.sight['x'][0]) + 1
        self.range_y = abs(self.sight['y'][1] - self.sight['y'][0]) + 1
        self.range_z = abs(self.sight['z'][1] - self.sight['z'][0]) + 1
        self.my_mission.observeGrid(self.sight['x'][0], self.sight['y'][0],
                                    self.sight['z'][0], self.sight['x'][1],
                                    self.sight['y'][1], self.sight['z'][1],
                                    'relative_view')
        self.scanning_range = 15

        # Goal specific variables
        self.num_victims_seen = 0
        self.num_doors_seen = 0
        self.total_victims = 3
        self.total_doors = 3
        self.victims_visited = np.zeros((self.envsize, self.envsize))
        self.victims_visited_sparse = set()

        # self.start_position = {'x': -2185.5, 'y': 28.0, 'z': 167.5}
        self.current_position = (self.range_z // 2, self.range_x // 2)
        self.relative_position = {
            'y': self.range_y // 2,
            'z': self.range_z // 2,
            'x': self.range_x // 2
        }
        self.absolute_position = None
        # NOTE that we start from 0 value of x and half value for z for recording into the array

        # Populate with `observe()` function
        self.grid = None
        self.ypos = None
        self.zpos = None
        self.xpos = None
        self.yaw = None
        self.pitch = None
        self.lineOfSight = None

        self.masked_grid = None
        self.relative_map = None
        self.absolute_map = np.zeros(
            (self.range_y, self.envsize, self.envsize))
        self.origin_coord = {'y': 27.0, 'z': 142.5, 'x': -2190.5}

        self.maze_map_dict = {}
    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()
my_mission.timeLimitInSeconds(10)
my_mission.requestVideo(320, 240)
my_mission.rewardForReachingPosition(19.5, 0.0, 19.5, 100.0, 1.1)
print my_mission

my_mission_record = MalmoPython.MissionRecordSpec("./saved_data.tgz")
my_mission_record.recordCommands()
my_mission_record.recordMP4(20, 400000)
my_mission_record.recordRewards()
my_mission_record.recordObservations()
print my_mission_record

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)
Beispiel #6
0
    def __init__(self,
                 maze_def,
                 reset,
                 video_dim=(32, 32),
                 num_parallel=1,
                 time_limit=30,
                 discrete_actions=False,
                 vision_observation=True,
                 depth=False,
                 num_frames=1,
                 grayscale=True):
        self.video_width, self.video_height = video_dim
        self.image_width, self.image_height = video_dim
        self.discrete_actions = discrete_actions
        self.vision_observation = vision_observation
        self.depth = depth
        self.num_parallel = num_parallel

        maze = create_maze(maze_def)
        self.mission_gen = MissionGen()
        self.mission = self.mission_gen.generate_mission(
            maze.create_maze_array(), reset=reset)
        self.XGoalPos, self.YGoalPos = self.mission_gen.goal_pos[
            0], self.mission_gen.goal_pos[2]

        # with open(mission_file, 'r') as f:
        #     print("Loading mission from %s" % mission_file)
        #     mission_xml = f.read()
        #     self.mission = MalmoPython.MissionSpec(mission_xml, True)
        self.mission.requestVideo(self.video_height, self.video_width)
        self.mission.observeRecentCommands()
        self.mission.allowAllContinuousMovementCommands()
        # self.mission.timeLimitInSeconds(time_limit)

        if self.num_parallel > 1:
            self.client_pool = MalmoPython.ClientPool()
            for i in range(num_parallel):
                port = 10000 + i
                self.client_pool.add(MalmoPython.ClientInfo("127.0.0.1", port))

        self.agent_host = MalmoPython.AgentHost()
        self.agent_host.setObservationsPolicy(
            MalmoPython.ObservationsPolicy.KEEP_ALL_OBSERVATIONS)
        # self.agent_host.setObservationsPolicy(MalmoPython.ObservationsPolicy.LATEST_OBSERVATION_ONLY)
        self.agent_host.setVideoPolicy(MalmoPython.VideoPolicy.KEEP_ALL_FRAMES)
        # self.agent_host.setVideoPolicy(MalmoPython.VideoPolicy.LATEST_FRAME_ONLY)

        self.mission_record_spec = MalmoPython.MissionRecordSpec()

        if discrete_actions:
            self._action_set = {0: "move 1", 1: "turn 1", 2: "turn -1"}
            self.action_space = Discrete(n=len(self._action_set))
        else:
            # self._action_set = ["move", "turn", "pitch"]
            # self.action_space = Box(np.array([0, -.5, -.5]), np.array([1, .5, .5]))
            self._action_set = [("move", (-1, 1)), ("turn", (-0.5, 0.5))]
            #("jump", (-1, 1))]
            lower_bound = np.asarray([x[1][0] for x in self._action_set])
            upper_bound = np.asarray([x[1][1] for x in self._action_set])
            self.action_space = Box(lower_bound, upper_bound)

        self.num_frames = num_frames
        self.grayscale = grayscale
        if self.grayscale:
            self.num_frame_channels = 1
            high = 1
        else:
            self.num_frame_channels = 3
            high = 255

        # Obs keys and bounds
        x_bounds = self.mission_gen.x_bounds
        z_bounds = self.mission_gen.z_bounds
        self.max_dist = np.linalg.norm((x_bounds[-1], z_bounds[-1]))
        self.minDistanceFromGoal = None
        if self.vision_observation:
            self.observation_space = Box(
                low=0,
                high=high,
                shape=(self.num_frames * self.num_frame_channels,
                       self.image_height, self.image_width))
        else:

            self.obs_keys = [(u'XPos', x_bounds), (u'ZPos', z_bounds),
                             (u'yaw', (0, 360)), (u'XGoalPos', x_bounds),
                             (u'YGoalPos', z_bounds),
                             (u'DistanceTravelled', (0, 30)),
                             (u'distanceFromGoal', (0, self.max_dist))]
            l_bounds = [key[1][0] for key in self.obs_keys]
            u_bounds = [key[1][1] for key in self.obs_keys]
            self.observation_space = Box(np.array(l_bounds),
                                         np.array(u_bounds))
        # self._horizon = env.spec.timestep_limit
        self.last_obs = None
        self.cum_reward = 0
        self.distance_travelled = 0
        self.terminal = False
        self.jump = 0
Beispiel #7
0
for iRepeat in range(num_reps):
    # Find the point at which to create the maze:
    xorg = (iRepeat % 64) * 16
    zorg = ((iRepeat / 64) % 64) * 16
    yorg = 200 + ((iRepeat / (64 * 64)) % 64) * 8

    print "Mission " + str(iRepeat) + " --- starting at " + str(
        xorg) + ", " + str(yorg) + ", " + str(zorg)

    # Create a mission:
    my_mission = MalmoPython.MissionSpec(
        GetMissionXML(iRepeat, xorg, yorg, zorg, iRepeat), validate)

    # Set up a recording - MUST be done once for each mission - don't do this outside the loop!
    my_mission_record = MalmoPython.MissionRecordSpec(recordingsDirectory +
                                                      "//" + "Quilt_" +
                                                      str(iRepeat) + ".tgz")
    my_mission_record.recordCommands()
    my_mission_record.recordMP4(24, 400000)
    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, str(experimentID))
            break
        except RuntimeError as e:
            if retry == max_retries - 1:
                print "Error starting mission", e
                exit(1)
            else:
Beispiel #8
0
    def __init__(self, xmlfile, recordingsDirectory="../human_trajectories"):

        self.agent_host = MalmoPython.AgentHost()
        self.my_mission = MalmoPython.MissionSpec(getMissionXML(xmlfile), True)
        self.my_mission_record = MalmoPython.MissionRecordSpec()

        # self.recordingsDirectory = recordingsDirectory
        # if(not os.path.exists(recordingsDirectory)):
        #     os.mkdir(recordingsDirectory)

        # if recordingsDirectory:
        #     self.my_mission_record.recordRewards()
        #     self.my_mission_record.recordObservations()
        #     self.my_mission_record.recordCommands()
        #     # if agent_host.receivedArgument("record_video"): # my_mission_record.recordMP4(24,2000000)
        #     self.my_mission_record.recordMP4(24,2000000)
        # recordingsDirectory = malmoutils.get_recordings_directory(agent_host)

        # Should not be here - move to agent
        self.priority = {'stone_button': 70, 'wooden_door': 40, 'lever': 20}
        self.objects_of_interest = ['stone_button', 'wooden_door', 'lever']
        self.object_to_index = {
            'air': 9, 
            'player': 8, 
            'wooden_door': 2, 
            'wool': 3, 
            'stained_hardened_clay': 4, 'iron_block': 4, 'clay': 4, 
            'fire': 5, 
            'lever': 6, 
            'stone_button': 7, 
            'gravel': 10
        }


        self.passable_objects = ['air'] #, 'wooden_door'] #, 'lever', 'gravel']
        self.passable_objects_with_cost = {'air': 1,  'lever': 1, 'wooden_door': 2, 'gravel': 5}
        self.non_opaque_list = ['air', 'player', 'wooden_door', 'fire', 'lever', 'stone_button']  # [9, 8, 1, 2, 5, 6, 7]
        
        # Env specific variables; (modify them wrt xmlfile)
        # self.sight= {'x': (-3, 3), 'z': (-3, 3), 'y':(-1, 1)}
        self.envsize = 61
        self.angle = 51     # for field of view imposition on (raw) observation from grid
        self.scanning_range = 15
        # self.pitch = np.ran
        self.origin_coord = {'y': 27.0, 'z': 140.5, 'x': -2195.5}

        self.sight= {'x': (-30, 30), 'z': (-30, 30), 'y':(-1, 1)}

        self.range_x = abs(self.sight['x'][1] - self.sight['x'][0]) + 1
        self.range_y = abs(self.sight['y'][1] - self.sight['y'][0]) + 1
        self.range_z = abs(self.sight['z'][1] - self.sight['z'][0]) + 1
        self.my_mission.observeGrid(self.sight['x'][0], self.sight['y'][0], self.sight['z'][0], 
            self.sight['x'][1], self.sight['y'][1], self.sight['z'][1], 'relative_view')
        
        # self.start_position = {'x': -2185.5, 'y': 28.0, 'z': 167.5}
        self.current_position = (self.range_z//2, self.range_x//2)
        self.relative_position = {'y':self.range_y//2, 'z':self.range_z//2, 'x':self.range_x//2}
        self.absolute_position = None
        # NOTE that we start from 0 value of x and half value for z for recording into the array
        
        # Populate with `observe()` function
        # self.grid = None
        # self.ypos = None
        # self.zpos = None
        # self.xpos = None
        # self.yaw = None
        # self.pitch = None
        # self.lineOfSight = None
        
        # self.masked_grid = None
        relative_map = None
        self.absolute_map = np.zeros((self.range_y, self.envsize, self.envsize))

        self.maze_map_dict = {}


        # (TODO: shift to the planning agent class>?' 
        # Goal specific variables
        self.num_victims_seen = 0
        self.num_doors_seen = 0
        self.total_victims = 3
        self.total_doors = 3
        self.victims_visited = np.zeros((self.envsize, self.envsize))
        self.victims_visited_sparse = set()
Beispiel #9
0
    n = 1
    num_repeats = 300
    agent = Agent(iterations=num_repeats)
    my_mission = MalmoPython.MissionSpec(missionXML, True)
    my_recording_mission = MalmoPython.MissionSpec(recordingXML, True)

    # Attempt to start a mission:
    max_retries = 3

    cumulative_rewards = []
    for i in range(num_repeats):

        for retry in range(max_retries):
            try:
                if RECORDING and (i % RECORDING_ITERATIONS == 0):
                    my_mission_record = MalmoPython.MissionRecordSpec(
                        "recording_" + str(i) + ".tgz")
                    my_mission_record.recordCommands()
                    my_mission_record.recordObservations()
                    #my_mission_record.recordMP4(60, 8000000)
                    agent.recording = True
                    agent_host.startMission(my_recording_mission,
                                            my_mission_record)
                else:
                    agent.recording = False
                    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)
Beispiel #10
0
def run(size, algo1, algo2):
    #algorithms = {"reflex": reflex.reflex, "hiddenMarkov": hiddenMarkov.hiddenMarkov, "minimax":minimax.minimax, "expectimax": expectimax.expectimax}
    algorithms = {
        "reflex": reflex.reflex,
        'random': randomagent.randommove,
        'smartrandom': smartrandomagent.randommove,
        'astarreflex': AStarReflex.search,
        "minimax": minimax.minmax
    }
    #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])
    map_size = int(size)
    map_minus = str(map_size - 1)
    agentAlgo = algorithms[algo1]
    enemyAlgo = algorithms[algo2]
    #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>
                    <Summary>Hello world!</Summary>
                  </About>
                  
                  <ServerSection>
                    <ServerInitialConditions>
                      <Time>
                        <StartTime>12000</StartTime>
                        <AllowPassageOfTime>false</AllowPassageOfTime>
                      </Time>
                    </ServerInitialConditions>
                    <ServerHandlers>
                      <FlatWorldGenerator generatorString="3;7,220*1,5*3,2;3;,biome_1"/>
                      <DrawingDecorator>
                        <!-- coordinates for cuboid are inclusive -->
                        <DrawCuboid x1="0" y1="45" z1="0" x2=''' + '"' + map_minus + '"' + ''' y2="300" z2=''' + '"' + map_minus + '"' + ''' type="air" />            <!-- limits of our arena -->
                        <DrawCuboid x1="0" y1="40" z1="0" x2=''' + '"' + map_minus + '"' + ''' y2="44" z2=''' + '"' + map_minus + '"' + ''' type="lava" />           <!-- lava floor -->
                        <DrawCuboid x1="0"  y1="46" z1="0"  x2=''' + '"' + map_minus + '"' + ''' y2="46" z2=''' + '"' + map_minus + '"' + ''' type="snow" />
                      </DrawingDecorator>
                      <ServerQuitFromTimeUp timeLimitMs="30000"/>
                      
                    </ServerHandlers>
                  </ServerSection>
                  
                  <AgentSection mode="Survival">
                    <Name>Agent</Name>
                    <AgentStart>
                        <Inventory>
                            <InventoryItem slot="0" type="diamond_shovel"/>
                        </Inventory>
                        <Placement x="0.5" y="47.0" z="0.5" pitch="50" yaw="0"/>
                    </AgentStart>
                    <AgentHandlers>
                      <ObservationFromFullStats/>
                      <ObservationFromGrid>
                          <Grid name="floor3x3W">
                            <min x="-1" y="0" z="-1"/>
                            <max x="1" y="0" z="1"/>
                          </Grid>
                          <Grid name="floor3x3F">
                            <min x="-1" y="-1" z="-1"/>
                            <max x="1" y="-1" z="1"/>
                          </Grid>
                      </ObservationFromGrid>
                      <DiscreteMovementCommands/>
                    </AgentHandlers>
                  </AgentSection>
                  
                  <AgentSection mode="Survival">
                    <Name>Enemy</Name>
                    <AgentStart>
                        <Inventory>
                            <InventoryItem slot="0" type="diamond_shovel"/>
                        </Inventory>
                        <Placement x=''' + '"' + str(
        float(map_size) - 0.5) + '"' + ''' y="47.0" z=''' + '"' + str(
            float(map_size) - 0.5) + '"' + ''' pitch="50" yaw="180"/>
                    </AgentStart>
                    
                    <AgentHandlers>
                      <ObservationFromFullStats/>
                      <DiscreteMovementCommands/>
                      <ObservationFromGrid>
                          <Grid name="floor3x3W">
                            <min x="-1" y="0" z="-1"/>
                            <max x="1" y="0" z="1"/>
                          </Grid>
                          <Grid name="floor3x3F">
                            <min x="-1" y="-1" z="-1"/>
                            <max x="1" y="-1" z="1"/>
                          </Grid>
                      </ObservationFromGrid>
                      <RewardForTouchingBlockType>
                        <Block reward="-100.0" type="lava" behaviour="onceOnly"/>
                      </RewardForTouchingBlockType>
                      <AgentQuitFromTouchingBlockType>
                        <Block type="lava" />
                      </AgentQuitFromTouchingBlockType>
                    </AgentHandlers>
                  </AgentSection>
                </Mission>'''

    # Create default Malmo objects:
    my_mission = MalmoPython.MissionSpec(missionXML, 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)
    my_mission_record = MalmoPython.MissionRecordSpec()

    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:
                    print("Not enough available Minecraft instances running.")
                    used_attempts += 1
                    if used_attempts < max_attempts:
                        print("Will wait in case they are starting up.",
                              max_attempts - used_attempts, "attempts left.")
                        time.sleep(2)
                elif errorCode == MalmoPython.MissionErrorCode.MISSION_SERVER_NOT_FOUND:
                    print(
                        "Server not found - has the mission with role 0 been started yet?"
                    )
                    used_attempts += 1
                    if used_attempts < max_attempts:
                        print("Will wait and retry.",
                              max_attempts - used_attempts, "attempts left.")
                        time.sleep(2)
                else:
                    print("Other error:", e.message)
                    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.")

    def safeWaitForStart(agent_hosts):
        print("Waiting for the mission to start", end=' ')
        start_flags = [False for a in agent_hosts]
        start_time = time.time()
        time_out = 120  # Allow two minutes for mission to start.
        while not all(start_flags) and time.time() - start_time < time_out:
            states = [a.peekWorldState() for a in agent_hosts]
            start_flags = [w.has_mission_begun for w in states]
            errors = [e for w in states for e in w.errors]
            if len(errors) > 0:
                print("Errors waiting for mission start:")
                for e in errors:
                    print(e.text)
                print("Bailing now.")
                exit(1)
            time.sleep(0.1)
            print(".", end=' ')
        print()
        if time.time() - start_time >= time_out:
            print("Timed out waiting for mission to begin. Bailing.")
            exit(1)
        print("Mission has started.")

    safeStartMission(agent_host1, my_mission, client_pool, my_mission_record,
                     0, '')
    safeStartMission(agent_host2, my_mission, client_pool, my_mission_record,
                     1, '')
    safeWaitForStart([agent_host1, agent_host2])

    def movement(ah, direction, pos):
        if direction == "north":
            ah.sendCommand("movenorth 1")
            position = (pos[0], pos[1] - 1)
        elif direction == "south":
            ah.sendCommand("movesouth 1")
            position = (pos[0], pos[1] + 1)
        elif direction == "west":
            ah.sendCommand("movewest 1")
            position = (pos[0] - 1, pos[1])
        elif direction == "east":
            ah.sendCommand("moveeast 1")
            position = (pos[0] + 1, pos[1])
        else:
            position = (pos[0], pos[1])
        time.sleep(0.1)
        return position

    def attack(ah, index, pos, map, enemy=False):
        #We are going to make it so the agent can only break the blocks immediately around them.
        #So a location will be one of the 8 locations around it
        #Enemy starts facing north (1), Agent starts facing south (3)
        #  Enemy: 0 1 0  Agent: 0 3 0
        #         4 X 2         2 X 4
        #         0 3 0         0 1 0
        x, y = math.floor(pos[0]), math.floor(pos[1])
        #print("Player position: {},{} Direction: {}".format(x,y, index))
        did_Break = False
        if enemy:
            if index == "north":
                # print("Index 1")
                ah.sendCommand("attack 1")
                time.sleep(0.1)
                y -= 1
                did_Break = True
            if index == "east":
                # print("Index 2")
                ah.sendCommand("turn 1")
                time.sleep(0.1)
                ah.sendCommand("attack 1")
                time.sleep(0.1)
                ah.sendCommand("turn -1")
                time.sleep(0.1)
                x += 1
                did_Break = True
            if index == "west":
                # print("Index 4")
                ah.sendCommand("turn -1")
                time.sleep(0.1)
                ah.sendCommand("attack 1")
                time.sleep(0.1)
                ah.sendCommand("turn 1")
                time.sleep(0.1)
                x -= 1
                did_Break = True
            if index == "south":
                # print("Index 3")
                ah.sendCommand("turn 1")
                time.sleep(0.1)
                ah.sendCommand("turn 1")
                time.sleep(0.1)
                ah.sendCommand("attack 1")
                time.sleep(0.1)
                ah.sendCommand("turn -1")
                time.sleep(0.1)
                ah.sendCommand("turn -1")
                time.sleep(0.1)
                y += 1
                did_Break = True
        else:
            # Agent: 0 3 0
            #        2 X 4
            #        0 1 0
            if index == "south":
                # print("Index 3")
                ah.sendCommand("attack 1")
                time.sleep(0.1)
                y += 1
                did_Break = True
            if index == "west":
                # print("Index 4")
                ah.sendCommand("turn 1")
                time.sleep(0.1)
                ah.sendCommand("attack 1")
                time.sleep(0.1)
                ah.sendCommand("turn -1")
                time.sleep(0.1)
                x -= 1
                did_Break = True
            if index == "east":
                # print("Index 2")
                ah.sendCommand("turn -1")
                time.sleep(0.1)
                ah.sendCommand("attack 1")
                time.sleep(0.1)
                ah.sendCommand("turn 1")
                time.sleep(0.1)
                x += 1
                did_Break = True
            if index == "north":
                # print("Index 3")
                ah.sendCommand("turn 1")
                time.sleep(0.1)
                ah.sendCommand("turn 1")
                time.sleep(0.1)
                ah.sendCommand("attack 1")
                time.sleep(0.1)
                ah.sendCommand("turn -1")
                time.sleep(0.1)
                ah.sendCommand("turn -1")
                time.sleep(0.1)
                y -= 1
                did_Break = True
        if did_Break:
            map[x][y] = False

    '''
    Sample Observation:
    {"DistanceTravelled":0,"TimeAlive":50,"MobsKilled":0,"PlayersKilled":0,"DamageTaken":0,"DamageDealt":0,
    "Life":20.0,"Score":0,"Food":20,"XP":0,"IsAlive":true,"Air":300,"Name":"Enemy","XPos":5.5,"YPos":47.0,
    "ZPos":5.5,"Pitch":50.0,"Yaw":180.0,"WorldTime":12000,"TotalTime":57}

    '''

    agent_score = 0
    #count = 0
    agent_ob = None
    enemy_ob = None

    map = [[True for i in range(0, int(map_size))]
           for j in range(0, int(map_size))]
    # for i in map:
    # print(i)

    while True:
        #Scores should decrease with time and get a bonus if they win
        agent_score -= 1
        agent_state = agent_host1.peekWorldState()
        enemy_state = agent_host2.peekWorldState()
        if agent_state.number_of_observations_since_last_state > 0:
            agent_ob = json.loads(agent_state.observations[-1].text)

        if enemy_state.number_of_observations_since_last_state > 0:
            enemy_ob = json.loads(enemy_state.observations[-1].text)
        if agent_ob is None or enemy_ob is None:
            continue
        if agent_state.is_mission_running == False:
            break
        agent_position = (agent_ob["XPos"], agent_ob["ZPos"])
        enemy_position = (enemy_ob["XPos"], enemy_ob["ZPos"])

        agent_grid = agent_ob.get(u'floor3x3F', 0)
        enemy_grid = enemy_ob.get(u'floor3x3F', 0)

        if "lava" in agent_grid:
            print("Enemy Won!")
            agent_score -= 100
            for i in map:
                print(i)
            return 0
            break
        if "lava" in enemy_grid:
            print("Agent Won!")
            agent_score += 100
            for i in map:
                print(i)
            return 1
            break

        agentMoveString, agentBreakIndex = agentAlgo(agent_host1,
                                                     agent_position,
                                                     enemy_position,
                                                     agent_grid, map)
        enemyMoveString, enemyBreakIndex = enemyAlgo(agent_host2,
                                                     enemy_position,
                                                     agent_position,
                                                     enemy_grid, map)

        # #Agent Turn to Break
        attack(agent_host1, agentBreakIndex, agent_position, map)
        # #Enemy Turn to Move
        pos = movement(agent_host2, enemyMoveString, enemy_position)

        # #Enemy Turn to Break
        attack(agent_host2, enemyBreakIndex, pos, map, enemy=True)
        # #Agent Turn to Move
        movement(agent_host1, agentMoveString, agent_position)
    for i in map:
        print(i)
    return 2
Beispiel #11
0
    if exception.errno != errno.EEXIST:  # ignore error if already existed
        raise

print "WELCOME TO THE OVERCLOCK TEST"
print "============================="
print "This will run the same simple mission with " + str(
    len(tickLengths)) + " different tick lengths."
print "(Each test should run faster than the previous one.)"

for iRepeat in range(len(tickLengths)):
    msPerTick = tickLengths[iRepeat]
    my_mission = MalmoPython.MissionSpec(GetMissionXML(str(msPerTick)),
                                         validate)
    # Set up a recording
    my_mission_record = MalmoPython.MissionRecordSpec(recordingsDirectory +
                                                      "//Overclock_Test" +
                                                      str(iRepeat) + ".tgz")
    my_mission_record.recordRewards()
    my_mission_record.recordObservations()
    my_mission_record.recordMP4(120, 1200000)  # Attempt to record at 120fps
    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)
Beispiel #12
0
        <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) )

agent_host1.startMission( my_mission, client_pool, MalmoPython.MissionRecordSpec(), 0, '' )
time.sleep(10)
agent_host2.startMission( my_mission, client_pool, MalmoPython.MissionRecordSpec(), 1, '' )

for agent_host in [ agent_host1, agent_host2 ]:
    print "Waiting for the mission to start",
    world_state = agent_host.peekWorldState()
    while not world_state.has_mission_begun:
        sys.stdout.write(".")
        time.sleep(0.1)
        world_state = agent_host.peekWorldState()
        for error in world_state.errors:
            print "Error:",error.text
    print

# perform a few actions
Beispiel #13
0
def run_trials(agent_host):
    # Load the mission

    # -- set up the mission -- #
    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>Cliff walking mission based on Sutton and Barto.</Summary>
      </About>

      <ServerSection>
        <ServerInitialConditions>
            <Time><StartTime>1</StartTime></Time>
        </ServerInitialConditions>
        <ServerHandlers>
          <FlatWorldGenerator generatorString="15;10,220*1,5*3,2;3;,biome_1"/>
          <DrawingDecorator>
            <!-- coordinates for cuboid are inclusive -->
            <DrawCuboid x1="-2" y1="46" z1="-2" x2="13" y2="50" z2="10" type="air" />            <!-- limits of our arena -->
            <DrawCuboid x1="-2" y1="45" z1="-2" x2="13" y2="45" z2="10" type="lava" />           <!-- lava floor -->
            <DrawCuboid x1="1"  y1="45" z1="1"  x2="11" y2="45" z2="3" type="sandstone" />      <!-- floor of the arena -->
            <DrawCuboid x1="4"  y1="45" z1="1"  x2="7" y2="45" z2="8" type="sandstone" />      <!-- floor of the arena -->
            <DrawCuboid x1="10"  y1="45" z1="1"  x2="12" y2="45" z2="8" type="sandstone" />      <!-- floor of the arena -->
            <DrawBlock x="1"  y="45" z="1" type="cobblestone" />    <!-- the starting marker -->
            <DrawBlock x="11"  y="45" z="7" type="lapis_block" />     <!-- the destination marker -->
          </DrawingDecorator>
          <ServerQuitFromTimeUp timeLimitMs="20000"/>
          <ServerQuitWhenAnyAgentFinishes/>
        </ServerHandlers>
      </ServerSection>

      <AgentSection mode="Survival">
        <Name>Cristina</Name>
        <AgentStart>
          <Placement x="1.5" y="46.0" z="1.5" pitch="30" yaw="0"/>
        </AgentStart>
        <AgentHandlers>
          <DiscreteMovementCommands/>
          <ObservationFromFullStats/>
          <RewardForTouchingBlockType>
            <Block reward="-100.0" type="lava" behaviour="onceOnly"/>
            <Block reward="100.0" type="lapis_block" behaviour="onceOnly"/>
          </RewardForTouchingBlockType>
          <RewardForSendingCommand reward="-1" />
          <AgentQuitFromTouchingBlockType>
              <Block type="lava" />
              <Block type="lapis_block" />
          </AgentQuitFromTouchingBlockType>
        </AgentHandlers>
      </AgentSection>

    </Mission>
    """

    # For Malmo, each episode is a new re-start of the Mission.
    # We want to measure the agent's performance by tracking how much reward it accumulates total over each episode.
    cumulative_rewards: List[float] = []
    q: Dict[Tuple[State, str], float] = {}  # The q-function we're learning
    for i in range(num_repeats):
        print()
        print('Repeat %d of %d' % (i + 1, num_repeats))
        # A mission record tracks the outcome of a given mission
        my_mission_record = MalmoPython.MissionRecordSpec()
        # Retries here have to do with successfully contacting the Malmo server
        for retry in range(max_retries):
            my_mission = MalmoPython.MissionSpec(mission_xml, True)
            # add 10% holes for interest
            for x in range(1, 4):
                for z in range(1, 13):
                    if random.random() < 0.1:
                        my_mission.drawBlock(x, 45, z, "lava")

            try:
                # Here's where the mission starts from the XML
                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.5)

        print("Waiting for the mission to start", end=' ')
        init_state = agent_host.getWorldState()
        # Wait for the mission to start
        while not init_state.has_mission_begun:
            print(".", end="")
            time.sleep(0.1)
            init_state = agent_host.getWorldState()
            for error in init_state.errors:
                print("Error:", error.text)
        print()
        print("Mission start!")
        # OK, now we can finally run the agent
        # -- run the agent in the world -- #
        cumulative_reward = qlearn_episode(q, agent_host)
        print('Cumulative reward: %d' % cumulative_reward)
        cumulative_rewards += [cumulative_reward]

        # -- clean up -- #
        time.sleep(0.5)  # (let the Mod reset)
    print("Total net reward:", sum(cumulative_rewards))
    print("Mean reward:", sum(cumulative_rewards) / float(len(cumulative_rewards)))
Beispiel #14
0
def main():
    flag = (len(sys.argv) == 4)
    mode = None
    Bot = None
    if flag:
        try:
            #Determine run type
            if sys.argv[2] == 'O':
                mode = 'OPTIMAL'
            else:
                mode = 'LEARN'
            #Determine Bot type
            if sys.argv[1] == 'GB':
                Bot = GeneralBot(
                    fname="gb_qtable.p" if mode != "LEARN" else None)
            elif sys.argv[1] == 'SB':
                Bot = SpecialistBot(
                    fname="sb_qtable.p" if mode != "LEARN" else None)
            else:
                Bot = GeneralBot(epsilon=1)
            #Determine Round number
            rounds = int(sys.argv[3])
        except Exception as e:
            print 'something went wrong:', e
            return
    else:
        raise TypeError(
            "Not enough args: ['SB'/'GB'/'DB'] ['L'/'O'/] [int numRounds]")

    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w',
                           0)  # flush print output immediately
    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_record = MalmoPython.MissionRecordSpec()
    ##########################################################
    ## Modify the below code in order to change the encounters
    ##########################################################
    encounters = len(Arena.ENTITY_LIST) * rounds
    for n in range(encounters):
        i = n % len(Arena.ENTITY_LIST)
        enemy = Arena.malmoName(
            Arena.ENTITY_LIST[i])  #"Zombie" if you want to run it exclusively
        # against Zombies
        print
        print 'Mission %d of %d: %s' % (n + 1, encounters, enemy)

        # Create the mission using the preset XML function fromarena_gen
        missxml = Arena.create_mission(enemy)
        my_mission = MalmoPython.MissionSpec(missxml, True)
        my_mission.forceWorldReset()  # RESET THE WORLD IN BETWEEN ENCOUNTERS

        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)

        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

        # -- run the agent in the world -- #
        if mode == "LEARN":
            Bot.run(agent_host)
        else:
            Bot.runOptimal(agent_host)
        print "Mission has stopped.\n"
        if ((n + 1) % len(Arena.ENTITY_LIST) == 0):
            print "Saving {}...\n".format("Q-Table & Results" if mode ==
                                          "LEARN" else "Results")
            if mode == "LEARN":
                Bot.log_Q()
            Bot.log_results("temp_" + sys.argv[1] + "_results.txt")
        # -- clean up -- #
        time.sleep(2)  # (let the Mod reset)
    print "Done."
    if mode == "LEARN":
        Bot.log_Q()
    f_str = 'results_' + sys.argv[1] + '_' + mode + '[email protected]'
    count = 1
    new_f = f_str.replace('@', str(count))
    while os.path.isfile(new_f):
        count += 1
        new_f = f_str.replace('@', str(count))
    Bot.log_results(new_f)
Beispiel #15
0
    f = open("numactions.txt", "a")
    f1 = open("average_rewards.txt", "w")
    f2 = open("max_rewards.txt", "w")
    f3 = open("loss.txt", "w")
    for i in range(num_repeats):
        rewards = []
        world.reset()

        envstate = world.observe()

        print()
        print('Repeat %d of %d' % (i + 1, num_repeats))

        # Record video config. Record just the first 10 missions or last 10 in the training
        if i < 10 or 40 < i < 50 or 90 < i < 100:
            my_mission_record = MalmoPython.MissionRecordSpec(
                "sheep_lurer_recording_" + str(i) + ".tgz")
            my_mission.requestVideo(800, 500)
            my_mission_record.recordMP4(30, 1000000)
            #records video with 30fps and at 1000000 bit rate
            my_mission.setViewpoint(1)

        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.5)
Beispiel #16
0
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"
Beispiel #17
0
            self.cum_reward += total_reward
            print("Mission is over with total reward and total steps",
                  self.cum_reward, self.total_steps)
        else:

            assert len(world_state.observations) > 0 and len(
                world_state.video_frames) > 0
            ob, total_reward = self._get_obs(world_state)
            self.last_obs = ob

        # if len(world_state.mission_control_messages) > 0:
        #     for x in world_state.mission_control_messages:
        #         print x.text

        # total_reward += 0.1 * self.distance_travelled
        # print self.distance_travelled

        self.cum_reward += total_reward

        return ob, total_reward, not world_state.is_mission_running, step_info


if __name__ == '__main__':
    maze = create_maze({'type': sys.argv[1]})
    mission = MissionGen().generate_mission(maze.create_maze_array(),
                                            reset=True)
    agent = MalmoPython.AgentHost()
    mission_record_spec = MalmoPython.MissionRecordSpec()
    agent.startMission(mission, mission_record_spec)
    print maze.create_maze_array()
except OSError as exception:
    if exception.errno != errno.EEXIST: # ignore error if already existed
        raise

print("WELCOME TO THE RENDER SPEED TEST")
print("================================")
print("This will run the same simple mission with " + str(len(sizes)) + " different frame sizes.")

for iRepeat in range(len(sizes) * 2):
    prioritiseOffscreen = "true" if iRepeat % 2 else "false"
    width,height = sizes[old_div(iRepeat,2)]
    if iRepeat % 2:
        num_pixels.append(width*height)
    my_mission = MalmoPython.MissionSpec(GetMissionXML(str(width), str(height), prioritiseOffscreen), validate)
    # Set up a recording
    my_mission_record = MalmoPython.MissionRecordSpec(recordingsDirectory + "//RenderSpeed_Test" + str(iRepeat) + ".tgz");
    my_mission_record.recordRewards()
    my_mission_record.recordObservations()
    my_mission_record.recordMP4(120,1200000) # Attempt to record at 120fps
    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)
Beispiel #19
0
    my_mission.setViewpoint( 1 )

    my_clients = MalmoPython.ClientPool()
    my_clients.add(MalmoPython.ClientInfo('127.0.0.1', 10000)) # add Minecraft machines here as available

    max_retries = 3
    agentID = 0
    expID = 'Q_learning'

    num_repeats = 300
    cumulative_rewards = []
    for i in range(num_repeats):
        
        print("\nMap %d - Mission %d of %d:" % ( imap, i+1, num_repeats ))

        my_mission_record = MalmoPython.MissionRecordSpec( "./save_%s-map%d-rep%d.tgz" % (expID, imap, i) )
        my_mission_record.recordCommands()
        my_mission_record.recordMP4(20, 400000)
        my_mission_record.recordRewards()
        my_mission_record.recordObservations()

        for retry in range(max_retries):
            try:
                agent_host.startMission( my_mission, my_clients, my_mission_record, agentID, "%s-%d" % (expID, i) )
                break
            except RuntimeError as e:
                if retry == max_retries - 1:
                    print("Error starting mission:",e)
                    exit(1)
                else:
                    time.sleep(2.5)
    print(agent_host.getUsage())
    exit(1)
if agent_host.receivedArgument("help"):
    print(agent_host.getUsage())
    exit(0)
if agent_host.receivedArgument("test"):
    exit(0) # TODO: discover test-time folder names

mission_file = mission_file_no_ext + ".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)
    
# Set up a recording 
my_mission_record = MalmoPython.MissionRecordSpec(mission_file_no_ext + ".tgz")
my_mission_record.recordRewards()
my_mission_record.recordMP4(24,400000)
# 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)
Beispiel #21
0
for action_set in action_sets:

    if action_set == 'discrete_absolute':
        my_mission.allowAllDiscreteMovementCommands()
    elif action_set == 'discrete_relative':
        my_mission.allowAllDiscreteMovementCommands()
    elif action_set == 'teleport':
        my_mission.allowAllAbsoluteMovementCommands()
    else:
        print('ERROR: Unsupported action set:', action_set)
        exit(1)

    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.5)

    print("Waiting for the mission to start", end=' ')
    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:
Beispiel #22
0

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_record = MalmoPython.MissionRecordSpec("./chase_pig.tgz")
my_mission_record.recordCommands()
my_mission_record.recordMP4(20, 400000)
my_mission_record.recordRewards()
my_mission_record.recordObservations()

# 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)
    print(agent_host1.getUsage())
    exit(0)

# get the size of maze from command line

# read xml from outside file
mission_file = './random_map_2.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)
client_pool = MalmoPython.ClientPool()
client_pool.add(MalmoPython.ClientInfo('127.0.0.1', 10000))
client_pool.add(MalmoPython.ClientInfo('127.0.0.1', 10001))
safeStartMission(agent_host1, my_mission, client_pool,
                 MalmoPython.MissionRecordSpec(), 0, '')
safeStartMission(agent_host2, my_mission, client_pool,
                 MalmoPython.MissionRecordSpec(), 1, '')
safeWaitForStart([agent_host1, agent_host2])

# DrawMazeBase(my_mission, length, width, "stonebrick")
# release_resource(resource_no, length, width)
# reset = True
# if reset:
# my_mission.forceWorldReset()

# Attempt to start a mission
max_retries = 3
cumulative_rewards = []
total_repeat = 5
Beispiel #24
0
        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?")
                    exit(1)
                else:
Beispiel #25
0
client_pool = MalmoPython.ClientPool()
for x in range(10001, 10001 + NUM_AGENTS + 1):
    client_pool.add( MalmoPython.ClientInfo('127.0.0.1', x) )


robot_scores = 0


num_missions = 5 if INTEGRATION_TEST_MODE else 30000
for mission_no in range(1, num_missions+1):
    print("Running mission #" + str(mission_no))
    my_mission = MalmoPython.MissionSpec(getXML("true" if mission_no == 1 else "false"), True)
    experimentID = str(uuid.uuid4())
    for i in range(len(agent_hosts)):
        startMission(agent_hosts[i], my_mission, client_pool, MalmoPython.MissionRecordSpec(), i, experimentID)
    print("Waiting for the mission to start ")
    hasBegun = False
    hadErrors = False
    while not hasBegun and not hadErrors:
        sys.stdout.write(".")
        time.sleep(0.1)
        for ah in agent_hosts:
            world_state = ah.getWorldState()
            if world_state.has_mission_begun:
                hasBegun = True
            if len(world_state.errors):
                hadErrors = True
                print("Errors from agent " + agentName(agent_hosts.index(ah)))
                for error in world_state.errors:
                    print("Error:", error.text)
Beispiel #26
0
    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

    for rep in range(2):
        my_mission = MalmoPython.MissionSpec()
        my_mission.setSummary('A sample mission - run onto the gold block')
        my_mission.requestVideo(640, 480)
        my_mission.timeLimitInSeconds(30)
Beispiel #27
0
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"):
    my_mission.timeLimitInSeconds(20)  # else mission runs forever

# Attempt to start the mission:
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
            print "Is the game running?"
            exit(1)
        else:
            time.sleep(2)

# Wait for the mission to start:
world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
    time.sleep(0.1)
    world_state = agent_host.getWorldState()
    MalmoPython.ObservationsPolicy.KEEP_ALL_OBSERVATIONS)
agent_host.setRewardsPolicy(MalmoPython.RewardsPolicy.KEEP_ALL_REWARDS)

recordingsDirectory = "QuitFromReachingPosition_Recordings"

try:
    os.makedirs(recordingsDirectory)
except OSError as exception:
    if exception.errno != errno.EEXIST:  # ignore error if already existed
        raise

for iRepeat in xrange(NUM_REPEATS):
    my_mission = MalmoPython.MissionSpec(GetMissionXML(iRepeat), validate)
    # Set up a recording - MUST be done once for each mission - don't do this outside the loop!
    my_mission_record = MalmoPython.MissionRecordSpec(
        recordingsDirectory + "//QuitFromReachingPosition_Test" +
        str(iRepeat) + ".tgz")
    my_mission_record.recordRewards()
    my_mission_record.recordObservations()
    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)
# 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)

# Loop until mission starts:
Beispiel #30
0
# 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
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: