-
Notifications
You must be signed in to change notification settings - Fork 1
/
car.py
171 lines (124 loc) · 6.43 KB
/
car.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
import pygame
from pygame.math import Vector2
from environment.config import Args
import math
import numpy as np
class Car(pygame.sprite.Sprite):
def __init__(self, x, y, configs: Args, index, trackImage, ):
super().__init__()
self.index = index
self.trackImage = trackImage
self.position = Vector2(x, y)
self.carTopPosition = Vector2(x, y)
self.lastPosition = Vector2(x, y)
self.velocity = Vector2(0.0, 0.0)
self.blended = False
self.configs = configs
self.angle = configs.angle
self.length = 16
self.maxAcceleration = configs.maxAcceleration
self.maxSteering = configs.maxSteering
self.maxVelocity = configs.maxVelocity
self.maxBraking = configs.maxBraking
self.freeDeceleration = configs.freeDeceleration
self.steeringValues = [0.0 for i in range(3)]
self.acceleration = 0.0
self.steering = 0.0
self.braking = 0.0
self.distanceToSee = configs.distanceToSee
self.color = np.array([255,255, 255])
self.color[self.index % 3] = int(self.index*200/(configs.numberOfCars + 1))
self.distance = 0
self.reward = 0
self.image_car = pygame.image.load(self.configs.carImagePath)
self.image = self.image_car
self.image_clean = self.image
self.image = pygame.transform.rotate(self.image_clean, self.angle)
self.dead = False
self.laserDistances = [0.0 for i in range(self.configs.numberOfLasers)]
self.pointsToMark = set()
self.genTrackPoint = (self.carTopPosition.x, self.carTopPosition.y)
self.trackPoints = set()
#initialize statehistory
self.stateHistory = [[0.0 for j in range(configs.numberOfLasers + 1)] for i in range(configs.valueStackSize)]
def getPixelAt(self, dist, angleOffset):
loc = [0,0]
loc[0] = min(self.configs.width-1, max(0, int(self.carTopPosition.x + dist*math.cos((self.angle + angleOffset)*math.pi/180))))
loc[1] = min(self.configs.height-1, max(0, int(self.carTopPosition.y - dist*math.sin((self.angle + angleOffset)*math.pi/180))))
return self.trackImage.get_at(loc)
def update(self, action):
if not self.dead:
######### Car physics
self.acceleration = action[1]*self.maxAcceleration
self.braking = action[2]*self.maxBraking
self.velocity += (self.acceleration - self.braking - self.freeDeceleration , 0)
self.velocity.x = max(0, min(self.velocity.x, self.maxVelocity))
self.steering = action[0]*((self.maxSteering - 5)*(1 - (self.velocity.x/self.maxVelocity)) + 5) ##max steering angle should change with speed
self.steeringValues.append(self.steering)
self.steeringValues.pop(0)
self.steering = np.average(self.steeringValues)
if self.steering:
turning_radius = self.length / math.sin(math.radians(self.steering))
angular_velocity = self.velocity.x / turning_radius
else:
angular_velocity = 0
self.position += self.velocity.rotate(-self.angle)
self.angle += math.degrees(angular_velocity)
self.image = pygame.transform.rotate(self.image_clean, self.angle)
self.carTopPosition = Vector2(self.position.x, self.position.y)
self.carTopPosition.x += int(self.image.get_rect().width/2 + self.length*(math.cos(self.angle*math.pi/180)))
self.carTopPosition.y += int(self.image.get_rect().height/2 - self.length*(math.sin(self.angle*math.pi/180)))
######## END CAR PHYSICS######
self.pointsToMark = set()
self.trackPoints = set()
# Distance sensor
for i in range(len(self.configs.anglesToSee)):
angleOffset = self.configs.anglesToSee[i]
pixel = None
low = 0
high = self.distanceToSee
while low <= high:
midJ = (low + high)//2
pixel = self.getPixelAt(midJ, angleOffset)
if pixel[2] == 255: #Alpha 0 => Track
low = midJ + 1
else:
high = midJ - 1
if abs(low - high) <= 2:
loc = (int(self.carTopPosition.x + low*math.cos((self.angle + angleOffset)*3.14/180)), int(self.carTopPosition.y - low*math.sin((self.angle + angleOffset)*3.14/180)))
dist = np.linalg.norm(np.array(loc) - np.array(self.carTopPosition))
self.laserDistances[i] = dist
self.pointsToMark.add(loc)
if dist < 120:
self.trackPoints.add(loc)
break
if pixel[2] == 255: #Alpha 0 => Track
self.laserDistances[i] = self.distanceToSee
self.reward -= 0.05 # Time penalty
# Reward for covering more distance
if (np.linalg.norm(self.lastPosition - self.position)) > 20:
self.reward += 3
self.distance += 20
self.lastPosition = (self.position.x, self.position.y)
if self.reward < -5: # Time death
self.dead = True
self.reward -= 10
for dist in self.laserDistances:
if dist < 5: # Green death
self.dead = True
self.reward -= 10
if self.configs.test: # For marking the track points
self.genTrackPoint = (self.carTopPosition.x, self.carTopPosition.y)
self.state = self.laserDistances.copy()
self.state.append(float(self.velocity.x/self.maxVelocity))
self.stateHistory.pop(0)
self.stateHistory.append(self.state)
return np.array(self.stateHistory).flatten(), self.dead, self.reward, self.distance
def draw(self, surface, cameraPosition):
if self.blended == False and self.dead:
self.image.fill((100, 0, 0, 255), None, pygame.BLEND_RGBA_MULT)
self.blended = True
surface.blit(self.image, self.position - cameraPosition)
if not self.dead:
for point in self.pointsToMark:
surface.fill((255, 255, 255), (point - cameraPosition, (3, 3)))