forked from abhisheknaik96/MultiAgentTORCS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playGame_DDPG.py
188 lines (144 loc) · 5.99 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
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
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 = 29 #Number of sensors input
env_name = 'Torcs_Env'
agent = DDPG(env_name, state_dim, action_dim)
# Generate a Torcs environment
print("I have been asked to use port: ", port)
env = TorcsEnv(vision=False, throttle=True, gear_change=False)
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))
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
# Counting the total reward and total steps in the current episode
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) # a_t is of the form: [steer, accel, brake]
ob, r_t, done, info = env.step(step, client, a_t, early_stop)
if done:
break
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))
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
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
# Saving the best model.
if ((save_indicator==1) and (train_indicator ==1 )):
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
agent.saveNetwork()
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)
if 'termination_cause' in info.keys() and info['termination_cause']=='hardReset':
print('Hard reset by some agent')
ob, client = env.reset(client=client)
else:
ob, client = env.reset(client=client, relaunch=True)
s_t = np.hstack((ob.angle, ob.track, ob.trackPos, ob.speedX, ob.speedY, ob.speedZ, ob.wheelSpinVel/100.0, ob.rpm))
# document_episode(i, distance_traversed, speed_array, trackPos_array, info, running_avg_reward, f_diagnostics)
env.end() # Shut 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=1, port=port)
# f_diagnostics.close()