-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.py
251 lines (196 loc) · 7.88 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
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
import keyboard
import math
import cv2 as cv
import numpy as np
import tensorflow as tf
import sys
class Car:
def __init__(self):
path = "./cars/basic-black.bmp"
#self.__position = [210, 260]
#self.__angle = 0
#self.__speed = 5
self.__content = cv.imread(path, cv.IMREAD_UNCHANGED)
#self.__transformed_content = self.__content
#self.__direction = [round(math.cos(self.__angle)), round(math.sin(self.__angle))]
#self.__bounding_box = [
# [0,0],
# [self.__content.shape[0], self.__content.shape[1]]
#]
self.__actions = [
"up", "down", "right", "left"
#, "s", "a"
]
self.__brain = self.__create_brain()
#self.__sensors = [
# 0, #front
# 0, #right
# 0 #left
#]
#self.__distance = 0
#self.__alive = True
self.reset_state()
def __create_brain(self):
output_length = len(self.__actions)
inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(3, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(output_length, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
def __predict(self, sensors):
input = np.array([sensors])
output = self.__brain.predict(
input,
batch_size=None,
verbose=0,
steps=None,
callbacks=None,
max_queue_size=10,
workers=1,
use_multiprocessing=False,
)
max = np.argmax(output[0])
result = self.__actions[max]
return result
def __rotate(self):
image = self.__content
angle = self.__angle
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rotation_matrix = cv.getRotationMatrix2D(image_center, angle, 1.0)
self.__transformed_content = cv.warpAffine(image, rotation_matrix, image.shape[1::-1], flags=cv.INTER_LINEAR)
def __update_rotation(self, action):
self.__old_angle = self.__angle
if keyboard.is_pressed("up") or action == "up":
self.__angle = 90
if keyboard.is_pressed("down") or action == "down":
self.__angle = -90
if keyboard.is_pressed("right") or action == "right":
self.__angle = 0
if keyboard.is_pressed("left") or action == "left":
self.__angle = 180
def __update_direction(self):
angle = (self.__angle * math.pi) / 180
self.__direction = [int(math.cos(angle)), -1*int(math.sin(angle))]
old_angle = (self.__old_angle * math.pi) / 180
old_direction = [int(math.cos(old_angle)), -1*int(math.sin(old_angle))]
if self.__went_forward_first == None:
self.__went_forward_first = self.__angle == 0
if self.__forward_only:
self.__forward_only = old_direction == self.__direction
if not self.__ping_pong_at_least_one_time:
self.__ping_pong_at_least_one_time = (old_direction == np.dot(-1, self.__direction)).all()
def __update_speed(self, action):
if keyboard.is_pressed("s") or action == "s":
self.__speed = 5
if keyboard.is_pressed("a") or action == "a":
self.__speed = 0
def __translate(self, map):
movement = np.dot(self.__speed, self.__direction)
position = np.add(self.__position, movement)
min_bounding_box = np.add(self.__bounding_box[0], movement)
max_bounding_box = np.add(self.__bounding_box[1], movement)
bounding_box = [min_bounding_box, max_bounding_box]
position_available = map.position_available(bounding_box)
if position_available:
displacement_vector = np.subtract(position, self.__position)
self.__distance += math.sqrt(np.dot(displacement_vector, displacement_vector))
self.__position = position
self.__bounding_box = bounding_box
if not position_available:
self.__alive = False
def __update_sensors(self, map):
min_point, max_point = self.__bounding_box
direction = self.__direction
center_x = int((max_point[0] - min_point[0])/2) + min_point[0]
center_y = int((max_point[1] - min_point[1])/2) + min_point[1]
distances = map.get_distances(direction, (center_x, center_y))
self.__sensors = distances
def __randomize_weights(self):
result = []
brain = self.__brain
for layer in brain.layers:
weights = layer.get_weights()
if len(weights) > 0:
for weight in weights:
#[a, b), b > a -> (b - a) * random_sample() + a
a = -sys.maxsize
b = sys.maxsize
item = (b - a) * np.random.random_sample(weight.shape) + a
result.append(item)
return result
def __translate_chromosome(self, chromosome):
result = []
brain = self.__brain
array_start, array_end = 0, 0
for layer in brain.layers:
weights = layer.get_weights()
if len(weights) > 0:
for weight in weights:
size = weight.shape[0]*weight.shape[1] if len(weight.shape) > 1 else weight.shape[0]
array_end = array_start + size
slice = chromosome[array_start:array_end]
item = np.reshape(slice, weight.shape)
result.append(item)
array_start = array_end
return result
def reset_state(self):
self.__position = [210, 260]
self.__angle = 0
self.__old_angle = 0
self.__went_forward_first = None
self.__forward_only = True
self.__ping_pong_at_least_one_time = False
self.__speed = 5
self.__transformed_content = self.__content
self.__direction = [round(math.cos(self.__angle)), round(math.sin(self.__angle))]
self.__bounding_box = [
[self.__position[0], self.__position[1]],
[self.__content.shape[0] + self.__position[0], self.__content.shape[1] + self.__position[1]]
]
self.__sensors = [
0, #front
0, #right
0 #left
]
self.__distance = 0
self.__alive = True
def set_chromosome(self, chromosome=None):
weights = []
if chromosome == None:
weights = self.__randomize_weights()
else:
weights = self.__translate_chromosome(chromosome)
self.__brain.set_weights(weights)
def get_chromosome(self):
result = []
brain = self.__brain
for layer in brain.layers:
weights = layer.get_weights()
if len(weights) > 0:
for weight in weights:
size = weight.shape[0]*weight.shape[1] if len(weight.shape) > 1 else weight.shape[0]
item = np.reshape(weight, (1,size))[0].tolist()
result += item
return result
def is_alive(self):
return self.__alive
def went_forward_only(self):
return self.__forward_only
def went_forward_first(self):
return self.__went_forward_first
def ping_pong_at_least_one_time(self):
return self.__ping_pong_at_least_one_time
def get_distance(self):
return self.__distance
def get_sensors(self):
return self.__sensors
def transform(self, map):
self.__update_sensors(map)
action = self.__predict(self.__sensors)
self.__update_rotation(action)
self.__update_direction()
self.__update_speed(action)
self.__rotate()
self.__translate(map)
def render(self):
return self.__position, self.__transformed_content