Esempio n. 1
0
import sys
import time
import json
import random
import errno
import math
import malmoutils
import numpy as np

import agentMC

malmoutils.fix_print()

agent_host = MalmoPython.AgentHost()
malmoutils.parse_command_line(agent_host)
recordingsDirectory = malmoutils.get_recordings_directory(agent_host)
video_requirements = '<VideoProducer><Width>860</Width><Height>480</Height></VideoProducer>' if agent_host.receivedArgument(
    "record_video") else ''

# Task parameters:
MAX_DISTANCE = 40
MAX_ZOMBIES = 16
####### SPEED OF GAME #######
SPEED = 8
ARENA_WIDTH = MAX_DISTANCE
ARENA_BREADTH = MAX_DISTANCE


def getCorner(index, top, left, expand=0, y=0):
    ''' Return part of the XML string that defines the requested corner'''
    x = str(-(expand + old_div(ARENA_WIDTH, 2))) if left else str(
Esempio n. 2
0
    print("Frames received: " + str(numFrames))
    print("Average fps: " + "{0:.2f}".format(averagefps))
    print("Frame data transferred: " + "{0:.2f}".format(dataShifted) + "MB")
    print("Data transfer rate: " + "{0:.2f}".format(datarate) + "MB/s")
    print("===============================================================================================")
    print()

    if iRepeat % 2:
        fps_offscreen.append(averagefps)
        datarate_offscreen.append(datarate)
    else:
        fps_onscreen.append(averagefps)
        datarate_onscreen.append(datarate)
        
    time.sleep(0.5) # Give mod a little time to get back to dormant state.

if SHOW_PLOT:
    # Now plot some graphs:
    plot_fpsoff = pylab.plot(num_pixels, fps_offscreen, 'r', label='render speed (no onscreen updates)')
    plot_fpson = pylab.plot(num_pixels, fps_onscreen, 'g', label='render speed (with onscreen updates)')
    plot_dataoff = pylab.plot(num_pixels, datarate_offscreen, 'b', label='data transfer speed (no onscreen updates)')
    plot_dataon = pylab.plot(num_pixels, datarate_onscreen, 'y', label='data transfer speed (with onscreen updates)')
    pylab.xlabel("Frame size (pixels)")
    pylab.ylabel("MB/s or frames/s")
    pylab.legend()
    pylab.title("Plot of render and data-transfer speeds for varying frame sizes, with and without onscreen rendering")
    if TESTING:
        pylab.savefig(malmoutils.get_recordings_directory(agent_host) + "//render_test_results.png")
    else:
        pylab.show()
Esempio n. 3
0
import os
import random
import sys
import time
import json
import copy
import errno
import xml.etree.ElementTree
from collections import deque
import malmoutils

malmoutils.fix_print()

agent_host = MalmoPython.AgentHost()
malmoutils.parse_command_line(agent_host)
recordingsDirectory = malmoutils.get_recordings_directory(agent_host)

# Set up some pallettes:
colourful=["stained_glass", "diamond_block", "lapis_block", "gold_block", "redstone_block", "obsidian"]
fiery=["stained_glass WHITE", "stained_glass PINK", "stained_glass ORANGE", "stained_glass RED", "wool BLACK", "glowstone"]
oresome=["gold_ore", "lapis_ore", "iron_ore", "emerald_ore", "redstone_ore", "quartz_ore"]
frilly=["skull", "stained_glass WHITE", "wool PINK", "wool WHITE", "stained_hardened_clay PINK", "stained_hardened_clay WHITE"]
icepalace=["ice", "stained_glass", "stained_glass", "stained_glass", "stained_glass", "snow"]
volatile=["tnt", "stained_glass", "stained_glass", "redstone_block", "stained_glass", "stained_glass"]
oak=["planks", "planks", "planks", "planks", "lapis_block", "lapis_block"]
sponge=["sponge", "glass", "sponge", "glass", "sponge", "glass"]
palletes = [colourful, fiery, oresome, frilly, icepalace, volatile, oak, sponge]

# dimensions of the test structure:
SIZE_X = 21
SIZE_Y = 31
Esempio n. 4
0
import malmoutils

agentHost = MalmoPython.AgentHost()

# See if we can parse our extended command line.
malmoutils.parse_command_line(agentHost)

# As we are not recording our video xml should be an empty string.
assert malmoutils.get_video_xml(agentHost) == ''

# Test that we can get a default recording spec.
assert type(malmoutils.get_default_recording_object(
    agentHost, "test")) == MalmoPython.MissionRecordSpec

# Default recordings directory is ''.
assert malmoutils.get_recordings_directory(agentHost) == ''


def clientInfos(cp):
    return [(c.ip_address, c.control_port, c.command_port) for c in cp.clients]


# Test adding some client infos to a client pool.
clientPool = MalmoPython.ClientPool()
assert len(clientPool.clients) == 0
c1 = ("localhost", 10000, 0)
client1 = MalmoPython.ClientInfo(*c1)
clientPool.add(client1)
assert clientInfos(clientPool) == [c1]
c2 = ("127.0.0.1", 10001, 20001)
client2 = MalmoPython.ClientInfo(*c2)
Esempio n. 5
0
def main(agent_host):
    device = torch.device("cpu")
    if VISION_ENABLED:
        eyes = Eyes()
    if GET_VISION_DATA:
        clear_images()
    malmoutils.fix_print()
    malmoutils.parse_command_line(agent_host)
    recordingsDirectory = malmoutils.get_recordings_directory(agent_host)

    q_network = QNetwork((2, Hyperparameters.OBS_SIZE, Hyperparameters.OBS_SIZE), len(Hyperparameters.ACTION_DICT))
    target_network = QNetwork((2, Hyperparameters.OBS_SIZE, Hyperparameters.OBS_SIZE), len(Hyperparameters.ACTION_DICT))
    target_network.load_state_dict(q_network.state_dict())

    optim = torch.optim.Adam(q_network.parameters(), lr= Hyperparameters.LEARNING_RATE)

    replay_buffer = deque(maxlen=Hyperparameters.REPLAY_BUFFER_SIZE)

    global_step = 0
    num_episode = 0
    epsilon = 1
    start_time = time.time()
    returns = []
    steps = []
    loss_array = []

    loop = tqdm(total=Hyperparameters.MAX_GLOBAL_STEPS, position=0, leave=False)

    result_dataset = []

    print("Global Step", Hyperparameters.MAX_GLOBAL_STEPS)
    while global_step < Hyperparameters.MAX_GLOBAL_STEPS:
        episode_step = 0
        episode_return = 0
        episode_loss = 0
        done = False
        

        #Initialize
        agent_host = init_malmo(agent_host,recordingsDirectory, video_width,video_height)
        world_state = agent_host.getWorldState()
        while not world_state.has_mission_begun:
            time.sleep(0.1)
            world_state = agent_host.getWorldState()
            #for error in world_state.errors:
                #print("\nError:",error.text)
        obs = get_observation(world_state, agent_host)


        #Testing  
        agent_host.sendCommand( "move 1" )

        while world_state.is_mission_running:
            #Depth Implementation
            while world_state.number_of_video_frames_since_last_state < 1 and world_state.is_mission_running:
                time.sleep(0.05)
                world_state = agent_host.getWorldState()

            if world_state.is_mission_running:
                frame = world_state.video_frames[0].pixels
                processFrame(frame)

                if GET_VISION_DATA:
                    try:
                        result_dataset.append(view_surrounding(video_height, video_width, frame, global_step))
                    except:
                        print("Error in getting image for training data.")
                
                elif VISION_ENABLED:
                    input_img_temp = get_img(world_state,frame,agent_host,eyes,device,video_width,video_height)
                        
                print("Yaw Delta ", current_yaw_delta_from_depth)  

                if current_yaw_delta_from_depth > 0:
                    agent_host.sendCommand(Hyperparameters.ACTION_DICT[1])
                else:
                    agent_host.sendCommand(Hyperparameters.ACTION_DICT[2])
        

            action_idx = get_action(obs, q_network, epsilon)
            command = Hyperparameters.ACTION_DICT[action_idx]

            agent_host.sendCommand(command)
            #agent_host.sendCommand( "turn " + str(current_yaw_delta_from_depth) )

            #time.sleep(.3)

            episode_step += 1
            if episode_step >= Hyperparameters.MAX_EPISODE_STEPS or \
                    (obs[0, int(Hyperparameters.OBS_SIZE/2)+1, int(Hyperparameters.OBS_SIZE/2)] == -1 and \
                    command == 'movesouth 1'):
                done = True
                time.sleep(2)  

            world_state = agent_host.getWorldState()            
                        
            for error in world_state.errors:
                print("Error:", error.text)
            
            next_obs = get_observation(world_state, agent_host) 
        
            reward = 0
            for r in world_state.rewards:
                reward += r.getValue()
            episode_return += reward

            replay_buffer.append((obs, action_idx, next_obs, reward, done))
            obs = next_obs

            global_step += 1
            #print(global_step)
            if global_step == Hyperparameters.MAX_GLOBAL_STEPS:
                break

            if global_step > Hyperparameters.START_TRAINING and global_step % Hyperparameters.LEARN_FREQUENCY == 0:
                batch = prepare_batch(replay_buffer)
                loss = learn(batch, optim, q_network, target_network)
                episode_loss += loss

                if epsilon > Hyperparameters.MIN_EPSILON:
                    epsilon *= Hyperparameters.EPSILON_DECAY

                if global_step % Hyperparameters.TARGET_UPDATE == 0:
                    target_network.load_state_dict(q_network.state_dict())



        num_episode += 1
        returns.append(episode_return)
        loss_array.append(episode_loss)
        steps.append(global_step)
        avg_return = sum(returns[-min(len(returns), 10):]) / min(len(returns), 10)
        loop.update(episode_step)
        loop.set_description('Episode: {} Steps: {} Time: {:.2f} Loss: {:.2f} Last Return: {:.2f} Avg Return: {:.2f}'.format(
            num_episode, global_step, (time.time() - start_time) / 60, episode_loss, episode_return, avg_return))

        if num_episode > 0 and num_episode % 10 == 0:
            log_returns(steps, loss_array)
            #print()

    #print(len(result_dataset))
    np.save("images/image_labels",np.array(result_dataset))
Esempio n. 6
0
import MalmoPython
import malmoutils

agentHost = MalmoPython.AgentHost()

# See if we can parse our extended command line.
malmoutils.parse_command_line(agentHost)

# As we are not recording our video xml should be an empty string.
assert malmoutils.get_video_xml(agentHost) == ''

# Test that we can get a default recording spec.
assert type(malmoutils.get_default_recording_object(agentHost, "test")) == MalmoPython.MissionRecordSpec

# Default recordings directory is ''.
assert malmoutils.get_recordings_directory(agentHost) == ''

def clientInfos(cp):
    return [(c.ip_address, c.control_port, c.command_port) for c in cp.clients]

# Test adding some client infos to a client pool.
clientPool = MalmoPython.ClientPool()
assert len(clientPool.clients) == 0
c1 = ("localhost", 10000, 0)
client1 = MalmoPython.ClientInfo(*c1)
clientPool.add(client1)
assert clientInfos(clientPool) == [c1]
c2 = ("127.0.0.1", 10001, 20001)
client2 = MalmoPython.ClientInfo(*c2)
clientPool.add(client2)
assert clientInfos(clientPool) == [c1, c2]
Esempio n. 7
0
        "damping_factor":dampen_factor
    }
    if SWEEP_DAMPENING_FACTOR:
        print(run)
    run_data.append(run)
    time.sleep(0.5) # Give mod a little time to get back to dormant state.

if SWEEP_DAMPENING_FACTOR:
    import matplotlib
    import numpy
    import pylab

    damp_vals = [x["damping_factor"] for x in run_data]
    reversals = [x["reversals"] for x in run_data]
    total_yaw = [x["total_abs_yaw_deltas"] for x in run_data]
    commands = [x["commands_sent"] for x in run_data]
    
    plot_reversals = pylab.plot(damp_vals, reversals, 'r-', label='Reversals')
    plot_yaw = pylab.plot(damp_vals, total_yaw, 'g-', label='Total abs yaw delta')
    plot_commands = pylab.plot(damp_vals, commands, 'b-', label='Commands sent')
    pylab.xlabel('Damping factor')
    pylab.legend()
    pylab.title("Sweep of dampening factor.")
    if TESTING:
        pylab.savefig(malmoutils.get_recordings_directory(agent_host) + "//dampening_factor_sweep.png")
    else:
        pylab.show()

if TESTING and successful_runs == 0:
    print("No successful runs!")
    exit(1)
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