forked from Hematite12/Game-of-Life-GA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenetics.py
57 lines (50 loc) · 1.37 KB
/
Genetics.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
import random
from Board import *
from Patterns import *
POPSIZE = 50
MUTPROB = .04
NUMSTEPS = 100
NUMGENS = 10
FRACTIONKEPTALIVE = 3
USEDPATTERN = BLINKERS
def generate():
popL = []
for i in range(POPSIZE):
popL.append(Board())
return popL
def mutateAll(popL):
for board in popL:
board.mutate(MUTPROB)
def calcFitnesses(popL):
for board in popL:
if USEDPATTERN != None:
board.calcFitness(NUMSTEPS, USEDPATTERN)
else:
board.calcFitness(NUMSTEPS)
def makePopFitL(popL):
calcFitnesses(popL)
popL.sort(key = lambda x: x.fitness, reverse=True)
def makePopPool(popL):
popPool = []
initialCount = len(popL)//FRACTIONKEPTALIVE
for i in range(len(popL)):
if initialCount > 0:
for j in range(initialCount):
popPool.append(popL[i])
initialCount -= 1
return popPool
def evolve():
popL = generate()
for i in range(NUMGENS):
makePopFitL(popL)
print(popL[0].fitness)
popL = popL[:len(popL)//FRACTIONKEPTALIVE]
popPool = makePopPool(popL)
newPop = []
for i in range(POPSIZE):
choice = random.choice(popPool)
newPop.append(choice.getCopy())
mutateAll(newPop)
popL = newPop
makePopFitL(popL)
return popL[0]