diff --git a/car.py b/car.py index 68b2079..a1db562 100644 --- a/car.py +++ b/car.py @@ -3,7 +3,7 @@ import random import pygame 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 IMG = pygame.image.load("car20.png")#.convert() @@ -21,7 +21,6 @@ class Car(pygame.sprite.Sprite): self.image = self.original_image self.rect = self.image.get_rect() - self.rect.center = (75, GY -50) self.vision_length = VISION_LENGTH # line liength self.vision_span = VISION_SPAN # degrees self.draw_sensors = True @@ -50,8 +49,12 @@ class Car(pygame.sprite.Sprite): self.run = True 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.heading = 0 + self.heading = random.random() * 20 self.heading_change = random.random() * 30 def update_sensors(self): @@ -99,7 +102,7 @@ class Car(pygame.sprite.Sprite): def probe_brain(self): 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 @@ -110,7 +113,7 @@ class Car(pygame.sprite.Sprite): self.rect = self.image.get_rect() self.rect.center = old_center self.update_position() - if self.speed < 0.01 : + if self.speed < 0.01 or self.brain.fitness > CAR_MAX_FITNESS : self.run = False print(f'Car {id(self)} crashed') # print( diff --git a/genetics.py b/genetics.py index 6fde45b..5653284 100644 --- a/genetics.py +++ b/genetics.py @@ -1,6 +1,7 @@ import numpy as np import random from brain import Neural_Network +from params import MUTATION_RATE def genetic_selection(brains): # tot_fitness = sum ([int(b.fitness) for b in brains]) @@ -36,7 +37,7 @@ def cross_mutate_genes(p1_gene, p2_gene): else : choice = p2_gene[idx] # Mutation - if random.random() < 0.005 : + if random.random() < MUTATION_RATE : choice[random.randint(0, len(choice) - 1)] = random.random() print("Mutation !") child.append(choice) diff --git a/maps.py b/maps.py index dc8665e..0b03be3 100644 --- a/maps.py +++ b/maps.py @@ -7,8 +7,8 @@ def generate_map_1() : (int(GX/2 + 75), int(GY-150)), (int(GX/2 + 150), int(GY-150)), (int(GX -75), int(GY/2)), - (int(GX/2), int(GY/2 - 75)), - (int(GX/2), int(GY/2 - 150)), + (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)), @@ -22,8 +22,8 @@ def generate_map_1() : (int(GX/2 + 50), int(GY-210)), (int(GX/2 + 110), int(GY-210)), (int(GX - 170), int(GY/2 + 30)), - (int(GX/2 - 60 ), int(GY/2 - 20)), - (int(GX/2 - 60), int(GY/2 - 200)), + (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)), diff --git a/params.py b/params.py index e977ae4..7437585 100644 --- a/params.py +++ b/params.py @@ -7,12 +7,14 @@ GX = 1000 GY = 1000 CELL_COLOR = (80,80,80) CAR_SIZE = 20 -CAR_MAX_SPEED = 7 +CAR_MAX_SPEED = 6 +CAR_MAX_FITNESS = 5000 CAR_STEERING_FACTOR = 10 -VISION_LENGTH = 50 +VISION_LENGTH = 60 VISION_SPAN = 35 # degrees THROTTLE_POWER = 3 +MUTATION_RATE = 0.01 pygame.init() screen = pygame.display.set_mode((GX, GY), FLAGS)