基于Matlab遗传算法的非线性方程组优化程序
clear,clc;%清理内存,清屏 circleN=200;%迭代次数 format long
%构造可能解的空间,确定染色体的个数、长度 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; for i=1:chromosomeSum%防止解的顺序号超出解的个数 if solutionSequence(i)>solutionN; solutionSequence(i)=solutionN; end end
%把解的十进制序号转成二进制序号
fatherChromosomeGroup=dec2bin(solutionSequence,chromosomeLength); holdLeastFunctionError=Inf;%可能解的最小误差的初值 holdBestChromosome=0;%对应最小误差的染色体的初值 %计算 circle=0;
while circle %1:由可能解的序号寻找解本身(关键步骤) x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum); %2:把解代入非线性方程计算误差 functionError=nonLinearSumError1(x);%把解代入方程计算误差 [solution,minError,isTrue]=isSolution(x,functionError,solutionSumError); if isTrue==1 '方程得解' solution minError circle return%结束程序 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); if order==0 order=1; end fatherChromosomeGroup(order,:)=holdBestChromosome; functionError(order)=holdLeastFunctionError; %6:为每一条染色体(即可能解的序号)定义一个概率(关键步骤) [p,trueP]=chromosomeProbability(functionError); if trueP =='Fail' '可能解严重不适应方程,请重新开始' return%结束程序 end %7:按照概率筛选染色体(关键步骤) ú=bin2dec(fatherChromosomeGroup)%显示父染色体 fatherChromosomeGroup=varianceCh(sonChromosomeGroup,0.1,solutionN); fatherChromosomeGroup=checkSequence(fatherChromosomeGroup,solutionN);%检查变异后的染色体是否越界 end %这个函数找出染色体(可能解的序号)对应的可能解x function x=chromosome_x(chromosomeGroup,oneDimensionSet,solutionSum) [row oneDimensionSetN]=size(oneDimensionSet); chromosomeSum=size(chromosomeGroup);%chromosomeSum:染色体的个数 xSequence=bin2dec(chromosomeGroup);%把可能解的二进制序号(染色体)转换成十进制序号 for i=1:chromosomeSum%i:染色体的编号 remainder=xSequence(i); for j=1:solutionSum dProduct=oneDimensionSetN^(solutionSum-j);%sNproduct: quotient=remainder/dProduct; remainder=mod(remainder,dProduct);%mod:取余函数 if remainder==0 oneDimensionSetOrder=quotient; else oneDimensionSetOrder=fix(quotient)+1;%fix:取整函数 end if oneDimensionSetOrder==0 oneDimensionSetOrder=oneDimensionSetN; end x(i,j)=oneDimensionSet(oneDimensionSetOrder); end end function funtionError=nonLinearSumError1(X)%方程的解是-7,5,1,-3 funtionError=... [ 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,:); if minError [bestChromosome,leastFunctionError]=best_worstChromosome(chromosomeGroup,functionError) [leastFunctionError minErrorOrder]=min(functionError); %[maxFunctionError maxErrorOrder]=max(functionError); bestChromosome=chromosomeGroup(minErrorOrder,:); %worstChromosome=chromosomeGroup(maxErrorOrder,:); function [newBestChromosome,newLeastFunctionError]... =compareBestChromosome(oldBestChromosome,oldLeastFunctionError,... bestChromosome,leastFunctionError) if oldLeastFunctionError>leastFunctionError newLeastFunctionError=leastFunctionError;