Esempio n. 1
0
    def _peek_obs(self):
        obs = None
        start_time = time.time()
        while not self.done and (obs is None or len(obs) == 0):
            peek_message = "<Peek/>"
            comms.send_message(self.client_socket, peek_message.encode())
            obs = comms.recv_message(self.client_socket)
            info = comms.recv_message(self.client_socket).decode('utf-8')
            reply = comms.recv_message(self.client_socket)
            done, = struct.unpack('!b', reply)
            self.done = done == 1
            if obs is None or len(obs) == 0:
                if time.time() - start_time > MAX_WAIT:
                    self.client_socket.close()
                    self.client_socket = None
                    raise MissionInitException(
                        'too long waiting for first observation')
                time.sleep(0.1)

            obs = np.frombuffer(obs, dtype=np.uint8)

        if obs is None or len(obs) == 0:
            obs = np.zeros((self.height, self.width, self.depth),
                           dtype=np.int8)
        return obs, info
Esempio n. 2
0
    def _find_server(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((self.server, self.port))
        self._hello(sock)

        start_time = time.time()
        port = 0
        while port == 0:
            comms.send_message(sock, ("<Find>" + self._get_token() +
                                      "</Find>").encode())
            reply = comms.recv_message(sock)
            port, = struct.unpack('!I', reply)
            if port == 0:
                if time.time() - start_time > MAX_WAIT:
                    if self.client_socket:
                        self.client_socket.close()
                        self.client_socket = None
                    raise MissionInitException(
                        'too long finding mission to join')
                time.sleep(1)
        sock.close()
        # print("Found mission integrated server port " + str(port))
        self.integratedServerPort = port
        e = self.xml.find(self.ns + 'MinecraftServerConnection')
        if e is not None:
            e.attrib['port'] = str(self.integratedServerPort)
Esempio n. 3
0
    def step(self, action):
        """gym api step"""
        obs = None
        reward = None
        info = None
        turn = True
        withturnkey = self.step_options < 2
        withinfo = self.step_options == 0 or self.step_options == 2

        warning_displayed = False
        start_time = time.time()

        while not self.done and \
                ((obs is None or len(obs) == 0) or
                 (withinfo and info is None) or turn):

            if (time.time() - start_time > 10) and not warning_displayed:
                warning_displayed = True
                print(f"WARNING! Long step time for {self.server2}:{self.port2}")
                o = len(obs) if obs is not None else "None"
                print(f"Done={self.done}, len(obs)={o}, turn={turn}")

            step_message = "<Step" + str(self.step_options) + ">" + \
                           self.action_space[action] + \
                           "</Step" + str(self.step_options) + " >"
            comms.send_message(self.client_socket, step_message.encode())
            if withturnkey:
                comms.send_message(self.client_socket, self.turn_key.encode())
            obs = comms.recv_message(self.client_socket)

            reply = comms.recv_message(self.client_socket)
            reward, done, sent = struct.unpack('!dbb', reply)
            self.done = done == 1
            if withinfo:
                info = comms.recv_message(self.client_socket).decode('utf-8')

            turn_key = comms.recv_message(self.client_socket).decode('utf-8') if withturnkey else ""
            # print("[" + str(self.role) + "] TK " + turn_key + " self.TK " + str(self.turn_key))
            if turn_key != "":
                if sent != 0:
                    turn = False
                # Done turns if: turn = self.turn_key == turn_key
                self.turn_key = turn_key
            else:
                turn = sent == 0

            if (obs is None or len(obs) == 0) or turn:
                time.sleep(0.1)
            obs = np.frombuffer(obs, dtype=np.uint8)

        if self.reshape:
            if obs.size == 0:
                obs = np.zeros((self.height, self.width, self.depth), dtype=np.uint8)
            else:
                obs = obs.reshape((self.height, self.width, self.depth)).astype(np.uint8)
        self.last_obs = obs

        # RLlib requires info be returned as a dict rather than a string
        info = { "raw_info": info }
        return obs, reward, self.done, info
Esempio n. 4
0
    def reinit(self):
        """Use carefully to reset the episode count to 0."""
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((self.server, self.port))
        self._hello(sock)

        comms.send_message(sock, ("<Init>" + self._get_token() + "</Init>").encode())
        reply = comms.recv_message(sock)
        sock.close()
        ok, = struct.unpack('!I', reply)
        return ok != 0
Esempio n. 5
0
    def step(self, action):
        """gym api step"""
        obs = None
        reward = None
        info = None
        turn = True
        withturnkey = self.step_options < 2
        print(withturnkey)
        withinfo = self.step_options == 0 or self.step_options == 2

        while not self.done and \
                ((obs is None or len(obs) == 0) or
                 (withinfo and info is None) or turn):
            step_message = "<Step" + str(self.step_options) + ">" + \
                           self.action_space[action] + \
                           "</Step" + str(self.step_options) + " >"
            t0 = time.time()
            comms.send_message(self.client_socket, step_message.encode())
            print("send action {}".format(time.time() - t0))
            t0 = time.time()
            if withturnkey:
                comms.send_message(self.client_socket, self.turn_key.encode())
            obs = comms.recv_message(self.client_socket)
            print("recieve obs {}".format(time.time() - t0))
            t0 = time.time()

            reply = comms.recv_message(self.client_socket)
            reward, done, sent = struct.unpack('!dbb', reply)
            print("recieve reward {}".format(time.time() - t0))
            t0 = time.time()
            self.done = done == 1
            if withinfo:
                info = comms.recv_message(self.client_socket).decode('utf-8')

            turn_key = comms.recv_message(
                self.client_socket).decode('utf-8') if withturnkey else ""
            # print("[" + str(self.role) + "] TK " + turn_key + " self.TK " + str(self.turn_key))
            if turn_key != "":
                if sent != 0:
                    turn = False
                # Done turns if: turn = self.turn_key == turn_key
                self.turn_key = turn_key
            else:
                turn = sent == 0

            # if (obs is None or len(obs) == 0) or turn:
            # time.sleep(0.1)
            print("turnkeyprocessor {}".format(time.time() - t0))
            t0 = time.time()
            obs = np.frombuffer(obs, dtype=np.uint8)
            print("creating obs from buffer {}".format(time.time() - t0))
            t0 = time.time()
        return obs, reward, self.done, info
Esempio n. 6
0
    def exit(self):
        """Use carefully to cause the Minecraft service to exit (and hopefully restart).
        Likely to throw communication errors so wrap in exception handler.
        """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((self.server2, self.port2))
        self._hello(sock)

        comms.send_message(sock, ("<Exit>" + self._get_token() + "</Exit>").encode())
        reply = comms.recv_message(sock)
        sock.close()
        ok, = struct.unpack('!I', reply)
        return ok != 0
Esempio n. 7
0
    def step(self, action):
        """gym api step"""
        obs = None
        reward = None
        info = None
        turn = True
        withturnkey = self.step_options < 2
        withinfo = self.step_options == 0 or self.step_options == 2

        while not self.done and \
                ((obs is None or len(obs) == 0) or
                 (withinfo and info is None) or turn):
            step_message = "<Step" + str(self.step_options) + ">" + \
                           self.action_space[action] + \
                           "</Step" + str(self.step_options) + " >"
            comms.send_message(self.client_socket, step_message.encode())
            if withturnkey:
                comms.send_message(self.client_socket, self.turn_key.encode())
            obs = comms.recv_message(self.client_socket)

            reply = comms.recv_message(self.client_socket)
            reward, done, sent = struct.unpack('!dbb', reply)
            self.done = done == 1
            if withinfo:
                info = comms.recv_message(self.client_socket).decode('utf-8')

            turn_key = comms.recv_message(
                self.client_socket).decode('utf-8') if withturnkey else ""
            # print("[" + str(self.role) + "] TK " + turn_key + " self.TK " + str(self.turn_key))
            if turn_key != "":
                if sent != 0:
                    turn = False
                # Done turns if: turn = self.turn_key == turn_key
                self.turn_key = turn_key
            else:
                turn = sent == 0

            if (obs is None or len(obs) == 0) or turn:
                time.sleep(0.1)
            obs = np.frombuffer(obs, dtype=np.uint8)

        if self.reshape:
            if obs.size == 0:
                obs = np.zeros((self.height, self.width, self.depth),
                               dtype=np.uint8)
            else:
                obs = obs.reshape(
                    (self.height, self.width, self.depth)).astype(np.uint8)
        self.last_obs = obs

        return obs, reward, self.done, info
Esempio n. 8
0
    def _init_mission(self):
        ok = 0
        while ok != 1:
            xml = etree.tostring(self.xml)
            token = (self._get_token() + ":" + str(self.agent_count)).encode()
            # print(xml.decode())
            comms.send_message(self.client_socket, xml)
            comms.send_message(self.client_socket, token)

            reply = comms.recv_message(self.client_socket)
            ok, = struct.unpack('!I', reply)
            self.turn_key = comms.recv_message(self.client_socket).decode('utf-8')
            if ok != 1:
                time.sleep(1)
Esempio n. 9
0
    def status(self, head):
        """Get status from server.
        head - Ping the the head node if True.
        """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if head:
            sock.connect((self.server, self.port))
        else:
            sock.connect((self.server2, self.port2))
        self._hello(sock)

        comms.send_message(sock, "<Status/>".encode())
        status = comms.recv_message(sock).decode('utf-8')
        sock.close()
        return status
Esempio n. 10
0
    def status(self, head):
        """Get status from server.
        head - Ping the the head node if True.
        """
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        if head:
            sock.connect((self.server, self.port))
        else:
            sock.connect((self.server2, self.port2))
        self._hello(sock)

        comms.send_message(sock, "<Status/>".encode())
        status = comms.recv_message(sock).decode('utf-8')
        sock.close()
        return status
Esempio n. 11
0
    def close(self):
        """gym api close"""
        try:
            # Purge last token from head node with <Close> message.
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect((self.server, self.port))
            self._hello(sock)

            comms.send_message(sock, ("<Close>" + self._get_token() + "</Close>").encode())
            reply = comms.recv_message(sock)
            ok, = struct.unpack('!I', reply)
            assert ok
            sock.close()
        except Exception as e:
            self._log_error(e)
        if self.client_socket:
            self.client_socket.close()
            self.client_socket = None
    def init_miss(self, raw_xml):
        ok = 0
        while ok != 1:
            # print(xml)
            # new_xml = etree.parse(xml)
            xml = etree.tostring(self.build_xml(raw_xml))  #self.env.xml)
            # print(xml)

            token = (self.env._get_token() + ":" +
                     str(self.env.agent_count)).encode()
            # print(xml.decode())
            comms.send_message(self.env.client_socket, xml)
            comms.send_message(self.env.client_socket, token)

            reply = comms.recv_message(self.env.client_socket)
            ok, = struct.unpack('!I', reply)
            self.turn_key = comms.recv_message(
                self.env.client_socket).decode('utf-8')
            if ok != 1:
                time.sleep(1)
Esempio n. 13
0
    def step(self, action):
        """gym api step"""
        obs = None
        reward = None
        info = None
        turn = True
        withturnkey = self.step_options < 2
        withinfo = self.step_options == 0 or self.step_options == 2

        while not self.done and \
                ((obs is None or len(obs) == 0) or
                 (withinfo and info is None) or turn):
            step_message = "<Step" + str(self.step_options) + ">" + \
                           self.action_space[action] + \
                           "</Step" + str(self.step_options) + " >"
            comms.send_message(self.client_socket, step_message.encode())
            if withturnkey:
                comms.send_message(self.client_socket, self.turn_key.encode())
            obs = comms.recv_message(self.client_socket)

            reply = comms.recv_message(self.client_socket)
            reward, done, sent = struct.unpack('!dbb', reply)
            self.done = done == 1
            if withinfo:
                info = comms.recv_message(self.client_socket).decode('utf-8')

            turn_key = comms.recv_message(self.client_socket).decode('utf-8') if withturnkey else ""
            # print("[" + str(self.role) + "] TK " + turn_key + " self.TK " + str(self.turn_key))
            if turn_key != "":
                if sent != 0:
                    turn = False
                # Done turns if: turn = self.turn_key == turn_key
                self.turn_key = turn_key
            else:
                turn = sent == 0

            if (obs is None or len(obs) == 0) or turn:
                time.sleep(0.1)
            obs = np.frombuffer(obs, dtype=np.uint8)

        return obs, reward, self.done, info
Esempio n. 14
0
    def _peek_obs(self):
        obs = None
        start_time = time.time()
        while not self.done and (obs is None or len(obs) == 0):
            peek_message = "<Peek/>"
            comms.send_message(self.client_socket, peek_message.encode())
            obs = comms.recv_message(self.client_socket)
            reply = comms.recv_message(self.client_socket)
            done, = struct.unpack('!b', reply)
            self.done = done == 1
            if obs is None or len(obs) == 0:
                if time.time() - start_time > MAX_WAIT:
                    self.client_socket.close()
                    self.client_socket = None
                    raise MissionInitException('too long waiting for first observation')
                time.sleep(0.1)

            obs = np.frombuffer(obs, dtype=np.uint8)

        if obs is None or len(obs) == 0:
            obs = np.zeros((self.height, self.width, self.depth), dtype=np.int8)
        return obs
Esempio n. 15
0
    def _find_server(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((self.server, self.port))
        self._hello(sock)

        start_time = time.time()
        port = 0
        while port == 0:
            comms.send_message(sock, ("<Find>" + self._get_token() + "</Find>").encode())
            reply = comms.recv_message(sock)
            port, = struct.unpack('!I', reply)
            if port == 0:
                if time.time() - start_time > MAX_WAIT:
                    if self.client_socket:
                        self.client_socket.close()
                        self.client_socket = None
                    raise MissionInitException('too long finding mission to join')
                time.sleep(1)
        sock.close()
        # print("Found mission integrated server port " + str(port))
        self.integratedServerPort = port
        e = self.xml.find(self.ns + 'MinecraftServerConnection')
        if e is not None:
            e.attrib['port'] = str(self.integratedServerPort)
Esempio n. 16
0
 def _quit_episode(self):
     comms.send_message(self.client_socket, "<Quit/>".encode())
     reply = comms.recv_message(self.client_socket)
     ok, = struct.unpack('!I', reply)
     return ok != 0
Esempio n. 17
0
 def _hello(sock):
     comms.send_message(sock, ("<MalmoEnv" + malmo_version + "/>").encode())
Esempio n. 18
0
 def _quit_episode(self):
     comms.send_message(self.client_socket, "<Quit/>".encode())
     reply = comms.recv_message(self.client_socket)
     ok, = struct.unpack('!I', reply)
     return ok != 0
Esempio n. 19
0
 def _hello(sock):
     comms.send_message(sock, ("<MalmoEnv" + malmo_version + "/>").encode())