# python遗传算法

``````
import numpy as np

DNA_size = 24
pops = 100
jiaohuan = 0.7
bianyi = 0.03
gen = 100
# x_bound=[-3,3]
# y_bound=[-3,3]
x1_bound = [0, 3147]
x2_bound = [0, 30977]
x3_bound = [0, 1724]
x4_bound = [0, 966]
x5_bound = [0, 971]
x6_bound = [0, 7661]
x7_bound = [0, 9385]
x8_bound = [0, 2521]
x9_bound = [0, 699]
x10_bound = [0, 36972]
x11_bound = [0, 7885]
x12_bound = [0, 10207]
x13_bound = [0, 1181]
x14_bound = [0, 9768]
x15_bound = [0, 8181]
x16_bound = [0, 1014]
x17_bound = [0, 21293]
x18_bound = [0, 2081]
x19_bound = [0, 2816]
x20_bound = [0, 21267]
x21_bound = [0, 1788]
x22_bound = [0, 736]
x23_bound = [0, 922]
x24_bound = [0, 595]
x25_bound = [0, 15114]
x26_bound = [0, 23695]
x27_bound = [0, 5398]
x28_bound = [0, 342]
x29_bound = [0, 2005]
x30_bound = [0, 381]

def fun(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24,
x25, x26, x27, x28, x29, x30):
return 1.2 * (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10) + 1.1 * (
x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20) + (
x21 + x22 + x23 + x24 + x25 + x26 + x27 + x28 + x29 + x30)

def get_fitness(pop):
x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30 = trans(
pop)
pred = fun(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23,
x24, x25, x26, x27, x28, x29, x30)
return pred

def trans(pop):
x1_pop = pop[:, 0:DNA_size]
x2_pop = pop[:, 0:DNA_size]
x3_pop = pop[:, 0:DNA_size]
x4_pop = pop[:, 0:DNA_size]
x5_pop = pop[:, 0:DNA_size]
x6_pop = pop[:, 0:DNA_size]
x7_pop = pop[:, 0:DNA_size]
x8_pop = pop[:, 0:DNA_size]
x9_pop = pop[:, 0:DNA_size]
x10_pop = pop[:, 0:DNA_size]
x11_pop = pop[:, 0:DNA_size]
x12_pop = pop[:, 0:DNA_size]
x13_pop = pop[:, 0:DNA_size]
x14_pop = pop[:, 0:DNA_size]
x15_pop = pop[:, 0:DNA_size]
x16_pop = pop[:, 0:DNA_size]
x17_pop = pop[:, 0:DNA_size]
x18_pop = pop[:, 0:DNA_size]
x19_pop = pop[:, 0:DNA_size]
x20_pop = pop[:, 0:DNA_size]
x21_pop = pop[:, 0:DNA_size]
x22_pop = pop[:, 0:DNA_size]
x23_pop = pop[:, 0:DNA_size]
x24_pop = pop[:, 0:DNA_size]
x25_pop = pop[:, 0:DNA_size]
x26_pop = pop[:, 0:DNA_size]
x27_pop = pop[:, 0:DNA_size]
x28_pop = pop[:, 0:DNA_size]
x29_pop = pop[:, 0:DNA_size]
x30_pop = pop[:, 0:DNA_size]

x1 = x1_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x1_bound[1] - x1_bound[0]) + x1_bound[
0]
x2 = x2_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x2_bound[1] - x2_bound[0]) + x2_bound[
0]
x3 = x3_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x3_bound[1] - x3_bound[0]) + x3_bound[
0]
x4 = x4_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x4_bound[1] - x4_bound[0]) + x4_bound[
0]
x5 = x5_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x5_bound[1] - x5_bound[0]) + x5_bound[
0]
x6 = x6_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x6_bound[1] - x6_bound[0]) + x6_bound[
0]
x7 = x7_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x7_bound[1] - x7_bound[0]) + x7_bound[
0]
x8 = x8_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x8_bound[1] - x8_bound[0]) + x8_bound[
0]
x9 = x9_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x9_bound[1] - x9_bound[0]) + x9_bound[
0]
x10 = x10_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x10_bound[1] - x10_bound[0]) + \
x10_bound[0]
x11 = x11_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x11_bound[1] - x11_bound[0]) + \
x11_bound[0]
x12 = x12_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x12_bound[1] - x12_bound[0]) + \
x12_bound[0]
x13 = x13_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x13_bound[1] - x13_bound[0]) + \
x13_bound[0]
x14 = x14_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x14_bound[1] - x14_bound[0]) + \
x14_bound[0]
x15 = x15_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x15_bound[1] - x15_bound[0]) + \
x15_bound[0]
x16 = x16_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x16_bound[1] - x16_bound[0]) + \
x16_bound[0]
x17 = x17_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x17_bound[1] - x17_bound[0]) + \
x17_bound[0]
x18 = x18_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x18_bound[1] - x18_bound[0]) + \
x18_bound[0]
x19 = x19_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x19_bound[1] - x19_bound[0]) + \
x19_bound[0]
x20 = x20_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x20_bound[1] - x20_bound[0]) + \
x20_bound[0]
x21 = x21_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x21_bound[1] - x21_bound[0]) + \
x21_bound[0]
x22 = x22_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x22_bound[1] - x22_bound[0]) + \
x22_bound[0]
x23 = x23_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x23_bound[1] - x23_bound[0]) + \
x23_bound[0]
x24 = x24_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x24_bound[1] - x24_bound[0]) + \
x24_bound[0]
x25 = x25_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x25_bound[1] - x25_bound[0]) + \
x25_bound[0]
x26 = x26_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x26_bound[1] - x26_bound[0]) + \
x26_bound[0]
x27 = x27_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x27_bound[1] - x27_bound[0]) + \
x27_bound[0]
x28 = x28_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x28_bound[1] - x28_bound[0]) + \
x28_bound[0]
x29 = x29_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x29_bound[1] - x29_bound[0]) + \
x29_bound[0]
x30 = x30_pop.dot(2 ** np.arange(DNA_size)[::-1]) / float(2 ** DNA_size - 1) * (x30_bound[1] - x30_bound[0]) + \
x30_bound[0]

