-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunScootBot.py
287 lines (234 loc) · 9.72 KB
/
runScootBot.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import sys, os, subprocess, glob
#sys.modules["ete2"]="ignore"
#sys.modules["ete2a1"]="ignore"
#From OrganismManager.py
import testOrgs
import random
import selector
import matplotlib.pyplot as pyplot
from copy import deepcopy
import os
import cProfile
import pstats
import OrganismManager
import TreeOrganism
class ScootBotManager(OrganismManager.OrganismManager):
def __init__(self, organismType, population, survival, mutateNum, threshold,
resultMap, verilogWriteFileName = 'organism.v',
verilogModuleName = None, numberOfInputs=5, numberOfOutputs=4, maxGeneration=100, **kwargs):
"""
organisms : <List> of <Organism>s
population : the maximum number of <Organism>s
survival : the number of <Organism>s that will survive
each generation
mutateNum : the number of <Organism>s that will be only mutated
The other organisms will be crossovered, not mutated
threshold : fitness at which the simulation stops
resultMap : <testOrgs.SimulationMap> of correct behavior
"""
assert (survival > 0), "At least one Organism should survive."
assert (population > 0), "At least one Organism should exist."
assert (population >= survival + mutateNum), \
"population should be greater than " \
"survival and mutateNum."
self.generationNumber = 0
self.organismType = organismType
self.organisms = []
self.population = population
self.survival = survival
self.mutateNum = mutateNum
self.threshold = threshold
self.maxGeneration = maxGeneration
self.crossoverNum = population - (survival + mutateNum)
#self._resultMap = resultMap
#self._numberOfInputs = resultMap.getNumberOfInputs()
self._numberOfInputs = numberOfInputs
#self._numberOfOutputs = resultMap.getNumberOfOutputs()
self._numberOfOutputs = numberOfOutputs
self._selectorPmf = None
self.verilogWriteFileName = verilogWriteFileName
if verilogModuleName is None:
verilogModuleName = verilogWriteFileName.split('.')[0]
self.verilogModuleName = verilogModuleName
self.kwargs = kwargs
def getRandomOrganism(self):
randOrganism = self.organismType(
self.verilogWriteFileName,
self.getNumberOfInputs(),
self.getNumberOfOutputs(),
randomInit=True,
moduleName=self.verilogModuleName,
**self.kwargs
)
randOrganism.evaluate(None)
return randOrganism
def updateOrganisms(self,visualize=False):
"""
Return Type: void
1. Keep (self.survival) <Organism>s from the
previous generation based on fitness
2. Selects two <Organism>s from the list, crossover them and add
a new <Organism>
3. Repeat 2. (population - survival) times
4. Sort the list by their fitness.
"""
newGeneration = [];
generatorPmf = self._selectorPmf.Copy()
for i in range(self.survival):
replicatedOrganism = generatorPmf.Random()
generatorPmf.Remove(replicatedOrganism)
generatorPmf.Normalize()
newGeneration.append(replicatedOrganism)
generatorPmf = self._selectorPmf
for i in range(self.mutateNum):
replicatedOrganism = deepcopy(generatorPmf.Random())
replicatedOrganism.mutate()
newGeneration.append(replicatedOrganism)
for i in range(self.crossoverNum):
p1,p2 = self.generateParents()
# print parent1, "\nORGANISM crossing over with\n", parent2
newOrganism = p1.crossover(p2)
newGeneration.append(newOrganism)
map( lambda scootBotOrganism : scootBotOrganism.evaluate(None),
newGeneration)
self.organisms = newGeneration
self.organisms.sort(reverse = True)
self._updateSelectorPmf()
if visualize:
self.visualize()
def execute(self,visualize=False,filename=''):
"""
Return Type: void
MainLoop
"""
self.writeSimulation()
self.populate(visualize)
key = lambda org: org.getFitness()
if visualize:
pyplot.ion()
while max(self.organisms,key=key).getFitness() < self.threshold \
and self.generationNumber < self.maxGeneration:
self.generationNumber += 1
self.updateOrganisms(visualize)
bestOrganism = max(self.organisms,key=key)
print "final fitness: ", bestOrganism.getFitness()
bestOrganism.toVerilog('Winner.v', self.verilogModuleName)
if visualize:
pyplot.show()
pyplot.ioff()
if isinstance(bestOrganism,TreeOrganism.TreeOrganism):
bestOrganism.visualize(filename)
#self.deleteSimulation()
class ScootBotOrganism(TreeOrganism.TreeOrganism):
treeCrossOverProbability = .7
treeMutateProbability = .5
def evaluate(self,correctResultMap):
"""
Evaluates the fitness function of an organism if it has not
already been evaluated. The correctResultMap determines
the correct mapping between inputs and outputs.
Args:
correctResultMap: testOrgs.SimulationMap
Return type: <float> or <int> (number)
"""
if self.fitness is None:
self.toVerilog(self.verilogFilePath, self.moduleName)
#change the arguments on the line below or it will not toVerilog
testOutput = testScootBotOrganism(
self.verilogFilePath,
'TestCode',
self.numInputs,
self.numOutputs,
self.moduleName,
writeSim=False,
clearFiles=False)
self.fitness = self.fitnessFunction(testOutput)
#print self.fitness
#print testOutput
#print "fitness: ", self.fitness
#raw_input("press enter yo")
return self.fitness
def fitnessFunction(self, testOutput):
return testOutput[0].count('PickedUp')
def crossover(self, otherParent):
"""
Return Type: <ScootBotOrganism>
Crossovers self with another <ScootOrganism>, and returns a new
<ScootOrganism>.
"""
result = ScootBotOrganism(self.verilogFilePath, self.numInputs,
self.numOutputs, randomInit=False, maxDepth=self.maxDepth,
inputProbability=self.inputProbability, moduleName=self.moduleName)
for i in range(self.numOutputs):
selfTree = self.trees[i]
otherTree = otherParent.trees[i]
if (random.random() > ScootBotOrganism.treeCrossOverProbability):
#print "not crosovered"
if (random.random() < .5):
result.trees.append(selfTree)
else:
result.trees.append(otherTree)
else:
#print "crosovered"
if (random.random() < .5):
result.trees.append(selfTree.crossover(otherTree))
else:
result.trees.append(otherTree.crossover(selfTree))
return result
def mutate(self):
"""
Return Type: void
Mutates stuff
"""
for i in range(len(self.trees)):
if (random.random() < ScootBotOrganism.treeMutateProbability):
self.trees[i].mutate()
def testScootBotOrganism(filepath, subdir, numInputs, numOutputs,
organismModuleName, clearFiles=True, testFileName = 'simulateScootBot2',
writeSim=False):
# write the verilog test file
if writeSim:
writeSimulation(
os.path.join(subdir,'%s.v'%testFileName),
filepath,
numInputs,
numOutputs,
organismModuleName
)
# print 'Testing organism: %s'%filepath
# compile the test file
subprocess.call([
'iverilog', '-o',
os.path.join(subdir,'%s.o'%testFileName),
os.path.join(subdir,'%s.v'%testFileName)]
)
# get the test file results
process = subprocess.Popen([
'vvp',
os.path.join(subdir,'%s.o'%testFileName)],
stdout=subprocess.PIPE
)
# pull output from pipe
output = process.communicate() #(stdout, stderr)
if clearFiles:
try:
os.remove(os.path.join(subdir,'%s.o'%testFileName))
os.remove(os.path.join(subdir,'%s.v'%testFileName))
except:
print "Error clearing files!"
print "Files are to be cleared, but the files probably don't exist."
return output
def main():
import matplotlib.pyplot as pyplot
import testOrgs
from BooleanLogic import BooleanLogicOrganism
from TreeOrganism import TreeOrganism
pyplot.ion()
manager = ScootBotManager(ScootBotOrganism,
50,12,12,50,None,verilogWriteFileName = 'scootBot.v', #no simMap
verilogModuleName="scootBot",maxDepth=3,inputProbability=.85,maxGeneration=100)
manager.execute(True)
pyplot.show()
pyplot.ioff()
if __name__ == '__main__':
main()