Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interacting via ZMQ #2

Open
rejuvyesh opened this issue Aug 8, 2017 · 2 comments
Open

Interacting via ZMQ #2

rejuvyesh opened this issue Aug 8, 2017 · 2 comments

Comments

@rejuvyesh
Copy link
Member

An example:

import zmq
import gym
from gym import spaces


class ZMQConnection:

    def __init__(self, ip, port):
        self._ip = ip
        self._port = port

        self._context = zmq.Context()
        self._socket = self._context.socket(zmq.REQ)
        self._socket.connect("tcp://{}:{}".format(ip, port))

    @property
    def socket(self):
        return self._socket

    def sendreq(self, msg):
        self.socket.send_json(msg)
        respmsg = self.socket.recv_json()
        return respmsg


class POMDPsZMQEnv(gym.Env):

    def __init__(self, ip='127.0.0.1', port=9393):
        self._conn = ZMQConnection(ip, port)

    def _step(self, action):
        data = self._conn.sendreq({"cmd": "step", "args": int(action + 1)})
        assert 'obs' in data
        assert 'rew' in data
        assert 'done' in data
        assert 'info' in data
        return data['obs'], data['rew'], data['done'], data['info']

    def _reset(self):
        data = self._conn.sendreq({"cmd": "reset"})
        assert 'obs' in data
        return data['obs']

    @property
    def action_space(self):
        data = self._conn.sendreq({"cmd": "n_actions"})
        assert 'n_actions' in data
        return spaces.Discrete(data['n_actions'])

    @property
    def observation_space(self):
        data = self._conn.sendreq({"cmd": "obs_dimensions"})
        assert 'obs_dim' in data
        return spaces.Box(-10, 10, shape=tuple(data['obs_dim']))


if __name__ == '__main__':
    env = POMDPsZMQEnv()
    obs = env.reset()
    while True:
        action = env.action_space.sample()
        ob, reward, done, _ = env.step(action)

        print("s ->{}".format(obs))
        print("a ->{}".format(action))
        print("sp->{}".format(ob))
        print("r ->{}".format(reward))

        obs = ob
        if done:
            break

    env.close()
@MaximeBouton
Copy link
Contributor

Shouldn't this go in the README.md ?

@zsunberg
Copy link
Member

Really, it should go in a python package in another repository that follows standard openai gym conventions. That would be extremely useful, especially if there was convenient machinery to use existing POMDP models without writing any julia code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants