new selections need to set a time limit
This commit is contained in:
5
car.py
5
car.py
@@ -24,7 +24,7 @@ class Car(pygame.sprite.Sprite):
|
|||||||
self.rect = self.image.get_rect()
|
self.rect = self.image.get_rect()
|
||||||
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 = False
|
||||||
|
|
||||||
# lets add 3 sensors as a start
|
# lets add 3 sensors as a start
|
||||||
# 1 straight ahead
|
# 1 straight ahead
|
||||||
@@ -77,7 +77,7 @@ class Car(pygame.sprite.Sprite):
|
|||||||
self.rect.center = (self.speed * vec[0] / 2 + old_center[0], -self.speed * vec[1] / 2 + old_center[1])
|
self.rect.center = (self.speed * vec[0] / 2 + old_center[0], -self.speed * vec[1] / 2 + old_center[1])
|
||||||
self.update_sensors()
|
self.update_sensors()
|
||||||
self.distance_run += int(distance(old_center, self.rect.center))
|
self.distance_run += int(distance(old_center, self.rect.center))
|
||||||
self.brain.fitness = math.sqrt(self.distance_run)
|
self.brain.fitness = int(math.sqrt(self.distance_run))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -89,6 +89,7 @@ class Car(pygame.sprite.Sprite):
|
|||||||
ip = segments_intersection(sensor, line)
|
ip = segments_intersection(sensor, line)
|
||||||
# print(ip)
|
# print(ip)
|
||||||
if ip :
|
if ip :
|
||||||
|
if self.draw_sensors :
|
||||||
pygame.draw.circle(screen, (125,125,255), ip, 4, 2)
|
pygame.draw.circle(screen, (125,125,255), ip, 4, 2)
|
||||||
dist = int(distance(ip,self.rect.center))
|
dist = int(distance(ip,self.rect.center))
|
||||||
self.probes[idx] = min(dist, self.probes[idx])
|
self.probes[idx] = min(dist, self.probes[idx])
|
||||||
|
|||||||
38
genetics.py
38
genetics.py
@@ -1,11 +1,37 @@
|
|||||||
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
|
from params import MUTATION_RATE, SELECTION_ALG, KWAY_TOURNAMENT_PLAYERS
|
||||||
|
|
||||||
|
def kway_selection(brains, exclude=None):
|
||||||
|
tourn_pool = []
|
||||||
|
best_play = None
|
||||||
|
if exclude :
|
||||||
|
brains = [x for x in brains if x != exclude]
|
||||||
|
for x in range(KWAY_TOURNAMENT_PLAYERS):
|
||||||
|
new_play = random.choice(brains)
|
||||||
|
while new_play in tourn_pool :
|
||||||
|
new_play = random.choice(brains)
|
||||||
|
if not best_play or best_play.fitness < new_play.fitness :
|
||||||
|
best_play = new_play
|
||||||
|
return best_play
|
||||||
|
|
||||||
def genetic_selection(brains):
|
def genetic_selection(brains):
|
||||||
# tot_fitness = sum ([int(b.fitness) for b in brains])
|
parents_pool = []
|
||||||
|
half_pop = int(len(brains)/2)
|
||||||
|
|
||||||
|
if SELECTION_ALG == "kway":
|
||||||
|
for x in range(half_pop) :
|
||||||
|
p1 = kway_selection(brains)
|
||||||
|
p2 = kway_selection(brains, exclude=p1)
|
||||||
|
parents_pool.append([
|
||||||
|
p1,
|
||||||
|
p2
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
elif SELECTION_ALG == "roulette" :
|
||||||
# does not seem very optimized... TBR
|
# does not seem very optimized... TBR
|
||||||
# constitute a list where every brain is represented
|
# constitute a list where every brain is represented
|
||||||
# proportionnally to its relative fitness
|
# proportionnally to its relative fitness
|
||||||
@@ -15,14 +41,14 @@ def genetic_selection(brains):
|
|||||||
|
|
||||||
|
|
||||||
tot_fitness = len(wheel)
|
tot_fitness = len(wheel)
|
||||||
half_pop = int(len(brains)/2)
|
|
||||||
|
|
||||||
# selection of pool/2 pair of parents to reproduce
|
# selection of pool/2 pair of parents to reproduce
|
||||||
parents_pool = []
|
|
||||||
for _ in range(half_pop):
|
for _ in range(half_pop):
|
||||||
|
idx1 = round(random.random()*tot_fitness - 1)
|
||||||
|
idx2 = round(random.random()*tot_fitness - 1)
|
||||||
parents_pool.append([
|
parents_pool.append([
|
||||||
wheel[round(random.random()*tot_fitness)],
|
wheel[idx1],
|
||||||
wheel[round(random.random()*tot_fitness)]
|
wheel[idx2]
|
||||||
])
|
])
|
||||||
return parents_pool
|
return parents_pool
|
||||||
|
|
||||||
|
|||||||
4
main.py
4
main.py
@@ -62,8 +62,8 @@ def run_round(all_cars):
|
|||||||
all_cars.empty()
|
all_cars.empty()
|
||||||
for b in new_brains :
|
for b in new_brains :
|
||||||
all_cars.add(Car(brain=b))
|
all_cars.add(Car(brain=b))
|
||||||
print("Waiting 5 secs before new run")
|
print("Waiting before new run")
|
||||||
for x in range(10) :
|
for x in range(1) :
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,16 @@ 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 = 6
|
CAR_MAX_SPEED = 100
|
||||||
CAR_MAX_FITNESS = 5000
|
CAR_MAX_FITNESS = 100
|
||||||
CAR_STEERING_FACTOR = 10
|
CAR_STEERING_FACTOR = 10
|
||||||
VISION_LENGTH = 60
|
VISION_LENGTH = 60
|
||||||
VISION_SPAN = 35 # degrees
|
VISION_SPAN = 35 # degrees
|
||||||
THROTTLE_POWER = 3
|
THROTTLE_POWER = 3
|
||||||
|
|
||||||
MUTATION_RATE = 0.01
|
MUTATION_RATE = 0.01
|
||||||
|
SELECTION_ALG = "kway" # roulette
|
||||||
|
KWAY_TOURNAMENT_PLAYERS = 3
|
||||||
|
|
||||||
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