-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maintest.py
804 lines (681 loc) · 30.6 KB
/
Maintest.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
import numpy as np
import matplotlib.pyplot as plt
from PyQt5 import uic
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import math
import random
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QVBoxLayout, QLineEdit, QLabel, QGroupBox, QPushButton, QApplication, QWidget
from PyQt5.uic import loadUi
e=math.e
DNA_SIZE = 24
POP_SIZE = 100
CROSSOVER_RATE = 0.8
MUTATION_RATE = 0.01
MUTATION_AMOUNT=0.05
N_GENERATIONS = 48
CHECKTIME = 20 #独立验证次数
#POP_SIZE = 100
#CROSSOVER_RATE = 0.8
#MUTATION_RATE = 0.01
#MUTATION_AMOUNT=0.05
#N_GENERATIONS = 48
#CHECKTIME = 20 #独立验证次数
ELITE_RATE=0.50
X_BOUND = [-4, 4]
Y_BOUND = [-4, 4]
CATASTROPHE_RATE = 0.001
#比较参数:POP_SIZE = 400,POP_SIZE = 400(精算);POP_SIZE = 200,N_GENERATIONS = 48(速算)
def GA(q):
def F(x_1, x_2):
n = 2
a = 20
b = 0.2
c = 2 * np.pi
return -a * e**(-b * (1 / n * (x_1 **2 + x_2 **2))*0.5) - e**(1 / n * (np.cos(c * x_1)+np.cos(c * x_2))) + a + e
# def F(x_1, x_2):
# return x_1**2+x_2**2
def plot_3d(ax):
X = np.linspace(*X_BOUND, 100)
Y = np.linspace(*Y_BOUND, 100)
X, Y = np.meshgrid(X, Y)
Z = F(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.pause(3)
plt.show()
def get_fitness(pop):
x, y = translateDNA(pop)
pred = F(x, y)
#return pred
#return pred - np.min(pred)+1e-5 # 求最大值时的适应度
return np.max(pred) - pred + 1e-5 # 求最小值时的适应度,通过这一步fitness的范围为[0, np.max(pred)-np.min(pred)]
def translateDNA(pop): # pop表示种群矩阵,一行表示一个二进制编码表示的DNA,矩阵的行数为种群数目
x_pop = pop[:, 0:DNA_SIZE] # 前DNA_SIZE位表示X
y_pop = pop[:, DNA_SIZE:] # 后DNA_SIZE位表示Y
x = x_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (X_BOUND[1] - X_BOUND[0]) + X_BOUND[0]
y = y_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (Y_BOUND[1] - Y_BOUND[0]) + Y_BOUND[0]
return x, y
def crossover_and_mutation(fitness,pop, CROSSOVER_RATE=0.8):
new_pop = []
count = 0
fitness_max=max(fitness)
fitness_avg=sum(fitness)/POP_SIZE
# print("适应度最大值为:",fitness_max)
# print("适应度平均值为:",fitness_avg)
for father in pop: # 遍历种群中的每一个个体,将该个体作为父亲
child = father # 孩子先得到父亲的全部基因(这里我把一串二进制串的那些0,1称为基因)
i = np.random.randint(POP_SIZE)
mother = pop[np.random.randint(POP_SIZE)] # 再种群中选择另一个个体,并将该个体作为母亲
#print(fitness[i]) #母亲的适应度
#print(fitness[count]) #父亲的适应度
f = max(fitness[i],fitness[count]) #两者较大的一个
count += 1
#print(f)
if f >= fitness_avg:
CROSSOVER_RATE_dynamic=CROSSOVER_RATE*((fitness_max-f)/(fitness_max-fitness_avg))#动态规划
MUTATION_RATE_dynamic=MUTATION_RATE*((fitness_max-f)/(fitness_max-fitness_avg))
elif f<fitness_avg:
CROSSOVER_RATE_dynamic=CROSSOVER_RATE
MUTATION_RATE_dynamic=MUTATION_RATE
if np.random.rand() < CROSSOVER_RATE_dynamic: # 产生子代时不是必然发生交叉,而是以一定的概率发生交叉
cross_points = np.random.randint(low=0, high=DNA_SIZE * 2) # 随机产生交叉的点
child[cross_points:] = mother[cross_points:] # 孩子得到位于交叉点后的母亲的基因
if np.random.rand() < MUTATION_RATE_dynamic:
mutation(child) # 每个后代有一定的机率发生变异
new_pop.append(child)
return new_pop
def mutation(child, MUTATION_RATE=0.003):
for _ in range(int(MUTATION_AMOUNT*DNA_SIZE)):
mutate_point = np.random.randint(0, DNA_SIZE) # 随机产生一个实数,代表要变异基因的位置
child[mutate_point] = child[mutate_point] ^ 1 # 将变异点的二进制为反转
def select(pop, fitness): # nature selection wrt pop's fitness
p = (fitness) / (fitness.sum())
idx = np.random.choice(np.arange(POP_SIZE), size=POP_SIZE, replace=True,
p=p)
#__________________精英主义
if np.random.rand() < ELITE_RATE and count<int(0.25*(N_GENERATIONS)):
for _ in range(int(0.05*POP_SIZE)):
elite = np.argmax(p)
#print("精英是:",elite)
idx[random.randint(0,POP_SIZE-1)]=idx[elite] #将精英随机选中一定比例的样本进行覆盖
#_______________灾变
# if np.random.rand() < CATASTROPHE_RATE and int(0.50*(N_GENERATIONS))>count>int(0.25*(N_GENERATIONS)):
# for _ in range(int(0.05 * POP_SIZE)):
# elite = np.argmax(p)
# idx[elite]=idx[random.randint(0,POP_SIZE-1)]
return pop[idx]
def print_info(pop):
fitness = get_fitness(pop)
max_fitness_index = np.argmax(fitness)
print("max_fitness:", fitness[max_fitness_index])
x, y = translateDNA(pop)
print("最优的基因型:", pop[max_fitness_index])
print("(x, y):", (x[max_fitness_index], y[max_fitness_index]))
# fig = plt.figure()
# ax = Axes3D(fig)
# plt.ion() # 将画图模式改为交互模式,程序遇到plt.show不会暂停,而是继续执行
# plot_3d(ax)
pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE * 2)) # matrix (POP_SIZE, DNA_SIZE)
count=0
#记录每次收敛的最大值:y,辅助以x构成折线图
#
each_y=[]
# 收敛矩阵
pop_matrix = []
for i in range(CHECKTIME):
temp = []
for j in range(N_GENERATIONS): # 迭代N代
#迭代次数计数器:
#print("计数器现在是",count)
count = count + 1
x, y = translateDNA(pop)
# if 'sca' in locals():
# sca.remove()
# sca = ax.scatter(x, y, F(x, y), c='black', marker='o')
# plt.show()
# plt.pause(0.1)
#________自适应
fitness = get_fitness(pop)
max_fitness_index = np.argmax(fitness)
pop = np.array(crossover_and_mutation(fitness, pop, CROSSOVER_RATE))
pop = select(pop, fitness) # 选择生成新的种群
temp.append(F(x[max_fitness_index], y[max_fitness_index]))
count=0
each_y.append(F(x[max_fitness_index], y[max_fitness_index]))
#print(temp)
pop_matrix.append(temp)
print(i+1,":",CHECKTIME)
pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE * 2))
#print(pop_matrix)
#time_end = time.time()
#time_c = time_end - time_start
#print('耗时', time_c, 's')
#收敛过程
x_axis_for_2d = np.arange(0, N_GENERATIONS, 1)
for i in range(CHECKTIME):
plt.plot(x_axis_for_2d,pop_matrix[i])
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel('迭代次数')
plt.ylabel('每世代各种群的收敛值')
plt.suptitle('收敛速度分析', fontsize=20)
#plt.show()
x_pattern1=x_axis_for_2d
y_pattern1_matrix=pop_matrix
x = []
for i in range(CHECKTIME):
x.append(i)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.ylim(0, 2)
plt.xlabel('实验次数')
plt.ylabel('最终收敛值')
plt.suptitle('独立测试中各种群最终收敛值', fontsize=20)
plt.plot(x,each_y)
#plt.show()
print("函数的平均最小值是",sum(each_y)/len(each_y))
print("函数最小值中位数为",np.median(each_y))
print_info(pop)
# plt.ioff()
# plot_3d(ax)
x_pattern0 = x
y_pattern0 = each_y
if (q == 0):#收敛值分析
return x_pattern0,y_pattern0#决定最后输出什么类型的图表
elif(q == 1) :#收敛速度分析
return x_pattern1,y_pattern1_matrix
else:
print("问题在这里")
print(q)
def GA_1(q):
def F(x_1, x_2):
n = 2
a = 20
b = 0.2
c = 2 * np.pi
return -a * e ** (-b * (1 / n * (x_1 ** 2 + x_2 ** 2)) * 0.5) - e ** (
1 / n * (np.cos(c * x_1) + np.cos(c * x_2))) + a + e
# def F(x_1, x_2):
# return x_1**2+x_2**2
def plot_3d(ax):
X = np.linspace(*X_BOUND, 100)
Y = np.linspace(*Y_BOUND, 100)
X, Y = np.meshgrid(X, Y)
Z = F(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.pause(3)
plt.show()
def get_fitness(pop):
x, y = translateDNA(pop)
pred = F(x, y)
# return pred
# return pred - np.min(pred)+1e-5 # 求最大值时的适应度
return np.max(pred) - pred + 1e-5 # 求最小值时的适应度,通过这一步fitness的范围为[0, np.max(pred)-np.min(pred)]
def translateDNA(pop): # pop表示种群矩阵,一行表示一个二进制编码表示的DNA,矩阵的行数为种群数目
x_pop = pop[:, 0:DNA_SIZE] # 前DNA_SIZE位表示X
y_pop = pop[:, DNA_SIZE:] # 后DNA_SIZE位表示Y
x = x_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (X_BOUND[1] - X_BOUND[0]) + X_BOUND[
0]
y = y_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (Y_BOUND[1] - Y_BOUND[0]) + Y_BOUND[
0]
return x, y
def crossover_and_mutation(fitness, pop, CROSSOVER_RATE=0.8):
new_pop = []
count = 0
fitness_max = max(fitness)
fitness_avg = sum(fitness) / POP_SIZE
# print("适应度最大值为:",fitness_max)
# print("适应度平均值为:",fitness_avg)
for father in pop: # 遍历种群中的每一个个体,将该个体作为父亲
child = father # 孩子先得到父亲的全部基因(这里我把一串二进制串的那些0,1称为基因)
i = np.random.randint(POP_SIZE)
mother = pop[np.random.randint(POP_SIZE)] # 再种群中选择另一个个体,并将该个体作为母亲
# print(fitness[i]) #母亲的适应度
# print(fitness[count]) #父亲的适应度
f = max(fitness[i], fitness[count]) # 两者较大的一个
count += 1
# print(f)
if f >= fitness_avg:
CROSSOVER_RATE_dynamic = CROSSOVER_RATE * ((fitness_max - f) / (fitness_max - fitness_avg)) # 动态规划
MUTATION_RATE_dynamic = MUTATION_RATE * ((fitness_max - f) / (fitness_max - fitness_avg))
CROSSOVER_RATE_dynamic = CROSSOVER_RATE
MUTATION_RATE_dynamic = MUTATION_RATE
elif f < fitness_avg:
CROSSOVER_RATE_dynamic = CROSSOVER_RATE
MUTATION_RATE_dynamic = MUTATION_RATE
if np.random.rand() < CROSSOVER_RATE_dynamic: # 产生子代时不是必然发生交叉,而是以一定的概率发生交叉
cross_points = np.random.randint(low=0, high=DNA_SIZE * 2) # 随机产生交叉的点
child[cross_points:] = mother[cross_points:] # 孩子得到位于交叉点后的母亲的基因
if np.random.rand() < MUTATION_RATE_dynamic:
mutation(child) # 每个后代有一定的机率发生变异
new_pop.append(child)
return new_pop
def mutation(child, MUTATION_RATE=0.003):
for _ in range(int(MUTATION_AMOUNT * DNA_SIZE)):
mutate_point = np.random.randint(0, DNA_SIZE) # 随机产生一个实数,代表要变异基因的位置
child[mutate_point] = child[mutate_point] ^ 1 # 将变异点的二进制为反转
def select(pop, fitness): # nature selection wrt pop's fitness
p = (fitness) / (fitness.sum())
idx = np.random.choice(np.arange(POP_SIZE), size=POP_SIZE, replace=True,
p=p)
# __________________精英主义
if np.random.rand() < ELITE_RATE and count < int(0.25 * (N_GENERATIONS)):
for _ in range(int(0.05 * POP_SIZE)):
elite = np.argmax(p)
# print("精英是:",elite)
idx[random.randint(0, POP_SIZE - 1)] = idx[elite] # 将精英随机选中一定比例的样本进行覆盖
# _______________灾变
# if np.random.rand() < CATASTROPHE_RATE and int(0.50*(N_GENERATIONS))>count>int(0.25*(N_GENERATIONS)):
# for _ in range(int(0.05 * POP_SIZE)):
# elite = np.argmax(p)
# idx[elite]=idx[random.randint(0,POP_SIZE-1)]
return pop[idx]
def print_info(pop):
fitness = get_fitness(pop)
max_fitness_index = np.argmax(fitness)
print("max_fitness:", fitness[max_fitness_index])
x, y = translateDNA(pop)
print("最优的基因型:", pop[max_fitness_index])
print("(x, y):", (x[max_fitness_index], y[max_fitness_index]))
# fig = plt.figure()
# ax = Axes3D(fig)
# plt.ion() # 将画图模式改为交互模式,程序遇到plt.show不会暂停,而是继续执行
# plot_3d(ax)
pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE * 2)) # matrix (POP_SIZE, DNA_SIZE)
count = 0
# 记录每次收敛的最大值:y,辅助以x构成折线图
#
each_y = []
# 收敛矩阵
pop_matrix = []
for i in range(CHECKTIME):
temp = []
for j in range(N_GENERATIONS): # 迭代N代
# 迭代次数计数器:
# print("计数器现在是",count)
count = count + 1
x, y = translateDNA(pop)
# if 'sca' in locals():
# sca.remove()
# sca = ax.scatter(x, y, F(x, y), c='black', marker='o')
# plt.show()
# plt.pause(0.1)
# ________自适应
fitness = get_fitness(pop)
max_fitness_index = np.argmax(fitness)
pop = np.array(crossover_and_mutation(fitness, pop, CROSSOVER_RATE))
pop = select(pop, fitness) # 选择生成新的种群
temp.append(F(x[max_fitness_index], y[max_fitness_index]))
count = 0
each_y.append(F(x[max_fitness_index], y[max_fitness_index]))
# print(temp)
pop_matrix.append(temp)
print(i + 1, ":", CHECKTIME)
pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE * 2))
# print(pop_matrix)
#time_end = time.time()
#time_c = time_end - time_start
#print('耗时', time_c, 's')
# 收敛过程
x_axis_for_2d = np.arange(0, N_GENERATIONS, 1)
for i in range(CHECKTIME):
plt.plot(x_axis_for_2d, pop_matrix[i])
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel('迭代次数')
plt.ylabel('每世代各种群的收敛值')
plt.suptitle('收敛速度分析', fontsize=20)
#plt.show()
x_pattern1 = x_axis_for_2d
y_pattern1 = pop_matrix
x = []
for i in range(CHECKTIME):
x.append(i)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.ylim(0, 2)
plt.xlabel('实验次数')
plt.ylabel('最终收敛值')
plt.suptitle('独立测试中各种群最终收敛值', fontsize=20)
plt.plot(x, each_y)
#plt.show()
print("函数的平均最小值是", sum(each_y) / len(each_y))
print("函数最小值中位数为", np.median(each_y))
print_info(pop)
# plt.ioff()
# plot_3d(ax)
x_pattern0 = x
y_pattern0 = each_y
print(q)
if(q == 1):
return x_pattern1,y_pattern1
elif(q==0):
return x_pattern0,y_pattern0
def AGA(q):
def F(x_1, x_2):
n = 2
a = 20
b = 0.2
c = 2 * np.pi
return -a * e**(-b * (1 / n * (x_1 **2 + x_2 **2))*0.5) - e**(1 / n * (np.cos(c * x_1)+np.cos(c * x_2))) + a + e
# def F(x_1, x_2):
# return x_1**2+x_2**2
def plot_3d(ax):
X = np.linspace(*X_BOUND, 100)
Y = np.linspace(*Y_BOUND, 100)
X, Y = np.meshgrid(X, Y)
Z = F(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.pause(3)
plt.show()
def get_fitness(pop):
x, y = translateDNA(pop)
pred = F(x, y)
#return pred
#return pred - np.min(pred)+1e-5 # 求最大值时的适应度
return np.max(pred) - pred + 1e-5 # 求最小值时的适应度,通过这一步fitness的范围为[0, np.max(pred)-np.min(pred)]
def translateDNA(pop): # pop表示种群矩阵,一行表示一个二进制编码表示的DNA,矩阵的行数为种群数目
x_pop = pop[:, 0:DNA_SIZE] # 前DNA_SIZE位表示X
y_pop = pop[:, DNA_SIZE:] # 后DNA_SIZE位表示Y
x = x_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (X_BOUND[1] - X_BOUND[0]) + X_BOUND[0]
y = y_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (Y_BOUND[1] - Y_BOUND[0]) + Y_BOUND[0]
return x, y
def crossover_and_mutation(fitness,pop):
new_pop = []
count = 0
fitness_max=max(fitness)
fitness_avg=sum(fitness)/POP_SIZE
var = np.var(fitness)
# 计算种群稠密度
density = 1 / (1 + math.exp(-var))
MUTATION_RATE_FACTOR = (1 - ((density - 0.5) / 0.5)) * 0.01 # 反向归一化,再映射到0~0.1作为变异率
CROSSOVER_RATE_FACTOR = (1 - ((density - 0.5) / 0.5)) * 0.5 +0.4 # 反向归一化,再映射到0.4~0.9作为交叉率
# print("适应度最大值为:",fitness_max)
# print("适应度平均值为:",fitness_avg)
for father in pop: # 遍历种群中的每一个个体,将该个体作为父亲
child = father # 孩子先得到父亲的全部基因(这里我把一串二进制串的那些0,1称为基因)
i = np.random.randint(POP_SIZE)
mother = pop[np.random.randint(POP_SIZE)] # 再种群中选择另一个个体,并将该个体作为母亲
#print(fitness[i]) #母亲的适应度
#print(fitness[count]) #父亲的适应度
f = max(fitness[i],fitness[count]) #两者较大的一个
count += 1
#print(f)
if f >= fitness_avg:
CROSSOVER_RATE_dynamic=(CROSSOVER_RATE*((fitness_max-f)/(fitness_max-fitness_avg)))*0.5+(CROSSOVER_RATE_FACTOR)*0.5
# CROSSOVER_RATE_dynamic=CROSSOVER_RATE*((fitness_max-f)/(fitness_max-fitness_avg))
#动态规划,好个体减少交叉率和变异率
# print("这是比较好的个体的交叉率")
# print(CROSSOVER_RATE_dynamic)
MUTATION_RATE_dynamic=(MUTATION_RATE*((fitness_max-f)/(fitness_max-fitness_avg)))*0.5+(MUTATION_RATE_FACTOR)*0.5
elif f<fitness_avg:
CROSSOVER_RATE_dynamic=CROSSOVER_RATE_FACTOR
MUTATION_RATE_dynamic=MUTATION_RATE_FACTOR
if np.random.rand() < CROSSOVER_RATE_dynamic: # 产生子代时不是必然发生交叉,而是以一定的概率发生交叉
cross_points = np.random.randint(low=0, high=DNA_SIZE * 2) # 随机产生交叉的点
child[cross_points:] = mother[cross_points:] # 孩子得到位于交叉点后的母亲的基因
if np.random.rand() < MUTATION_RATE_dynamic:
mutation(child) # 每个后代有一定的机率发生变异
new_pop.append(child)
return new_pop
def mutation(child):
for _ in range(int(MUTATION_AMOUNT*DNA_SIZE)):
mutate_point = np.random.randint(0, DNA_SIZE) # 随机产生一个实数,代表要变异基因的位置
child[mutate_point] = child[mutate_point] ^ 1 # 将变异点的二进制为反转
def select(pop, fitness): # nature selection wrt pop's fitness
p = (fitness) / (fitness.sum())
idx = np.random.choice(np.arange(POP_SIZE), size=POP_SIZE, replace=True,
p=p)
#__________________精英主义
if np.random.rand() < ELITE_RATE and count<int(0.25*(N_GENERATIONS)):
for _ in range(int(0.05*POP_SIZE)):
elite = np.argmax(p)
#print("精英是:",elite)
idx[random.randint(0,POP_SIZE-1)]=idx[elite] #将精英随机选中一定比例的样本进行覆盖
#_______________灾变
return pop[idx]
def print_info(pop):
fitness = get_fitness(pop)
# print(fitness)
max_fitness_index = np.argmax(fitness)
print("max_fitness:", fitness[max_fitness_index])
x, y = translateDNA(pop)
print("最优的基因型:", pop[max_fitness_index])
print("(x, y):", (x[max_fitness_index], y[max_fitness_index]))
def get_var(pop):
fitness = get_fitness(pop)
var=np.var(fitness)
#计算种群稠密度,映射到1~0.5,越接近1越稀疏
density =1/(1+math.exp(-var))
MUTATION_RATE_FACTOR = (1-((density - 0.5)/0.5))*0.001 #反向归一化,再映射到0~0.001作为变异率
print(MUTATION_RATE_FACTOR)
# 一般来说,到最后可能整个区域都是一个基因型,需要变动
# print(density)
# fig = plt.figure()
# ax = Axes3D(fig)
# plt.ion() # 将画图模式改为交互模式,程序遇到plt.show不会暂停,而是继续执行
# plot_3d(ax)
pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE * 2)) # matrix (POP_SIZE, DNA_SIZE)
count=0
#记录每次收敛的最大值:y,辅助以x构成折线图
#
each_y=[]
# 收敛矩阵
pop_matrix = []
for i in range(CHECKTIME):
temp = []
for j in range(N_GENERATIONS): # 迭代N代
#迭代次数计数器:
#print("计数器现在是",count)
count = count + 1
x, y = translateDNA(pop)
# if 'sca' in locals():
# sca.remove()
# sca = ax.scatter(x, y, F(x, y), c='black', marker='o')
# plt.show()
# plt.pause(0.1)
#________自适应
fitness = get_fitness(pop)
max_fitness_index = np.argmax(fitness)
pop = np.array(crossover_and_mutation(fitness, pop))
pop = select(pop, fitness) # 选择生成新的种群
temp.append(F(x[max_fitness_index], y[max_fitness_index]))
# get_var(pop)
count=0
each_y.append(F(x[max_fitness_index], y[max_fitness_index]))
#print(temp)
pop_matrix.append(temp)
print(i+1,":",CHECKTIME)
pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE * 2))
#print(pop_matrix)
#time_end = time.time()
#time_c = time_end - time_start
#print('耗时', time_c, 's')
#收敛过程
x_axis_for_2d = np.arange(0, N_GENERATIONS, 1)
for i in range(CHECKTIME):
plt.plot(x_axis_for_2d,pop_matrix[i])
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel('迭代次数')
plt.ylabel('每世代各种群的收敛值')
plt.suptitle('收敛速度分析', fontsize=20)
#plt.show()
x_pattern1=x_axis_for_2d
y_pattern1_matrix=pop_matrix
x = []
for i in range(CHECKTIME):
x.append(i)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.ylim(0, 2)
plt.xlabel('实验次数')
plt.ylabel('最终收敛值')
plt.suptitle('独立测试中各种群最终收敛值', fontsize=20)
plt.plot(x,each_y)
#plt.show()
print("函数的平均最小值是",sum(each_y)/len(each_y))
print("函数最小值中位数为",np.median(each_y))
print_info(pop)
# plt.ioff()
# plot_3d(ax)
x_pattern0 = x
y_pattern0 = each_y
if (q == 0):#收敛值分析
return x_pattern0,y_pattern0#决定最后输出什么类型的图表
elif(q == 1) :#收敛速度分析
return x_pattern1,y_pattern1_matrix
else:
print("问题在这里")
print(q)
import sys
import numpy as np
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QLineEdit, QGroupBox
from PyQt5.QtGui import QPixmap
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
#
#
# POP_SIZE = 100
# CROSSOVER_RATE = 0.8
# MUTATION_RATE = 0.01
# MUTATION_AMOUNT=0.05
# N_GENERATIONS = 48
# CHECKTIME = 20 #独立验证次数
class GeneticAlgorithmUI(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("遗传算法界面")
self.initUI()
def initUI(self):
uic.loadUi("UI.ui", self)
#触发器
self.pushButton_17.clicked.connect(self.submit_parameters)
self.pushButton_16.clicked.connect(lambda: self.run_algorithm("standard_convergence", 0))
self.pushButton_14.clicked.connect(lambda: self.run_algorithm("adaptive_convergence", 0))
self.pushButton_13.clicked.connect(lambda: self.run_algorithm("standard_speed", 1))
self.pushButton_15.clicked.connect(lambda: self.run_algorithm("adaptive_speed", 1))
def submit_parameters(self):
# 根据控件名称获取对应的控件对象
global POP_SIZE, CROSSOVER_RATE, MUTATION_RATE, MUTATION_AMOUNT, N_GENERATIONS, CHECKTIM
POP_SIZE= int(self.textEdit_13.toPlainText())
CROSSOVER_RATE = float(self.textEdit_14.toPlainText())
MUTATION_RATE = float(self.textEdit_15.toPlainText())
MUTATION_AMOUNT =float( self.textEdit_16.toPlainText())
N_GENERATIONS = int(self.textEdit_17.toPlainText())
CHECKTIME = int(self.textEdit_18.toPlainText())
print("目前的参数如下",POP_SIZE, CROSSOVER_RATE, MUTATION_RATE, MUTATION_AMOUNT, N_GENERATIONS, CHECKTIME)
# # Algorithm buttons
def run_algorithm(self, algorithm_type=None, analysis_type=None):
global POP_SIZE, CROSSOVER_RATE, MUTATION_RATE, MUTATION_AMOUNT, N_GENERATIONS, CHECKTIME
POP_SIZE = int(self.textEdit_13.toPlainText())
CROSSOVER_RATE = float(self.textEdit_14.toPlainText())
MUTATION_RATE = float(self.textEdit_15.toPlainText())
MUTATION_AMOUNT = float(self.textEdit_16.toPlainText())
N_GENERATIONS = int(self.textEdit_17.toPlainText())
CHECKTIME = int(self.textEdit_18.toPlainText())
print( "跑算法的函数被调用了!")
if algorithm_type == "standard_convergence":
# 调用标准遗传算法函数,收敛值分析
x, y = GA(analysis_type,)
self.create_standard_image_amount(x, y)
elif algorithm_type == "adaptive_convergence":
# 调用自适应遗传算法函数,收敛值分析
x, y = AGA(analysis_type)
self.create_adaptive_image_amount(x, y)
elif algorithm_type == "standard_speed":
# 调用标准遗传算法函数,收敛速度分析
x, y = GA(analysis_type)
self.create_standard_image_speed(x, y)
elif algorithm_type == "adaptive_speed":
# 调用自适应遗传算法函数,收敛速度分析
x, y = AGA(analysis_type)
self.create_adaptive_image_speed(x, y)
def display_image(self, layout, image):
# 清除原有的图片
self.clear_images(layout)
# 添加新的图片
layout.addWidget(image)
def create_standard_image_amount(self, x, y):
fig = plt.Figure(figsize=(3.99, 3.89))
ax = fig.add_subplot(111)
ax.plot(x, y)
ax.set_xlabel('实验次数')
ax.set_ylabel('最终收敛值')
ax.set_title('独立测试中各种群最终收敛值', fontsize=20)
ax.set_ylim(0, 1)
# Create a Canvas object from the figure
canvas = FigureCanvas(fig)
# Convert Canvas to QPixmap
pixmap = QPixmap(canvas.size())
canvas.render(pixmap)
# Set the QPixmap to the QLabel
self.label_22.setPixmap(pixmap)
def create_standard_image_speed(self, x, y):
fig = plt.Figure(figsize=(3.99, 3.89))
ax = fig.add_subplot(111)
ax.set_ylim(0, 1)
for i in range(CHECKTIME):
ax.plot(x, y[i])
ax.set_xlabel('迭代次数')
ax.set_ylabel('每世代各种群的收敛值')
ax.set_title('收敛速度分析', fontsize=20)
# Create a Canvas object from the figure
canvas = FigureCanvas(fig)
# Convert Canvas to QPixmap
pixmap = QPixmap(canvas.size())
canvas.render(pixmap)
# Set the QPixmap to the QLabel
self.label_22.setPixmap(pixmap)
def create_adaptive_image_amount(self,x,y):
fig = plt.Figure(figsize=(3.99, 3.89))
ax = fig.add_subplot(111)
ax.plot(x, y)
ax.set_xlabel('实验次数')
ax.set_ylabel('最终收敛值')
ax.set_title('独立测试中各种群最终收敛值', fontsize=20)
ax.set_ylim(0, 1)
# Create a Canvas object from the figure
canvas = FigureCanvas(fig)
# Convert Canvas to QPixmap
pixmap = QPixmap(canvas.size())
canvas.render(pixmap)
# Set the QPixmap to the QLabel
self.label_23.setPixmap(pixmap)
def create_adaptive_image_speed(self,x,y):
fig = plt.Figure(figsize=(3.99, 3.89))
ax = fig.add_subplot(111)
ax.set_ylim(0, 1)
for i in range(CHECKTIME):
ax.plot(x, y[i])
ax.set_xlabel('迭代次数')
ax.set_ylabel('每世代各种群的收敛值')
ax.set_title('收敛速度分析', fontsize=20)
# Create a Canvas object from the figure
canvas = FigureCanvas(fig)
# Convert Canvas to QPixmap
pixmap = QPixmap(canvas.size())
canvas.render(pixmap)
# Set the QPixmap to the QLabel
self.label_23.setPixmap(pixmap)
if __name__ == '__main__':
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
window = GeneticAlgorithmUI()
window.show()
sys.exit(app.exec_())