return x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30

def crossover(pop):
new_pop = []
pred = get_fitness(pop)
index = np.argmax(pred)
index2 = np.argmin(pred)
for father in pop:
if (father == pop[index2]).all() == True:
child = pop[index]
else:
child = father
if np.random.rand() < jiaohuan:
mother = pop[np.random.randint(pops)]
cross_point = np.random.randint(low=0, high=DNA_size * 2)
child[cross_point:] = mother[cross_point:]
child = mutation(child)
new_pop.append(child)
return new_pop

def mutation(child):
if np.random.rand() < bianyi:
mutate_point = np.random.randint(0, DNA_size)
child[mutate_point] = child[mutate_point] ^ 1
return child

def select(pop, fitness):
x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30 = trans(
pop)
if (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10) / 0.6 + (
x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20) / 0.66 + (
x21 + x22 + x23 + x24 + x25 + x26 + x27 + x28 + x29 + x30) / 0.72 <= 56400:
if (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10) <= 4642:
if (x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20) <= 23833:
if (x21 + x22 + x23 + x24 + x25 + x26 + x27 + x28 + x29 + x30) <= 8155:
idx = np.random.choice(np.arange(pops), size=pops, replace=True,
p=(fitness) / (fitness.sum()))
return pop[idx]

def print_info(pop):
fitness = get_fitness(pop)
max_index = np.argmax(fitness)
print("最优解：", fitness[max_index])
x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30 = trans(
pop)
print("最优基因型：", pop[max_index])
print("最优解的x,y :", x1[max_index], x2[max_index], x3[max_index], x4[max_index], x5[max_index], x6[max_index],
x7[max_index], x8[max_index], x9[max_index], x10[max_index], x11[max_index], x12[max_index], x13[max_index],
x14[max_index], x15[max_index], x16[max_index], x17[max_index], x18[max_index], x19[max_index],
x20[max_index], x21[max_index], x22[max_index], x23[max_index], x24[max_index], x25[max_index],
x26[max_index], x27[max_index], x28[max_index], x29[max_index], x30[max_index])

if __name__ == "__main__":
pop = np.random.randint(2, size=(pops, DNA_size * 2))
fitness = get_fitness(pop)
for i in range(gen):
pop = np.array(crossover(pop))
fitness = get_fitness(pop)
pop = select(pop, fitness)
print_info(pop)

``````

• 写回答

#### 1条回答默认 最新

• 亖夕 Python领域新星创作者 2022-09-04 18:24
关注

值错误:包含一个以上元素的数组的真值是不明确的，要使用a.any()或a.all()
183行改成
if ((x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10) / 0.6 + (x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20) / 0.66 + (x21 + x22 + x23 + x24 + x25 + x26 + x27 + x28 + x29 + x30) / 0.72 <= 56400).all():

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

• 系统已结题 9月16日
• 已采纳回答 9月8日
• 创建了问题 9月3日

#### 悬赏问题

• ¥60 最新ukb数据下载方法
• ¥15 关于#python#的问题：您好可以加您一下联系方式吗，在复现的时候确实有点问题难以解决
• ¥15 联想电脑重装系统时无法发现硬盘
• ¥15 MATLAB与UR10e实体机械臂建立通讯
• ¥15 c++题需要快一点不用opencv
• ¥15 关于#java#的问题：想要咨询Flowable流程引擎框架的问题
• ¥15 vscode里面怎么用plaformio强调串口啊
• ¥20 针对计算后数据做一致性检验可以用Bland Altman法吗
• ¥15 win32如何自绘编辑框的背景图片(语言-c++|操作系统-windows)
• ¥15 微信夜间被转走了1w对，当天手机剪切板里就出现了这个乱码，有铁子可以看看是啥吗可以