This commit is contained in:
2019-10-31 13:30:50 +01:00
parent 545ffd21ac
commit 2e27d4a0c4
4 changed files with 27 additions and 7 deletions

Binary file not shown.

4
car.py
View File

@@ -134,8 +134,8 @@ class Car(pygame.sprite.Sprite):
# print(f'Car {id(self)} crashed')
return
# else :
# self.probes[idx] = self.vision_length * 2
else:
self.probes[idx] = self.vision_length * 1.1
# print(self.probes)
def probe_brain(self):

25
main.py
View File

@@ -2,12 +2,13 @@
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, GX, GY, screen
from params import CELL_COLOR, FRAMERATE, GX, GY, screen
# https://medium.com/intel-student-ambassadors/demystifying-genetic-algorithms-to-enhance-neural-networks-cde902384b6e
@@ -19,6 +20,7 @@ all_cars = pygame.sprite.Group()
loop = 0
max_fitness = 0
avg_fitness = 0
fps = FRAMERATE
for x in range(100):
@@ -26,14 +28,29 @@ for x in range(100):
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:
@@ -47,12 +64,14 @@ def run_round(all_cars):
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(48)
clock.tick(fps)
# for c in all_cars :
# print(f"Car {id(c)} Fitness : {c.brain.fitness})")
@@ -75,7 +94,7 @@ def run_round(all_cars):
all_cars.add(Car(brain=b))
print("Waiting before new run")
for x in range(1):
time.sleep(0.5)
time.sleep(0.25)
pygame.display.flip()

View File

@@ -5,19 +5,20 @@ FLAGS = HWSURFACE | DOUBLEBUF # | FULLSCREEN
GX = 1000
GY = 1000
FRAMERATE = 24
CELL_COLOR = (80, 80, 80)
CAR_SIZE = 20
CAR_MAX_SPEED = 100
CAR_MAX_FITNESS = 100
CAR_STEERING_FACTOR = 10
MAX_RUN_TIME = 120
VISION_LENGTH = 100
VISION_LENGTH = 75
VISION_SPAN = 35 # degrees
THROTTLE_POWER = 3
MUTATION_RATE = 0.01
SELECTION_ALG = "kway" # roulette
KWAY_TOURNAMENT_PLAYERS = 3
KWAY_TOURNAMENT_PLAYERS = 10
pygame.init()
screen = pygame.display.set_mode((GX, GY), FLAGS)