-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuse-GA.py
68 lines (33 loc) · 980 Bytes
/
use-GA.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
from pyevolve import G1DList
from pyevolve import GSimpleGA
from pyevolve import DBAdapters
# This function is the evaluation function, we want
# to give high score to more zero'ed chromosomes
ink = 0
def eval_func(chromosome):
global ink
score = 0.0
# print "@#$%^", ink
ink += 1
# iterate over the chromosome elements (items)
A = []
for value in chromosome:
A.append(value)
if value==0:
score += 1.0
# print "A: ", A
return score+1000000
# Genome instance
genome = G1DList.G1DList(5)
genome.setParams(rangemin=0, rangemax=31)
# The evaluator function (objective function)
genome.evaluator.set(eval_func)
ga = GSimpleGA.GSimpleGA(genome)
ga.setDBAdapter( DBAdapters.DBSQLite( identify="ex1" ) )
ga.setGenerations(9)
ga.setPopulationSize(50)
# Do the evolution, with stats dump
# frequency of 10 generations
ga.evolve(freq_stats=10)
# Best individual
print ga.bestIndividual()