-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
185 lines (138 loc) · 5.05 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import math
import os
import numpy
import pygame
import neat
from BackGround import BG
from Car import Car
from RoadBlock import RoadBlock
import Funcs
WIN_SIZE_X = 1000
WIN_SIZE_Y = 800
WIN = pygame.display.set_mode((WIN_SIZE_X, WIN_SIZE_Y))
pygame.display.set_caption("Race")
pygame.font.init()
STAT_FONT = pygame.font.SysFont("comics", 50)
ROTATE = 2
GEN = 0
def draw(win, cars, roads, bg, score, gen):
bg.draw(win)
for car in cars:
car.draw(win)
for road in roads:
road.draw(win)
# score
score_label = STAT_FONT.render("Score: " + str(score), True, (255, 255, 255))
win.blit(score_label, (WIN_SIZE_X - score_label.get_width() - 15, 10))
# generations
score_label = STAT_FONT.render("Gens: " + str(gen - 1), True, (255, 255, 255))
win.blit(score_label, (10, 10))
# alive
score_label = STAT_FONT.render("Alive: " + str(len(cars)), True, (255, 255, 255))
win.blit(score_label, (10, 50))
pygame.display.update()
def main(genomes, config):
global WIN, GEN
GEN += 1
nets = []
ge = []
cars = []
for genome_id, genome in genomes:
genome.fitness = 0
net = neat.nn.FeedForwardNetwork.create(genome, config)
nets.append(net)
cars.append(Car(WIN_SIZE_X / 2 + 25, 700))
ge.append(genome)
bg = BG()
roads = [RoadBlock(450, 800)]
for i in range(0, 6):
New_Block = RoadBlock(roads[i].x2, roads[i].y2)
roads.append(New_Block)
last_road = roads[len(roads) - 1]
if last_road.x - 100 < 0:
RoadBlock.COMPELLED_TURN = 1
elif last_road.x + RoadBlock.GAP + 100 > WIN_SIZE_X:
RoadBlock.COMPELLED_TURN = -1
score = 0
clock = pygame.time.Clock()
a_pressed = False
d_pressed = False
main_run = True
while main_run and len(cars) > 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
main_run = False
pygame.quit()
quit()
for i, car in enumerate(cars):
if roads[0].collide(car):
ge[i].fitness -= 1
cars.pop(i)
nets.pop(i)
ge.pop(i)
car.rotate()
a1 = math.radians(car.tilt + Car.SENSOR_ANGEL)
a2 = math.radians(car.tilt - Car.SENSOR_ANGEL)
ray1 = pygame.Vector2(-math.sin(a1), -math.cos(a1))
ray2 = pygame.Vector2(-math.sin(a2), -math.cos(a2))
min_d1 = math.inf
min_d2 = math.inf
for road in roads:
d1 = Funcs.distance_to_segment_by_ray(car.center, ray1, [[road.x, road.y], [road.x2, road.y2]])
d2 = Funcs.distance_to_segment_by_ray(car.center, ray2, [[road.x + road.GAP, road.y],
[road.x2 + road.GAP, road.y2]])
if d1 < min_d1:
min_d1 = d1
if d2 < min_d2:
min_d2 = d2
car.distances = [min_d1, min_d2]
if (i < len(cars)) :
ge[i].fitness += 0.1
output = nets[i].activate((car.distances[0], car.distances[1]))
if output[0] > 0.5:
ge[i].fitness += 0.1
a_pressed = True
elif output[0] < 0.5:
a_pressed = False
if output[1] > 0.5:
ge[i].fitness += 0.1
d_pressed = True
elif output[1] < 0.5:
d_pressed = False
if a_pressed and -RoadBlock.MAX_ROT <= car.tilt <= RoadBlock.MAX_ROT:
car.tilt += ROTATE
elif d_pressed and -RoadBlock.MAX_ROT <= car.tilt <= RoadBlock.MAX_ROT:
car.tilt -= ROTATE
rem = []
add_road = False
for road in roads:
road.move()
if road.y2 > WIN_SIZE_Y:
rem.append(road)
if not road.passed and len(cars) > 0 and road.y2 > cars[0].y:
road.passed = True
add_road = True
if add_road:
score += 1
for g in ge:
g.fitness += 5
roads.append(RoadBlock(last_road.x2, last_road.y2))
last_road = roads[len(roads) - 1]
for r in rem:
roads.remove(r)
bg.move()
draw(WIN, cars, roads, bg, score, GEN)
clock.tick(30)
def run(config_file):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet,
neat.DefaultStagnation, config_file)
population = neat.Population(config)
population.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
population.add_reporter(stats)
winner = population.run(main, 50)
print('\nBest genome:\n{!s}'.format(winner))
if __name__ == "__main__":
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config-feedforward.txt")
run(config_path)