-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction_mode.py
397 lines (334 loc) · 13.6 KB
/
prediction_mode.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import pygame
import random
from enum import Enum
from collections import namedtuple
import csv
from input import InputBox
import pandas as pd
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
import numpy as np
import pickle
pygame.init()
font_heading = pygame.font.Font('arial/arial.ttf',25)
font_text = pygame.font.Font('arial/arial.ttf',15)
# Reset
# Reward
# Play(action) -> Direction
# Game_Iteration
# is_collision
class Direction(Enum):
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
NONE = 5
BLOCK_SIZE=10
SPEED =10
WHITE = (255,255,255)
RED = (200,0,0)
BLUE1 = (0,0,255)
BLUE2 = (0,100,255)
BLACK = (0,0,0)
WHITE = (255, 255, 255)
CAR_LEN = 20
CAR_WID = 20
DEFAULT_SPEED = 40
INCREMENT = 5
#used for recording
SECONDS_PER_DATA = 15
RECORD_INTERVAL = 1
#Data stuff
RECORD = False
DATA = []
if RECORD:
with open('car_passing_data-1.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',',quoting = csv.QUOTE_NONNUMERIC)
for row in csv_reader:
DATA.append(row)
WRITE_FILE = '11-23-2022-example-3.csv'
#Model Machine Learning Stuff
# load model
filename = "./models/first.pickle"
model = pickle.load(open(filename, "rb"))
# Pause / Play Button
PAUSE_IMG = pygame.transform.scale(pygame.image.load("./assets/pause.png").convert(), (50, 50))
PLAY_IMG = pygame.transform.scale(pygame.image.load("./assets/play.png").convert(), (50, 50))
#PASSING
# Car = namedtuple('Car','x , y , speed')
class Car:
def __init__(self, x, y, speed):
self.x = x
self.y = y
self.speed = speed
self.dragging = False
class CarGame:
def __init__(self,write_file,w=900,h=600):
self.w=w
self.h=h
#init display
self.display = pygame.display.set_mode((self.w,self.h))
pygame.display.set_caption('Car')
self.clock = pygame.time.Clock()
#init game state
self.direction = Direction.NONE
self.score = 0
self.game_over = False
self.quit_button = pygame.Rect(800, 500, 50, 30)
#timer options
self.seconds = 0
self.pause = False #this will double as play function
self.success = "None"
self.pause_button = pygame.Rect(40, 200, 50, 50)
#labels on the side
self.labels = ["Main Car Y: ", "Main Car V: ", "Car 1 Y: ", "Car 1 V: ", "Car 2 Y: ", "Car 2 V: "]
self.input_boxes = []
y = 100
for label in self.labels:
input_box = InputBox(500, y, 50, 32, label)
self.input_boxes.append(input_box)
y += 70
#model_prediction
self.prediction = None
self._reset()
#data index
if RECORD:
self.data_index = 1
temp = DATA[self.data_index]
self._set_scene(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8])
#setting up the write file
self.WRITE_FILE = write_file
def _reset(self):
#starting positions
self.car1 = Car(self.w/2,self.h/2 + 15 * BLOCK_SIZE, DEFAULT_SPEED)
self.car2 = Car(self.w/2 - 2 * CAR_WID, 0, -DEFAULT_SPEED)
self.main = Car(self.w/2, self.h/2 + 20 * BLOCK_SIZE, DEFAULT_SPEED)
self.pause = False
def _set_scene(self, mainy, mainv, car1y, car1v, car2y, car2v):
self.car1 = Car(self.w/2, car1y, car1v)
self.car2 = Car(self.w/2 - 2 * CAR_WID, car2y, -car2v)
self.main = Car(self.w/2, mainy, mainv)
self.pause = False
self.success = "None"
def record(self, record_string):
with open (self.WRITE_FILE,"a") as f:
f.write(record_string)
return
def play_step(self):
cars = [self.main, self.car1, self.car2]
# 1. Collect the user input
for event in pygame.event.get():
if(event.type == pygame.QUIT):
temp = '1,\n' if (self.success == "Passed") else '0,\n'
self.record(temp)
pygame.quit()
quit()
if self.pause:
for box in self.input_boxes:
box.handle_event(event)
if ((event.type == pygame.KEYDOWN) and (event.key == pygame.K_RIGHT)) or \
(event.type == pygame.MOUSEBUTTONDOWN and self.pause_button.collidepoint(pygame.mouse.get_pos())):
#TODO: fix logic completely
#record the last pass or fail
temp = '1,\n' if (self.success == "Passed") else '0,\n'
self.record(temp)
temp = []
for box in self.input_boxes:
if box.text:
temp.append(int(box.text))
#hack
else:
print("apending default value")
temp.append(0)
# hack for adding drag and drop functionality
# instead of changing the input, I will change the actual values before set scene
# TODO: actually fix set scene to be more versatile
temp[0] = self.main.y
temp[2] = self.car1.y
temp[4] = self.car2.y
# model makes a prediction here
self.prediction = model.predict(np.array([484, 50, 450.0, 50, 10.0, 40]).reshape(1, -1))
print(f"Model Predicted: {self.prediction}")
self._set_scene(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5])
self.record(f'{temp[0]},{temp[1]},{temp[2]},{temp[3]},{temp[4]},{temp[5]},')
pygame.time.wait(500)
if(event.type == pygame.KEYDOWN):
if (event.key == pygame.K_SPACE):
self._reset()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if pygame.mouse.get_pressed()[0]:
if self.quit_button.collidepoint(x, y):
self.end_game()
# drag and drop logic
if event.button == 1:
for car in cars:
if pygame.Rect(car.x, car.y, CAR_LEN , CAR_WID).collidepoint(event.pos):
car.dragging = True
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
for car in cars:
car.dragging = False
elif event.type == pygame.MOUSEMOTION:
for car in cars:
if car.dragging:
car.x, car.y = event.pos
# if (event.key == pygame.K_RIGHT):
# self.record()
# self.data_index += 1
# temp = DATA[self.data_index]
# self._set_scene(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8])
# pygame.time.wait(500)
# if(event.type == pygame.KEYDOWN):
# if(event.key == pygame.K_LEFT):
# self.direction = Direction.LEFT
# elif(event.key == pygame.K_RIGHT):
# self.direction = Direction.RIGHT
# elif(event.key == pygame.K_UP):
# self.direction = Direction.UP
# elif(event.key == pygame.K_DOWN):
# self.direction = Direction.DOWN
# else:
# self.direction = Direction.NONE
self.direction = self._passing()
# 2. Move
if not self.pause:
self._move(self.direction)
# self.snake.insert(0,self.head)
# 3. Check if game Over
if(self._is_collision()):
# game_over=True
# return game_over,self.score
self.success = "Failed"
# self.record('0,\n')
# self.data_index += 1
# temp = DATA[self.data_index]
# self._set_scene(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8])
# pygame.time.wait(500)
self.pause = True
if(self._score()):
self.success = "Passed"
# self.record('1,\n')
# self.data_index += 1
# temp = DATA[self.data_index]
# self._set_scene(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8])
# pygame.time.wait(500)
self.pause = True
# self.score += 1
# self._reset()
# 5. Update UI and clock
self._update_ui()
# self.record()
self.clock.tick(SPEED)
# 6. Return game Over and Display Score
return self.game_over,self.score
def _update_ui(self):
self.display.fill(BLACK)
pygame.draw.line(self.display, WHITE, (self.w/2 - 3*CAR_WID, 0),(self.w/2 - 3*CAR_WID, self.h))
pygame.draw.line(self.display, WHITE, (self.w/2 + 2*CAR_WID, 0),(self.w/2 + 2*CAR_WID, self.h))
pygame.draw.rect(self.display, BLUE1, pygame.Rect(self.main.x, self.main.y, CAR_LEN , CAR_WID))
pygame.draw.rect(self.display, RED, pygame.Rect(self.car1.x, self.car1.y, CAR_LEN, CAR_WID))
pygame.draw.rect(self.display, RED, pygame.Rect(self.car2.x, self.car2.y, CAR_LEN, CAR_WID))
pygame.draw.rect(self.display, "pink", self.quit_button)
self.display.blit(font_text.render("Menu",True, BLACK), [self.quit_button.x+5, self.quit_button.y+5])
text = font_heading.render("Score: "+str(self.score)+'\n',True,WHITE)
stats = [font_text.render(f'Main Car: {self.main.speed} mph ({self.main.x}, {self.main.y})',True, WHITE),
font_text.render(f'Car 1: {self.car1.speed} mph ({self.car1.x}, {self.car1.y})', True, WHITE),
font_text.render(f'Car 2: {self.car2.speed} mph ({self.car2.x}, {self.car2.y})', True, WHITE),
font_text.render(f'Prediction: {self.prediction}', True, WHITE),
font_text.render(f'Status: {self.success}', True, WHITE)
]
cursor = 25
self.display.blit(text,[0,0])
for i in range(len(stats)):
self.display.blit(stats[i], [10, cursor])
cursor += 20
# pause and play button
self.display.blit(font_heading.render("Play Button", True, WHITE), [10, cursor])
cursor += 20
temp_button = PLAY_IMG if self.pause else PAUSE_IMG
self.display.blit(temp_button, self.pause_button)
for index, box in enumerate(self.input_boxes):
#hack for velocity input
#TODO: fix this
if index % 2 == 1:
box.draw(self.display)
pygame.display.flip()
def end_game(self):
self.game_over = True
def _score(self):
return self.main.y < self.car1.y and self.main.x >= self.car1.x
def _move(self,direction):
# x = self.main.x
# y = self.main.y
if(direction == Direction.RIGHT):
# x+=BLOCK_SIZE
self.main.x += BLOCK_SIZE
elif(direction == Direction.LEFT):
self.main.x -= BLOCK_SIZE
elif(direction == Direction.DOWN):
# y+=BLOCK_SIZE
self.main.speed -= INCREMENT
elif(direction == Direction.UP):
# y-=BLOCK_SIZE
self.main.speed += INCREMENT
self.main.y -= (self.main.speed - self.car1.speed) * BLOCK_SIZE / 40
#update opposing car
self.car2.y += (self.main.speed) * BLOCK_SIZE / 40
#probability another car shows up is around 50%
if (self.car2.y > self.h and random.randint(1, 10) <= 5):
self.car2.y = 0
def _is_collision(self):
#hit boundary
if(self.main.x>(self.w/2 + 1 * CAR_WID) or self.main.x<(self.w/2 - 3 *CAR_WID) or self.main.y>self.h - BLOCK_SIZE or self.main.y<0):
return True
right = self.car1.x + (CAR_WID * 3/2)
left = self.car1.x - (CAR_WID * 3/2)
top = self.car1.y - (CAR_LEN * 3/2)
bottom = self.car1.y + (CAR_LEN * 3/2)
if (left < self.main.x and self.main.x < right):
if (top < self.main.y and self.main.y < bottom):
return True
right = self.car2.x + (CAR_WID * 3/2)
left = self.car2.x - (CAR_WID * 3/2)
top = self.car2.y - (CAR_LEN * 3/2)
bottom = self.car2.y + (CAR_LEN * 3/2)
if (left < self.main.x and self.main.x < right):
if (top < self.main.y and self.main.y < bottom):
return True
# if(self.head in self.snake[1:]):
# return True
return False
def _passing(self):
if self.main.x > self.car1.x - 3/2 * CAR_WID and self.main.y > self.car1.y - CAR_LEN:
# print(self.main.x, self.main.y, self.car1.x)
# print("go left")
return Direction.LEFT
elif self.main.y > self.car1.y - 3/2 *CAR_LEN:
if self.main.speed >= self.car1.speed + 15:
# print("cruisin to pass")
return Direction.NONE
else:
# print("go up")
return Direction.UP
elif self.main.x < self.car1.x:
# print("go right")
return Direction.RIGHT
#successfully passed
elif self.main.speed > self.car1.speed:
# print("nothing")
return Direction.DOWN
else:
return Direction.NONE
if __name__=="__main__":
game = CarGame()
#Game loop
#game_over=False
while True:
game_over,score=game.play_step()
if(game_over == True):
break
print('Final Score',score)
pygame.quit()