-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_colector.py
More file actions
204 lines (142 loc) · 6.26 KB
/
data_colector.py
File metadata and controls
204 lines (142 loc) · 6.26 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
import argparse
import math
import gym
from collections import deque
import numpy as np
from CarRacingDQNAgent import CarRacingDQNAgent
from common_functions import process_state_image2
from common_functions import process_state_image
from common_functions import generate_state_frame_stack_from_queue
import point
import pandas as pd
#caculate the speed
def calSpeed(position1,position2):
x, y = position1
x2, y2 = position2
return math.sqrt((x-x2)**2+(y-y2)**2)
def isInTrack(position,trackList):
x,y=position
pp = point.Point(x, y)
for i in range(len(trackList)):
p1 = point.Point(trackList[i][0][0][0],trackList[i][0][0][1])
p2 = point.Point(trackList[i][0][1][0],trackList[i][0][1][1])
p3 = point.Point(trackList[i][0][2][0],trackList[i][0][2][1])
p4 = point.Point(trackList[i][0][3][0],trackList[i][0][3][1])
if point.IsPointInMatrix(p1, p2, p3, p4, pp):
return True
return False
def isInTrack2(position,trackList):
x,y=position
for i in range(len(trackList)):
xc=(trackList[i][0][0][0]+trackList[i][0][1][0]+trackList[i][0][2][0]+trackList[i][0][3][0])/4
yc=(trackList[i][0][0][1]+trackList[i][0][1][1]+trackList[i][0][2][1]+trackList[i][0][3][1])/4
if (xc-5<x<xc+5)and (yc-5<y<yc+5):
return True
return False
def origin(path,epchos):
#load models and init env
parser = argparse.ArgumentParser(description='Play CarRacing by the trained model.')
parser.add_argument('-m', '--model', required=False,default="models/trial_400.h5", help='The `.h5` file of the trained model.')
parser.add_argument('-e', '--episodes', type=int, default=epchos, help='The number of episodes should the model plays.')
args = parser.parse_args()
train_model = args.model
play_episodes = args.episodes
env = gym.make('CarRacing-v0')
agent = CarRacingDQNAgent(epsilon=0) # Set epsilon to 0 to ensure all actions are instructed by the agent
agent.load(train_model)
# datas need to collect
Speed=[]
Ave_Color_Value=[]
Frames=[]
Safety=[]
lastPosition = [(0,0)]
e_number=[]
end_number=[]
guard = 0
for e in range(play_episodes):
if len(Frames)>10000:
break
frameCounter=0
init_state = env.reset()
init_state = process_state_image(init_state)
total_reward = 0
state_frame_stack_queue = deque([init_state]*agent.frame_stack_num, maxlen=agent.frame_stack_num)
time_frame_counter = 1
while True:
env.render()
current_state_frame_stack = generate_state_frame_stack_from_queue(state_frame_stack_queue)
action = agent.act(current_state_frame_stack)
next_state, reward, done, info = env.step(action)
posx,posy=info[0]
lastPosition.append((posx,posy))
# stuckChecker=0
# if frameCounter>60:
# if lastPosition==info[0]:
# stuckChecker=stuckChecker+1
# if stuckChecker>60:
# ind = ind + 1
# endFrameNumber.append(2000)
# break
if frameCounter!=0 :
Speed.append(calSpeed(lastPosition[frameCounter+1],lastPosition[frameCounter]))
print("speed is {}".format(Speed[frameCounter-1]))
#
# if frameCounter != 1:
# Speed.append(calSpeed(lastPosition[frameCounter], lastPosition[frameCounter - 1]))
# print("speed is {}".format(Speed[frameCounter]))
# else:
# Speed.append(0)
#if speed is 0, then break
if frameCounter>10:
if Speed[frameCounter-1]==0:
for i in range(frameCounter):
end_number.append(frameCounter)
if i + 60 > frameCounter:
Safety.append(0)
else:
Safety.append(1)
break
print(info[0])
#if out of track, then break
if isInTrack(info[0],info[1])==False:
for i in range(frameCounter):
end_number.append(frameCounter)
if i+60>frameCounter:
Safety.append(0)
else:
Safety.append(1)
break
frameCounter=frameCounter+1
print('total accuracy is {}, {} out of {}, total track is {}, reward is {}'.format(guard/frameCounter,guard,frameCounter,len(info[1]),total_reward))
total_reward += reward
print("frameCounter is {}".format(frameCounter))
next_state, ave_pixel_value= process_state_image2(next_state)
Frames.append(next_state)
state_frame_stack_queue.append(next_state)
e_number.append(e)
Ave_Color_Value.append(ave_pixel_value)
if done:
print('Episode: {}/{}, Scores(Time Frames): {}, Total Rewards: {:.2}'.format(e+1, play_episodes, time_frame_counter, float(total_reward)))
for i in range(frameCounter):
end_number.append(frameCounter)
Safety.append(1)
break
time_frame_counter += 1
guard=guard+1
print(len(Speed))
print(len(Ave_Color_Value))
print(len(Safety))
print(e)
# fr = np.array(Frames)
safeties=np.array(Safety)
# np.save("datas/safes.npy", fr)
np.save(path+"/labels.npy", safeties)
# dataframe = pd.DataFrame({'Speed': Speed,})
dataframe = pd.DataFrame({ 'Ave_Color_Value': Ave_Color_Value,'Speed':Speed,'Safety': Safety})
dataframe.to_csv(path+"/safes.csv",index=False,sep=',')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--path', type=str, default="./datas")
parser.add_argument('--epoch', type=int, default=50)
args = parser.parse_args()
origin(args.path,args.epoch)