-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
654 lines (510 loc) · 19.9 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# Import the necessary libraries
# for ui
import pygame
# for dna
import random
# for math
import math
# for analysis
import matplotlib.pyplot as plt
# for time
import time
# Initialize Pygame module functions
pygame.init()
# Set the screen size
screen_width = 540
screen_height = 540
# Set the clock
# clock is used to control the frame rate of the game
clock = pygame.time.Clock()
# set the screen
screen = pygame.display.set_mode((screen_height, screen_width))
# set the icon of the game
# pygame.display.set_icon(pygame.image.load("title1.png"))
# Set the debug flag
debug = False
# Set the generation number initially to 0
generation = 0
# Define the Vehicle class
class Vehicle:
# constructor for the Vehicle class
def __init__(self, x, y, dna=None):
# point to the global generation variable
global generation
"""
Initializes a new instance of the Vehicle class.
Args:
x (int): The x-coordinate of the vehicle's starting position.
y (int): The y-coordinate of the vehicle's starting position.
"""
# Initialize the position, velocity, acceleration, radius, maxspeed, maxforce, health, and DNA of the vehicle
# math.Vector2 is a 2D vector
self.position = pygame.math.Vector2(x, y)
self.velocity = pygame.math.Vector2(0, -2)
# without any attraction or repulsion , the vehicle will move in a straight line, initially as the acceleration is 0
self.acceleration = pygame.math.Vector2(0, 0)
# radius of the vehicle
self.r = 6
# maximum speed of the vehicle limited to 4
self.maxspeed = 4
self.maxforce = 0.2
# controversial, but we keep it initially to 0
self.health = 0
# if DNA already exists, then mutate it
# it means that the vehicle is a child of another vehicle
if dna != None:
# aHealth.append(1)
print("T")
generation = generation + 1
print(generation)
show = True
# while show:
# showtext(screen,"Generation: "+str(generation),(screen_width/2-180,screen_height/2),50)
# pygame.display.update()
# time.sleep(2)
# show = False
# randomized DNA closely related to the parent to mtuation
# ro to introduce probability of mutation
ro = random.uniform(-0.1, 0.1)
# <----------------------------------------------------------------------------------------------->
# if DNA already exists, then mutate it
# which DNA to mutate is decided by the random number ro
# any 2 of the 4 DNA can be mutated
# we mutates the DNA by adding a random number between -0.1 and 0.1 to the DNA
# it can either be positive or negative
# so the DNA can either increase or decrease
# so we find the correct direction of mutation to generate better vehicles
# this is the crux of reinforcement learning
if -0.1 < ro < -0.075:
self.dna = [dna[0], dna[1], dna[2] +
random.uniform(-5, 5), dna[3] + random.uniform(-5, 5)]
elif -0.075 < ro < -0.05:
self.dna = [dna[0], dna[1] -
random.uniform(-0.1, 0.1), dna[2], dna[3] - random.uniform(-5, 5)]
elif -0.05 < ro < -0.025:
self.dna = [dna[0], dna[1] -
random.uniform(-0.1, 0.1), dna[2] - random.uniform(-5, 5), dna[3]]
elif -0.025 < ro < 0:
self.dna = [dna[0] + random.uniform(-0.1, 0.1), dna[1], dna[2] -
random.uniform(-5, 5), dna[3] - random.uniform(-5, 5)]
elif 0 < ro < 0.025:
self.dna = [
dna[0] + random.uniform(-0.1, 0.1), dna[1], dna[2], dna[3] + random.uniform(-5, 5)]
elif 0.025 < ro < 0.05:
self.dna = [
dna[0] + random.uniform(-0.1, 0.1), dna[1], dna[2] - random.uniform(-5, 5), dna[3]]
elif 0.05 < ro < 0.075:
self.dna = [
dna[0] + random.uniform(-0.1, 0.1), dna[1] + random.uniform(-0.1, 0.1), dna[2], dna[3]]
else:
self.dna = [dna[0], dna[1] + random.uniform(-0.1, 0.1), dna[2], dna[3]]
else:
self.dna = [random.uniform(-2, 2), random.uniform(-2, 2),
random.uniform(0, 150), random.uniform(0, 150)]
# <----------------------------------------------------------------------------------------------->
# if -0.1 < ro < -0.05:
# self.dna = [dna[0], dna[1], dna[2], dna[3] + random.uniform(-5, 5)]
# elif -0.05 < ro < 0:
# self.dna = [dna[0], dna[1] - random.uniform(-0.1, 0.1), dna[2], dna[3]]
# elif 0 < ro < 0.05:
# self.dna = [dna[0] + random.uniform(-0.1, 0.1), dna[1], dna[2], dna[3]]
# elif 0.05 < ro < 0.1:
# self.dna = [dna[0], dna[1], dna[2] + random.uniform(-5, 5), dna[3]]
# else:
# self.dna = [random.uniform(-2, 2), random.uniform(-2, 2), random.uniform(0, 150), random.uniform(0, 150)]
# <---------------------------------------------------------------------------------------------------->
def update(self):
"""
Updates the vehicle's health, velocity, position, and acceleration.
"""
# Update the health, velocity, position, and acceleration of the vehicle
# self.health -= 0.003
self.velocity += self.acceleration
# Limit the velocity to the maximum speed
if self.velocity.length() > self.maxspeed:
self.velocity.scale_to_length(self.maxspeed)
self.position += self.velocity
# reset the acceleration so that it does not keep on increasing forever becuase its not velocity
self.acceleration *= 0
def applyForce(self, force):
"""
Applies a force to the acceleration of the vehicle.
Args:
force (Vector2): The force to apply to the acceleration of the vehicle.
"""
# Apply a force to the acceleration of the vehicle
self.acceleration += force
def behaviors(self, good, bad):
"""
Calculates the steering forces for eating good and bad particles.
Args:
good (list): A list of good particles.
bad (list): A list of bad particles.
"""
# Calculate the steering forces for eating good and bad particles
steerG = self.eat(good, 0.3, self.dna[2])
steerB = self.eat(bad, -0.75, self.dna[3])
# Apply the DNA multipliers to the steering forces
steerG *= self.dna[0]
steerB *= self.dna[1]
# Apply the steering forces to the acceleration of the vehicle
self.applyForce(steerG)
self.applyForce(steerB)
def clone(self):
"""
Clones the vehicle with a small probability.
Cloning depends on the health of the vehicle.
Higher the health, higher the probability of cloning.
Also itis controlled by the random number generated.
But the probability of cloning is higher for higher health.
Returns:
Vehicle: A new instance of the Vehicle class with the same position as the original vehicle.
"""
# Clone the vehicle with a small probability
if random.random() < 0.005 and self.health >= 1.6:
return Vehicle(self.position.x, self.position.y, self.dna)
elif 0.1 < random.random() <= 0.004 and self.health >= 1.5:
return Vehicle(self.position.x, self.position.y, self.dna)
elif 0.01 < random.random() < 0.003 and self.health >= 1.4:
return Vehicle(self.position.x, self.position.y, self.dna)
elif 0.005 < random.random() < 0.002 and self.health >= 1.3:
return Vehicle(self.position.x, self.position.y, self.dna)
elif random.random() < 0.001 and self.health >= 1.2:
return Vehicle(self.position.x, self.position.y, self.dna)
elif random.random() < 0.0001 and self.health >= 1.1:
return Vehicle(self.position.x, self.position.y, self.dna)
# elif 0.005 < random.random() < 0.003 and self.health > 1:
# return Vehicle(self.position.x, self.position.y,self.dna)
# elif random.random() < 0.001 and self.health > 0.75:
# return Vehicle(self.position.x, self.position.y,self.dna)
# elif random.random() < 0.0001 and self.health > 0.5:
# return Vehicle(self.position.x, self.position.y,self.dna)
def eat(self, lst, nutrition, perception):
"""
Finds the closest good or bad particle and eats it if it is within range.
Args:
lst (list): A list of particles to search for food.
nutrition (float): The amount of nutrition to add to the vehicle's health when it eats a particle.
perception (float): The maximum distance at which the vehicle can detect particles.
Returns:
Vector2: The steering force required to reach the closest particle.
"""
# Find the closest good or bad particle and eat it if it is within range
# set to a very large number infinity
# record is the distance to the closest particle
record = float("inf")
# closest to none for now
# closest is the closest particle
closest = None
# loop through the list of particles backwards to remove them
for i in range(len(lst)-1, -1, -1):
# distance to particle
d = self.position.distance_to(lst[i])
# if the distance is less than the record and less than the perception
if d < self.maxspeed:
lst.pop(i)
self.health += nutrition
else:
# perception for particular particle according to DNA and evolution
if d < record and d < perception:
# update the record and closest particle
record = d
# now this is the closest particle
closest = lst[i]
# if there is a closest particle
# seek it
if closest is not None:
return self.seek(closest)
return pygame.math.Vector2(0, 0)
def seek(self, target):
"""
Seeks a target and returns the steering force required to reach it.
Args:
target (Vector2): The target to seek.
Returns:
Vector2: The steering force required to reach the target.
"""
# Seek a target and return the steering force required to reach it
# vector pointing from the position to the target
desired = target - self.position
# normalize the vector
# vector scaled (0,1)
desired.normalize()
# scale the vector to the maximum speed
# vector scaled (0,maxspeed)
desired *= self.maxspeed
steer = desired - self.velocity
if steer.length() >= self.maxforce:
steer.scale_to_length(self.maxforce)
return steer
def dead(self):
"""
Checks if the vehicle is dead.
Returns:
bool: True if the vehicle is dead, False otherwise.
"""
# Check if the vehicle is dead
return self.health < -1
def display(self):
"""
Displays the vehicle on the screen.
"""
# Calculate the angle of the vehicle's velocity vector
angle = self.velocity.angle_to(pygame.math.Vector2(0, -1)) + math.pi / 2
# Draw the vehicle on the screen
try:
pygame.draw.circle(screen, (255-255*self.health, 255), 255,
0), (int(self.position.x), int(self.position.y)), self.r
except:
pygame.draw.circle(screen, (255, 255, 0), (int(
self.position.x), int(self.position.y)), self.r)
# Draw the vehicle's debug information if the debug flag is set
# def drawDebug(self, angle):
# """
# Draws the vehicle's debug information.
# Args:
# angle (float): The angle of the vehicle's velocity vector.
# """
# # Set the scale of the debug vectors
# scale = 25
# # Draw the velocity vector
# vel = self.velocity.copy()
# vel.normalize()
# vel *= scale
# drawVector(self.position, vel, (0, 255, 0))
# # Draw the acceleration vector
# acc = self.acceleration.copy()
# acc.normalize()
# acc *= scale
# drawVector(self.position, acc, (0, 0, 255))
# # Draw the desired velocity vector
# desired = self.velocity.copy()
# desired.normalize()
# desired *= scale * 2
# drawVector(self.position, desired, (255, 0, 0))
# # Draw the heading vector
# heading = pygame.math.Vector2(0, -1)
# heading.rotate_ip(angle)
# heading *= scale
# drawVector(self.position, heading, (0, 255, 255))
# # Draw the perception radius
# pygame.draw.circle(screen, (255, 255, 0), (int(self.position.x), int(self.position.y)), int(self.dna[2]))
# # Draw the food and poison perception radius
# pygame.draw.circle(screen, (0, 255, 0), (int(self.position.x), int(self.position.y)), int(self.dna[2] * self.dna[0]))
# pygame.draw.circle(screen, (255, 0, 0), (int(self.position.x), int(self.position.y)), int(self.dna[3] * self.dna[1]))
def drawVector(position, vector, color):
"""
Draws a vector on the screen.
Args:
position (Vector2): The starting position of the vector.
vector (Vector2): The vector to draw.
color (tuple): The color of the vector.
"""
# Draw a line representing the vector
pygame.draw.line(screen, color, (int(position.x), int(position.y)), (int(
position.x + vector.x), int(position.y + vector.y)), 1)
def addFood():
# Add a food particle with a small probability
if random.random() < 0.4:
x = random.randint(0, screen_width)
y = random.randint(0, screen_height)
food.append(pygame.math.Vector2(x, y))
def addPoison():
# Add a poison particle with a small probability
if random.random() < 0.4:
x = random.randint(0, screen_width)
y = random.randint(0, screen_height)
poison.append(pygame.math.Vector2(x, y))
def drawFood():
# Draw the food particles on the screen
for f in food:
pygame.draw.circle(screen, (0, 255, 0), (int(f.x), int(f.y)), 4)
def drawPoison():
# Draw the poison particles on the screen
for p in poison:
pygame.draw.circle(screen, (255, 0, 0), (int(p.x), int(p.y)), 4)
def drawVehicles():
# Draw the vehicles on the screen
for v in vehicles:
# v.boundaries()
v.behaviors(food, poison)
v.update()
v.display()
# mutate the vehicle with a small probability
newVehicle = v.clone()
if newVehicle is not None:
# add the child vehicle to the list of vehicles
vehicles.append(newVehicle)
if v.dead():
x = v.position.x
y = v.position.y
# when a vehicle exits our environment it is removed from the list of vehicles
# and a food particle is added at its position
food.append(pygame.math.Vector2(x, y))
vehicles.remove(v)
aHealth = []
def averageHealth(aHealth):
# Calculate the average health of the vehicles
total = 0
# add the health of each vehicle to the total
for v in vehicles:
total += v.health
average_Health = total / len(vehicles)
# print(v.health)
# store the average health of the vehicles in a list
# for the graph
aHealth.append(average_Health)
return average_Health
def appearText():
# Display the text on the screen
text = "Average Health: " + str(100*averageHealth(aHealth))
pygame.display.set_caption(text)
def drawRangeCircles(vehicle):
# Draw the hollow range circles around the vehicle
pygame.draw.circle(screen, (0, 255, 0), (int(vehicle.position.x), int(
vehicle.position.y)), int(vehicle.dna[2]), 1)
pygame.draw.circle(screen, (255, 0, 0), (int(vehicle.position.x), int(
vehicle.position.y)), int(vehicle.dna[3]), 1)
def DrawAttractionLines(vehicle):
pygame.draw.line(screen, (0, 255, 0), (int(vehicle.position.x), int(vehicle.position.y)), (int(
vehicle.position.x + 40*vehicle.dna[0]), int(vehicle.position.y + 40*vehicle.dna[0])), 2)
pygame.draw.line(screen, (255, 0, 0), (int(vehicle.position.x), int(vehicle.position.y)), (int(
vehicle.position.x - 40*vehicle.dna[1]), int(vehicle.position.y - 40*vehicle.dna[1])), 2)
def showtext(screen, text, location, fontsize):
font = pygame.font.SysFont("Copperplate gothic", fontsize, True, False)
textObject = font.render(text, 0, pygame.Color('White'))
location1 = pygame.Rect(location, location)
# textLocation = p.Rect(0, 0, screen_width, screen_height).move(screen_width / 2 - textObject.get_width() / 2, screen_height / 2 - textObject.get_height() / 2)
# white = p.Color("black")
# screen.blit(white,p.rect(textLocation,textLocation,200,200))
screen.blit(textObject, location1)
# Initialize the variables
vehicles = []
food = []
poison = []
# Set the debug flag
debug = False
# Create the vehicles
# 50 vehicles
for i in range(50):
# random position limited to the screen size
x = random.randint(0, screen_width)
y = random.randint(0, screen_height)
# add the vehicle to the list of vehicles
# call object of the class Vehicle by calling the constructor
vehicles.append(Vehicle(x, y))
# same as above
for i in range(100):
x = random.randint(0, screen_width)
y = random.randint(0, screen_height)
food.append(pygame.math.Vector2(x, y))
# same as above
for i in range(100):
x = random.randint(0, screen_width)
y = random.randint(0, screen_height)
poison.append(pygame.math.Vector2(x, y))
# Start the game loop
running = True
Paused = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# break at QUIT
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
# debug mode on pressing D
debug = not debug
if event.key == pygame.K_p:
# pause on pressing P
Paused = not Paused
elif event.type == pygame.MOUSEBUTTONDOWN:
# add a vehicle on clicking the mouse
# get the position of the mouse
x, y = pygame.mouse.get_pos()
# add the vehicle to the list of vehicles
vehicles.append(Vehicle(x, y))
if Paused:
continue
# Clear the screen initially
screen.fill((0, 0, 0))
# Add food and poison particles on the screen
# our gameloop always running functions
# so we introduce probability of adding food and poison particles
addFood()
addPoison()
# Initial food and poison particles appear on the screen
# blit is used to draw the text on the screen
drawFood()
drawPoison()
# calculate the average health of the vehicles
# set the caption of the window
appearText()
if debug:
# vehicles is a list of objects of the class Vehicle
for v in vehicles:
# draw the debug information
drawRangeCircles(v)
DrawAttractionLines(v)
# Draw the vehicles
drawVehicles()
# Update the display
pygame.display.flip()
# Set the frame rate
clock.tick(30)
# Define the x-axis values
x = range(len(aHealth))
# Plot the graph
plt.plot(x, aHealth)
# Add labels and title
plt.xlabel('Generation')
plt.ylabel('Average Health')
plt.title('Average Health per Generation')
# Show the graph
plt.show()
# Quit Pygame
pygame.quit()
# def nextGeneration():
# """
# Creates the next generation of vehicles.
# """
# global vehicles
# # Create a new list of vehicles for the next generation
# newVehicles = []
# # Calculate the total health of all vehicles in the current generation
# totalHealth = 0
# for v in vehicles:
# totalHealth += v.health
# # For each vehicle in the current generation
# for i in range(len(vehicles)):
# # Calculate the fitness of the vehicle as its health divided by the total health of all vehicles
# fitness = vehicles[i].health / totalHealth
# # Select two parent vehicles based on their fitness
# parentA = pickParent(vehicles, fitness)
# parentB = pickParent(vehicles, fitness)
# # Create a new vehicle by crossing over the DNA of the two parent vehicles
# child = parentA.crossover(parentB)
# # Add the new vehicle to the list of vehicles for the next generation
# newVehicles.append(child)
# # Replace the current generation with the list of vehicles for the next generation
# vehicles = newVehicles
# def pickParent(vehicles, fitness):
# """
# Picks a parent vehicle based on its fitness.
# Args:
# vehicles (list): The list of vehicles to choose from.
# fitness (float): The fitness of the vehicles.
# Returns:
# Vehicle: The parent vehicle.
# """
# # Pick a random number between 0 and 1
# r = random.uniform(0, 1)
# # Loop through the vehicles and calculate the cumulative fitness
# cumulativeFitness = 0
# for v in vehicles:
# cumulativeFitness += v.health / fitness
# # If the cumulative fitness is greater than the random number, return the vehicle
# if cumulativeFitness > r:
# return v
# # If no vehicle was selected, return the last vehicle in the list
# return vehicles[-1]