您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > 遗传算法及蚂蚁算法作业
实用文档文案大全题目1:z=2-exp[-(x2+y2)],x,y[-5,+5],求函数最小值(1)用遗传算法来做:第一步:确定决策变量及其约束条件s.t.-5=x=5第二步:建立优化模型y.^2)))+exp(-(x.^2--(2)(maxxf第三步:确定编码方法,用长度为50位的二进制编码串来表示决策变量x第四步:确定解码方法yx12)5(550第五步:确定个体评价方法个体的适应度取为每次迭代的最小值的绝对值加上目标函数值,即)(|)}(min{|)(xfxfxF第六步:确定参数本题种群规模n=30,迭代次数ger=200,交叉概率pc=0.65,变异概率pm=0.05代码:clearall;closeall;clc;tic;实用文档文案大全n=30;ger=200;pc=0.65;pm=0.05;%生成初始种群v=init_population(n,50);[N,L]=size(v);disp(sprintf('Numberofgenerations:%d',ger));disp(sprintf('Populationsize:%d',N));disp(sprintf('Crossoverprobability:%.3f',pc));disp(sprintf('Mutationprobability:%.3f',pm));%待优化问题xmin=-5;xmax=5;ymin=-5;ymax=5;f='-(2-exp(-(x.^2+y.^2)))';[x,y]=meshgrid(xmin:0.1:xmax,ymin:0.1:ymax);vxp=x;实用文档文案大全vyp=y;vzp=eval(f);figure(1);mesh(vxp,vyp,-vzp);holdon;gridon;%计算适应度,并画出初始种群图形x=decode(v(:,1:25),xmin,xmax);y=decode(v(:,26:50),ymin,ymax);fit=eval(f);plot3(x,y,-fit,'k*');title('(a)染色体的初始位置');xlabel('x');ylabel('y');zlabel('f(x,y)');%迭代前的初始化vmfit=[];vx=[];实用文档文案大全it=1;%迭代计数器%开始进化whileit=ger%Reproduction(Bi-classistSelection)vtemp=roulette(v,fit);%Crossoverv=crossover(vtemp,pc);%MutationM=rand(N,L)=pm;%M(1,:)=zeros(1,L);v=v-2.*(v.*M)+M;%Resultsx=decode(v(:,1:25),xmin,xmax);y=decode(v(:,26:50),ymin,ymax);fit=eval(f);[sol,indb]=max(fit);%每次迭代中最优目标函数值v(1,:)=v(indb,:);实用文档文案大全fit_mean=mean(fit);%每次迭代中目标函数值的平均值vx=[vxsol];vmfit=[vmfitfit_mean];it=it+1;end%%%%最后结果disp(sprintf('\n'));%空一行%显示最优解及最优值disp(sprintf('Maximumfound[x,f(x)]:[%.4f,%.4f,%.4f]',x(indb),y(indb),-sol));%图形显示最优结果figure(2);mesh(vxp,vyp,-vzp);holdon;gridon;plot3(x,y,-fit,'r*');title('染色体的最终位置');xlabel('x');ylabel('y');实用文档文案大全zlabel('f(x,y)');%图形显示最优及平均函数值变化趋势figure(3);plot(-vx);%title('最优,平均函数值变化趋势');xlabel('Generations');ylabel('f(x)');holdon;plot(-vmfit,'r');holdoff;runtime=toc运行结果:Maximumfound[x,f(x)]:[0.0033,0.0017,1.0000](2)用蚁群算法来做代码:%Antmainprogramclearall;closeall;clc;实用文档文案大全tic;Ant=100;Ger=50;xmin=-5;xmax=5;ymin=-5;ymax=5;tcl=0.05;f='-(2-exp(-(x.^2+y.^2)))';%待优化的目标函数[x,y]=meshgrid(xmin:tcl:xmax,ymin:tcl:ymax);vxp=x;vyp=y;vzp=eval(f);figure(1);mesh(vxp,vyp,-vzp);holdon;%初始化蚂蚁位置实用文档文案大全fori=1:AntX(i,1)=(xmin+(xmax-xmin)*rand(1));X(i,2)=(ymin+(ymax-ymin)*rand(1));%T0----信息素,函数值越大,信息素浓度越大T0(i)=exp(-(X(i,1).^2+X(i,2).^2))-2;endplot3(X(:,1),X(:,2),-T0,'k*');holdon;gridon;title('蚂蚁的初始分布位置');xlabel('x');ylabel('y');zlabel('f(x,y)');%开始寻优fori_ger=1:GerP0=0.2;%P0----全局转移选择因子P=0.8;%P----信息素蒸发系数lamda=1/i_ger;%转移步长参数[T_Best(i_ger),BestIndex]=max(T0);实用文档文案大全forj_g=1:Ant%求取全局转移概率r=T0(BestIndex)-T0(j_g);Prob(i_ger,j_g)=r/T0(BestIndex);endforj_g_tr=1:AntifProb(i_ger,j_g_tr)P0temp1=X(j_g_tr,1)+(2*rand(1)-1)*lamda;temp2=X(j_g_tr,2)+(2*rand(1)-1)*lamda;elsetemp1=X(j_g_tr,1)+(xmax-xmin)*(rand(1)-0.5);temp2=X(j_g_tr,2)+(ymax-ymin)*(rand(1)-0.5);endiftemp1xmintemp1=xmin;endiftemp1xmaxtemp1=xmax;endiftemp2ymintemp2=ymin;实用文档文案大全endiftemp2ymaxtemp2=ymax;endif-(2-exp(-(temp1.^2+temp2.^2)))-(2-exp(-(X(j_g_tr,1).^2+X(j_g_tr,2).^2)))X(j_g_tr,1)=temp1;X(j_g_tr,2)=temp2;endend%信息素更新fort_t=1:AntT0(t_t)=(1-P)*T0(t_t)-(2-exp(-(X(t_t,1).^2+X(t_t,2).^2)));end[c_iter,i_iter]=max(T0);maxpoint_iter=[X(i_iter,1),X(i_iter,2)];实用文档文案大全max_local(i_ger)=-(2-exp(-(X(i_iter,1).^2+X(i_iter,2).^2)));%将每代全局最优解存到max_global矩阵中ifi_ger=2ifmax_local(i_ger)max_global(i_ger-1)max_global(i_ger)=max_local(i_ger);elsemax_global(i_ger)=max_global(i_ger-1);endelsemax_global(i_ger)=max_local(i_ger);endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(2);mesh(vxp,vyp,-vzp);holdon;x=X(:,1);y=X(:,2);plot3(x,y,-eval(f),'b*');实用文档文案大全holdon;gridon;title('蚂蚁的最终分布位置');xlabel('x');ylabel('y');zlabel('f(x,y)');figure(3);plot(1:Ger,-max_global,'b-')holdon;title('最优函数值变化趋势');xlabel('iteration');ylabel('f(x)');gridon;[c_max,i_max]=max(T0);maxpoint=[X(i_max,1),X(i_max,2)]maxvalue=-(2-exp(-(X(i_max,1).^2+X(i_max,2).^2)))runtime=toc结果:maxvalue=1.0000实用文档文案大全实用文档文案大全题目2:利用蚁群算法求下面加权有向图中从A到G的最短路:解:第一步:初始化N只蚂蚁,也就是N条道路第二步:初始化运行参数,开始迭代第三步:在迭代步数范围之内,计算转移概率,如果小于全局转移概实用文档文案大全率就进行小范围的搜索,否则就进行大范围的搜索第四步:更新信息素,记录状态,准备进行下一次迭代第五步:转第三步第六步:输出结果代码:functionshortroad_ant_main%Antmainprogramclearall;closeall;clc;%清屏tic;%计时开始Ant=50;Ger=100;%运行参数初始化power=[053100100100100100100100100100100100100100;1000100136100100100100100100100100100100;1001000100876100100100100100100100100100;100100100010010010068100100100100100100100;100100100100010010035100100100100100100100;100100100100100010010033100100100100100100;100100100100100100010084100100100100100100;100100100100100100100010010022100100100100;100100100100100100100100010010012100100100;100100100100100100100100100010033100100100;100100100100100100100100100100010010035100;实用文档文案大全100100100100100100100100100100100010052100;100100100100100100100100100100100100066100;10010010010010010010010010010010010010001004;10010010010010010010010010010010010010010003;1001001001001001001001001001001001001001001000];[PMPN]=size(power);%初始化蚂蚁位置v=init_population(Ant,PN);v(:,1)=1;v(:,PN)=1;%始点和终点纳入路径%把距离当信息素浓度fit=short_road_fun(v,power);%距离越小越好,所以要和信息素浓度相对应。T0=max(fit)-fit;%画出图形figure(1);gridon;holdon;plot(fit,'k*');tit
本文标题:遗传算法及蚂蚁算法作业
链接地址:https://www.777doc.com/doc-5342680 .html