-
Notifications
You must be signed in to change notification settings - Fork 0
/
population.py
65 lines (53 loc) · 2.03 KB
/
population.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
from unit import unit
from physics import physicsHandler
import random
class populationHandler:
def __init__(self):
""" This class handles the populations, generates the generations."""
self.generations = []
self.pHandler = physicsHandler(1,1)
def initFirstGen(self, size):
""" Initialize first generation. """
self.generations.append([unit([random.randint(-10, 10) for x in range(4)]) for x in range(size)])
def newGeneration(self, generation=None):
""" Creating a new generation from the last one. """
if not generation:
generation = self.generations[-1]
for unit in generation:
if not unit.fitness:
self.pHandler.calculateFitness(unit)
self.bubbleSort(generation)
# TODO we now have a organized generation and need to generate a new one
def fuckFest(self, oldGen):
""" This method gives birth to the new generation."""
newGen = []
# TODO implement fuckFest
return newGen
def fuck(self, parent_1, parent_2):
""" This method mixes the genes of the parents by a certain rule. """
newGenes = None # yet
# TODO implement fuck
child = unit(newGenes)
return child
def bubbleSort(self, gen):
""" This method goes through a generation and swaps two units in the list if they
are in the wrong order. The result is the sorted array with the most fitted unit last.
"""
areWeCool = True
n = len(gen)
for j in range(n):
for i in range(0, n-j-1):
if gen[i].fitness > gen[i+1].fitness:
gen[i], gen[i+1] = gen[i+1], gen[i]
areWeCool = False
if areWeCool:
break
if __name__ == '__main__':
p = populationHandler()
p.initFirstGen(10)
for unit in p.generations[0]:
unit.fitness = random.randint(-10, 10)
p.newGeneration()
for unit in p.generations[0]:
print(unit.fitness)
pass