weixin_39017744 2019-11-12 18:50 采纳率: 83.3%
浏览 277
已结题

请问如何代码中优化python基因算法的过程?

from random import randint as rd

fc=20 #父代数量
equipment=[["knife",10,1],["Beans",20,5],["Potatoes",15,10],["Onions",2,1],["Sleeping Bag",30,7],["Rope",10,5],["Compass",30,1]]
fathers=[]  #父代,每个父代7个基因位(因为一共7个物品)。
maxValue=0 #最高得分
maxSon=[]

def makeFathers(fathers): #产生初始父代
    for i in range(fc):
        father=[]
        for j in range(7):
            gen=rd(0,6) #随即生成父代基因位
            if gen in father: #如果该基因已存在
                father.append(7) #则添加一个7号基因(即不存在,表示此位置不带物品)
            else:
                father.append(gen) #否则添加该随即基因
        fathers.append(father)

def makeSons(sons): #交配(父代基因交换)
    sons.clear()
    for i in range(fc-1):
        for j in range(fc-1-i):
            son=fathers[i].copy()
            son[4:]=fathers[i+j+1][4:]
            sons.append(son)

def exchange(sons): #等位交换
    for i in sons:
        if rd(0,10)>8: #概率发生
            g1=rd(0,6) #发生交换的基因位1
            g2=rd(0,6) #发生交换的基因位2
            i[g1],i[g2]=i[g2],i[g1]

def change(sons): #突变
    for i in sons:
        if rd(0,10)>8: #概率发生
            i[rd(0,6)]=rd(0,7) #随即一位的基因变为随机

def kill(sons): #杀死不符合的子代
    sonsTmp=[]
    for i in sons:
        son=i.copy()
        son.sort()
        flag=0 #基因重复标记
        weight=0 #重量计数
        for j in range(6):
            if son[j]==son[j+1] and son[j]!=7:
                flag+=1
            if son[j]!=7:
                weight+=equipment[son[j]][2] #基因位重量累加
        if son[6]!=7:
            weight+=equipment[son[6]][2]
        if not (flag>0 or weight>20): #如果不出现重复或超重
            sonsTmp.append(i)
    sons.clear()
    for i in sonsTmp:
        sons.append(i)

def rank(sons): #子代评分
    global maxValue
    global maxSon
    values=[]
    for i in sons:
        value=0 #初始评分
        for j in range(7):
            if i[j]!=7:
                value+=equipment[i[j]][1] #物品得分累加
        if value>120:
            print(i)
        i.append(value) #为子代添加评分
        values.append(value) #为总评分列表添加评分
    values.sort() #评分排序
    if maxValue<values[-1]:
        maxValue=values[-1]
    sonsTmp=[]
    for i in sons:  #将评分低于第20高得分的子代杀死
        if i[-1]>values[-fc]:
            sonsTmp.append(i)
        if i[-1]==maxValue:
            maxSon=i[:-1].copy()
    print("本轮最高得分 :%d ,目前最佳得分 :%d" % (values[-1],maxValue))
    print(maxSon)
    fathers=sonsTmp.copy() #剩余子代作为新父代

def main():
    sons=[]
    makeFathers(fathers)
    for i in range(100):  #100次迭代
        makeSons(sons)
        exchange(sons)
        change(sons)
        kill(sons)
        rank(sons)

main()

代码如上
将剩余子代做为新父代那一句灰色,导致无法一代代繁衍,只能优化第一代。找了很久解决方案,直接global 调来fathers会导致很多错误,修了好久没成功。多数是由于父代长度变了造成的报错,但是全部修复后发现似乎fathers会在makesons功能中失去所有fathers。

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2019-11-13 10:51
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?