-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
217 lines (168 loc) · 5.44 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import pygame
import random
import sys
import math
import neat
width=1300
height=1100
bg=(213,193,154,255)
generation=0
class Car:
def __init__(self):
self.random_sprite()
self.angle=0
self.speed=5
self.radars=[]
self.collision_points=[]
self.is_alive=True
self.goal=False
self.distance=0
self.time_spent=0
def random_sprite(self):
self.car_sprite=pygame.image.load('res/car.png')
self.car_sprite=pygame.transform.scale(self.car_sprite,
(math.floor(self.car_sprite.get_size()[0]/2),math.floor(self.car_sprite.get_size()[1]/2)))
self.car=self.car_sprite
self.pos=[650,930]
self.compute_center()
def compute_center(self):
self.center=(self.pos[0]+(self.car.get_size()[0]/2),self.pos[1]+(self.car.get_size()[1] / 2))
def draw(self,screen):
screen.blit(self.car,self.pos)
self.draw_radars(screen)
def draw_center(self,screen):
pygame.draw.circle(screen,(0,72,186),(math.floor(self.center[0]),math.floor(self.center[1])),5)
def draw_radars(self,screen):
for r in self.radars:
p,d=r
pygame.draw.line(screen,(183,235,70),self.center,p,1)
pygame.draw.circle(screen,(183,235,70),p,5)
def compute_radars(self,degree,road):
length=0
x=int(self.center[0]+math.cos(math.radians(360-(self.angle+degree)))*length)
y=int(self.center[1]+math.sin(math.radians(360-(self.angle+degree)))*length)
while not road.get_at((x,y)) == bg and length<300:
length=length+1
x=int(self.center[0]+math.cos(math.radians(360-(self.angle+degree)))*length)
y=int(self.center[1]+math.sin(math.radians(360-(self.angle+degree)))*length)
dist=int(math.sqrt(math.pow(x-self.center[0],2)+math.pow(y-self.center[1],2)))
self.radars.append([(x,y),dist])
def compute_collision_points(self):
self.compute_center()
lw=65
lh=65
lt=[self.center[0]+math.cos(math.radians(360-(self.angle+20)))*lw,self.center[1]+math.sin(math.radians(360-(self.angle+20)))*lh]
rt=[self.center[0]+math.cos(math.radians(360-(self.angle+160)))*lw,self.center[1]+math.sin(math.radians(360-(self.angle+160)))*lh]
lb=[self.center[0]+math.cos(math.radians(360-(self.angle+200)))*lw,self.center[1]+math.sin(math.radians(360-(self.angle+200)))*lh]
rb=[self.center[0]+math.cos(math.radians(360-(self.angle+340)))*lw,self.center[1]+math.sin(math.radians(360-(self.angle+340)))*lh]
self.collision_points=[lt,rt,lb,rb]
def draw_collision_points(self,road,screen):
if not self.collision_points:
self.compute_collision_points()
for p in self.collision_points:
if(road.get_at((int(p[0]),int(p[1]))) == bg):
pygame.draw.circle(screen,(255,0,0),(int(p[0]),int(p[1])),5)
else:
pygame.draw.circle(screen,(15,192,252),(int(p[0]),int(p[1])),5)
def check_collision(self,road):
self.is_alive=True
for p in self.collision_points:
try:
if road.get_at((int(p[0]),int(p[1]))) == bg:
self.is_alive=False
break
except IndexError:
self.is_alive=False
def rotate(self,angle):
orig_rect=self.car_sprite.get_rect()
rot_image=pygame.transform.rotate(self.car_sprite,angle)
rot_rect=orig_rect.copy()
rot_rect.center=rot_image.get_rect().center
rot_image=rot_image.subsurface(rot_rect).copy()
self.car=rot_image
def get_data(self):
radars=self.radars
data=[0,0,0,0,0]
for i,r in enumerate(radars):
data[i]=int(r[1] / 30)
return data
def get_reward(self):
return self.distance / 50.0
def update(self,road):
self.speed=5
self.rotate(self.angle)
self.pos[0]+=math.cos(math.radians(360-self.angle))*self.speed
if self.pos[0]<20:
self.pos[0]=20
elif self.pos[0]>width-120:
self.pos[0]=width-120
self.pos[1]+=math.sin(math.radians(360-self.angle))*self.speed
if self.pos[1]<20:
self.pos[1]=20
elif self.pos[1]>height-120:
self.pos[1]=height-120
self.distance+=self.speed
self.time_spent+=1
self.compute_collision_points()
self.check_collision(road)
self.radars.clear()
for d in range(-90,120,45):
self.compute_radars(d,road)
start=False
def run_generation(genomes,config):
nets=[]
cars=[]
for i,g in genomes:
net=neat.nn.FeedForwardNetwork.create(g,config)
nets.append(net)
g.fitness=0
cars.append(Car())
pygame.init()
screen=pygame.display.set_mode((width,height))
clock=pygame.time.Clock()
road=pygame.image.load('res/road.png')
font=pygame.font.SysFont("Roboto",40)
heading_font=pygame.font.SysFont("Roboto",80)
global generation
global start
generation+=1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
start=True
if not start:
continue
for i,car in enumerate(cars):
output=nets[i].activate(car.get_data())
i=output.index(max(output))
if i == 0:
car.angle+=5
elif i == 1:
car.angle=car.angle
elif i == 2:
car.angle-=5
cars_left=0
for i,car in enumerate(cars):
if car.is_alive:
cars_left+=1
car.update(road)
genomes[i][1].fitness+=car.get_reward()
if not cars_left:
break
screen.blit(road,(0,0))
for car in cars:
if car.is_alive:
car.draw(screen)
label=heading_font.render("Поколение: "+str(generation),True,(73,168,70))
label_rect=label.get_rect()
label_rect.center=(width / 1.5,300)
screen.blit(label,label_rect)
pygame.display.flip()
clock.tick(0)
config_path="config-feedforward.txt"
config=neat.config.Config(neat.DefaultGenome,neat.DefaultReproduction,neat.DefaultSpeciesSet,neat.DefaultStagnation,config_path)
p=neat.Population(config)
p.run(run_generation,1000)