-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrocket.py
375 lines (282 loc) · 15 KB
/
rocket.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
from pyglet import shapes
import math
import pygame
from pyglet.window import key
from vision_line import vision_line
from utils import collides_with_obstacles, collision_point_circle
import mymap
import numpy as np
from fastbook import *
import copy
import itertools
vec2 = pygame.math.Vector2
MAX_ACCELERATION = 400
class rocket:
def __init__(self, batch, brain, show_vision_lines = True, ):
self.position = (100, 100)
self.x_speed =0
self.y_speed =0
self.rotation = 0
self.rotate_speed = 5.0
self.acceleration =0
self.points =0
self.show_vision_lines = show_vision_lines
self.sprite = shapes.Rectangle(0, 0, 30, 60, color=(255, 22, 20), batch=batch)
self.sprite.anchor_x = self.sprite.width / 2
self.sprite.anchor_y = self.sprite.height / 2
self.status = "alive"
self.fitness= 0
self.life = 0
self.life_reward = 0
self.brain = brain
self.current_reward_gate_id = 0
self.batch = batch
self.vision_lines = [
vision_line(self.position, 0, 200,batch, self.show_vision_lines),
vision_line(self.position, -15, 200, batch, self.show_vision_lines),
vision_line(self.position, 15, 200, batch, self.show_vision_lines),
vision_line(self.position, -30,200, batch, self.show_vision_lines),
vision_line(self.position, 30,200, batch, self.show_vision_lines),
vision_line(self.position, -45,200, batch, self.show_vision_lines),
vision_line(self.position, 45,200, batch, self.show_vision_lines),
vision_line(self.position, -60,200, batch, self.show_vision_lines),
vision_line(self.position, 60,200, batch, self.show_vision_lines),
vision_line(self.position, -75,200, batch, self.show_vision_lines),
vision_line(self.position, 75,200, batch, self.show_vision_lines),
vision_line(self.position, -90,200, batch, self.show_vision_lines),
vision_line(self.position, 90,200, batch, self.show_vision_lines)
]
self.vision_lines1 = pd.DataFrame(data={
"x": np.full(11, 100),
"y": np.full(11, 100),
"x2": np.full(11, 100 + 200), #origine + length of vision line
"y2": np.full(11, 100),
"length": np.full(11, 200),
"angle": np.arange(-75, 76, 15),
})
# self.vision_lines1 = pd.DataFrame(data={
# "x": [self.position[0]],
# "y": [self.position[1]],
# "x2": [self.position[0] + 200],
# "y2": [self.position[1]],
# "length": [200],
# "angle": [15]
# })
self.lines = [
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch),
shapes.Line(self.position[0], self.position[1], self.position[0] + 200, 0, color=(30,144,255), batch=batch)
]
self.col_points = [
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch),
shapes.Circle(self.position[0], self.position[1], 5, color=(255, 255, 255), batch=batch)
]
self.calculate_vision_lines()
self.data = torch.tensor([self.position[0], self.position[1], self.rotation, self.points, *[0] * len(self.vision_lines)*2])
#print(self.data.shape)
#def getControls(self, dt):
def getControls(self, dt, keys):
# output = self.brain.read_outputs(self.data)
# if output == 0:
# self.rotation = self.rotation + dt * self.rotate_speed
# if output == 1:
# self.rotation = self.rotation - dt * self.rotate_speed
# if output == 2:
# if(self.acceleration < MAX_ACCELERATION):
# self.acceleration += 25
# else:
# #get slower if the user is not pressing the up key
# if(self.acceleration > 0):
# self.acceleration -= 10
#controls for human player
if keys[key.LEFT]:
self.rotation = self.rotation + dt * self.rotate_speed
if keys[key.RIGHT]:
self.rotation = self.rotation - dt * self.rotate_speed
if keys[key.UP]:
if(self.acceleration < MAX_ACCELERATION):
self.acceleration += 25
#print("\nFORWARD\n")
else:
#get slower if the user is not pressing the up key
if(self.acceleration > 0):
self.acceleration -= 10
def crossover(self, partner):
self.brain.crossover(partner.brain)
def update_vision_lines(self, rotation = 0):
self.calculate_vision_lines(rotation)
return self.check_collisions_vision_lines()
#Calulate vision lines with vectorization
def calculate_vision_lines(self, rotation_degree = 0):
old_angle = self.vision_lines1["angle"].copy()
self.vision_lines1["angle"] = rotation_degree
old_x = self.vision_lines1["x2"] - self.vision_lines1["x"]
old_y = self.vision_lines1["y2"] - self.vision_lines1["y"]
diff_angle = np.radians( -(self.vision_lines1["angle"] - old_angle ))
self.vision_lines1["x2"] = ((old_x * np.cos(diff_angle) + old_y * np.sin(diff_angle)) + self.position[0])
self.vision_lines1["y2"] = ((-old_x * np.sin(diff_angle) + old_y * np.cos(diff_angle)) + self.position[1])
self.vision_lines1["x"] = self.position[0]
self.vision_lines1["y"] = self.position[1]
#juste pour montrer les lignes de collisions
for row in self.vision_lines1.itertuples():
self.lines[row.Index].x = row.x
self.lines[row.Index].y = row.y
self.lines[row.Index].x2 = row.x2
self.lines[row.Index].y2 = row.y2
#Calculate vision lines collision points with vectorization
def check_collisions_vision_lines(self):
#start = time.time()
x1 = self.vision_lines1["x"].to_numpy()[:, None]
y1 = self.vision_lines1["y"].to_numpy()[:, None]
x2 = self.vision_lines1["x2"].to_numpy()[:, None]
y2 = self.vision_lines1["y2"].to_numpy()[:, None]
# end = time.time()
# print("init1: ", end - start)
#start = time.time()
x3 = mymap.obstaclesDf["x1"].values
y3 = mymap.obstaclesDf["y1"].values
x4 = mymap.obstaclesDf["x2"].values
y4 = mymap.obstaclesDf["y2"].values
# end = time.time()
# print("init2: ", end - start)
uA = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))
uB = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))
intersectionX = np.where((0 <= uA) & (uA <= 1), x1 + (uA * (x2 - x1)), np.inf)
intersectionY = np.where((0 <= uB) & (uB <= 1), y1 + (uA * (y2 - y1)), np.inf)
#calculate the distance between the car and the collision point for each vision line
#start = time.time()
dist = np.sqrt(((intersectionX[:] - x1[0])**2 + (intersectionY[:] - y1[0])**2).astype(float))
#get the minimum distance row index
min_row = np.nanargmin(dist, axis=1)
coll_points_X = intersectionX[range(11), min_row]
coll_points_Y = intersectionY[range(11), min_row]
#showing collision points
for i in range(len(coll_points_X)):
self.col_points[i].x = coll_points_X[i]
self.col_points[i].y = coll_points_Y[i]
if(np.isnan(coll_points_X[i]) or np.isnan(coll_points_Y[i])):
self.col_points[i].visible = False
else:
self.col_points[i].visible = True
return [coll_points_X, coll_points_Y]
#def update(self, dt):
def update(self,dt, keys):
self.getControls(dt, keys)
#self.getControls(dt)
self.x_speed = math.cos(self.rotation) * self.acceleration
self.y_speed = math.sin(self.rotation) * self.acceleration
self.position = (self.position[0] + dt * self.x_speed, self.position[1] + dt * self.y_speed)
self.sprite.rotation = 90 - math.degrees(self.rotation)
self.sprite.x = self.position[0]
self.sprite.y = self.position[1]
rotation_degree = math.degrees(self.rotation)
collisions = np.array([])
start = time.time()
collisions = self.update_vision_lines(rotation_degree)
# print(collisions)
# for vision_line in self.vision_lines:
# vision_line.update(rotation_degree, self.position)
# collisions = np.append(collisions, vision_line.collision_point)
#print(collisions)
# end = time.time()
# print("total time: ", end - start)
# print("---------------------------------")
# for obstacle in mymap.obstacles:
# if(collides_with(self.sprite, obstacle.sprite)):
# self.status = "dead"
# self.points = self.points * 0.95
# #print("dead by obstacle")
if(self.current_reward_gate_id < len(mymap.reward_gates) and
collides_with(self.sprite, mymap.reward_gates[self.current_reward_gate_id].sprite)):
self.points += 25 + 100 / ((self.life_reward) * 0.2)
self.current_reward_gate_id += 1
self.life_reward = 0
if(collision_point_circle(self.position, mymap.big_prize)):
#print("big prize")
self.status = "won"
self.points += 100+ 1000 / (self.life_reward * 0.2)
if(collision_wall(self.position)):
self.status = "dead"
self.points = self.points * 0.95
#print("dead by wall")
if(self.life > 20000):
self.status = "dead"
#print("dead by life")
if(self.status == "dead" ):
self.sprite.color = (255, 255, 255)
self.points += 100 / self.calculate_shortest_distance_to_reward()
#print("distance: ", self.calculate_shortest_distance_to_reward())
#print("points: ", self.points)
else:
self.life += 1
self.life_reward += 1
self.sprite.color = (255, 22, 20)
self.data = torch.tensor([self.position[0], self.position[1], self.rotation, self.points, *self.flattened_collision_points()])
def flattened_collision_points(self):
collisions = np.array([])
for o in self.vision_lines:
collisions = np.append(collisions, o.collision_point)
return collisions
def reset(self):
self.position = (100, 100)
self.x_speed =0
self.y_speed =0
self.rotation = 0
self.acceleration =0
self.points =0
self.sprite.position = self.position
self.status = "alive"
self.fitness= 0
self.life = 0
self.life_reward = 0
self.current_reward_gate_id = 0
def calculate_shortest_distance_to_reward(self):
if(self.current_reward_gate_id < len(mymap.reward_gates)):
return self.calculate_shortest_distance_to_reward_gate()
if(self.current_reward_gate_id == len(mymap.reward_gates)):
return math.sqrt((self.position[0] - mymap.big_prize.x)**2 + (self.position[1] - mymap.big_prize.y )**2)
def calculate_shortest_distance_to_reward_gate(self):
current_gate = mymap.reward_gates[self.current_reward_gate_id]
shortest_distance = 100000
#depending on the angle of the reward gate, calculate shortest distance
#using multiple points with interval of 20 pixels
#print("Current reward gate: ", self.current_reward_gate_id)
if(current_gate.rotation == 0):
upper_limit = current_gate.sprite.width
else:
upper_limit = current_gate.sprite.height
for possible_col in range(0, upper_limit, 20):
#print("rotation: ", current_gate.rotation)
if(current_gate.rotation == 0):
distance = math.sqrt((self.position[0] - (current_gate.sprite.x + possible_col))**2 + (self.position[1] - current_gate.sprite.y )**2)
else:
# print((self.current_reward_gate.sprite.x , self.current_reward_gate.sprite.y+ possible_col))
# print(self.position)
# self.short = shapes.Line(self.position[0], self.position[1], self.current_reward_gate.sprite.x, self.current_reward_gate.sprite.y + possible_col, color=(255, 255, 255), batch=self.batch)
distance = math.sqrt((self.position[0] - current_gate.sprite.x)**2 + (self.position[1] - (current_gate.sprite.y + possible_col))**2)
if(distance < shortest_distance):
shortest_distance = distance
return shortest_distance
def sigmoid(x):
return 1 / (1 + math.e ** -x)
def collision_wall(position):
if(position[0] < 0 or position[0] > mymap.window_dimensions[0] or position[1] < 0 or position[1] > mymap.window_dimensions[1]):
return True
return False