基于Matlab遗传算法的非线性方程组优化程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

基于Matlab遗传算法的非线性方程组优化程序clear,clc;%清理内存,清屏circleN=200;%迭代次数formatlong%构造可能解的空间,确定染色体的个数、长度solutionSum=4;leftBoundary=-10;rightBoundary=10;distance=1;chromosomeSum=500;solutionSumError=0.1;oneDimensionSet=leftBoundary:distance:rightBoundary;oneDimensionSetN=size(oneDimensionSet,2);%返回oneDimensionSet中的元素个数solutionN=oneDimensionSetN^solutionSum;%解空间(解集合)中可能解的总数binSolutionN=dec2bin(solutionN);%把可能解的总数转换成二进制数chromosomeLength=size(binSolutionN,2);%由解空间中可能解的总数(二进制数)计算染色体的长度%程序初始化%随机生成初始可能解的顺序号,+1是为了防止出现0顺序号solutionSequence=fix(rand(chromosomeSum,1)*solutionN)+1;fori=1:chromosomeSum%防止解的顺序号超出解的个数ifsolutionSequence(i)solutionN;solutionSequence(i)=solutionN;endend%把解的十进制序号转成二进制序号fatherChromosomeGroup=dec2bin(solutionSequence,chromosomeLength);holdLeastFunctionError=Inf;%可能解的最小误差的初值holdBestChromosome=0;%对应最小误差的染色体的初值%计算circle=0;whilecirclecircleN%开始迭代求解circle=circle+1;%记录迭代次数%1:由可能解的序号寻找解本身(关键步骤)x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);%2:把解代入非线性方程计算误差functionError=nonLinearSumError1(x);%把解代入方程计算误差[solution,minError,isTrue]=isSolution(x,functionError,solutionSumError);ifisTrue==1'方程得解'solutionminErrorcirclereturn%结束程序end%3:选择最好解对应的最优染色体[bestChromosome,leastFunctionError]=best_worstChromosome(fatherChromosomeGroup,functionError);%4:保留每次迭代产生的最好的染色体[holdBestChromosome,holdLeastFunctionError]...=compareBestChromosome(holdBestChromosome,holdLeastFunctionError,...bestChromosome,leastFunctionError);%circle%minError%solution%holdLeastFunctionError%5:把保留的最好的染色体holdBestChromosome加入到染色体群中order=round(rand(1)*chromosomeSum);iforder==0order=1;endfatherChromosomeGroup(order,:)=holdBestChromosome;functionError(order)=holdLeastFunctionError;%6:为每一条染色体(即可能解的序号)定义一个概率(关键步骤)[p,trueP]=chromosomeProbability(functionError);iftrueP=='Fail''可能解严重不适应方程,请重新开始'return%结束程序end%7:按照概率筛选染色体(关键步骤)%fa=bin2dec(fatherChromosomeGroup)%显示父染色体fatherChromosomeGroup=varianceCh(sonChromosomeGroup,0.1,solutionN);fatherChromosomeGroup=checkSequence(fatherChromosomeGroup,solutionN);%检查变异后的染色体是否越界end%这个函数找出染色体(可能解的序号)对应的可能解xfunctionx=chromosome_x(chromosomeGroup,oneDimensionSet,solutionSum)[rowoneDimensionSetN]=size(oneDimensionSet);chromosomeSum=size(chromosomeGroup);%chromosomeSum:染色体的个数xSequence=bin2dec(chromosomeGroup);%把可能解的二进制序号(染色体)转换成十进制序号fori=1:chromosomeSum%i:染色体的编号remainder=xSequence(i);forj=1:solutionSumdProduct=oneDimensionSetN^(solutionSum-j);%sNproduct:quotient=remainder/dProduct;remainder=mod(remainder,dProduct);%mod:取余函数ifremainder==0oneDimensionSetOrder=quotient;elseoneDimensionSetOrder=fix(quotient)+1;%fix:取整函数endifoneDimensionSetOrder==0oneDimensionSetOrder=oneDimensionSetN;endx(i,j)=oneDimensionSet(oneDimensionSetOrder);endendfunctionfuntionError=nonLinearSumError1(X)%方程的解是-7,5,1,-3funtionError=...[abs(X(:,1).^2-sin(X(:,2).^3)+X(:,3).^2-exp(X(:,4))-50.566253390821)+...abs(X(:,1).^3+X(:,2).^2-X(:,4).^2+327)+...abs(cos(X(:,1).^4)+X(:,2).^4-X(:,3).^3-624.679868769613)+...abs(X(:,1).^4-X(:,2).^3+2.^X(:,3)-X(:,4).^4-2197)];%判断方程是否解开function[solution,minError,isTrue]=isSolution(x,functionError,precision)[minError,xi]=min(functionError);%找到最小误差,最小误差所对应的行号solution=x(xi,:);ifminErrorprecisionisTrue=1;elseisTrue=0;endfunction[bestChromosome,leastFunctionError]=best_worstChromosome(chromosomeGroup,functionError)[leastFunctionErrorminErrorOrder]=min(functionError);%[maxFunctionErrormaxErrorOrder]=max(functionError);bestChromosome=chromosomeGroup(minErrorOrder,:);%worstChromosome=chromosomeGroup(maxErrorOrder,:);function[newBestChromosome,newLeastFunctionError]...=compareBestChromosome(oldBestChromosome,oldLeastFunctionError,...bestChromosome,leastFunctionError)ifoldLeastFunctionErrorleastFunctionErrornewLeastFunctionError=leastFunctionError;newBestChromosome=bestChromosome;elsenewLeastFunctionError=oldLeastFunctionError;newBestChromosome=oldBestChromosome;endfunction[p,isP]=chromosomeProbability(x_Error)InfN=sum(isinf(x_Error));%估计非线性方程计算的结果NaNN=sum(isnan(x_Error));ifInfN0||NaNN0isP='Fail';p=0;returnelseisP='True';errorReciprocal=1./x_Error;sumReciprocal=sum(errorReciprocal);p=errorReciprocal/sumReciprocal;%p:可能解所对应的染色体的概率endfunctionchromosome=selecteChromosome(chromosomeGroup,p)cumuP=cumsum(p);%累积概率,也就是把每个染色体的概率映射到0~1的区间[chromosomeSum,chromosomeLength]=size(chromosomeGroup);fori=1:chromosomeSum%这个循环产生概率值rN=rand(1);ifrN==1chromosome(i,:)=chromosomeGroup(chromosomeSum,:);elseif(0=rN)&&(rNcumuP(1))chromosome(i,:)=chromosomeGroup(1,:);%第1条染色体被选中elseforj=2:chromosomeSum%这个循环确定第1条以后的哪一条染色体被选中if(cumuP(j-1)=rN)&&(rNcumuP(j))chromosome(i,:)=chromosomeGroup(j,:);breakendendendendfunctionsonChromosome=crossChromosome(fatherChromosome,parameter)[chromosomeSum,chromosomeLength]=size(fatherChromosome);switchparametercase1%随机选择父染色体进行交叉重组fori=1:chromosomeSum/2crossDot=fix(rand(1)*chromosomeLength);%随机选择染色体的交叉点位randChromosomeSequence1=round(rand(1)*chromosomeSum);randChromosomeSequence2=round(rand(1)*chromosomeSum);ifrandChromosomeSequence1==0%防止产生0序号randChromosomeSequence1=1;endifrandChromosomeSequence2==0%防止产生0序号randChromosomeSequence

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功