-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
72 lines (61 loc) · 2.34 KB
/
Copy pathMain.py
File metadata and controls
72 lines (61 loc) · 2.34 KB
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
import random
from Const import *
population = []
class FirstGeneration:
def __init__(self):
self.desireUp = random.uniform(0, 1)
self.desireDown = random.uniform(0, 1)
self.desireLeft = random.uniform(0, 1)
self.desireRight = random.uniform(0, 1)
self.xPos = SCREEN_WIDTH / 2
self.yPos = SCREEN_HEIGHT / 2
def setNewGenome(self, desireUp, desireDown, desireLeft, desireRight):
self.desireUp = desireUp
self.desireDown = desireDown
self.desireLeft = desireLeft
self.desireRight = desireRight
def printGenome(self):
print(
{
"Up": self.desireUp,
"Down": self.desireDown,
"Left": self.desireLeft,
"Right": self.desireRight,
"Fecundity": self.fecundity,
}
)
def updatePos(self):
if self.xPos >= SCREEN_WIDTH - 5:
self.xPos = SCREEN_WIDTH - 5
elif self.xPos < 100:
self.xPos = 100
else:
self.xPos += self.desireRight - self.desireLeft
if self.yPos >= SCREEN_HEIGHT - 5:
self.yPos = SCREEN_HEIGHT - 5
elif self.yPos < 0:
self.yPos = 0
else:
self.yPos += self.desireDown - self.desireUp
def decideSurvivors(self, previousPopulation):
Survivors = []
for i in range(len(previousPopulation)):
if (
previousPopulation[i].xPos > survivingCoditionX
or previousPopulation[i].yPos > survivingCoditionY
):
Survivors.append(previousPopulation[i])
population.clear()
population.extend(Survivors)
def reproduce(self, population):
for i in range(len(population)):
population[i].setNewGenome(
random.choice(population).desireUp
+ (random.choice(population).desireUp) * random.uniform(0, 1),
random.choice(population).desireDown
+ (random.choice(population).desireDown) * random.uniform(0, 1),
random.choice(population).desireLeft
+ (random.choice(population).desireLeft) * random.uniform(0, 1),
random.choice(population).desireRight
+ (random.choice(population).desireRight) * random.uniform(0, 1),
)