adjustements
This commit is contained in:
14
car.py
14
car.py
@@ -62,6 +62,7 @@ class Car(pygame.sprite.Sprite):
|
|||||||
self.run = True
|
self.run = True
|
||||||
self.distance_run = 0
|
self.distance_run = 0
|
||||||
self.creation_dt = datetime.datetime.now()
|
self.creation_dt = datetime.datetime.now()
|
||||||
|
self.run_time = 0
|
||||||
|
|
||||||
def reset_car_pos(self):
|
def reset_car_pos(self):
|
||||||
self.rect.center = (
|
self.rect.center = (
|
||||||
@@ -109,10 +110,9 @@ class Car(pygame.sprite.Sprite):
|
|||||||
-self.speed * vec[1] / 2 + old_center[1],
|
-self.speed * vec[1] / 2 + old_center[1],
|
||||||
)
|
)
|
||||||
self.update_sensors()
|
self.update_sensors()
|
||||||
|
if self.run:
|
||||||
self.distance_run += int(distance(old_center, self.rect.center))
|
self.distance_run += int(distance(old_center, self.rect.center))
|
||||||
self.brain.fitness = int(
|
self.brain.fitness = math.sqrt(self.distance_run)
|
||||||
math.sqrt(self.distance_run)
|
|
||||||
) # - 2 # penalize jittering
|
|
||||||
|
|
||||||
def probe_lines_proximity(self, lines):
|
def probe_lines_proximity(self, lines):
|
||||||
# print(self.center_sensor, lines[0])
|
# print(self.center_sensor, lines[0])
|
||||||
@@ -150,16 +150,16 @@ 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()
|
||||||
run_time = (datetime.datetime.now() - self.creation_dt).seconds
|
self.run_time = (datetime.datetime.now() - self.creation_dt).seconds
|
||||||
if run_time > MAX_RUN_TIME:
|
if self.run_time > MAX_RUN_TIME:
|
||||||
print("RUNTIME EXCEEDED")
|
print("RUNTIME EXCEEDED")
|
||||||
if (
|
if (
|
||||||
self.speed < 0.01
|
self.speed < 0.01
|
||||||
or self.brain.fitness > CAR_MAX_FITNESS
|
or self.brain.fitness > CAR_MAX_FITNESS
|
||||||
or run_time > MAX_RUN_TIME
|
or self.run_time > MAX_RUN_TIME
|
||||||
):
|
):
|
||||||
self.run = False
|
self.run = False
|
||||||
print(f"Car {id(self)} crashed")
|
# print(f"Car {id(self)} crashed")
|
||||||
# print(
|
# print(
|
||||||
# 'id', id(self),
|
# 'id', id(self),
|
||||||
# 'Speed', self.speed,
|
# 'Speed', self.speed,
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ def genetic_selection(brains):
|
|||||||
# proportionnally to its relative fitness
|
# proportionnally to its relative fitness
|
||||||
wheel = []
|
wheel = []
|
||||||
for b in brains:
|
for b in brains:
|
||||||
wheel += [b] * b.fitness
|
wheel += [b] * int(b.fitness)
|
||||||
|
|
||||||
tot_fitness = len(wheel)
|
tot_fitness = len(wheel)
|
||||||
|
|
||||||
|
|||||||
20
main.py
20
main.py
@@ -13,11 +13,13 @@ from params import CELL_COLOR, GX, GY, screen
|
|||||||
# https://medium.com/intel-student-ambassadors/demystifying-genetic-algorithms-to-enhance-neural-networks-cde902384b6e
|
# https://medium.com/intel-student-ambassadors/demystifying-genetic-algorithms-to-enhance-neural-networks-cde902384b6e
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
font = pygame.font.SysFont("hack", 24)
|
font = pygame.font.SysFont("hack", 24)
|
||||||
|
small_font = pygame.font.SysFont("hack", 12)
|
||||||
map_lines = map1
|
map_lines = map1
|
||||||
|
|
||||||
|
|
||||||
all_cars = pygame.sprite.Group()
|
all_cars = pygame.sprite.Group()
|
||||||
|
loop = 0
|
||||||
|
max_fitness = 0
|
||||||
|
avg_fitness = 0
|
||||||
|
|
||||||
|
|
||||||
for x in range(100):
|
for x in range(100):
|
||||||
car = Car()
|
car = Car()
|
||||||
@@ -25,6 +27,8 @@ for x in range(100):
|
|||||||
|
|
||||||
|
|
||||||
def run_round(all_cars):
|
def run_round(all_cars):
|
||||||
|
global max_fitness
|
||||||
|
global avg_fitness
|
||||||
running_cars = True
|
running_cars = True
|
||||||
while running_cars:
|
while running_cars:
|
||||||
running_cars = False
|
running_cars = False
|
||||||
@@ -40,8 +44,12 @@ def run_round(all_cars):
|
|||||||
|
|
||||||
for line in map_lines:
|
for line in map_lines:
|
||||||
pygame.draw.line(screen, (255, 255, 255), line[0], line[1])
|
pygame.draw.line(screen, (255, 255, 255), line[0], line[1])
|
||||||
text = font.render(f"Generation {loop}", True, (128, 128, 128))
|
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))
|
||||||
screen.blit(text, (GX - 50 - text.get_width(), text.get_height() // 2))
|
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))
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
clock.tick(48)
|
clock.tick(48)
|
||||||
@@ -51,6 +59,9 @@ def run_round(all_cars):
|
|||||||
|
|
||||||
print("Collecting brains")
|
print("Collecting brains")
|
||||||
brains = [c.brain for c in all_cars]
|
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"Max fitness = {max([b.fitness for b in brains])}")
|
||||||
print(f"Avg fitness = {sum([b.fitness for b in brains])/len(brains)}")
|
print(f"Avg fitness = {sum([b.fitness for b in brains])/len(brains)}")
|
||||||
print("selecting")
|
print("selecting")
|
||||||
@@ -68,7 +79,6 @@ def run_round(all_cars):
|
|||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
loop = 0
|
|
||||||
while True:
|
while True:
|
||||||
loop += 1
|
loop += 1
|
||||||
run_round(all_cars)
|
run_round(all_cars)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ CAR_SIZE = 20
|
|||||||
CAR_MAX_SPEED = 100
|
CAR_MAX_SPEED = 100
|
||||||
CAR_MAX_FITNESS = 100
|
CAR_MAX_FITNESS = 100
|
||||||
CAR_STEERING_FACTOR = 10
|
CAR_STEERING_FACTOR = 10
|
||||||
MAX_RUN_TIME = 60
|
MAX_RUN_TIME = 120
|
||||||
VISION_LENGTH = 100
|
VISION_LENGTH = 100
|
||||||
VISION_SPAN = 35 # degrees
|
VISION_SPAN = 35 # degrees
|
||||||
THROTTLE_POWER = 3
|
THROTTLE_POWER = 3
|
||||||
|
|||||||
Reference in New Issue
Block a user