-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecond_phase.py
75 lines (58 loc) · 1.98 KB
/
second_phase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
import neat
import pygame
from car import Car
from constants import *
from util import get_constants_main
def run_simulation_phase_two(genomes, config, with_detection):
print("SECOND PHASE STARTED !!!")
global nets
nets = []
cars = []
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
for i, g in genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
nets.append(net)
g.fitness = 0
sprite_path = 'maps/red_car.png' if i == 1 else 'maps/blue_car.png'
cars.append(Car(sprite_path))
clock = pygame.time.Clock()
MAP, PICKLE_FOLDER = get_constants_main(MAP_INDEX)
game_map = pygame.image.load(MAP).convert()
global current_generation
current_generation += 1
counter = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
for i, car in enumerate(cars):
output = nets[i].activate(car.get_data())
choice = output.index(max(output))
if choice == 0:
car.angle += 10 # Left
elif choice == 1:
car.angle -= 10 # Right
elif choice == 2:
if car.speed - 2 >= 12:
car.speed -= 2 # Slow Down
else:
car.speed += 2 # Speed Up
still_alive = 0
for i, car in enumerate(cars):
if car.is_alive():
still_alive += 1
car.update(2, cars[1 - i], screen, with_detection)
genomes[i][1].fitness += car.get_reward()
if still_alive == 0:
break
counter += 1
if counter == 30 * 40:
break
screen.blit(game_map, (0, 0))
for car in cars:
if car.is_alive():
car.draw_only_car(screen)
pygame.display.flip()
clock.tick(60)