forked from abhisheknaik96/MultiAgentTORCS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayGame_record.py
More file actions
219 lines (172 loc) · 7.01 KB
/
playGame_record.py
File metadata and controls
219 lines (172 loc) · 7.01 KB
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
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 gc
import pyxhook
import pickle
up = False
down = False
left = False
right = False
actions = []
gc.enable()
max_eps = 1
max_steps_eps = 3000
epsilon_start = 0.9
def kbup(event):
global up, down, left, right
if event.Ascii == 81:
left = True
elif event.Ascii == 82:
up = True
elif event.Ascii == 83:
right = True
elif event.Ascii == 84:
down = True
def kbdown(event):
global up, down, left, right
if event.Ascii == 81:
left = False
elif event.Ascii == 82:
up = False
elif event.Ascii == 83:
right = False
elif event.Ascii == 84:
down = False
def playGame(f_diagnostics, train_indicator, port=3101): # 1 means Train, 0 means simply Run
global actions
action_dim = 3 # Steering/Acceleration/Brake
state_dim = 29 # Number of sensors input
env_name = 'Torcs_Env'
# 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)
# 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 # 1 to save the learned weights, 0 otherwise
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):
# Hard-coded steer=0, accel=1 and brake=0, define a_t as per any other algorithm
# a_t = np.asarray([0.0, 1.0, 0.0]) # [steer, accel, brake]
steer = float(left) - float(right)
accel = float(up)
brake = float(down)
a_t = np.asarray([steer, accel, brake])
actions.append(a_t)
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
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('\n\n***Hard reset by some agent***\n\n')
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))
##uncomment this to get some statistics per episode like total distance traversed, average speed, distance from center of track, etc
# 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()
# f_diagnostics = open('output_logs/diagnostics', 'w') #Add date and time to file name
f_diagnostics = ""
hookman = pyxhook.HookManager()
hookman.KeyDown = kbup
hookman.KeyUp = kbdown
hookman.HookKeyboard()
hookman.start()
playGame(f_diagnostics, train_indicator=1, port=port)
pickle.dump(actions, open("saved_actions.p", 'wb'))
print(actions)
hookman.cancel()
# f_diagnostics.close()