Files
racing_pyai/main.py
2019-10-31 13:30:50 +01:00

106 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python
import math
import pygame
import random
import sys
import time
from car import Car
from genetics import genetic_selection, genetic_reproduction
from maps import map1
from params import CELL_COLOR, FRAMERATE, 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)
small_font = pygame.font.SysFont("hack", 12)
map_lines = map1
all_cars = pygame.sprite.Group()
loop = 0
max_fitness = 0
avg_fitness = 0
fps = FRAMERATE
for x in range(100):
car = Car()
all_cars.add(car)
def process_keys():
global fps
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key in [pygame.K_KP_PLUS, pygame.K_PLUS]:
fps = int(fps * 1.2)
elif event.key in [pygame.K_KP_MINUS, pygame.K_MINUS]:
fps = int(fps / 1.25)
elif event.key in [pygame.K_ESCAPE]:
sys.exit(0)
def run_round(all_cars):
global max_fitness
global avg_fitness
global fps
running_cars = True
while running_cars:
running_cars = False
screen.fill(CELL_COLOR)
all_cars.draw(screen)
process_keys()
for c in all_cars:
c.show_features()
if c.run:
running_cars = True
c.probe_lines_proximity(map_lines)
c.probe_brain()
c.update()
for line in map_lines:
pygame.draw.line(screen, (255, 255, 255), line[0], line[1])
text = font.render(f"Gen # : {loop}", True, (128, 128, 128))
mft = small_font.render(f"max fitness : {max_fitness}", True, (128, 128, 128))
aft = small_font.render(f"avg fitness : {avg_fitness}", True, (128, 128, 128))
fpst = small_font.render(f"FPS : {fps}", True, (128, 128, 128))
screen.blit(text, (GX - 50 - text.get_width(), text.get_height() // 2))
screen.blit(mft, (GX - 50 - text.get_width(), text.get_height() // 2 + 30))
screen.blit(aft, (GX - 50 - text.get_width(), text.get_height() // 2 + 50))
screen.blit(fpst, (GX - 50 - text.get_width(), text.get_height() // 2 + 75))
pygame.display.flip()
clock.tick(fps)
# for c in all_cars :
# print(f"Car {id(c)} Fitness : {c.brain.fitness})")
print("Collecting brains")
brains = [c.brain for c in all_cars]
max_fitness = round(max([b.fitness for b in brains]), 2)
avg_fitness = round(sum([b.fitness for b in brains]) / len(brains), 2)
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")
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")
all_cars.empty()
for b in new_brains:
all_cars.add(Car(brain=b))
print("Waiting before new run")
for x in range(1):
time.sleep(0.25)
pygame.display.flip()
while True:
loop += 1
run_round(all_cars)
# pygame.display.flip()
# clock.tick(24)