-
Notifications
You must be signed in to change notification settings - Fork 0
/
10path with cluster plotbestxftfJt.py
454 lines (401 loc) · 15.3 KB
/
10path with cluster plotbestxftfJt.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import larch
import random
from larch_plugins.xafs import feffdat
from larch_plugins.xafs import xftf
from larch import Interpreter
import operator
import numpy as np
from operator import itemgetter
from larch_plugins.io import read_ascii
from larch_plugins.xafs import autobk
import datetime
import time
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
mylarch = Interpreter()
#range for rdm num generator
rangeA = (np.linspace(5,95,91) * 0.01).tolist()
rangeB = (np.linspace(-100,100,201) * 0.01).tolist()
largerRangeB = (np.linspace(-600,600,1201) * 0.01).tolist()
rangeC = (np.linspace(1,15,15) * 0.001).tolist()
rangeD = (np.linspace(-20,20,41) * 0.01).tolist()
rangeA.append(0)
#rangeB.append(0)
#rangeC.append(0)
#rangeD.append(0)
front = '/Users/csp572/Downloads/EXAFS-master-8/Cu Data/path Data/feff'
end = '.dat'
intervalK = (np.linspace(80,340,261)).tolist()
def fitness(indi,exp):
loss = 0
yTotal = [0]*(401)
for i in range(1,10):
if i < 10:
filename = front+'000'+str(i)+end
elif i< 100:
filename = front+'00'+str(i)+end
else:
filename = front+'0'+str(i)+end
path=feffdat.feffpath(filename, s02=str(indi[i-1][0]), e0=str(indi[i-1][1]), sigma2=str(indi[i-1][2]), deltar=str(indi[i-1][3]), _larch=mylarch)
feffdat._path2chi(path, _larch=mylarch)
y = path.chi
for k in intervalK:
yTotal[int(k)] += y[int(k)]
global g
for j in intervalK:
#loss = loss + (yTotal[int(j)]*g.k[int(j)]**2 - exp[int(j)]*g.k[int(j)]**2)**2
loss = loss + abs(yTotal[int(j)]*g.k[int(j)]**2 - exp[int(j)]*g.k[int(j)]**2)*(abs(exp[int(j)]*g.k[int(j)]**2))**(0.5)
return loss
def generateACombo():
a = random.choice(rangeA)
global b
c = random.choice(rangeC)
d = random.choice(rangeD)
return [a,b,c,d]
def generateIndi():
indi = []
for i in range(10):
indi.append(generateACombo())
return indi
def generateFirstGen(genSize):
gen = []
i = 0
while i < genSize:
gen.append(generateIndi())
i+=1
return gen
def computePerfPop(pop,exp):
populationPerf = {}
global MetaDictionary
for individual in pop:
#counter for dictionary
n = 1
individualTuple = tuple(tuple(x) for x in individual)
fit = fitness(individual, exp)
#Meta Dic
for combo in individual:
if n not in MetaDictionary:
MetaDictionary[n] = {"A":{},"C":{},"D":{}}
if (combo[0] not in MetaDictionary[n]["A"]):
MetaDictionary[n]["A"][combo[0]] = fit
elif fit < MetaDictionary[n]["A"][combo[0]]:
MetaDictionary[n]["A"][combo[0]] = fit
if (combo[2] not in MetaDictionary[n]["C"]):
MetaDictionary[n]["C"][combo[2]] = fit
elif fit < MetaDictionary[n]["C"][combo[2]]:
MetaDictionary[n]["C"][combo[2]] = fit
if (combo[3] not in MetaDictionary[n]["D"]):
MetaDictionary[n]["D"][combo[3]] = fit
elif fit < MetaDictionary[n]["D"][combo[3]]:
MetaDictionary[n]["D"][combo[3]] = fit
n += 1
#Sorted Dic
populationPerf[individualTuple] = fit
return sorted(populationPerf.items(), key = operator.itemgetter(1), reverse=False)
#def computePerfPop(pop,exp):
# populationPerf = {}
# for individual in pop:
# individualTuple = tuple(tuple(x) for x in individual)
# populationPerf[individualTuple] = fitness(individual, exp)
# return sorted(populationPerf.items(), key = operator.itemgetter(1), reverse=False)
def selectFromPopulation(populationSorted, best_sample, lucky_few):
nextBreeders = []
# fitSum = 0
# current = 0
# for indi in populationSorted:
# fitSum += indi[1]
# pick = random.uniform(0, fitSum)
# for indi in populationSorted:
# current += indi[1]
# if current < pick:
# newIndi = []
# for combo in indi[0]:
# newIndi.append(list(combo))
# nextBreeders.append(newIndi)
for i in range(best_sample):
nextBreeders.append(populationSorted[i])
for i in range(lucky_few):
j = random.randint(best_sample,len(populationSorted)-1)
nextBreeders.append(populationSorted[j])
random.shuffle(nextBreeders)
return nextBreeders
def createChild(individual1, individual2):
child = []
global diffCounter
global chance_of_mutation
global original_chance_of_mutation
if diffCounter > 10:
print("******************Different Breeding******************")
chance_of_mutation = 40
diffCounter = 0
for i in range(len(individual1)):
if (int(100 * random.random()) < 50):
child.append(individual1[i][0:2] + individual2[i][2:4])
else:
child.append(individual2[i][0:2] + individual1[i][2:4])
else:
chance_of_mutation = original_chance_of_mutation
for i in range(len(individual1)):
j = random.randint(0,1)
if j == 0:
child.append(individual1[i])
elif j == 1:
child.append(individual2[i])
return child
def createChildren(breeders, number_of_child):
nextPopulation = []
for i in range(int(len(breeders)/2)):
for j in range(number_of_child):
# b1 = random.randint(0,len(breeders)-1)
# b2 = random.randint(0,len(breeders)-1)
# nextPopulation.append(createChild(breeders[b1],breeders[b2]))
nextPopulation.append(createChild(breeders[i], breeders[len(breeders) -1 -i]))
return nextPopulation
def mutateIndi(indi):
indi = generateIndi()
return indi
def mutatePopulation(population, chance_of_mutation, chance_of_mutation_e0):
mutateTime = 0
for i in range(len(population)):
if random.random() * 100 < chance_of_mutation:
mutateTime+=1
population[i] = mutateIndi(population[i])
if random.random() * 100 < chance_of_mutation_e0:
global b
e0 = random.choice(rangeB)
b = e0
print("Mutate e0 to:", e0)
for i in range(len(population)):
for j in population[i]:
j[1] = e0
print("Mutate Times:", mutateTime)
return population
def nextGeneration (firstGeneration, exp, best_sample, lucky_few, number_of_child, chance_of_mutation, chance_of_mutation_e0):
st = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
global genNum
global historyBest
global bestDiff
global diffCounter
genNum+=1
print(st)
print("Gen:", genNum)
populationTupleSorted = computePerfPop(firstGeneration, exp)
populationSorted = []
print("Best Fit:", populationTupleSorted[0][1])
for indi in populationTupleSorted:
newIndi = []
for combo in indi[0]:
#print("--",combo)
newIndi.append(list(combo))
populationSorted.append(newIndi)
bestDiff = abs(populationTupleSorted[0][1]-historyBest)
historyBest = populationTupleSorted[0][1]
global bestBest
global best
global bestFitIndi
#if this indi is better than the history best
if historyBest < bestBest:
bestBest = historyBest
bestFitIndi = populationTupleSorted[0][0]
global bestYTotal
bestYTotal = [0]*(401)
global bestChir_magTotal
bestChir_magTotal = [0]*(326)
# lenY = 0
for i in range(1,10):
if i < 10:
filename = front+'000'+str(i)+end
elif i< 100:
filename = front+'00'+str(i)+end
else:
filename = front+'0'+str(i)+end
path=feffdat.feffpath(filename, s02=str(bestFitIndi[i-1][0]), e0=str(bestFitIndi[i-1][1]), sigma2=str(bestFitIndi[i-1][2]), deltar=str(bestFitIndi[i-1][3]), _larch=mylarch)
feffdat._path2chi(path, _larch=mylarch)
y = path.chi
# lenY = len(y)
for k in intervalK:
bestYTotal[int(k)] += y[int(k)]
'''Jeff'''
best.chi = bestYTotal
best.k = path.k
xftf(best.k, best.chi, kmin=3, kmax=17, dk=4, window='hanning', kweight=2, group=best, _larch=mylarch)
#whats the interval?? 326.
#for k in range(0, 326, 1):
#bestChir_magTotal[k] += path.chir_mag[k]
'''Jeff end'''
if bestDiff < 0.1:
diffCounter += 1
else:
diffCounter = 0
print("2nd Fit:",populationTupleSorted[1][1])
print("3rd Fit:",populationTupleSorted[2][1])
print("4th Fit:",populationTupleSorted[3][1])
print("Last Fit:",populationTupleSorted[len(populationTupleSorted)-1][1])
print("Different from last best fit:",bestDiff)
print("Best fit combination:\n",populationTupleSorted[0][0])
print("History Best:",bestBest)
print("History Best Indi:\n",bestFitIndi)
if genNum%1 == 0:
# print("Best fit combination:\n",populationTupleSorted[0][0])
indi = populationTupleSorted[0][0]
yTotal = [0]*(401)
chir_magTotal = [0]*(326)
# lenY = 0
for i in range(1,10):
if i < 10:
filename = front+'000'+str(i)+end
elif i< 100:
filename = front+'00'+str(i)+end
else:
filename = front+'0'+str(i)+end
path=feffdat.feffpath(filename, s02=str(indi[i-1][0]), e0=str(indi[i-1][1]), sigma2=str(indi[i-1][2]), deltar=str(indi[i-1][3]), _larch=mylarch)
feffdat._path2chi(path, _larch=mylarch)
y = path.chi
# lenY = len(y)
for k in intervalK:
yTotal[int(k)] += y[int(k)]
'''chang'''
SumGroup = path
SumGroup.chi = yTotal
SumGroup.k = path.k
xftf(SumGroup.k, SumGroup.chi, kmin=3, kmax=17, dk=4, window='hanning', kweight=2, group=SumGroup, _larch=mylarch)
'''chang end'''
global g
# for m in range(lenY):
# yTotal[m] = yTotal[m]*g.k**2
global global_yTotal
plt.plot(g.k, g.chi*g.k**2)
plt.plot(g.k[80:341], yTotal[80:341]*g.k[80:341]**2)
# plt.ylim(top=6, bottom=-6)
plt.show()
plt.plot(g.k, g.chi*g.k**2)
plt.plot(g.k[80:341], bestYTotal[80:341]*g.k[80:341]**2)
# plt.ylim(top=6, bottom=-6)
plt.show()
'''chang: plot fourier'''
plt.plot(g.r, g.chir_mag)
plt.plot(SumGroup.r, SumGroup.chir_mag)
plt.show()
plt.plot(g.r, g.chir_mag)
plt.plot(best.r, best.chir_mag)
plt.show()
'''chang end'''
# file.write("Gen Num: %d" % genNum)
# file.write("Fitness:"+str(populationTupleSorted[0][1]))
# file.write("Combination:"+str(populationTupleSorted[0][0]))
nextBreeders = selectFromPopulation(populationSorted, best_sample, lucky_few)
nextPopulation = createChildren(nextBreeders, number_of_child)
lenDiff = len(firstGeneration)-len(nextPopulation)
for i in range(lenDiff):
j = random.randint(0,len(firstGeneration)-1)
nextPopulation.append(firstGeneration[j])
print(len(nextPopulation))
nextGeneration = mutatePopulation(nextPopulation, chance_of_mutation, chance_of_mutation_e0)
return nextGeneration
def multipleGeneration(number_of_generation, exp, size_population, best_sample, lucky_few, number_of_child, chance_of_mutation, chance_of_mutation_e0):
historic = []
historic.append(generateFirstGen(size_population))
#fix
for i in range(int(number_of_generation/2)):
historic.append(nextGeneration(historic[i], exp, best_sample, lucky_few, number_of_child, chance_of_mutation, chance_of_mutation_e0))
global bestFitIndi
newE0 = findE0(bestFitIndi,exp)
global b
b = newE0
chance_of_mutation_e0 = 0
#change all E0 to fixed
for indi in historic[-1]:
for combo in indi:
combo[1] = newE0
for i in range (int(number_of_generation/2), number_of_generation):
historic.append(nextGeneration(historic[i], exp, best_sample, lucky_few, number_of_child, chance_of_mutation, chance_of_mutation_e0))
#print plots for ACD
global MetaDictionary
for key in MetaDictionary:
for attr in MetaDictionary[key]:
print("path: ",str(key),", attribute: ",str(attr),"printed below:")
listOfX = [x for x in MetaDictionary[key][attr]]
listOfY = [y for y in MetaDictionary[key][attr].values()]
plt.scatter(listOfX,listOfY)
plt.show()
return historic
#printing tool - NOT DONE!!!!!!!!!!!!!
def printSimpleResult(historic, exp, number_of_generation): #bestSolution in historic. Caution not the last
result = getListBestIndividualFromHistorique(historic, exp)[number_of_generation-1]
#print ("solution: \"" + result[0] + "\" de fitness: " + str(result[1]))
#analysis tools
def getBestIndividualFromPopulation (population, exp):
return computePerfPop(population, exp)[0]
def getListBestIndividualFromHistorique (historic, exp):
bestIndividuals = []
for population in historic:
bestIndividuals.append(getBestIndividualFromPopulation(population, exp))
return bestIndividuals
#fitE0
def findE0(bestFit,exp):
print("Finished First Half of Generation, Optimizing E0...")
lowestX = 99999
lowestY = 99999
listOfx = []
listOfy = []
bestFitList = [list(x) for x in bestFit]
for i in largerRangeB:
for j in bestFitList:
j[1] = i
indi = tuple(tuple(x) for x in bestFitList)
fit = fitness(indi,exp)
if fit < lowestY:
lowestY = fit
lowestX = i
listOfx.append(i)
listOfy.append(fit)
#print(listOfy)
plt.plot(listOfx,listOfy)
plt.show()
print("Continue With E0 =",lowestX)
return lowestX
#main program
file = open("Result.txt","w")
g = read_ascii('/Users/csp572/Downloads/EXAFS-master-8/Cu Data/cu_10k.xmu', _larch = mylarch)
best = read_ascii('/Users/csp572/Downloads/EXAFS-master-8/Cu Data/cu_10k.xmu', _larch = mylarch)
autobk(g, rbkg=1.45, _larch = mylarch)
autobk(best, rbkg=1.45, _larch=mylarch)
'''chang'''
xftf(g.k, g.chi, kmin=3, kmax=17, dk=4, window='hanning',
kweight=2, group=g, _larch=mylarch)
xftf(best.k, best.chi, kmin=3, kmax=17, dk=4, window='hanning',
kweight=2, group=best, _larch=mylarch)
#show(g, _larch=mylarch)
'''chang end'''
exp = g.chi
#kidNum = 0
genNum = 0
size_population = 1000
best_sample = 400
lucky_few = 200
number_of_child = 3
#number of generations
number_of_generation = 1000
chance_of_mutation = 20
original_chance_of_mutation = chance_of_mutation
chance_of_mutation_e0 = 20
historyBest = 0
bestDiff = 9999
diffCounter = 0
bestBest = 999999999
bestFitIndi = (())
bestChir_magTotal = [0]*(326)
bestYTotal = [0]*(401)
MetaDictionary = {}
#range fixed at 10, change later
for i in range(10):
bestFitIndi+=((0,0,0,0),)
#e0
b = random.choice(rangeB)
if ((best_sample + lucky_few) / 2 * number_of_child > size_population):
print ("population size not stable")
else:
historic = multipleGeneration(number_of_generation, exp, size_population, best_sample, lucky_few, number_of_child, chance_of_mutation, chance_of_mutation_e0)
#"How much longer you gonna be on this?"
printSimpleResult(historic, exp, number_of_generation)