-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpyclient.py
156 lines (118 loc) · 4.54 KB
/
pyclient.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
2#!/usr/bin/env python
'''
Created on Apr 4, 2012
@author: lanquarden
Edited by: Ahmed Raafat
'''
import os
import sys
import argparse
import socket
import driver
import carState
import GetState2
if __name__ == '__main__':
pass
# Configure the argument parser
parser = argparse.ArgumentParser(description = 'Python client to connect to the TORCS SCRC server.')
parser.add_argument('--host', action='store', dest='host_ip', default='localhost',
help='Host IP address (default: localhost)')
parser.add_argument('--port', action='store', type=int, dest='host_port', default=3001,
help='Host port number (default: 3001)')
parser.add_argument('--id', action='store', dest='id', default='SCR',
help='Bot ID (default: SCR)')
parser.add_argument('--maxEpisodes', action='store', dest='max_episodes', type=int, default=1,
help='Maximum number of learning episodes (default: 1)')
parser.add_argument('--maxSteps', action='store', dest='max_steps', type=int, default=0,
help='Maximum number of steps (default: 0)')
parser.add_argument('--track', action='store', dest='track', default=None,
help='Name of the track')
parser.add_argument('--stage', action='store', dest='stage', type=int, default=3,
help='Stage (0 - Warm-Up, 1 - Qualifying, 2 - Race, 3 - Unknown)')
arguments = parser.parse_args()
# Print summary
print 'Connecting to server host ip:', arguments.host_ip, '@ port:', arguments.host_port
print 'Bot ID:', arguments.id
print 'Maximum episodes:', arguments.max_episodes
print 'Maximum steps:', arguments.max_steps
print 'Track:', arguments.track
print 'Stage:', arguments.stage
print '*********************************************'
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error, msg:
print 'Could not make a socket.'
sys.exit(-1)
# one second timeout
sock.settimeout(1.0)
shutdownClient = False
curEpisode = 0
verbose = False
d = driver.Driver(arguments.stage)
while not shutdownClient:
while True:
print 'Sending id to server: ', arguments.id
buf = arguments.id + d.init()
print 'Sending init string to server:', buf
try:
sock.sendto(buf, (arguments.host_ip, arguments.host_port))
except socket.error, msg:
print "Failed to send data...Exiting..."
sys.exit(-1)
try:
buf, addr = sock.recvfrom(1000)
except socket.error, msg:
print "didn't get response from server..."
if buf.find('***identified***') >= 0:
print 'Received: ', buf
break
currentStep = 0
while True:
#print(State(d.state.sensors["speedX"],))
# wait for an answer from server
buf = None
try:
buf, addr = sock.recvfrom(1000)
except socket.error, msg:
print "didn't get response from server..."
if verbose:
print 'Received: ', buf
if buf != None and buf.find('***shutdown***') >= 0:
d.onShutDown()
shutdownClient = True
print 'Client Shutdown'
#print(d.table) #Prints the Qtable
#Creates Xcel File filled with Qtable
d.table.to_csv(path_or_buf="../input_path/Qtable.csv",index=False)
break
if buf != None and buf.find('***restart***') >= 0:
d.onRestart()
print 'Client Restart'
d.table.to_csv(path_or_buf="../input_path/Qtable.csv",index=False)
f=open("../input_path/episode.txt","r")
x=int(f.read())+1
f.close()
f=open("../input_path/episode.txt","w")
f.write(str(x))
f.close()
print("Episod number = "+str(x))
execfile('pyclient.py') #Do After first iteration
break
currentStep += 1
if currentStep != arguments.max_steps:
if buf != None:
buf = d.drive(buf)
else:
buf = '(meta 1)'
if verbose:
print 'Sending: ', buf
if buf != None:
try:
sock.sendto(buf, (arguments.host_ip, arguments.host_port))
except socket.error, msg:
print "Failed to send data...Exiting..."
sys.exit(-1)
curEpisode += 1
if curEpisode == arguments.max_episodes:
shutdownClient = True
sock.close()