直接上代码
# -*- coding: utf-8 -*-
import array
import random
import json
import numpy
import numpy as np
import pandas as pd
import os
from deap import algorithms,base,creator,tools
import multiprocessing
POP_SIZE = 200
#生成模拟数据
df=[int((800 -500) * np.random.random_sample()+ 500) for h in range(1800)]
IND_SIZE=1800
"""
假设有这么一个应用场景:
高一新生对其所有的新生进行分班,分为30个班
要求每个班的人数和分数(中考成绩)总和尽可能相等,每个班60个人
再把问题进行简化,新生总人数是60的倍数,总人数为1800人
假设学生的学号是1-1800
"""
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", array.array, typecode='i', fitness=creator.FitnessMin)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("indices", random.sample, range(IND_SIZE), IND_SIZE)
# Structure initializers
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.indices)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def translateDNA(DNA, df):
#print(DNA)
score_p = np.empty_like(DNA, dtype=np.int64)
for i, d in enumerate(DNA):
score_p[i]=df[d]
return score_p
def evalTSP(individual):
score_p=translateDNA(individual, df)
sum_score=[0 for h in range(30)]
for index_c in range(30):#总班级人数
sum_score[index_c]=np.sum(score_p[60*index_c:60*(index_c+1)])
loss=np.std(sum_score)
return loss,
toolbox.register("mate", tools.cxPartialyMatched)
toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", evalTSP)
if __name__ == "__main__":
random.seed(234)
# 开启8个进程进行并发计算
pool = multiprocessing.Pool(processes=8)
toolbox.register("map", pool.map)
pop = toolbox.population(n=POP_SIZE)
hof = tools.HallOfFame(1)
##将优化的结果进行统计
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)
pop, logbook=algorithms.eaSimple(pop, toolbox, 0.7, 0.1, 200, stats=stats, halloffame=hof)
pool.close()
#将优化的结果写入本地文件
#os.chdir("data")
#f=open("result.txt","w")
#print(hof,file=f)
运行结果:
gen nevals avg std min max
0 200 663.038 90.2416 363.649 863.209
1 156 641.244 81.6451 446.055 860.084
2 145 621.187 81.9976 409.38 821.071
3 147 625.44 95.0025 446.055 836.111
4 139 624.654 94.4089 446.055 874.641
5 137 611.439 90.5689 435.377 850.604
6 140 603.547 94.0976 436.471 939.92
7 139 596.746 92.2356 417.962 862.49
8 137 608.979 96.9298 425.851 857.976
9 152 618.447 93.0757 394.298 906.374
10 147 617.68 92.8426 391.621 886.907
11 163 626.885 89.9386 420.405 853.935
12 155 620.154 80.2981 456.917 873.394
13 149 624.371 78.9701 430.235 860.649
14 147 619.778 87.3665 375.372 905.794
15 151 628.543 87.992 432.697 863.73
16 129 612.099 84.33 455.366 888.036
17 153 620.685 88.6167 455.366 873.521
18 151 616.773 92.4059 445.476 899.183
19 144 601.46 87.0975 439.18 839.135
20 153 622.978 96.2228 439.18 924.719
21 153 622.135 91.1509 439.18 898.276
22 132 609.145 90.9044 414.759 881.22
23 144 606.942 99.9945 398.425 896.75
24 154 607.987 93.5085 398.425 900.884
25 154 614.743 96.834 414.759 853.219
26 148 619.891 94.4711 414.759 904.185
27 159 615.82 85.6307 342.606 804.844
28 139 611.906 90.8058 342.606 865.811
29 131 605.906 92.8274 376.479 858.809
30 133 610.802 89.235 376.479 854.539
31 150 620.746 92.4849 347.747 898.484
32 150 633.816 96.8833 347.747 848.105
33 122 605.245 82.5861 461.519 850.322
34 136 605.907 87.4045 455.886 805.494
35 150 613.93 90.5069 439.717 833.191
36 143 615.437 89.8362 448.975 891.014
37 155 626.868 97.3996 448.975 863.669
38 143 605.845 93.2684 400.593 880.837
39 156 618.728 89.2249 417.003 846.104
40 158 615.108 87.9471 412.018 892.327
41 147 612.426 87.8064 412.018 850.3
42 161 617.258 89.74 412.018 878.717
43 142 598.385 82.9323 364.204 807.321
44 161 618.88 99.5308 364.204 863.889
45 144 620.402 101.472 413.767 898.001
46 154 619.989 94.6417 413.767 843.491
47 149 620.219 94.0138 413.767 872.909
48 150 623.76 89.0978 435.689 857.686
49 142 610.458 96.8607 416.293 879.229
50 134 610.428 96.5349 416.293 965.44
51 144 610.099 90.7105 416.293 864.423
52 151 606.891 92.1193 412.897 898.567
53 160 615.897 89.2048 420.063 883.352
54 154 611.504 86.4881 384.11 851.355
55 160 621.214 89.7605 390.348 922.545
56 137 621.134 94.8716 386.73 889.607
57 147 611.827 92.4354 386.73 886.414
58 151 613.889 85.8398 386.73 846.488
59 163 618.961 92.3281 386.73 912.147
60 147 606.135 95.4461 386.73 799.906
61 145 599.177 100.464 386.73 918.148
62 140 596.29 103.138 407.975 838.963
63 155 598.97 104.974 399.679 950.353
64 131 590.668 111.906 399.679 887.453
65 151 608.757 107.644 414.235 883.795
66 143 586.678 99.8318 409.461 902.711
67 146 597.168 103.958 409.461 918.553
68 153 592.626 99.5577 401.98 861.703
69 132 586.817 102.877 401.98 871.028
70 137 576.783 100.241 374.134 810.682
71 144 580.324 111.737 374.134 899.763
72 155 587.824 96.4835 378.306 826.919
73 149 591.233 103.414 378.306 848.464
74 146 598.649 102.959 364.068 871.585
75 141 593.202 106.871 378.306 815.996
76 141 592.947 99.1806 378.306 816.309
77 144 594.647 98.3097 401.786 892.711
78 153 599.18 103.961 401.786 882.52
79 138 583.153 105.373 342.556 841.103
80 155 601.218 103.075 342.556 858.644
81 146 589.758 109.763 317.582 875.008
82 141 597.814 106.902 342.556 906.727
83 143 595.112 98.2713 397.56 828.849
84 144 601.375 102.444 397.56 893.868
85 137 584.082 95.8824 369.167 868.654
86 142 585.447 106.479 369.167 919.349
87 148 584.884 100.315 380.191 829.273
88 146 596.739 102.949 380.191 849.355
89 146 603.621 97.2974 380.191 865.493
90 154 610.267 97.4239 449.524 895.541
91 148 604.385 92.8878 393.602 900.65
92 146 596.254 90.6152 377.784 821.835
93 145 602.367 89.1771 377.784 894.626
94 147 605.487 96.1223 377.784 891.553
95 156 609.273 96.9593 377.784 839.919
96 159 596.615 91.5023 349.644 806.063
97 146 602.137 95.613 349.644 905.014
98 160 597.269 95.1558 390.21 835.139
99 150 581.713 88.372 390.21 780.696
100 149 590.036 98.4911 340.685 842.513
101 145 594.842 102.32 340.685 812.099
102 145 591.583 101.987 340.685 819.834
103 146 586.822 116.159 340.685 961.222
104 149 598.939 105.974 340.685 866.798
105 142 592.032 95.8593 328.899 865.973
106 140 585.267 106.222 318.955 883.363
107 148 587.732 106.592 328.899 856.287
108 163 590.005 93.5052 390.21 868.732
109 150 593.998 97.9191 399.661 850.379
110 155 587.238 93.3875 346.807 882.84
111 145 592.179 100.63 346.807 882.36
112 133 579.662 96.732 346.807 865.363
113 133 577.834 95.2702 344.75 825.723
114 145 600.461 100.232 346.807 846.283
115 146 585.689 100.147 346.807 863.542
116 159 607.494 101.154 346.807 851.886
117 160 598.754 93.6461 346.807 897.75
118 144 594.531 87.9266 346.807 799.927
119 146 600.983 89.7996 392.053 835.43
120 123 590.145 99.6178 383.331 927.474
121 151 577.331 90.5086 392.053 848.591
122 140 583.552 102.932 392.053 859.359
123 153 603.32 100.266 336.52 867.913
124 142 592.551 98.2895 336.52 877.013
125 139 602.544 105.631 336.52 941.727
126 141 597.774 101.001 336.52 903.286
127 146 589.245 94.018 348.27 839.544
128 149 601.697 95.4355 348.27 870.068
129 156 594.255 88.1518 400.433 919.061
130 153 598.717 91.5016 360.762 839.044
131 148 609.811 102.587 360.762 894.364
132 135 596.405 93.0674 360.762 844.391
133 138 600.701 101.49 360.762 850.958
134 139 597.136 104.378 360.762 880.864
135 146 614.275 104.635 360.762 884.376
136 149 596.863 94.9803 360.762 889.67
137 141 593.37 113.412 360.762 970.732
138 151 595.896 104.946 344.775 888.112
139 156 604.163 99.5229 344.775 819.933
140 138 595.839 96.3621 360.762 856.111
141 155 605.624 103.671 360.762 962.1
142 162 603.625 94.0956 360.762 873.828
143 139 595.102 82.5992 391.656 921.484
144 144 598.406 95.7784 381.215 861.924
145 134 594.89 92.0286 355.365 848.736
146 147 590.889 87.9088 355.365 807.406
147 140 589.291 97.7183 381.215 880.184
148 143 604.856 99.1614 391.656 853.367
149 140 598.659 100.526 382.99 883.613
150 141 588.401 92.9983 382.99 816.608
151 150 601.411 106.17 376.954 872.771
152 139 605.276 95.7904 391.656 847.436
153 147 599.941 98.6259 370.696 845.273
154 165 605.472 90.6497 391.656 852.442
155 151 605.455 92.8414 414.418 874.544
156 162 600.926 85.6537 414.418 843.022
157 141 592.059 89.3239 406.141 817.025
158 141 602.702 89.1685 414.418 891.501
159 155 621.426 91.4592 414.418 850.561
160 137 597.681 88.2213 414.418 828.274
161 152 617.349 100.492 405.856 852.43
162 144 607.967 100.219 415.749 863.014
163 144 597.438 94.4677 374.057 853.395
164 143 606.649 106.269 417.212 927.626
165 139 601.061 96.1181 417.212 876.191
166 138 608.182 94.6305 417.212 874.352
167 142 606.341 102.96 417.212 857.715
168 154 612.315 100.458 417.212 853.163
169 144 600.548 94.6036 389.043 912.677
170 147 596.326 93.1186 387.175 857.377
171 151 596.977 96.7877 417.355 916.968
172 154 601.28 96.2508 413.302 861.239
173 156 595.5 89.7786 358.787 846.475
174 153 598.559 90.441 388.117 841.884
175 159 599.361 76.5572 388.117 809.678
176 146 605.174 80.0781 388.117 845.562
177 152 603.332 87.9707 402.822 865.972
178 149 601.575 85.8932 402.822 819.41
179 147 596.651 85.7033 408.2 811.55
180 148 602.271 92.7995 409.478 873.351
181 149 605.633 91.6442 415.586 849.937
182 153 617.046 86.7802 437.072 846.955
183 141 617.407 93.4319 392.197 938.941
184 140 612.417 93.3851 442.852 914.159
185 136 596.893 91.5885 408.708 860.199
186 149 601.23 93.8262 397.826 870.708
187 138 597.682 89.6111 447.327 865.306
188 154 608.569 93.5705 427.847 889.18
189 130 595.448 93.9506 355.013 812.869
190 143 604.446 88.7994 450.377 847.469
191 154 603.6 92.1093 360.186 849.777
192 142 601.09 95.1065 360.186 929.13
193 157 611.321 84.6007 402.369 840.993
194 148 614.902 91.5541 402.369 871.742
195 146 598.961 88.846 414.413 828.086
196 147 585.712 99.989 414.413 938.569
197 138 596.541 104.046 401.733 881.945
198 148 589.805 87.2444 401.733 838.474
199 136 589.293 94.0039 401.733 865.789
200 144 581.681 95.4379 382.09 844.959
[Finished in 19.1s]