-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga_test.py
40 lines (31 loc) · 923 Bytes
/
ga_test.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
# Generator example
from pybrain.tools.shortcuts import buildNetwork
from random import randint
from ga import *
def geni(s):
for i in range(s):
net = buildNetwork(1, 3, 1)
yield net.params
def testIndividual():
print "==========Testing Individual=========="
i = Individual([random() for i in range(4)])
i.setFitness(3)
print "params:", i.params
print "fitness:", i.fitness
print "## After Mutation"
i.mutate(0.1)
print "params:", i.params
def testPopulation():
print "==========Testing Population=========="
p = Population(4, 0.1, 0.5)
p.generateInitialPop(geni)
print "pop:", p.pop
for ind in p.pop:
ind.setFitness(randint(0, 10))
print "fitnesses:", [i.fitness for i in p.pop]
print "---- Next generation"
p.generateNextPop()
print "pop:", p.pop
if __name__ == '__main__':
testIndividual()
testPopulation()