-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSOManager.py
395 lines (361 loc) · 15.3 KB
/
PSOManager.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import ROOT
from array import array
from subprocess import call
from particle import Particle
from QueHelper import QueHelper
import time as timer
import sys
import json
class PSOManager:
def __init__(self,Path="",Verbose=True,PSOConfig=""):
self.Verbose=Verbose
self.vw=1.0
self.vp=1.0
self.vg=1.0
self.Particles=[]
self.BestFOMGlobal=0.0
self.BestKSGlobal=0.0
self.BestCoordinatesGlobal=[]
self.Coordinates=[]
self.rand=ROOT.TRandom3(0) ## change to !=0 if you want repeatable random seeds
self.Path=Path
self.nParticles=10
self.TenBestMVAs=[[0.0,0.0,"",[],[],[]] for i in range(10)]
#print self.TenBestMVAs
self.KSThreshold=0.1
RunSystem="EKPSL5"
self.NIterations=1
self.FOM="ROCIntegral"
self.MaxVariablesInCombination=10
self.ImprovementThreshold=1.00
self.FactoryString=""
self.PreparationString=""
self.SignalWeightExpression=""
self.BackgroundWeightExpression=""
self.MethodType="TMVA::Types::kBDT"
self.MethodParams=""
self.SignalTreeName="MVATree"
self.BackgroundTreeName="MVATree"
self.RepeatTrainingNTimes=0
self.DrawNRandomAsStartingVars=0
self.SaveTrainingsToTrees="False"
self.UseEvenOddSplitting=0
initialVariables=[]
additionalVariables=[]
ivars=[]
avars=[]
weightVariables=[]
configFile=open(PSOConfig,"r")
configLines=list(configFile)
configFile.close()
for i,l in enumerate(configLines):
configLines[i]=configLines[i].replace("\n","")
if "InitialVariables:" in configLines and "EndVariables" in configLines:
id1=configLines.index("InitialVariables:")
id2=configLines.index("EndVariables")
initialVariables=configLines[id1+1:id2]
configLines=configLines[:id1]+configLines[id2+1:]
if "AdditionalVariables:" in configLines and "EndVariables" in configLines:
id1=configLines.index("AdditionalVariables:")
id2=configLines.index("EndVariables")
additionalVariables=configLines[id1+1:id2]
configLines=configLines[:id1]+configLines[id2+1:]
if len(initialVariables)>0:
for i, var in enumerate(initialVariables):
if "#" in var:
continue
v=json.loads(var)
ivars.append(str(v[0]))
if len(additionalVariables)>0:
for i, var in enumerate(additionalVariables):
if "#" in var:
continue
v=json.loads(var)
avars.append(str(v[0]))
#check the input variables
print "inital Variables ", len(ivars), ivars
print "additional Variables ", len(avars), avars
allVars=avars+ivars
for var1 in allVars:
counter=0
for var2 in allVars:
if var1==var2:
counter+=1
if counter>=2:
print "ERROR: input variables defined multiple times"
print var1
exit(1)
self.usedVariables=ivars
self.unusedVariables=avars
#self.TenBestMVAs[0]=(0.0,0.0,0.0,0.0,0.0,0,2,self.usedVariables, self.unusedVariables)
#done with variables
#read the rest of the stuff
for line in configLines:
if "#" in line:
continue
if "RunOn" in line:
RunSystem=line.split("=",1)[1]
if "NParticles" in line:
self.nParticles=int(line.split("=",1)[1])
if "NIterations" in line:
self.NIterations=int(line.split("=",1)[1])
if "wIneratia" in line:
self.vw=float(line.split("=",1)[1])
if "wMemory" in line:
self.vp=float(line.split("=",1)[1])
if "wSwarm" in line:
self.vg=float(line.split("=",1)[1])
if "FOM=" in line:
self.FOM=line.split("=",1)[1]
if "KSThreshold" in line:
self.KSThreshold=float(line.split("=",1)[1])
if "MaxVariablesInCombination" in line:
self.MaxVariablesInCombination=int(line.split("=",1)[1])
if "ImprovementThreshold" in line:
self.ImprovementThreshold=float(line.split("=",1)[1])
if "FactoryString" in line:
self.FactoryString=line.split("=",1)[1]
if "PreparationString" in line:
self.PreparationString=line.split("=",1)[1]
if "nTrain_Signal" in line:
buff=line.split("=",1)[1]
if buff!="" and buff!=" ":
self.PreparationString="nTrain_Signal="+buff+":"+self.PreparationString
if "nTrain_Background" in line:
buff=line.split("=",1)[1]
if buff!="" and buff!=" ":
self.PreparationString="nTrain_Background="+buff+":"+self.PreparationString
if "nTest_Signal" in line:
buff=line.split("=",1)[1]
if buff!="" and buff!=" ":
self.PreparationString="nTest_Signal="+buff+":"+self.PreparationString
if "nTest_Background" in line:
buff=line.split("=",1)[1]
if buff!="" and buff!=" ":
self.PreparationString="nTest_Background="+buff+":"+self.PreparationString
if "SignalWeightExpression" in line:
self.SignalWeightExpression=line.split("=",1)[1]
if "BackgroundWeightExpression" in line:
self.BackgroundWeightExpression=line.split("=",1)[1]
if "MethodType" in line:
self.MethodType=line.split("=",1)[1]
if "MethodParams" in line:
self.MethodParams=line.split("=",1)[1]
if "UseEvenOddSplitting" in line:
self.UseEvenOddSplitting=line.split("=",1)[1]
if "SourceBackgroundTree" in line:
self.BackgroundTreeName=line.split("=",1)[1]
if "SourceSignalTree" in line:
self.SignalTreeName=line.split("=",1)[1]
if "RepeatTrainingNTimes" in line:
self.RepeatTrainingNTimes=int(line.split("=",1)[1])
if "DrawNRandomAsStartingVars" in line:
self.DrawNRandomAsStartingVars=int(line.split("=",1)[1])
if "SaveTrainingsToTrees" in line:
self.SaveTrainingsToTrees=line.split("=",1)[1]
if "coord" in line:
buff=line.split("=",1)[1]
coord=json.loads(buff)
print "setting up coordinate ",coord
if coord not in self.Coordinates:
self.Coordinates.append(coord)
self.BestCoordinatesGlobal.append([coord[0],0.0])
#set up the que system
self.QueHelper=QueHelper(RunSystem)
def SetupParticles(self):
for i in range(self.nParticles):
call(["rm","-r","-f","Particles/Particle"+str(i)])
call(['cp','-r','InitData',"Particles/Particle"+str(i)])
call(['mv',"Particles/Particle"+str(i)+"/PSO.sh","Particles/Particle"+str(i)+"/PSO"+str(i)+".sh" ])
#print str(self.nParticles)+" Particles set up"
def InitParticles(self):
for part in range(self.nParticles):
#get starting values for the particle
#uniformly distributed in coord space
initialCoords=[]
for coord in self.Coordinates:
if coord[4]=="int":
initValue=int(self.rand.Uniform(coord[1],coord[2]))
initVelocity=int(self.rand.Uniform(-(coord[2]-coord[1]),(coord[2]-coord[1])))
#now bound the velocity by the maximum
initVelocity=int(cmp(initVelocity,0)*min(abs(initVelocity),coord[3]))
elif coord[4]=="float":
initValue=float(self.rand.Uniform(coord[1],coord[2]))
initVelocity=float(self.rand.Uniform(-(coord[2]-coord[1]),(coord[2]-coord[1])))
#now bound the velocity by the maximum
initVelocity=float(cmp(initVelocity,0.0)*min(abs(initVelocity),coord[3]))
else:
print "ERROR: coordinates have to be int or float"
exit(1)
initialCoords.append([coord[0],initValue,initVelocity])
print "Particle", part, " has inital coords ", initialCoords
particle=Particle(
self.Path+"Particles/Particle"+str(part)+"/",
part,
self.Verbose,
self.usedVariables,
self.unusedVariables,
self.vw, self.vp, self.vg,
self.Coordinates,
initialCoords,
self.FOM,
self.KSThreshold,
self.FactoryString,
self.PreparationString,
self.SignalWeightExpression,
self.BackgroundWeightExpression,
self.SignalTreeName,
self.BackgroundTreeName,
self.MethodType,
self.MethodParams,
self.QueHelper,
self.MaxVariablesInCombination,
self.ImprovementThreshold,
self.RepeatTrainingNTimes,
self.DrawNRandomAsStartingVars,
self.SaveTrainingsToTrees,
self.UseEvenOddSplitting)
#particle.SetTestPoint(initTree,initShrinkage,initBagging,initCuts,2,1,0)
self.Particles.append(particle)
def RunPSO(self):
print "\n-------------------------------------------------------------"
print "Starting Optimization"
print "doing ", self.NIterations, " Iterations\n"
startTime=0
finishTime=0
totalTime=0.0
nIterations=self.NIterations
for it in range(nIterations):
startTime=timer.time()
for particle in self.Particles:
particle.StartEvaluation()
running=True
print "\nIteration ", it
if it!=0:
estTime=totalTime/it * (nIterations-it)/60.0/60.0
print "optimization finished in ca ", estTime, " hours"
print "Number of particles finished :"
while running:
nFinished=0
timer.sleep(30)
for particle in self.Particles:
partRun = particle.CheckJobStatus()
if partRun==False:
nFinished+=1
#print nFinished
sys.stdout.write(str(nFinished)+" ")
sys.stdout.flush()
#print(str(nFinished)+" "),
if nFinished==self.nParticles:
running=False
print " "
for particle in self.Particles:
fom, KS, methodString, currentcoords, usedVars, unusedVars = particle.GetResult()
if self.Verbose:
print "particle returned: "
print fom, KS, methodString, currentcoords, usedVars, unusedVars
if fom>=self.TenBestMVAs[0][0]:
if fom==0.0:
self.TenBestMVAs[0]=[fom, KS, methodString, usedVars, unusedVars,currentcoords]
#print "starting fix done"
else:
self.TenBestMVAs[9]=[fom, KS, methodString, usedVars, unusedVars,currentcoords]
self.TenBestMVAs=sorted(self.TenBestMVAs, key=lambda s:s[0], reverse=True)
#print "first"
elif fom>=self.TenBestMVAs[9][0]:
self.TenBestMVAs[9]=[fom, KS, methodString, usedVars, unusedVars,currentcoords]
self.TenBestMVAs=sorted(self.TenBestMVAs, key=lambda s:s[0], reverse=True)
#print "second"
self.BestFOMGlobal=self.TenBestMVAs[0][0]
self.BestKSGlobal=self.TenBestMVAs[0][1]
self.BestCoordinatesGlobal=self.TenBestMVAs[0][5]
#print self.TenBestMVAs[0]
for particle in self.Particles:
particle.UpdateParticle(self.BestCoordinatesGlobal,it,self.BestFOMGlobal,self.BestKSGlobal)
print "\n------------------------------------------------------------------------"
print "Best Result after Iteration "+str(it)
print self.TenBestMVAs[0][:5]
self.SaveStatus("PSOResult.txt")
finishTime=timer.time()
totalTime+=(finishTime-startTime)
def PrintResult(self):
print "Ten Best MVAs"
for i in range(10):
print self.TenBestMVAs[i][:5]
def SaveStatus(self, SaveFile):
savefile=open(SaveFile,"w")
savefile.write("nParticles "+str(self.nParticles)+"\n")
savefile.write("wIneratia "+str(self.vw)+"\n")
savefile.write("wMemory "+str(self.vp)+"\n")
savefile.write("wSwarm "+str(self.vg)+"\n")
savefile.write("path "+str(self.Path)+"\n")
savefile.write("usedVariables "+str(self.usedVariables)+"\n")
savefile.write("unusedVariables "+str(self.unusedVariables)+"\n")
savefile.write("KSThreshold "+str(self.KSThreshold)+"\n")
savefile.write("TenBestMVAs\n")
for i in range(10):
savefile.write(str(self.TenBestMVAs[i][:5])+"\n")
savefile.close()
#for part in self.Particles:
#part.SaveParticleStatus()
splitPath = self.Path.split("/")
#print splitPath
last = splitPath[len(splitPath)-2]
CatDef = last.partition("_")[2]
#print CatDef
bestBDTFile=open("FinalMVAConfig_"+CatDef+".txt","w")
bestBDTFile.write("FOM "+str(self.TenBestMVAs[0][0])+"\n")
bestBDTFile.write("KS "+str(self.TenBestMVAs[0][1])+"\n")
bestBDTFile.write("Method "+str(self.TenBestMVAs[0][2])+"\n")
bestBDTFile.write("Variables "+str(self.TenBestMVAs[0][3])+"\n")
bestBDTFile.write("Factory "+str(self.FactoryString)+"\n")
bestBDTFile.write("Preparation "+str(self.PreparationString)+"\n")
bestBDTFile.write("SignalWeight "+str(self.SignalWeightExpression)+"\n")
bestBDTFile.write("Preparation "+str(self.BackgroundWeightExpression)+"\n")
bestBDTFile.write(str(self.TenBestMVAs[0][:5])+"\n")
bestBDTFile.close()
def GetVariableNumbers(self):
outfile=open(self.Path+"/VariableNumbers.txt","w")
for var in self.usedVariables:
VarCount=0
for i in range(self.nParticles):
file=open(self.Path+"Particles/Particle"+str(i)+"/ParticleRoute.txt","r")
lines=list(file)
for line in lines:
#print line
if "0.0 " not in line:
#print line
if var in line:
#print line
VarCount+=1
file.close()
print var, VarCount
outfile.write(var+" "+str(VarCount)+"\n")
for var in self.unusedVariables:
VarCount=0
for i in range(self.nParticles):
file=open(self.Path+"Particles/Particle"+str(i)+"/ParticleRoute.txt","r")
lines=list(file)
for line in lines:
if "0.0 " not in line:
if var in line:
VarCount+=1
file.close()
print var, VarCount
outfile.write(var+" "+str(VarCount)+"\n")
outfile.close()
def testFunction(self):
for part in self.Particles:
part.WriteConfig()
part.UpdateParticle([[u'NTrees', 1000], [u'Shrinkage', 0.02], [u'GradBaggingFraction', 0.2], [u'nCuts', 50]])
part.StartEvaluation()
part.CheckJobStatus()
print self.BestCoordinatesGlobal
bg=[[u'NTrees', 0.0], [u'Shrinkage', 0.0], [u'GradBaggingFraction', 0.0], [u'nCuts', 0.0]]
print "did the tests"
def CompileAndSetupClientExecutable(self):
print "Freshly Compiling PSO/Particle.C"
call(["g++ -o PSO/Particle PSO/Particle.C `root-config --cflags --glibs` -lTMVA"],shell=True)
print "Copying the freshly compiled Particle exec to InitialData"
call(["cp","PSO/Particle","InitData/Particle"])
print "done"