blacked all
This commit is contained in:
BIN
Confirmation_PP_FR8039550.pdf
Normal file
BIN
Confirmation_PP_FR8039550.pdf
Normal file
Binary file not shown.
23
brain.py
23
brain.py
@@ -1,8 +1,13 @@
|
||||
import numpy as np
|
||||
import random
|
||||
|
||||
|
||||
def mat_mult(A, B):
|
||||
return [[sum([A[i][m]*B[m][j] for m in range(len(A[0]))]) for j in range(len(B[0]))] for i in range(len(A))]
|
||||
return [
|
||||
[sum([A[i][m] * B[m][j] for m in range(len(A[0]))]) for j in range(len(B[0]))]
|
||||
for i in range(len(A))
|
||||
]
|
||||
|
||||
|
||||
class Neural_Network(object):
|
||||
# inspired from https://enlight.nyc/projects/neural-network/
|
||||
@@ -17,20 +22,28 @@ class Neural_Network(object):
|
||||
if W1 is not None:
|
||||
self.W1 = W1
|
||||
else:
|
||||
self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # weights from input to hidden layer
|
||||
self.W1 = np.random.randn(
|
||||
self.inputSize, self.hiddenSize
|
||||
) # weights from input to hidden layer
|
||||
|
||||
if W2 is not None:
|
||||
self.W2 = W2
|
||||
else:
|
||||
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # weights from hidden to output layer
|
||||
self.W2 = np.random.randn(
|
||||
self.hiddenSize, self.outputSize
|
||||
) # weights from hidden to output layer
|
||||
# self.w1 = [[random.random() for i in range(self.hiddenSize)] for i in range(self.inputSize)]
|
||||
# self.w2 = [[random.random() for i in range(self.outputSize)] for i in range(self.hiddenSize)]
|
||||
|
||||
def predict(self, X):
|
||||
# forward propagation through our network
|
||||
self.z = np.dot(X, self.W1) # dot product of X (input) and first set of 3x2 weights
|
||||
self.z = np.dot(
|
||||
X, self.W1
|
||||
) # dot product of X (input) and first set of 3x2 weights
|
||||
self.z2 = self.sigmoid(self.z) # activation function
|
||||
self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2) and second set of 3x1 weights
|
||||
self.z3 = np.dot(
|
||||
self.z2, self.W2
|
||||
) # dot product of hidden layer (z2) and second set of 3x1 weights
|
||||
o = self.sigmoid(self.z3) # final activation function
|
||||
# self.z = mat_mult(X, self.w1) # dot product of X (input) and first set of 3x2 weights
|
||||
# self.z2 = self.sigmoid(self.z) # activation function
|
||||
|
||||
69
car.py
69
car.py
@@ -4,7 +4,17 @@ import random
|
||||
import pygame
|
||||
|
||||
from brain import Neural_Network
|
||||
from params import GY, CAR_MAX_SPEED, CAR_MAX_FITNESS, CAR_SIZE, CAR_STEERING_FACTOR, VISION_LENGTH, VISION_SPAN, THROTTLE_POWER, screen
|
||||
from params import (
|
||||
GY,
|
||||
CAR_MAX_SPEED,
|
||||
CAR_MAX_FITNESS,
|
||||
CAR_SIZE,
|
||||
CAR_STEERING_FACTOR,
|
||||
VISION_LENGTH,
|
||||
VISION_SPAN,
|
||||
THROTTLE_POWER,
|
||||
screen,
|
||||
)
|
||||
from trigo import angle_to_vector, get_line_feats, segments_intersection, distance
|
||||
|
||||
IMG = pygame.image.load("car20.png") # .convert()
|
||||
@@ -53,7 +63,7 @@ class Car(pygame.sprite.Sprite):
|
||||
def reset_car_pos(self):
|
||||
self.rect.center = (
|
||||
75 - int(random.random() * 20) - 10,
|
||||
GY -50 - int(random.random()*20)-10
|
||||
GY - 50 - int(random.random() * 20) - 10,
|
||||
)
|
||||
self.speed = 1
|
||||
self.heading = random.random() * 20
|
||||
@@ -62,29 +72,49 @@ class Car(pygame.sprite.Sprite):
|
||||
def update_sensors(self):
|
||||
center = self.rect.center
|
||||
vc = angle_to_vector(self.heading)
|
||||
self.center_sensor = [center, (int(self.vision_length * vc[0] + center[0]), int(-self.vision_length * vc[1] + center[1]))]
|
||||
self.center_sensor = [
|
||||
center,
|
||||
(
|
||||
int(self.vision_length * vc[0] + center[0]),
|
||||
int(-self.vision_length * vc[1] + center[1]),
|
||||
),
|
||||
]
|
||||
|
||||
vl = angle_to_vector(self.heading + self.vision_span)
|
||||
self.left_sensor = [center, (int(self.vision_length * vl[0] + center[0]), int(-self.vision_length * vl[1] + center[1]))]
|
||||
self.left_sensor = [
|
||||
center,
|
||||
(
|
||||
int(self.vision_length * vl[0] + center[0]),
|
||||
int(-self.vision_length * vl[1] + center[1]),
|
||||
),
|
||||
]
|
||||
|
||||
vr = angle_to_vector(self.heading - self.vision_span)
|
||||
self.right_sensor = [center, (int(self.vision_length * vr[0] + center[0]), int(-self.vision_length * vr[1] + center[1]))]
|
||||
|
||||
self.right_sensor = [
|
||||
center,
|
||||
(
|
||||
int(self.vision_length * vr[0] + center[0]),
|
||||
int(-self.vision_length * vr[1] + center[1]),
|
||||
),
|
||||
]
|
||||
|
||||
def update_position(self):
|
||||
vec = angle_to_vector(self.heading)
|
||||
old_center = self.rect.center
|
||||
self.rect.center = (self.speed * vec[0] / 2 + old_center[0], -self.speed * vec[1] / 2 + old_center[1])
|
||||
self.rect.center = (
|
||||
self.speed * vec[0] / 2 + old_center[0],
|
||||
-self.speed * vec[1] / 2 + old_center[1],
|
||||
)
|
||||
self.update_sensors()
|
||||
self.distance_run += int(distance(old_center, self.rect.center))
|
||||
self.brain.fitness = int(math.sqrt(self.distance_run))
|
||||
|
||||
|
||||
|
||||
def probe_lines_proximity(self, lines):
|
||||
# print(self.center_sensor, lines[0])
|
||||
self.probes = [self.vision_length * 2] * 3
|
||||
for idx,sensor in enumerate([self.left_sensor, self.center_sensor, self.right_sensor]) :
|
||||
for idx, sensor in enumerate(
|
||||
[self.left_sensor, self.center_sensor, self.right_sensor]
|
||||
):
|
||||
for line in lines:
|
||||
ip = segments_intersection(sensor, line)
|
||||
# print(ip)
|
||||
@@ -103,13 +133,11 @@ class Car(pygame.sprite.Sprite):
|
||||
# self.probes[idx] = self.vision_length * 2
|
||||
# print(self.probes)
|
||||
|
||||
|
||||
def probe_brain(self):
|
||||
res = self.brain.predict(np.array(self.probes))
|
||||
self.heading_change = res[0] * 15
|
||||
self.throttle = res[1] * 10
|
||||
|
||||
|
||||
def update(self):
|
||||
# rotate
|
||||
old_center = self.rect.center
|
||||
@@ -119,7 +147,7 @@ class Car(pygame.sprite.Sprite):
|
||||
self.update_position()
|
||||
if self.speed < 0.01 or self.brain.fitness > CAR_MAX_FITNESS:
|
||||
self.run = False
|
||||
print(f'Car {id(self)} crashed')
|
||||
print(f"Car {id(self)} crashed")
|
||||
# print(
|
||||
# 'id', id(self),
|
||||
# 'Speed', self.speed,
|
||||
@@ -143,12 +171,15 @@ class Car(pygame.sprite.Sprite):
|
||||
|
||||
super().update()
|
||||
|
||||
|
||||
def show_features(self):
|
||||
if self.draw_sensors:
|
||||
pygame.draw.line(screen, (255,0,0), self.center_sensor[0], self.center_sensor[1])
|
||||
pygame.draw.line(screen, (0,255,0), self.left_sensor[0], self.left_sensor[1])
|
||||
pygame.draw.line(screen, (0,0,255), self.right_sensor[0], self.right_sensor[1])
|
||||
pygame.draw.line(
|
||||
screen, (255, 0, 0), self.center_sensor[0], self.center_sensor[1]
|
||||
)
|
||||
pygame.draw.line(
|
||||
screen, (0, 255, 0), self.left_sensor[0], self.left_sensor[1]
|
||||
)
|
||||
pygame.draw.line(
|
||||
screen, (0, 0, 255), self.right_sensor[0], self.right_sensor[1]
|
||||
)
|
||||
pygame.draw.circle(screen, (125, 255, 125), self.rect.center, 4, 2)
|
||||
|
||||
|
||||
|
||||
19
genetics.py
19
genetics.py
@@ -3,6 +3,7 @@ import random
|
||||
from brain import Neural_Network
|
||||
from params import MUTATION_RATE, SELECTION_ALG, KWAY_TOURNAMENT_PLAYERS
|
||||
|
||||
|
||||
def kway_selection(brains, exclude=None):
|
||||
tourn_pool = []
|
||||
best_play = None
|
||||
@@ -16,6 +17,7 @@ def kway_selection(brains, exclude=None):
|
||||
best_play = new_play
|
||||
return best_play
|
||||
|
||||
|
||||
def genetic_selection(brains):
|
||||
parents_pool = []
|
||||
half_pop = int(len(brains) / 2)
|
||||
@@ -24,12 +26,7 @@ def genetic_selection(brains):
|
||||
for x in range(half_pop):
|
||||
p1 = kway_selection(brains)
|
||||
p2 = kway_selection(brains, exclude=p1)
|
||||
parents_pool.append([
|
||||
p1,
|
||||
p2
|
||||
])
|
||||
|
||||
|
||||
parents_pool.append([p1, p2])
|
||||
|
||||
elif SELECTION_ALG == "roulette":
|
||||
# does not seem very optimized... TBR
|
||||
@@ -39,17 +36,13 @@ def genetic_selection(brains):
|
||||
for b in brains:
|
||||
wheel += [b] * b.fitness
|
||||
|
||||
|
||||
tot_fitness = len(wheel)
|
||||
|
||||
# selection of pool/2 pair of parents to reproduce
|
||||
for _ in range(half_pop):
|
||||
idx1 = round(random.random() * tot_fitness - 1)
|
||||
idx2 = round(random.random() * tot_fitness - 1)
|
||||
parents_pool.append([
|
||||
wheel[idx1],
|
||||
wheel[idx2]
|
||||
])
|
||||
parents_pool.append([wheel[idx1], wheel[idx2]])
|
||||
return parents_pool
|
||||
|
||||
|
||||
@@ -81,7 +74,3 @@ def genetic_reproduction(parents_pool):
|
||||
new_pop.append(c_brain1)
|
||||
new_pop.append(c_brain2)
|
||||
return new_pop
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
8
main.py
8
main.py
@@ -10,7 +10,6 @@ from maps import map1
|
||||
from params import CELL_COLOR, screen
|
||||
|
||||
|
||||
|
||||
# https://medium.com/intel-student-ambassadors/demystifying-genetic-algorithms-to-enhance-neural-networks-cde902384b6e
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
@@ -18,7 +17,6 @@ clock = pygame.time.Clock()
|
||||
map_lines = map1
|
||||
|
||||
|
||||
|
||||
all_cars = pygame.sprite.Group()
|
||||
|
||||
for x in range(100):
|
||||
@@ -49,16 +47,16 @@ def run_round(all_cars):
|
||||
# for c in all_cars :
|
||||
# print(f"Car {id(c)} Fitness : {c.brain.fitness})")
|
||||
|
||||
print('Collecting brains')
|
||||
print("Collecting brains")
|
||||
brains = [c.brain for c in all_cars]
|
||||
print(f"Max fitness = {max([b.fitness for b in brains])}")
|
||||
print(f"Avg fitness = {sum([b.fitness for b in brains])/len(brains)}")
|
||||
print('selecting')
|
||||
print("selecting")
|
||||
parents_pool = genetic_selection(brains)
|
||||
# import ipdb; ipdb.set_trace()
|
||||
print("breeding")
|
||||
new_brains = genetic_reproduction(parents_pool)
|
||||
print(f'building {len(new_brains)} cars with new brains')
|
||||
print(f"building {len(new_brains)} cars with new brains")
|
||||
all_cars.empty()
|
||||
for b in new_brains:
|
||||
all_cars.add(Car(brain=b))
|
||||
|
||||
2
maps.py
2
maps.py
@@ -1,5 +1,6 @@
|
||||
from params import GX, GY
|
||||
|
||||
|
||||
def generate_map_1():
|
||||
path = [
|
||||
(25, int(GY - 25)),
|
||||
@@ -37,4 +38,5 @@ def generate_map_1() :
|
||||
lines = lines + lines2
|
||||
return lines
|
||||
|
||||
|
||||
map1 = generate_map_1()
|
||||
6
trigo.py
6
trigo.py
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
import math
|
||||
|
||||
|
||||
def angle_to_vector(angle):
|
||||
angle = angle * math.pi / 180
|
||||
return [math.cos(angle), math.sin(angle)]
|
||||
@@ -25,7 +26,6 @@ def segments_intersection(line1, line2):
|
||||
if p3[0] == p4[0]:
|
||||
p3 = (p3[0] + 1, p3[1])
|
||||
|
||||
|
||||
a1, b1 = get_line_feats(p1, p2)
|
||||
a2, b2 = get_line_feats(p3, p4)
|
||||
|
||||
@@ -34,7 +34,9 @@ def segments_intersection(line1, line2):
|
||||
|
||||
x = (b2 - b1) / (a1 - a2)
|
||||
|
||||
if min(p1[0], p2[0]) <= x <= max (p1[0], p2[0]) and min(p3[0], p4[0]) <= x <= max (p3[0], p4[0]) :
|
||||
if min(p1[0], p2[0]) <= x <= max(p1[0], p2[0]) and min(p3[0], p4[0]) <= x <= max(
|
||||
p3[0], p4[0]
|
||||
):
|
||||
y = a1 * x + b1
|
||||
return x, y
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user