-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGA_gene.py
More file actions
76 lines (57 loc) · 2.53 KB
/
GA_gene.py
File metadata and controls
76 lines (57 loc) · 2.53 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
73
74
75
76
class GA_gene:
""" GA_gene genetic algoritm
implementation of a genetic algorithm using GA_population with GA_gene to do magic!
both need to be inherented so you can give your own implementations of mutation and crossover and fitness and stuff...
the fitnessValue should be kept upToDate after any changes to the model!
the lower its value the better it is!
history contains:
if:
create - 'create', fitnessValue, geneID
mutate - 'mutate', fitnessValue
crossover - 'crossover', fitnessValue, geneID of crossover partner, fitnessValue of crossover partner
"""
fitnessValue=None # the fitnessValue of the current gene (should be set to None if anything in the model changes...)
ID=0 # ID number given on creation, so multiple copies can exicst if mutated or crossover copies are preserved
__ID__=0 # class ID counter
history=[]
def __init__(self):
pass
## def load(self, filename):
## """... load a individual from a file, this function needs to be overwritten"""
## pass
def __create__(self):
# create unique identifier
GA_gene.__ID__+=1
self.ID=GA_gene.__ID__
self.create()
self.history=[['create', self.fitnessValue, self.ID]]
return self
def create(self):
""" create a new individual, this function needs to be overwritten
its fitness should be calculated using the implementation of fitness()
"""
return self
def fitness(self):
""" calculate the fitness of this indivdual this needs to be overwritten should return the fitness value
aswell as storing it in fitnessValue
"""
pass
def __mutate__(self):
self.mutate()
self.history.append(['mutate', self.fitnessValue])
def mutate(self):
""" mutate this individual, this function needs to be overwritten
its fitness should be calculated using the implementation of fitness()
"""
pass
def __crossover__(self, other):
self.crossover(other)
self.history.append(['crossover', self.fitnessValue, other.ID, other.fitnessValue])
other.history.append(['crossover', other.fitnessValue, self.ID, self.fitnessValue])
def crossover(self, other):
""" mutate this individual, this function needs to be overwritten
its fitness should be calculated using the implementation of fitness()
"""
pass
if __name__ == '__main__':
pass