From 92cf57e54a5bce560c4b5cf6ddd3079ee2a44ee7 Mon Sep 17 00:00:00 2001 From: rudrasohan Date: Thu, 24 Jan 2019 00:46:09 +0530 Subject: [PATCH 1/4] wip changes --- MPE/gym_torcs.py | 14 ++++++-------- MPE/snakeoil3_gym.py | 16 +++++++++------- MPE/torcs_world.py | 9 +++++++-- MPE/train.py | 10 +++++++--- MPE/train_maddpg.py | 5 +++-- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/MPE/gym_torcs.py b/MPE/gym_torcs.py index ef5fe77..5f8b7b2 100644 --- a/MPE/gym_torcs.py +++ b/MPE/gym_torcs.py @@ -7,9 +7,6 @@ import collections as col import os import time -import random - -import theano class TorcsEnv: terminal_judge_start = 100 # If after 100 timestep still no progress, terminated @@ -31,7 +28,8 @@ def __init__(self, vision=False, throttle=False, gear_change=False, main = False self.currState = None - os.system("cd ~/vtorcs3 && ./torcs &") + #os.system("cd ~/vtorcs3 && ./torcs &") + os.system("torcs -t 10000000 -nolaptime &") time.sleep(1.0) os.system(u'sh autostart.sh') @@ -39,8 +37,8 @@ def __init__(self, vision=False, throttle=False, gear_change=False, main = False if throttle is False: # Throttle is generally True self.action_space = spaces.Box(low=-1.0, high=1.0, shape=(1,)) else: - high = np.array([1., 1., 1.], dtype=theano.config.floatX) - low = np.array([-1., 0., 0.], dtype=theano.config.floatX) + high = np.array([1., 1., 1.], dtype=np.float32) + low = np.array([-1., 0., 0.], dtype=np.float32) self.action_space = spaces.Box(low=low, high=high) # steer, accel, brake (according to agent_to_torcs() (check the function definition)) if vision is False: # Vision is generally False @@ -48,8 +46,8 @@ def __init__(self, vision=False, throttle=False, gear_change=False, main = False low = -high self.observation_space = spaces.Box(low, high) else: - high = np.array([1., np.inf, np.inf, np.inf, 1., np.inf, 1., np.inf, 255], dtype=theano.config.floatX) - low = np.array([0., -np.inf, -np.inf, -np.inf, 0., -np.inf, 0., -np.inf, 0], dtype=theano.config.floatX) + high = np.array([1., np.inf, np.inf, np.inf, 1., np.inf, 1., np.inf, 255], dtype=np.float32) + low = np.array([0., -np.inf, -np.inf, -np.inf, 0., -np.inf, 0., -np.inf, 0], dtype=np.float32) self.observation_space = spaces.Box(low=low, high=high) def terminate(self): diff --git a/MPE/snakeoil3_gym.py b/MPE/snakeoil3_gym.py index ce3b8c2..e65d40b 100644 --- a/MPE/snakeoil3_gym.py +++ b/MPE/snakeoil3_gym.py @@ -168,7 +168,7 @@ def setup_connection(self): sockdata= str() try: sockdata,addr= self.so.recvfrom(data_size) - sockdata = sockdata.decode(u'utf-8') + sockdata = sockdata.decode('utf-8') except socket.error as emsg: print(u"Waiting for server on %d............" % self.port) print(u"Count Down : " + str(n_fail)) @@ -244,30 +244,32 @@ def get_servers_input(self, step): while True: try: # Receive server data - sockdata,addr= self.so.recvfrom(data_size) - sockdata = sockdata.decode(u'utf-8') + sockdata, addr = self.so.recvfrom(data_size) + print("GOT SOCKDATA") + sockdata = sockdata.decode('utf-8') except socket.error as emsg: print(u'.') print("Waiting for server data on %d.............." % self.port) print(u"Server count down : " + str(n_fail)) if n_fail < 0: + print("SHUTDOWN") self.shutdown() return -1 n_fail = n_fail_org n_fail -= 1 - if u'***identified***' in sockdata: + if '***identified***' in sockdata: print(u"Client connected on %d.............." % self.port) continue - elif u'***shutdown***' in sockdata: + elif '***shutdown***' in sockdata: print ((u"Server has stopped the race on %d. "+ u"You were in %d place.") % (self.port,self.S.d[u'racePos'])) self.shutdown() return -1 - elif u'***restart***' in sockdata: + elif '***restart***' in sockdata: # What do I do here? print( u"Server has restarted the race on %d." % self.port) # I haven't actually caught the server doing this. @@ -581,7 +583,7 @@ def drive_example(c): # ================ MAIN ================ if __name__ == u"__main__": - C= Client(p=3101) + C= Client(p=3001) for step in range(C.maxSteps,0,-1): C.get_servers_input() drive_example(C) diff --git a/MPE/torcs_world.py b/MPE/torcs_world.py index bcf95b3..3ed8840 100644 --- a/MPE/torcs_world.py +++ b/MPE/torcs_world.py @@ -9,7 +9,7 @@ class Agent(object): def __init__(self,idx=0): self.idx = idx - self.port = 3101+self.idx + self.port = 3001+self.idx self.action_dim = 3 self.state_dim = 65 self.obs = [] @@ -40,6 +40,8 @@ def initialize_agents(self): agent = Agent(idx = i) #agent.s_t = self.get_initial_observation(agent,0) #can remove step from here, can even remove the function self.agent_list.append(agent) + + print("Agents created") ''' for i in range(self.n2): #uncomment this for competitive agents agent = Agent(idx = i + self.n) @@ -56,10 +58,13 @@ def reset_agents(self): self.agent_list[i].client.R.d['meta'] = True def get_initial_observation(self, agent,step_count=0): - agent.client.get_servers_input(step_count) + print("ENTERED FUNCTION#########################{}".format(agent.port)) + out = agent.client.get_servers_input(step_count) obs = agent.client.S.d + print("STATUS {} {}".format(out, agent.port)) ob = self.env.make_observation(obs) agent.s_t = np.hstack((ob.angle, ob.track, ob.trackPos, ob.speedX, ob.speedY, ob.speedZ, ob.wheelSpinVel/100.0, ob.rpm, ob.opponents)) + print(agent.s_t.shape) return agent.s_t def update_agent_state(self, agent): #this should be a function in agent class diff --git a/MPE/train.py b/MPE/train.py index 419eda3..fcc2418 100644 --- a/MPE/train.py +++ b/MPE/train.py @@ -27,7 +27,7 @@ def parse_args(): parser.add_argument("--num-units", type=int, default=300, help="number of units in the mlp") # Checkpointing parser.add_argument("--exp-name", type=str, default=None, help="name of the experiment") - parser.add_argument("--save-dir", type=str, default="/home/meha/maddpgCheckPoints", help="directory in which training state and model should be saved") + parser.add_argument("--save-dir", type=str, default="/home/sohan/shipping/maddpgCheckPoints", help="directory in which training state and model should be saved") parser.add_argument("--save-rate", type=int, default=300, help="save model once every time this many episodes are completed") parser.add_argument("--load-dir", type=str, default="", help="directory in which training state and model are loaded") # Evaluation @@ -70,13 +70,15 @@ def train(arglist): with U.single_threaded_session(): # create world world = World() + print("World Created") # Create environment env = MultiAgentTorcsEnv(world, 0, world.reset_world, world.reward, world.observation, done_callback = world.done) - + print("Env Created") # Create agent trainers obs_shape_n = [env.observation_space[i].shape for i in range(env.n)] num_adversaries = env.adv#min(env.n, arglist.num_adversaries) + print("Adversaries created") trainers = get_trainers(env, num_adversaries, obs_shape_n, arglist) print('Using good policy {} and adv policy {}'.format(arglist.good_policy, arglist.adv_policy)) @@ -100,9 +102,11 @@ def train(arglist): #todo : call reset function here os.system("pkill torcs") - os.system("cd ~/vtorcs3 && ./torcs &") #use the location of torcs installation on your system + os.system("torcs -t 10000000 -nolaptime &") #use the location of torcs installation on your system + print("TORCS STARTED") time.sleep(0.5) os.system('sh autostart.sh') + print("USED AUTOSTART") time.sleep(1) obs_n = [] diff --git a/MPE/train_maddpg.py b/MPE/train_maddpg.py index 419eda3..e1bda5b 100644 --- a/MPE/train_maddpg.py +++ b/MPE/train_maddpg.py @@ -27,7 +27,7 @@ def parse_args(): parser.add_argument("--num-units", type=int, default=300, help="number of units in the mlp") # Checkpointing parser.add_argument("--exp-name", type=str, default=None, help="name of the experiment") - parser.add_argument("--save-dir", type=str, default="/home/meha/maddpgCheckPoints", help="directory in which training state and model should be saved") + parser.add_argument("--save-dir", type=str, default="/home/sohan/shipping/maddpgCheckPoints", help="directory in which training state and model should be saved") parser.add_argument("--save-rate", type=int, default=300, help="save model once every time this many episodes are completed") parser.add_argument("--load-dir", type=str, default="", help="directory in which training state and model are loaded") # Evaluation @@ -100,7 +100,8 @@ def train(arglist): #todo : call reset function here os.system("pkill torcs") - os.system("cd ~/vtorcs3 && ./torcs &") #use the location of torcs installation on your system + #os.system("cd ~/vtorcs3 && ./torcs &") #use the location of torcs installation on your system + os.system("torcs -t 10000000 -nolaptime") time.sleep(0.5) os.system('sh autostart.sh') time.sleep(1) From 71c6c5ac2503a4867ef42862e758038676ed2cd0 Mon Sep 17 00:00:00 2001 From: rudrasohan Date: Thu, 24 Jan 2019 01:33:50 +0530 Subject: [PATCH 2/4] fix timeout problem --- MPE/gym_torcs.py | 2 +- MPE/snakeoil3_gym.py | 2 -- MPE/torcs_world.py | 4 ++-- MPE/train.py | 2 +- MPE/train_maddpg.py | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/MPE/gym_torcs.py b/MPE/gym_torcs.py index 5f8b7b2..6b7b84f 100644 --- a/MPE/gym_torcs.py +++ b/MPE/gym_torcs.py @@ -29,7 +29,7 @@ def __init__(self, vision=False, throttle=False, gear_change=False, main = False self.currState = None #os.system("cd ~/vtorcs3 && ./torcs &") - os.system("torcs -t 10000000 -nolaptime &") + os.system("torcs -nolaptime &") time.sleep(1.0) os.system(u'sh autostart.sh') diff --git a/MPE/snakeoil3_gym.py b/MPE/snakeoil3_gym.py index e65d40b..eed31de 100644 --- a/MPE/snakeoil3_gym.py +++ b/MPE/snakeoil3_gym.py @@ -245,7 +245,6 @@ def get_servers_input(self, step): try: # Receive server data sockdata, addr = self.so.recvfrom(data_size) - print("GOT SOCKDATA") sockdata = sockdata.decode('utf-8') except socket.error as emsg: print(u'.') @@ -253,7 +252,6 @@ def get_servers_input(self, step): print(u"Server count down : " + str(n_fail)) if n_fail < 0: - print("SHUTDOWN") self.shutdown() return -1 n_fail = n_fail_org diff --git a/MPE/torcs_world.py b/MPE/torcs_world.py index 3ed8840..47dda79 100644 --- a/MPE/torcs_world.py +++ b/MPE/torcs_world.py @@ -58,10 +58,10 @@ def reset_agents(self): self.agent_list[i].client.R.d['meta'] = True def get_initial_observation(self, agent,step_count=0): - print("ENTERED FUNCTION#########################{}".format(agent.port)) + #print("ENTERED FUNCTION#########################{}".format(agent.port)) out = agent.client.get_servers_input(step_count) obs = agent.client.S.d - print("STATUS {} {}".format(out, agent.port)) + #print("STATUS {} {}".format(out, agent.port)) ob = self.env.make_observation(obs) agent.s_t = np.hstack((ob.angle, ob.track, ob.trackPos, ob.speedX, ob.speedY, ob.speedZ, ob.wheelSpinVel/100.0, ob.rpm, ob.opponents)) print(agent.s_t.shape) diff --git a/MPE/train.py b/MPE/train.py index fcc2418..c2cb35f 100644 --- a/MPE/train.py +++ b/MPE/train.py @@ -102,7 +102,7 @@ def train(arglist): #todo : call reset function here os.system("pkill torcs") - os.system("torcs -t 10000000 -nolaptime &") #use the location of torcs installation on your system + os.system("torcs -nolaptime &") #use the location of torcs installation on your system print("TORCS STARTED") time.sleep(0.5) os.system('sh autostart.sh') diff --git a/MPE/train_maddpg.py b/MPE/train_maddpg.py index e1bda5b..e24b6ca 100644 --- a/MPE/train_maddpg.py +++ b/MPE/train_maddpg.py @@ -101,7 +101,7 @@ def train(arglist): #todo : call reset function here os.system("pkill torcs") #os.system("cd ~/vtorcs3 && ./torcs &") #use the location of torcs installation on your system - os.system("torcs -t 10000000 -nolaptime") + os.system("torcs -nolaptime") time.sleep(0.5) os.system('sh autostart.sh') time.sleep(1) From c7412493789e929734a541d86dce043eb3d46c27 Mon Sep 17 00:00:00 2001 From: rudrasohan Date: Thu, 24 Jan 2019 01:54:15 +0530 Subject: [PATCH 3/4] fixed relaunch errors --- MPE/gym_torcs.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MPE/gym_torcs.py b/MPE/gym_torcs.py index 6b7b84f..eb62a02 100644 --- a/MPE/gym_torcs.py +++ b/MPE/gym_torcs.py @@ -215,6 +215,9 @@ def get_obs(self): def reset_torcs(self): #print("relaunch torcs") os.system('pkill torcs &') + os.system("torcs -nolaptime &") + time.sleep(1.0) + os.system(u'sh autostart.sh') def agent_to_torcs(self, u): torcs_action = {'steer': u[0]} From 35bf5d0fa9b7deb47d9e89ea7d6a24fd9c622062 Mon Sep 17 00:00:00 2001 From: rudrasohan Date: Thu, 24 Jan 2019 02:14:28 +0530 Subject: [PATCH 4/4] changed savedir to generic path --- MPE/train.py | 2 +- MPE/train_maddpg.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MPE/train.py b/MPE/train.py index c2cb35f..edd2d19 100644 --- a/MPE/train.py +++ b/MPE/train.py @@ -27,7 +27,7 @@ def parse_args(): parser.add_argument("--num-units", type=int, default=300, help="number of units in the mlp") # Checkpointing parser.add_argument("--exp-name", type=str, default=None, help="name of the experiment") - parser.add_argument("--save-dir", type=str, default="/home/sohan/shipping/maddpgCheckPoints", help="directory in which training state and model should be saved") + parser.add_argument("--save-dir", type=str, default="/tmp/MADRaS/maddpgCheckPoints", help="directory in which training state and model should be saved") parser.add_argument("--save-rate", type=int, default=300, help="save model once every time this many episodes are completed") parser.add_argument("--load-dir", type=str, default="", help="directory in which training state and model are loaded") # Evaluation diff --git a/MPE/train_maddpg.py b/MPE/train_maddpg.py index e24b6ca..b452cf8 100644 --- a/MPE/train_maddpg.py +++ b/MPE/train_maddpg.py @@ -27,7 +27,7 @@ def parse_args(): parser.add_argument("--num-units", type=int, default=300, help="number of units in the mlp") # Checkpointing parser.add_argument("--exp-name", type=str, default=None, help="name of the experiment") - parser.add_argument("--save-dir", type=str, default="/home/sohan/shipping/maddpgCheckPoints", help="directory in which training state and model should be saved") + parser.add_argument("--save-dir", type=str, default="/tmp/MADRaS/maddpgCheckPoints", help="directory in which training state and model should be saved") parser.add_argument("--save-rate", type=int, default=300, help="save model once every time this many episodes are completed") parser.add_argument("--load-dir", type=str, default="", help="directory in which training state and model are loaded") # Evaluation