it s alive, need to smooth fitness
This commit is contained in:
13
car.py
13
car.py
@@ -3,7 +3,7 @@ import random
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from brain import Neural_Network
|
from brain import Neural_Network
|
||||||
from params import GY, CAR_MAX_SPEED, 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
|
from trigo import angle_to_vector, get_line_feats, segments_intersection, distance
|
||||||
|
|
||||||
IMG = pygame.image.load("car20.png")#.convert()
|
IMG = pygame.image.load("car20.png")#.convert()
|
||||||
@@ -21,7 +21,6 @@ class Car(pygame.sprite.Sprite):
|
|||||||
self.image = self.original_image
|
self.image = self.original_image
|
||||||
|
|
||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
self.rect.center = (75, GY -50)
|
|
||||||
self.vision_length = VISION_LENGTH # line liength
|
self.vision_length = VISION_LENGTH # line liength
|
||||||
self.vision_span = VISION_SPAN # degrees
|
self.vision_span = VISION_SPAN # degrees
|
||||||
self.draw_sensors = True
|
self.draw_sensors = True
|
||||||
@@ -50,8 +49,12 @@ class Car(pygame.sprite.Sprite):
|
|||||||
self.run = True
|
self.run = True
|
||||||
|
|
||||||
def reset_car_pos(self):
|
def reset_car_pos(self):
|
||||||
|
self.rect.center = (
|
||||||
|
75 - int(random.random()*20) - 10,
|
||||||
|
GY -50 - int(random.random()*20)-10
|
||||||
|
)
|
||||||
self.speed = 1
|
self.speed = 1
|
||||||
self.heading = 0
|
self.heading = random.random() * 20
|
||||||
self.heading_change = random.random() * 30
|
self.heading_change = random.random() * 30
|
||||||
|
|
||||||
def update_sensors(self):
|
def update_sensors(self):
|
||||||
@@ -99,7 +102,7 @@ class Car(pygame.sprite.Sprite):
|
|||||||
|
|
||||||
def probe_brain(self):
|
def probe_brain(self):
|
||||||
res = self.brain.predict(np.array(self.probes))
|
res = self.brain.predict(np.array(self.probes))
|
||||||
self.heading_change = res[0] * 10
|
self.heading_change = res[0] * 15
|
||||||
self.throttle = res[1] * 10
|
self.throttle = res[1] * 10
|
||||||
|
|
||||||
|
|
||||||
@@ -110,7 +113,7 @@ class Car(pygame.sprite.Sprite):
|
|||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
self.rect.center = old_center
|
self.rect.center = old_center
|
||||||
self.update_position()
|
self.update_position()
|
||||||
if self.speed < 0.01 :
|
if self.speed < 0.01 or self.brain.fitness > CAR_MAX_FITNESS :
|
||||||
self.run = False
|
self.run = False
|
||||||
print(f'Car {id(self)} crashed')
|
print(f'Car {id(self)} crashed')
|
||||||
# print(
|
# print(
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import random
|
import random
|
||||||
from brain import Neural_Network
|
from brain import Neural_Network
|
||||||
|
from params import MUTATION_RATE
|
||||||
|
|
||||||
def genetic_selection(brains):
|
def genetic_selection(brains):
|
||||||
# tot_fitness = sum ([int(b.fitness) for b in brains])
|
# tot_fitness = sum ([int(b.fitness) for b in brains])
|
||||||
@@ -36,7 +37,7 @@ def cross_mutate_genes(p1_gene, p2_gene):
|
|||||||
else :
|
else :
|
||||||
choice = p2_gene[idx]
|
choice = p2_gene[idx]
|
||||||
# Mutation
|
# Mutation
|
||||||
if random.random() < 0.005 :
|
if random.random() < MUTATION_RATE :
|
||||||
choice[random.randint(0, len(choice) - 1)] = random.random()
|
choice[random.randint(0, len(choice) - 1)] = random.random()
|
||||||
print("Mutation !")
|
print("Mutation !")
|
||||||
child.append(choice)
|
child.append(choice)
|
||||||
|
|||||||
8
maps.py
8
maps.py
@@ -7,8 +7,8 @@ def generate_map_1() :
|
|||||||
(int(GX/2 + 75), int(GY-150)),
|
(int(GX/2 + 75), int(GY-150)),
|
||||||
(int(GX/2 + 150), int(GY-150)),
|
(int(GX/2 + 150), int(GY-150)),
|
||||||
(int(GX -75), int(GY/2)),
|
(int(GX -75), int(GY/2)),
|
||||||
(int(GX/2), int(GY/2 - 75)),
|
(int(GX - 100), int(GY/2 - 75)),
|
||||||
(int(GX/2), int(GY/2 - 150)),
|
(int(GX - 100), int(GY/2 - 150)),
|
||||||
(int(GX -50), int( GY/4 )),
|
(int(GX -50), int( GY/4 )),
|
||||||
(int(3*GX/4 - 50), int(50)),
|
(int(3*GX/4 - 50), int(50)),
|
||||||
(int(50), int(50)),
|
(int(50), int(50)),
|
||||||
@@ -22,8 +22,8 @@ def generate_map_1() :
|
|||||||
(int(GX/2 + 50), int(GY-210)),
|
(int(GX/2 + 50), int(GY-210)),
|
||||||
(int(GX/2 + 110), int(GY-210)),
|
(int(GX/2 + 110), int(GY-210)),
|
||||||
(int(GX - 170), int(GY/2 + 30)),
|
(int(GX - 170), int(GY/2 + 30)),
|
||||||
(int(GX/2 - 60 ), int(GY/2 - 20)),
|
(int(GX - 200 ), int(GY/2 - 20)),
|
||||||
(int(GX/2 - 60), int(GY/2 - 200)),
|
(int(GX - 200), int(GY/2 - 200)),
|
||||||
(int(GX -170), int( GY/4 -20)),
|
(int(GX -170), int( GY/4 -20)),
|
||||||
(int(3*GX/4 - 100), int(120)),
|
(int(3*GX/4 - 100), int(120)),
|
||||||
(int(120), int(120)),
|
(int(120), int(120)),
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ GX = 1000
|
|||||||
GY = 1000
|
GY = 1000
|
||||||
CELL_COLOR = (80,80,80)
|
CELL_COLOR = (80,80,80)
|
||||||
CAR_SIZE = 20
|
CAR_SIZE = 20
|
||||||
CAR_MAX_SPEED = 7
|
CAR_MAX_SPEED = 6
|
||||||
|
CAR_MAX_FITNESS = 5000
|
||||||
CAR_STEERING_FACTOR = 10
|
CAR_STEERING_FACTOR = 10
|
||||||
VISION_LENGTH = 50
|
VISION_LENGTH = 60
|
||||||
VISION_SPAN = 35 # degrees
|
VISION_SPAN = 35 # degrees
|
||||||
THROTTLE_POWER = 3
|
THROTTLE_POWER = 3
|
||||||
|
|
||||||
|
MUTATION_RATE = 0.01
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
screen = pygame.display.set_mode((GX, GY), FLAGS)
|
screen = pygame.display.set_mode((GX, GY), FLAGS)
|
||||||
|
|||||||
Reference in New Issue
Block a user