python遗传算法解决分配问题
import numpy as np
import random
import matplotlib.pyplot as pltdef get_rand():select = [x for x in range(10)]random.shuffle(select)return selecttime = np.array([[82, 16, 66, 71, 44, 28, 76, 85, 36, 8],[91, 98, 4, 4, 39, 68, 26, 26, 84, 6],[13, 96, 85, 28, 77, 66, 51, 82, 59, 54],[92, 49, 94, 5, 80, 17, 70, 25, 55, 78],[64, 81, 68, 10, 19, 12, 90, 93, 92, 94],[10, 15, 76, 83, 49, 50, 96, 35, 29, 13],[28, 43, 75, 70, 45, 96, 55, 20, 76, 57],[55, 92, 40, 32, 65, 35, 14, 26, 76, 47],[96, 80, 66, 96, 71, 59, 15, 62, 39, 2],[97, 96, 18, 4, 76, 23, 26, 48, 57, 34]])
'''种群数量'''
magnit = 100
'''工人数量'''
pop = (time.shape)[1]
'''初始化群体'''
# print(pop)
rang = np.zeros((magnit, pop), int)
# print(rang)
# print(type(rang[1][1]))
for i in range(magnit):select = [x for x in range(10)]random.shuffle(select)rang[i, :] = select# print(rang)def cal_time(rang, time, pop):sum_time_pop = []for choice in rang:single_array_sum_time = 0for i in range(pop):cut = choice[i]single_array_sum_time += time[cut][i]sum_time_pop.append(single_array_sum_time)sum_time_pop = np.array(sum_time_pop).reshape(-1, 1)num = sum_time_pop.shape[0]return sum_time_pop, numcount = 0
bestpop = []
while count < 100:'''计算每一种分配的总时间'''sum_time_pop = []for choice in rang:single_array_sum_time = 0for i in range(pop):cut = choice[i]single_array_sum_time += time[cut][i]sum_time_pop.append(single_array_sum_time)sum_time_pop = np.array(sum_time_pop).reshape(-1, 1)num = sum_time_pop.shape[0]# print(sum_time_pop)'''排序'''index = np.argsort(sum_time_pop, 0)sum_time_pop = sum_time_pop[index].reshape(num, 1)[0:magnit, :]rang = rang[index].reshape(num, pop)[0:magnit, :]'''计算适值和适应度'''fit = 1000 - sum_time_pop[:] # 适值fitplus = np.cumsum(fit).reshape(-1, 1) # 适值向下叠加# print(fitplus[magnit-1, :])fitlevelplus = fitplus[:] / fitplus[magnit - 1, :] # 适应度向下叠加fitlevelplus = np.insert(fitlevelplus, 0, np.array([0]), 0) # 在第一行添加0# print(fitlevelplus)# a = (np.shape(fitlevelplus))[0]'''选择'''newchoose = []new_sum_time = []for _ in range(magnit):rand = random.random()for row in range(magnit):if rand > fitlevelplus[row, :] and rand < fitlevelplus[row + 1, :]:newchoose.append(rang[row, :])new_sum_time.append(sum_time_pop[row, :])continuenewchoose = np.array(newchoose)prechoose = newchoose.copy()new_sum_time = np.array(new_sum_time)# print(newchoose)# print((np.shape(newchoose))[0])# print(new_sum_time)'''交叉'''pc = 0.8for row in range(0, (np.shape(newchoose))[0], 2):if pc > random.random():gen_1 = newchoose[row, :].copy()gen_2 = newchoose[row + 1, :].copy()select = [x for x in range(10)]random.shuffle(select)r1 = select[0]r2 = select[1]r1, r2 = min(r1, r2), max(r1, r2)cr1, cr2 = gen_1[r1:r2 + 1].copy(), gen_2[r1:r2 + 1].copy()# print(r1, r2, cr1, cr2)for site in range(r2 - r1 + 1):s1 = np.where(gen_1 == cr2[site])gen_1[s1] = (gen_1[r1:r2 + 1])[site].copy()a = gen_1for site in range(r2 - r1 + 1):s2 = np.where(gen_2 == cr1[site])gen_2[s2] = (gen_2[r1:r2 + 1])[site].copy()b = gen_2gen_1[r1:r2 + 1] = cr2.copy()gen_2[r1:r2 + 1] = cr1.copy()newchoose[row, :] = gen_1.copy()newchoose[row + 1, :] = gen_2.copy()'''变异'''pm = 0.1for row in range(0, (np.shape(newchoose))[0]):if pm > random.random():select = [x for x in range(10)]random.shuffle(select)r1 = select[0]r2 = select[1]gen = newchoose[row, :]gen[r1], gen[r2] = gen[r2], gen[r1]newchoose[row, :] = gen.copy()# print(r1,r2,gen[r1],gen[r2])# print(newchoose,'--------')# print(prechoose)rang = np.append(prechoose, newchoose, 0)bestpop.append(sum_time_pop[0, :])count += 1# print(rang.shape)
bestpop = np.array(bestpop)
# print(bestpop)
print(sum_time_pop[0, :])
print(rang[0, :])x = [a for a in range(1,101)]
y = bestpop
plt.plot(x,y)
plt.xlabel('迭代次数')
plt.ylabel('总时间')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.show()
运行结果:
最短时间:138
分配情况:2 0 1 9 4 3 7 6 5 8(0~9)