您好,欢迎访问三七文档
自然计算大作业学院:专业:学号:姓名:日期:成绩:指导教师:目标函数:maxf(x1,x2)x1·sin(4x1)+x2·sin(20x2)s.t.-3.0x14.1x2编码方式:本程序采用的是二进制编码精确到小数点后五位,经过计算可知对于𝑥1其编码长度为18,对于𝑥2其编码长度为15,因此每个基于的长度为33。参数设置:种群大小𝑝𝑜𝑝=100,交配池𝑃𝑝𝑜𝑜𝑙=𝑝𝑜𝑝/2,变异概率𝑃𝑚=100,交叉概率𝑃𝑐=100,最大迭代次数𝐺𝑚𝑎𝑥=300,自学习变异概率𝑆𝑝𝑚=0.05。算法步骤:设计的程序主要分为以下步骤:1、参数设置;2、种群初始化;3、用轮盘赌方法选择其中一半较好的个体作为父代;4、交叉和变异;5、更新最优解;6、对最有个体进行自学习操作;7结果输出。其算法流程图为:参数设置初始化判断是否已经达到迭代次数选择其中一半的个体作为父代进行交叉编译操作更新最优解输出结果否是自学习操作图1算法结果:由程序输出可知其最终优化结果为38.85029,𝑥1=11.625589,𝑥2=5.725031。输出基因编码为[110010111111101111010110100101111]。函数图像附录:importnumpyasnpimportrandomimportmathimportcopyclassInd():def__init__(self):self.fitness=0self.x=np.zeros(33)self.place=0self.x1=0self.x2=0defCal_fit(x,upper,lower):#计算适应度值函数Temp1=0foriinrange(18):Temp1+=x[i]*math.pow(2,i)Temp2=0foriinrange(18,33,1):Temp2+=math.pow(2,i-18)*x[i]x1=lower[0]+Temp1*(upper[0]-lower[0])/(math.pow(2,18)-1)x2=lower[1]+Temp2*(upper[1]-lower[1])/(math.pow(2,15)-1)ifx1upper[0]:x1=random.uniform(lower[0],upper[0])ifx2upper[1]:x2=random.uniform(lower[1],upper[1])return21.5+x1*math.sin(4*math.pi*(x1))+x2*math.sin(20*math.pi*x2)defInit(G,upper,lower,Pop):#初始化函数foriinrange(Pop):forjinrange(33):G[i].x[j]=random.randint(0,1)G[i].fitness=Cal_fit(G[i].x,upper,lower)G[i].place=idefFind_Best(G,Pop):Temp=copy.deepcopy(G[0])foriinrange(1,Pop,1):ifG[i].fitnessTemp.fitness:Temp=copy.deepcopy(G[i])returnTempdefSelection(G,Gparent,Pop,Ppool):#选择函数fit_sum=np.zeros(Pop)fit_sum[0]=G[0].fitnessforiinrange(1,Pop,1):fit_sum[i]=G[i].fitness+fit_sum[i-1]fit_sum=fit_sum/fit_sum.max()foriinrange(Ppool):rate=random.random()Gparent[i]=copy.deepcopy(G[np.where(fit_sumrate)[0][0]])defCross_and_Mutation(Gparent,Gchild,Pc,Pm,upper,lower,Pop,Ppool):#交叉和变异foriinrange(Ppool):place=random.sample([_for_inrange(Ppool)],2)parent1=copy.deepcopy(Gparent[place[0]])parent2=copy.deepcopy(Gparent[place[1]])parent3=copy.deepcopy(parent2)ifrandom.random()Pc:num=random.sample([_for_inrange(1,32,1)],2)num.sort()ifrandom.random()0.5:forjinrange(num[0],num[1],1):parent2.x[j]=parent1.x[j]else:forjinrange(0,num[0],1):parent2.x[j]=parent1.x[j]forjinrange(num[1],33,1):parent2.x[j]=parent1.x[j]num=random.sample([_for_inrange(1,32,1)],2)num.sort()num.sort()ifrandom.random()0.5:forjinrange(num[0],num[1],1):parent1.x[j]=parent3.x[j]else:forjinrange(0,num[0],1):parent1.x[j]=parent3.x[j]forjinrange(num[1],33,1):parent1.x[j]=parent3.x[j]forjinrange(33):ifrandom.random()Pm:parent1.x[j]=(parent1.x[j]+1)%2ifrandom.random()Pm:parent2.x[j]=(parent2.x[j]+1)%2parent1.fitness=Cal_fit(parent1.x,upper,lower)parent2.fitness=Cal_fit(parent2.x,upper,lower)Gchild[2*i]=copy.deepcopy(parent1)Gchild[2*i+1]=copy.deepcopy(parent2)defChoose_next(G,Gchild,Gsum,Pop):#选择下一代函数foriinrange(Pop):Gsum[i]=copy.deepcopy(G[i])Gsum[2*i+1]=copy.deepcopy(Gchild[i])Gsum=sorted(Gsum,key=lambdax:x.fitness,reverse=True)foriinrange(Pop):G[i]=copy.deepcopy(Gsum[i])G[i].place=idefDecode(x):#解码函数Temp1=0foriinrange(18):Temp1+=x[i]*math.pow(2,i)Temp2=0foriinrange(18,33,1):Temp2+=math.pow(2,i-18)*x[i]x1=lower[0]+Temp1*(upper[0]-lower[0])/(math.pow(2,18)-1)x2=lower[1]+Temp2*(upper[1]-lower[1])/(math.pow(2,15)-1)ifx1upper[0]:x1=random.uniform(lower[0],upper[0])ifx2upper[1]:x2=random.uniform(lower[1],upper[1])returnx1,x2defSelf_Learn(Best,upper,lower,sPm,sLearn):#自学习操作num=0Temp=copy.deepcopy(Best)whileTrue:num+=1forjinrange(33):ifrandom.random()sPm:Temp.x[j]=(Temp.x[j]+1)%2Temp.fitness=Cal_fit(Temp.x,upper,lower)ifTemp.fitnessBest.fitness:Best=copy.deepcopy(Temp)num=0ifnumsLearn:breakreturnBestif__name__=='__main__':upper=[12.1,5.8]lower=[-3,4.1]Pop=100Ppool=50G_max=300Pc=0.8Pm=0.1sPm=0.05sLearn=20G=np.array([Ind()for_inrange(Pop)])Gparent=np.array([Ind()for_inrange(Ppool)])Gchild=np.array([Ind()for_inrange(Pop)])Gsum=np.array([Ind()for_inrange(Pop*2)])Init(G,upper,lower,Pop)#初始化Best=Find_Best(G,Pop)forkinrange(G_max):Selection(G,Gparent,Pop,Ppool)#使用轮盘赌方法选择其中50%为父代Cross_and_Mutation(Gparent,Gchild,Pc,Pm,upper,lower,Pop,Ppool)#交叉和变异生成子代Choose_next(G,Gchild,Gsum,Pop)#选择出父代和子代中较优秀的个体Cbest=Find_Best(G,Pop)ifBest.fitnessCbest.fitness:Best=copy.deepcopy(Cbest)#跟新最优解else:G[Cbest.place]=copy.deepcopy(Best)Best=Self_Learn(Best,upper,lower,sPm,sLearn)print(Best.fitness)x1,x2=Decode(Best.x)print(Best.x)print([x1,x2])
本文标题:自然计算大作业
链接地址:https://www.777doc.com/doc-4881886 .html