forked from abhisheknaik96/MultiAgentTORCS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
playGame_DDPG.py
222 lines (180 loc) · 7.2 KB
/
playGame_DDPG.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import sys
sys.path.append('./sample_DDPG_agent/')
import numpy as np
np.random.seed(1337)
from gym_torcs import TorcsEnv
import snakeoil3_gym as snakeoil3
import collections as col
import random
import argparse
import tensorflow as tf
import timeit
import math
import sys
import os
from configurations import *
from ddpg import *
import gc
gc.enable()
# config parameters are printed in main
def playGame(f_diagnostics, train_indicator, port=3101): # 1 means Train, 0 means simply Run
action_dim = 3 #Steering/Acceleration/Brake
state_dim = 65 #of sensors input
env_name = 'Torcs_Env'
save_location = "./weights/"+str(port)+"/"
agent = DDPG(env_name, state_dim, action_dim, save_location)
# Generate a Torcs environment
print("I have been asked to use port: ", port)
env = TorcsEnv(vision=False, throttle=True, gear_change=False)
ob = None
while ob is None:
try:
client = snakeoil3.Client(p=port, vision=False) # Open new UDP in vtorcs
client.MAX_STEPS = np.inf
client.get_servers_input(0) # Get the initial input from torcs
obs = client.S.d # Get the current full-observation from torcs
ob = env.make_observation(obs)
s_t = np.hstack((ob.angle, ob.track, ob.trackPos, ob.speedX, ob.speedY, ob.speedZ, ob.wheelSpinVel/100.0, ob.rpm, ob.opponents))
except:
pass
EXPLORE = total_explore
episode_count = max_eps
max_steps = max_steps_eps
epsilon = epsilon_start
done = False
epsilon_steady_state = 0.01 # This is used for early stopping.
totalSteps = 0
best_reward = -100000
running_avg_reward = 0.
print("TORCS Experiment Start.")
for i in range(episode_count):
save_indicator = 0
early_stop = 1
total_reward = 0.
info = {'termination_cause':0}
distance_traversed = 0.
speed_array=[]
trackPos_array=[]
print('\n\nStarting new episode...\n')
for step in range(max_steps):
# Take noisy actions during training
if (train_indicator):
epsilon -= 1.0 / EXPLORE
epsilon = max(epsilon, epsilon_steady_state)
a_t = agent.noise_action(s_t,epsilon) #Take noisy actions during training
else:
a_t = agent.action(s_t) # [steer, accel, brake]
try:
ob, r_t, done, info = env.step(step, client, a_t, early_stop)
if done:
break
# print done
# print 'Action taken'
analyse_info(info, printing=False)
s_t1 = np.hstack((ob.angle, ob.track, ob.trackPos, ob.speedX, ob.speedY, ob.speedZ, ob.wheelSpinVel/100.0, ob.rpm, ob.opponents))
distance_traversed += ob.speedX*np.cos(ob.angle) #Assuming 1 step = 1 second
speed_array.append(ob.speedX*np.cos(ob.angle))
trackPos_array.append(ob.trackPos)
#Checking for nan rewards: TODO: This was actually below the following block
if (math.isnan( r_t )):
r_t = 0.0
for bad_r in range( 50 ):
print( 'Bad Reward Found' )
break #Introduced by Anirban
# Add to replay buffer only if training
if (train_indicator):
agent.perceive(s_t,a_t,r_t,s_t1,done) # Add experience to replay buffer
except Exception as e:
print("Exception caught at port " + str(i) + str(e) )
ob = None
while ob is None:
try:
client = snakeoil3.Client(p=port, vision=False) # Open new UDP in vtorcs
client.MAX_STEPS = np.inf
client.get_servers_input(0) # Get the initial input from torcs
obs = client.S.d # Get the current full-observation from torcs
ob = env.make_observation(obs)
except:
pass
continue
total_reward += r_t
s_t = s_t1
# Displaying progress every 15 steps.
if ( (np.mod(step,15)==0) ):
print("Episode", i, "Step", step, "Epsilon", epsilon , "Action", a_t, "Reward", r_t )
totalSteps += 1
if done:
break
if ((save_indicator==1) and (train_indicator ==1 )):
# Uncomment this for saving only the best model.
#if (total_reward >= best_reward):
# print("Now we save model with reward " + str(total_reward) + " previous best reward was " + str(best_reward))
best_reward = total_reward
if(i%300 == 0):
agent.saveNetwork(i)
running_avg_reward = running_average(running_avg_reward, i+1, total_reward)
print("TOTAL REWARD @ " + str(i) +"-th Episode : Num_Steps= " + str(step) + "; Max_steps= " + str(max_steps) +"; Reward= " + str(total_reward) +"; Running average reward= " + str(running_avg_reward))
print("Total Step: " + str(totalSteps))
print("")
print(info)
try:
if 'termination_cause' in info.keys() and info['termination_cause']=='hardReset':
print('Hard reset by some agent')
ob, client = env.reset(client=client, relaunch=True)
else:
ob, client = env.reset(client=client, relaunch=True)
except Exception as e:
print("Exception caught at point B at port " + str(i) + str(e) )
ob = None
while ob is None:
try:
client = snakeoil3.Client(p=port, vision=False) # Open new UDP in vtorcs
client.MAX_STEPS = np.inf
client.get_servers_input(0) # Get the initial input from torcs
obs = client.S.d # Get the current full-observation from torcs
ob = env.make_observation(obs)
except:
print("Exception caught at at point C at port " + str(i) + str(e) )
s_t = np.hstack((ob.angle, ob.track, ob.trackPos, ob.speedX, ob.speedY, ob.speedZ, ob.wheelSpinVel/100.0, ob.rpm, ob.opponents))
# document_episode(i, distance_traversed, speed_array, trackPos_array, info, running_avg_reward, f_diagnostics)
env.end() # This is for shutting down TORCS
print("Finish.")
def document_episode(episode_no, distance_traversed, speed_array, trackPos_array, info, running_avg_reward, f_diagnostics):
"""
Note down a tuple of diagnostic values for each episode
(episode_no, distance_traversed, mean(speed_array), std(speed_array), mean(trackPos_array), std(trackPos_array), info[termination_cause], running_avg_reward)
"""
f_diagnostics.write(str(episode_no)+",")
f_diagnostics.write(str(distance_traversed)+",")
f_diagnostics.write(str(np.mean(speed_array))+",")
f_diagnostics.write(str(np.std(speed_array))+",")
f_diagnostics.write(str(np.mean(trackPos_array))+",")
f_diagnostics.write(str(np.std(trackPos_array))+",")
f_diagnostics.write(str(info['termination_cause'])+",")
f_diagnostics.write(str(running_avg_reward)+"\n")
def running_average(prev_avg, num_episodes, new_val):
total = prev_avg*(num_episodes-1)
total += new_val
return np.float(total/num_episodes)
def analyse_info(info, printing=True):
simulation_state = ['Normal', 'Terminated as car is OUT OF TRACK', 'Terminated as car has SMALL PROGRESS', 'Terminated as car has TURNED BACKWARDS']
if printing and info['termination_cause']!=0:
print(simulation_state[info['termination_cause']])
if __name__ == "__main__":
try:
port = int(sys.argv[1])
except Exception as e:
# raise e
print("Usage : python %s <port>" % (sys.argv[0]))
sys.exit()
print('is_training : ' + str(is_training))
print('Starting best_reward : ' + str(start_reward))
print( total_explore )
print( max_eps )
print( max_steps_eps )
print( epsilon_start )
print('config_file : ' + str(configFile))
# f_diagnostics = open('output_logs/diagnostics_for_window_' + sys.argv[1]+'_with_fixed_episode_length', 'w') #Add date and time to file name
f_diagnostics = ""
playGame(f_diagnostics, train_indicator=0, port=port)
# f_diagnostics.close()