Commit 31473a0e authored by nimishsantosh107's avatar nimishsantosh107
Browse files

zmq-client, timeout updated, fastforwarding logic cleaned

parent 3f6e2cfa
__pycache__ __pycache__
deepracer-gym/deepracer_gym.egg-info/ deepracer-gym/deepracer_gym.egg-info/
\ No newline at end of file playground/
\ No newline at end of file
import zmq import zmq
import time
import msgpack import msgpack
import msgpack_numpy as m import msgpack_numpy as m
...@@ -10,12 +11,13 @@ class DeepracerZMQClient: ...@@ -10,12 +11,13 @@ class DeepracerZMQClient:
self.host = host self.host = host
self.port = port self.port = port
self.socket = zmq.Context().socket(zmq.REQ) self.socket = zmq.Context().socket(zmq.REQ)
self.socket.set(zmq.SNDTIMEO, 20000) # Large timout for first connection
self.socket.set(zmq.RCVTIMEO, 20000) self.socket.set(zmq.SNDTIMEO, 600000) # 10m
self.socket.set(zmq.RCVTIMEO, 600000)
self.socket.connect(f"tcp://{self.host}:{self.port}") self.socket.connect(f"tcp://{self.host}:{self.port}")
def set_agent_ready(self): def set_agent_ready(self):
packed_msg = msgpack.packb({"Agent Ready": 1}) packed_msg = msgpack.packb({"ready": 1})
self.socket.send(packed_msg) self.socket.send(packed_msg)
def recieve_response(self): def recieve_response(self):
...@@ -35,26 +37,21 @@ class DeepracerEnvHelper: ...@@ -35,26 +37,21 @@ class DeepracerEnvHelper:
self.zmq_client = DeepracerZMQClient() self.zmq_client = DeepracerZMQClient()
self.zmq_client.set_agent_ready() self.zmq_client.set_agent_ready()
self.obs = None self.obs = None
self.previous_done = False
def send_act_rcv_obs(self, action): def send_act_rcv_obs(self, action):
action_dict = {"action": action} action_dict = {"action": action}
self.obs = self.zmq_client.send_msg(action_dict) self.obs = self.zmq_client.send_msg(action_dict)
self.previous_done = self.obs['_game_over']
return self.obs return self.obs
def env_reset(self): def env_reset(self):
if self.obs is None: # First communication to zmq server if self.obs is None: # First communication to zmq server
self.obs = self.zmq_client.recieve_response() self.obs = self.zmq_client.recieve_response()
elif self.previous_done: # To prevent dummy episode on already done env # Smaller timeout after first connection
pass self.zmq_client.socket.set(zmq.SNDTIMEO, 20000) # 20s
else: # Can't reset env before episode completes - Passing '1' until episode completes self.zmq_client.socket.set(zmq.RCVTIMEO, 20000)
action = 1
done = False else: # If prev_episode done and reset called, fast forward one step for new episode
while not done: self.obs = self.send_act_rcv_obs(4) # Action ignored due to reset()
self.obs = self.send_act_rcv_obs(action)
done = self.obs['_game_over']
self.previous_done = True
return self.obs return self.obs
...@@ -70,7 +67,7 @@ class DeepracerEnvHelper: ...@@ -70,7 +67,7 @@ class DeepracerEnvHelper:
if __name__ == "__main__": if __name__ == "__main__":
client = DeepracerZMQClient() client = DeepracerZMQClient()
packed_msg = msgpack.packb({"Ready": 1}) packed_msg = msgpack.packb({"ready": 1})
client.socket.send(packed_msg) client.socket.send(packed_msg)
episodes_completed = 0 episodes_completed = 0
steps_completed = 0 steps_completed = 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment