/
EvolutionManager.py
216 lines (180 loc) · 8.81 KB
/
EvolutionManager.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import signal
import time
from FileManager import *
from internal.ChromosomeType import *
from internal.GenerationType import *
class EvolutionManager(object):
def __init__(self,
fitnessFunction,
individualsPerGeneration=100,
elitism=1,
randIndividuals=0,
randFitness=None,
mutationRate=0.2,
mutationSTDEV=0,
maxGenerations=None,
stopWithFitness=None,
stopAfterTime=None,
logDir=None,
generationsToKeep=0,
snapshotGenerations=None,
threads=1,
startingGeneration=None):
"""
:param individualsPerGeneration: the size of the new generation
:type individualsPerGeneration: int
:param elitism: preserve the n most fit individuals without mutations or crossovers
:type elitism: int
:param randIndividuals: add n random chromosomes to the breeding population
:type randIndividuals: int
:param randFitness: random individuals may have very low fitness. If not None, the maximum of this value and
the actual random fitness is used
:type randFitness: float
:param mutationRate: the average number of mutations each gene will undergo
:type mutationRate: float
:param mutationSTDEV: the standard deviation for the number of mutations each gene will undergo
:type mutationSTDEV: float
:param maxGenerations: stop computing after this many generations. None means no limit
:type maxGenerations: int
:param stopWithFitness: stop computing if fitness meets or exceeds this value. None means no limit
:type stopWithFitness: float
:param stopAfterTime: stop computing after this many seconds. None means no limit
:type stopAfterTime: float
:param logDirL: if provided with a log directory then certain generations may be saved
:type logDir: str
:param generationsToKeep: the number of generations to save to the log directory. For example, if set to 5
then the 5 most recent generations will be saved
:param snapshotGenerations: take a snapshot of the system very N trials
:type snapshotGenerations: int
:param threads: the number of threads to use for fitness tests
:type threads: int
:param startingGeneration: start with generation defined in YAML instead of a random generation
:type startingGeneration: str
"""
self.geneTypes = []
self.startingChromosomes = []
self.chromosomeType = None
self.fitnessFunction = fitnessFunction
self.individualsPerGeneration = individualsPerGeneration
self.elitism = elitism
self.randIndividuals = randIndividuals
self.randFitness = randFitness
self.mutationRate = mutationRate
self.mutationSTDEV = mutationSTDEV
self.maxGenerations = maxGenerations
self.stopWithFitness = stopWithFitness
self.stopAfterTime = stopAfterTime
self.logDir = logDir
self.generationsToKeep = generationsToKeep
self.snapshotGenerations = snapshotGenerations
self.threads = threads
self.startingGenration = startingGeneration
self.FM = FileManager()
self.oldGenerations = []
self.oldGenerations_perm = []
def signal_handler(signal, frame):
print "dumping data"
self.dataDump()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def addGeneType(self, geneType):
"""
Add a new gene type to the expiriment
:type geneType: GeneType
"""
if self.chromosomeType is not None:
raise Exception("New gene types cannot be added after the chromosome template has been finalized!")
self.geneTypes.append(geneType)
def getChromomeTemplate(self):
"""
Return a chromosome of the appropriate type. Useful for creating specific chromosomes to be added
:rtype: Chromosome
"""
if self.chromosomeType is None:
self.chromosomeType = ChromosomeType(self.fitnessFunction, self.geneTypes)
return self.chromosomeType.getRandomChromosome()
def addChromosome(self, chromsome):
"""
Add a chomosome to the first generation. This should not be called before adding all gene types
:type chromsome: Chromosome
"""
self.startingChromosomes.append(chromsome)
def run(self):
if self.chromosomeType is None:
self.chromosomeType = ChromosomeType(self.fitnessFunction, self.geneTypes)
generationType = GenerationType(self.chromosomeType)
if self.startingGenration is None:
currentGeneration = generationType.getRandomGeneration(max(0, self.individualsPerGeneration-len(self.startingChromosomes)))
else:
fobj = open(self.startingGenration)
flist = []
for line in fobj:
flist.append(line)
fline = "".join(flist)
currentGeneration = generationType.fromYAML(fline)
fobj.close()
currentGeneration.population += self.startingChromosomes
currentGeneration.doFitnessTests(threads=self.threads)
print "The most fit individual in the starting generation is\n"
print currentGeneration.getMostFit()
startTime = None
if self.stopAfterTime is not None:
startTime = time.time()
try:
trials = 0
while True:
trials += 1
#take a snapshot
if self.snapshotGenerations is not None and trials % self.snapshotGenerations == 0:
self.oldGenerations_perm.append((trials, currentGeneration))
#save this trial in temporary storage
elif self.generationsToKeep > 0:
if len(self.oldGenerations) >= self.generationsToKeep:
self.oldGenerations = self.oldGenerations[1:]
self.oldGenerations.append((trials, currentGeneration))
#exit conditions
if self.maxGenerations is not None and trials > self.maxGenerations:
print "Maximum number of generations reached"
self.dataDump()
return currentGeneration.getMostFit()
if self.stopWithFitness is not None and currentGeneration.getMostFit().fitness >= self.stopWithFitness:
print "Sufficient fitness achieved"
self.dataDump()
return currentGeneration.getMostFit()
if self.stopAfterTime is not None and (time.time() - startTime) >= self.stopAfterTime:
print "Time limit reached"
self.dataDump()
return currentGeneration.getMostFit()
print "-" * 100
print "Begining computations for generation " + str(trials)
nextGeneration = currentGeneration.getNextGeneration(self.individualsPerGeneration,
self.elitism,
self.randIndividuals,
self.randFitness,
self.mutationRate,
self.mutationSTDEV)
nextGeneration.doFitnessTests(threads=self.threads)
print "The most fit individual in this generation is\n"
print nextGeneration.getMostFit()
currentGeneration = nextGeneration
except Chromosome.PerfectMatch as e:
print "A perfect match has been found"
print e.message
if self.generationsToKeep > 0:
if len(self.oldGenerations) >= self.generationsToKeep:
self.oldGenerations = self.oldGenerations[1:]
self.oldGenerations.append((trials, currentGeneration))
self.dataDump()
return e.message
def dataDump(self):
"""
Call this function to write all of the pending generations to disk
"""
if self.logDir is not None:
if not os.path.exists(self.logDir):
os.mkdir(self.logDir)
for trial in self.oldGenerations:
self.FM.write(trial[1], self.logDir + "/" + str(trial[0]) + ".yaml")
for trial in self.oldGenerations_perm:
self.FM.write(trial[1], self.logDir + "/" + str(trial[0]) + ".yaml")