working , rc release !
This commit is contained in:
22
car.py
22
car.py
@@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
import math
|
||||
import numpy as np
|
||||
import random
|
||||
@@ -10,6 +11,7 @@ from params import (
|
||||
CAR_MAX_FITNESS,
|
||||
CAR_SIZE,
|
||||
CAR_STEERING_FACTOR,
|
||||
MAX_RUN_TIME,
|
||||
VISION_LENGTH,
|
||||
VISION_SPAN,
|
||||
THROTTLE_POWER,
|
||||
@@ -59,6 +61,7 @@ class Car(pygame.sprite.Sprite):
|
||||
self.probe_brain()
|
||||
self.run = True
|
||||
self.distance_run = 0
|
||||
self.creation_dt = datetime.datetime.now()
|
||||
|
||||
def reset_car_pos(self):
|
||||
self.rect.center = (
|
||||
@@ -107,7 +110,9 @@ class Car(pygame.sprite.Sprite):
|
||||
)
|
||||
self.update_sensors()
|
||||
self.distance_run += int(distance(old_center, self.rect.center))
|
||||
self.brain.fitness = int(math.sqrt(self.distance_run))
|
||||
self.brain.fitness = int(
|
||||
math.sqrt(self.distance_run)
|
||||
) # - 2 # penalize jittering
|
||||
|
||||
def probe_lines_proximity(self, lines):
|
||||
# print(self.center_sensor, lines[0])
|
||||
@@ -123,7 +128,7 @@ class Car(pygame.sprite.Sprite):
|
||||
pygame.draw.circle(screen, (125, 125, 255), ip, 4, 2)
|
||||
dist = int(distance(ip, self.rect.center))
|
||||
self.probes[idx] = min(dist, self.probes[idx])
|
||||
if dist < 1.2 * self.speed or self.speed < 0.01:
|
||||
if dist < 1 * self.speed or self.speed < 0.01:
|
||||
self.run = False
|
||||
self.speed = 0
|
||||
# print(f'Car {id(self)} crashed')
|
||||
@@ -135,8 +140,8 @@ class Car(pygame.sprite.Sprite):
|
||||
|
||||
def probe_brain(self):
|
||||
res = self.brain.predict(np.array(self.probes))
|
||||
self.heading_change = res[0] * 15
|
||||
self.throttle = res[1] * 10
|
||||
self.heading_change = res[0] * 30
|
||||
self.throttle = res[1] * 5
|
||||
|
||||
def update(self):
|
||||
# rotate
|
||||
@@ -145,7 +150,14 @@ class Car(pygame.sprite.Sprite):
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.center = old_center
|
||||
self.update_position()
|
||||
if self.speed < 0.01 or self.brain.fitness > CAR_MAX_FITNESS:
|
||||
run_time = (datetime.datetime.now() - self.creation_dt).seconds
|
||||
if run_time > MAX_RUN_TIME:
|
||||
print("RUNTIME EXCEEDED")
|
||||
if (
|
||||
self.speed < 0.01
|
||||
or self.brain.fitness > CAR_MAX_FITNESS
|
||||
or run_time > MAX_RUN_TIME
|
||||
):
|
||||
self.run = False
|
||||
print(f"Car {id(self)} crashed")
|
||||
# print(
|
||||
|
||||
12
main.py
12
main.py
@@ -7,12 +7,12 @@ import time
|
||||
from car import Car
|
||||
from genetics import genetic_selection, genetic_reproduction
|
||||
from maps import map1
|
||||
from params import CELL_COLOR, screen
|
||||
from params import CELL_COLOR, GX, GY, screen
|
||||
|
||||
|
||||
# https://medium.com/intel-student-ambassadors/demystifying-genetic-algorithms-to-enhance-neural-networks-cde902384b6e
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
font = pygame.font.SysFont("hack", 24)
|
||||
|
||||
map_lines = map1
|
||||
|
||||
@@ -40,6 +40,8 @@ def run_round(all_cars):
|
||||
|
||||
for line in map_lines:
|
||||
pygame.draw.line(screen, (255, 255, 255), line[0], line[1])
|
||||
text = font.render(f"Generation {loop}", True, (128, 128, 128))
|
||||
screen.blit(text, (GX - 50 - text.get_width(), text.get_height() // 2))
|
||||
|
||||
pygame.display.flip()
|
||||
clock.tick(48)
|
||||
@@ -66,7 +68,9 @@ def run_round(all_cars):
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
loop = 0
|
||||
while True:
|
||||
loop += 1
|
||||
run_round(all_cars)
|
||||
pygame.display.flip()
|
||||
clock.tick(24)
|
||||
# pygame.display.flip()
|
||||
# clock.tick(24)
|
||||
|
||||
14
maps.py
14
maps.py
@@ -5,15 +5,18 @@ def generate_map_1():
|
||||
path = [
|
||||
(25, int(GY - 25)),
|
||||
(int(GX / 2), int(GY - 25)),
|
||||
(int(GX / 2 + 75), int(GY - 150)),
|
||||
(int(GX / 2 + 75), int(GY - 130)),
|
||||
(int(GX / 2 + 150), int(GY - 150)),
|
||||
(int(GX - 75), int(GY / 2)),
|
||||
(int(GX - 100), int(GY / 2 - 75)),
|
||||
(int(GX - 100), int(GY / 2 - 150)),
|
||||
(int(GX - 50), int(GY / 4)),
|
||||
(int(3 * GX / 4 - 50), int(50)),
|
||||
(int(50), int(50)),
|
||||
(int(150), int(75)),
|
||||
(int(50), int(150)),
|
||||
(int(100), int(GY / 2)),
|
||||
(int(120), int(GY / 2) + 75),
|
||||
(25, int(GY - 150)),
|
||||
(25, int(GY - 25)),
|
||||
]
|
||||
|
||||
@@ -21,14 +24,17 @@ def generate_map_1():
|
||||
(100, int(GY - 85)),
|
||||
(int(GX / 2 - 50), int(GY - 85)),
|
||||
(int(GX / 2 + 50), int(GY - 210)),
|
||||
(int(GX / 2 + 110), int(GY - 210)),
|
||||
(int(GX / 2 + 110), int(GY - 220)),
|
||||
(int(GX - 170), int(GY / 2 + 30)),
|
||||
(int(GX - 200), int(GY / 2 - 20)),
|
||||
(int(GX - 200), int(GY / 2 - 200)),
|
||||
(int(GX - 170), int(GY / 4 - 20)),
|
||||
(int(3 * GX / 4 - 100), int(120)),
|
||||
(int(120), int(120)),
|
||||
(int(180), int(140)),
|
||||
(int(120), int(180)),
|
||||
(int(175), int(GY / 2)),
|
||||
(int(220), int(GY / 2) + 100),
|
||||
(100, int(GY - 120)),
|
||||
(100, int(GY - 85)),
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user