程序如下.docx
- 文档编号:5843221
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:16
- 大小:27.72KB
程序如下.docx
《程序如下.docx》由会员分享,可在线阅读,更多相关《程序如下.docx(16页珍藏版)》请在冰豆网上搜索。
程序如下
程序如下
%SimpleGeneticAlgorithm
%Revisedaccordingtotheprogramwhichisthefirstappendixofthebook
%"GeneticAlgorithmstheoryandApplication"
%ProgrammedbyYangWuyi2005.6.15
%Tocalculatethemaximalvalueofthefunction
%Inthisprogramthefunctionis
%F(x1,x2)=100*(x1^2-x2)^2+(1-x1)^2
%s.t-XRang<=x1,x2<=XRang
clearall
closeall
%SomeparametersoftheSimpleGeneticAlgorithm
PopulationSize=80;%Thepopulationofthecolony
MaxGeneration=100;%Themaximalgeneration
ProCro=0.6;%Theprobabilityofcrossover
ProMut=0.001;%Theprobabilityofmutation
XRang=2.048;
Vmax=100;%
Vmin=0;%
%FunctionMode>=0findthemaximumofthefunction
%FunctionMode<0findtheminimumofthefunction
FunctionMode=1;%
ChromosomeLen1=10;%Thelenghtofthefirstchromosome
ChromosomeLen2=10;%Thelenghtofthesecondchromosome
ChromosomeLen=10;
ChromosomeLength=ChromosomeLen1+ChromosomeLen2;%Thetotallenghtofchromosome
X1=zeros(1,PopulationSize);
X2=zeros(1,PopulationSize);
%Generatethefirstgenerationofthepopulation
rand('state',sum(100*clock));%Resetsthegeneratortoadifferentstateeachtime
forj=1:
PopulationSize
fork=1:
ChromosomeLength
Temp=rand
(1);
ifTemp<0.5
Population(j).Chromosome(k)=0;
else
Population(j).Chromosome(k)=1;
end%Endofif
end%Endoffor-
X1(j)=DecodeChromosome(Population(j).Chromosome,1,ChromosomeLen);
X2(j)=DecodeChromosome(Population(j).Chromosome,ChromosomeLen+1,ChromosomeLen);
Population(j).Value=0;
Population(j).Fitness=0;
end
X1=2*XRang*X1/1023-XRang;
X2=2*XRang*X2/1023-XRang;
figure
plot(X1,X2,'*'),grid;
BestValue=zeros(1,MaxGeneration);
AverageValue=zeros(1,MaxGeneration);
forGen=1:
MaxGeneration
%Calculatethevalueaccordingtothefunctionwhichwewanttofinditsmaximum
%Inthisprogramthefunctionis
%F(x1,x2)=100*(x1^2-x2)^2+(1-x1)^2
%s.t-XRang<=x1,x2<=XRang
forj=1:
PopulationSize
TempValue=CalculateObjectValue(Population(j).Chromosome,ChromosomeLen);
Population(j).Value=TempValue;
end%Endoffor-j
%Calculatefitnessvalue
forj=1:
PopulationSize
TempValue=CalculateFitnessValue(Population(j).Value,Vmax,Vmin,FunctionMode);
Population(j).Fitness=TempValue;
end%Endoffor-j
%Findoutthebestandworstindividualofthisgeneration
BestValue(Gen)=Population
(1).Value;
forj=1:
PopulationSize
ifPopulation(j).Value>BestValue(Gen)
BestValue(Gen)=Population(j).Value;
end
end
Sum=0;
forj=1:
PopulationSize
Sum=Sum+Population(j).Value;
end
AverageValue(Gen)=Sum/PopulationSize;
%Reproduceachromosomebyproportionalselection
Sum=0;
CFitness=zeros(1,PopulationSize);%Thecumulativefitnessvalue
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Calculatecumulativefitness
forj=1:
PopulationSize
Sum=Sum+Population(j).Fitness;
end%Endoffor-j
forj=1:
PopulationSize
CFitness(j)=Population(j).Fitness/Sum;
end%Endoffor-j
forj=2:
PopulationSize
CFitness(j)=CFitness(j-1)+CFitness(j);
end%Endoffor-j
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Selectoperation
forj=1:
PopulationSize
RandP=rand
(1);
Index=1;
whileRandP>CFitness(Index)
Index=Index+1;
end%Endofwhile
NewPopulation(j)=Population(Index);
end%Endoffor-j
forj=1:
PopulationSize
Population(j)=NewPopulation(j);
end%Endoffor-j
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Crossovertwochromosome
IndexPair=zeros(1,PopulationSize);
forj=1:
PopulationSize
IndexPair(j)=j;
end%Endoffor-j
forj=1:
PopulationSize
Point=floor((PopulationSize-j-1)*rand
(1));
Temp=IndexPair(j);
IndexPair(j)=IndexPair(j+Point);
IndexPair(j+Point)=Temp;
end%Endoffor-j
forj=1:
2:
(PopulationSize-1)
RandP=rand
(1);
ifRandP Point=floor(rand (1)*ChromosomeLen); ifPoint==0 Point=1; end fork=Point: (ChromosomeLen+Point-1) Temp=Population(IndexPair(j)).Chromosome(k); Population(IndexPair(j)).Chromosome(k)=Population(IndexPair(j+1)).Chromosome(k); Population(IndexPair(j+1)).Chromosome(k)=Temp; end%Endoffor-k end%Endofif end%Endoffor-j %Crossovertwochromosome %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Mutationofachromosome forj=1: PopulationSize fork=1: 2*ChromosomeLen RandP=rand (1); ifRandP ifPopulation(j).Chromosome(k)==1 Population(j).Chromosome(k)=0; else Population(j).Chromosome(k)=1; end%Endofif end%Endofif end%Endoffor-k end%Endoffor-j end%Endoffor-Gen forj=1: PopulationSize X1(j)=DecodeChromosome(Population(j).Chromosome,1,ChromosomeLen); X2(j)=DecodeChromosome(Population(j).Chromosome,ChromosomeLen+1,ChromosomeLen); end Xmin=-2.5; Xmax=2.5; Ymin=-2.5; Ymax=2.5; X1=2*XRang*X1/1023-XRang; X2=2*XRang*X2/1023-XRang; figure plot(X1,X2,'*'),grid,axis([XminXmaxYminYmax]); figure [X,Y]=meshgrid(-XRang: 0.1: XRang); Z=100*(X.^2-Y).^2+(1-X).^2; mesh(X,Y,Z); AimValue=ones(1,MaxGeneration)*(100*(XRang^2+XRang)^2+(1+XRang)^2); Xmin=0; Xmax=MaxGeneration; Ymin=0; Ymax=5000; figure holdon plot(BestValue,'r'); plot(AverageValue,'b'); plot(AimValue,'g'); holdoff grid axis([XminXmaxYminYmax]); 下面是四个子函数 function[Fitness]=CalculateFitnessValue(Value,Vmax,Vmin,FunctionMode) %Tocalculatefitnessvalue %FunctionMode: %>=0findthemaximumofthefunction %<0findtheminimumofthefunction %Fitnessisbigthan0 ifFunctionMode>=0 if(Value+Vmin)>0 Fitness=Value+Vmin; else Fitness=0; end else ifValue Fitness=Vmax-Value; else Fitness=0; end end function[Value]=CalculateObjectValue(Chromosome,Len) %Tocalculatevalueaccordingtothefunctionwhichwewanttofindits %maximumorminimum %Inthisprogramthefunctionis %F(x1,x2)=100*(x1^2-x2)^2+(1-x1)^2 %s.t-2.048<=x1,x2<=2.048 Value1=DecodeChromosome(Chromosome,1,Len); Value2=DecodeChromosome(Chromosome,Len+1,Len); Value1=4.096*Value1/1023-2.048; Value2=4.096*Value2/1023-2.048; Value=100*(Value1^2-Value2)^2+(1-Value1)^2; function[Value]=DecodeChromosome(ChromosomeCode,CodePos,Len) %Function: Todecodeabinarychromosomecodeintoadecimalinteger Value=0; k=Len-1; Ed=CodePos+Len-1; forj=CodePos: Ed Value=Value+ChromosomeCode(j)*(2^(k)); k=k-1; end 简单的说就是赌轮选择。 arg是获得下标。 loadbayg29.txt; Dis=bayg29; CITY=length(bayg29); %CITY=29; STEP=CITY;%时刻标记 ECHO=1000; ANT=5; T0=29*1601.^-1; Q=1;%fix1inACS Q0=0.9;%SelectDivide Alpha=0.1;%GlobalUpdate P=0.1;%LocalUpdate A=1;%fix1 B=2;%Nijpowe %%各条路径的初始权值 fori=1: CITY; forj=1: i-1; T(i,j)=T0; TN(i,j)=(T(i,j).^A)*(1/Dis(i,j).^; end; forj=i: CITY; T(i,j)=0; TN(i,j)=0; end; end; save('T.mat','T'); %fid=fopen('T.txt','w'); %fid2=fopen('TN.txt','w'); %fwrite(fid,T,'integer*4'); %fwrite(fid2,TN,'integer*4'); %fclose(fid); %fclose(fid2); forEcho=1: ECHO; %设置起点 t=1;%step1 fori=1: ANT; Mileage(i)=0; City(t,i)=ceil(CITY*rand (1));%[1-29]随机值 forj=2: STEP;%j此处表位置 %City(j,i)=mod(City(t,i)+j,CITY)+1;%wrong ifCity(t,i)+j-1>CITY%j此处表增量 City(j,i)=City(t,i)+j-1-CITY; else City(j,i)=City(t,i)+j-1; end; end; end; %pause; fort=2: STEP-1;%最后一步不需select过程 %选出下一城市City(citypos,i) fori=1: ANT; base=City(t-1,i); q=0; whileq==0 q=rand (1); end; ifq>Q0%ACselect %Prob SUM(i)=0; forj=t: STEP%回路 SUM(i)=SUM(i)+TN(base,City(j,i))+TN(City(j,i),base); end; forj=1: STEP; ifj Prob(i,City(j,i))=0; else Prob(i,City(j,i))=(TN(base,City(j,i))+TN(City(j,i),base))/SUM(i); end; end; %round_select run=0; mile=0; whilerun==0 run=rand (1); end; forj=t: STEP; mile=mile+Prob(i,City(j,i)); ifrun citypos=j; break; end; end; else%ACSSelect citypos=t; forj=t: STEP; if(TN(base,City(j,i))+TN(City(j,i),base))>(TN(base,City(citypos,i))+TN(City(citypos,i),base)) citypos=j; end; end; end;%endofselect %cityposisselected temp=City(t,i); City(t,i)=City(citypos,i); City(citypos,i)=temp; Mileage(i)=Mileage(i)+Dis(City(t-1,i),City(t,i))+Dis(City(t,i),City(t-1,i)); %LocalUpdate ifCity(t-1,i)>City(t,i) T(City(t-1,i),City(t,i))=T(City(t-1,i),City(t,i))*(1-P)+T0*P; else T(City(t,i),City(t-1,i))=T(City(t,i),City(t-1,i))*(1-P)+T0*P; end; end;%fori=1: ANT; ifEcho==1 save('T.mat','T','-APPEND'); end; end;%fort=2: STEP-1; %laststep.noneedtoselect. fori=1: ANT; %sumtheeage(last,first) Mileage(i)=Mileage(i)+Dis(City(STEP,i),City(1,i))+Dis(City(1,i),City(STEP,i)); %LocalUpdate ifCity(1,i)>City(STEP,i) T(City(1,i),City(STEP,i))=T(City(1,i),City(STEP,i))*(1-P)+T0*P; else T(City(STEP,i),City(1,i))=T(City(STEP,i),City(1,i))*(1-P)+T0*P; end; end; ifEcho==1 end; %GlobalUpdate.T,TN minant=1; fori=1: ANT; ifMileage(i) minant=i; end; end; %T圈中的每条边 forj=1: STEP-1; ifCity(j,minant)>City(j+1,minant) T(City(j,minant),City(j+1,minant))=T(City(j,minant),City(j+1,minant))+(Q/Mileage(minant))*Alpha/(1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序 如下