forked from Hilicot/Neural_Network_NEAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
125 lines (96 loc) · 3.4 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
import pygame as py
import neat
import time
import os
import random
from car import Car
from road import Road
from world import World
from NNdraw import NN
from config_variables import *
py.font.init()
bg = py.Surface((WIN_WIDTH, WIN_HEIGHT))
bg.fill(GRAY)
def draw_win(cars, road, world, GEN): #x e y sono le coordinate della macchina migliore
road.draw(world)
for car in cars:
car.draw(world)
text = STAT_FONT.render("Best Car Score: "+str(int(world.getScore())), 1, BLACK)
world.win.blit(text, (world.win_width-text.get_width() - 10, 10))
text = STAT_FONT.render("Gen: "+str(GEN), 1, BLACK)
world.win.blit(text, (world.win_width-text.get_width() - 10, 50))
world.bestNN.draw(world)
py.display.update()
world.win.blit(bg, (0,0)) #blit dello sfondo subito dopo l'update così se ho delle draw prima della draw_win non vengono coperte dallo sfondo
def main(genomes = [], config = []):
global GEN
GEN += 1
nets = []
ge = []
cars = []
t = 0
world = World(STARTING_POS, WIN_WIDTH, WIN_HEIGHT)
world.win.blit(bg, (0,0))
NNs = []
for _,g in genomes:
net = neat.nn.FeedForwardNetwork.create(g, config)
nets.append(net)
cars.append(Car(0, 0, 0))
g.fitness = 0
ge.append(g)
NNs.append(NN(config, g, (90, 210)))
road = Road(world)
clock = py.time.Clock()
run = True
while run:
t += 1
clock.tick(FPS)
world.updateScore(0)
for event in py.event.get():
if event.type == py.QUIT:
run = False
py.quit()
quit()
(xb, yb) = (0,0)
i = 0
while(i < len(cars)):
car = cars[i]
input = car.getInputs(world, road)
input.append(car.vel/MAX_VEL)
car.commands = nets[i].activate(tuple(input))
y_old = car.y
(x, y) = car.move(road,t)
if t>10 and (car.detectCollision(road) or y > world.getBestCarPos()[1] + BAD_GENOME_TRESHOLD or y>y_old or car.vel < 0.1): #il t serve a evitare di eliminare macchine nei primi tot frame (nei primi frame getCollision() restituisce sempre true)
ge[i].fitness -= 1
cars.pop(i)
nets.pop(i)
ge.pop(i)
NNs.pop(i)
else:
ge[i].fitness += -(y - y_old)/100 + car.vel*SCORE_VEL_MULTIPLIER
if(ge[i].fitness > world.getScore()):
world.updateScore(ge[i].fitness)
world.bestNN = NNs[i]
world.bestInputs = input
world.bestCommands = car.commands
i += 1
if y < yb:
(xb, yb) = (x, y)
if len(cars) == 0:
run = False
break
world.updateBestCarPos((xb, yb))
road.update(world)
draw_win(cars, road, world, GEN)
#NEAT function
def run(config_path):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
p = neat.Population(config)
p.add_reporter(neat.StdOutReporter(True))
stats =neat.StatisticsReporter()
p.add_reporter(stats)
winner = p.run(main, 10000)
if __name__ == "__main__":
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config_file.txt")
run(config_path)