| 
 | 
 
9蚁群算法优化求解TSP问题--视频分享 
百度网盘链接:http://pan.baidu.com/s/1cvLIZ8 密码:gtty 
具体链接在halcom.cn论坛,联系人QQ:3283892722 
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。 
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1 
 
具体的代码如下: 
- clc,clear,close all
 
 - warning off
 
 - % 城市节点的坐标
 
 - x=[41,37,54,25,7,2,68,71,54,83,64,18,22,83,91,...
 
 -     25,24,58,71,74,87,18,13,82,62,58,45,41,44,4];
 
 - y=[94,84,67,62,64,99,58,44,62,69,60,54,60,46,38,...
 
 -     38,42,69,71,78,76,40,40,7,32,35,21,26,35,50];
 
 - % plot(x,y,'ro')
 
 - citynum = 30;  % citynum表示城市数目
 
 - q = 10^6;      % Q常量
 
 - maxgen = 1e2;  % 迭代次数
 
 - r=0.9;         % r表示轨迹持久性
 
 - a=1.5;         % a表示轨迹相对重要性
 
 - b=2;           % b表示能见度相对重要性
 
 - sizepop = 30;  % m表示蚂蚁数目
 
 - % 城市各节点之间的距离
 
 - for i=1:citynum
 
 -     for j=1:citynum
 
 -         dij(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2); % 距离
 
 -     end
 
 - end
 
 - for i=1:citynum
 
 -     dij(i,i)= 99999999999999999;
 
 - end
 
  
- % 初始化信息素强度表
 
 - tao = ones(citynum)*100;
 
 - zbest = randperm(citynum);  % 最优路径初始化
 
 - fitnesszbest = inf;         % 最短路径初始化
 
 - % 进入主循环
 
 - for i=1:maxgen
 
 -     
 
 -     tabu=ones(sizepop,citynum);%禁忌表
 
 -     tabu(:,1)=0;
 
 -     % 路径的初始化
 
 -     for jj=1:sizepop
 
 - %         path(jj,:) = randperm(citynum );
 
 -         path(jj,:) = ones(1,citynum );
 
 -         
 
 -         for step=1:citynum-1
 
 -             ta=tao.^a;     % a表示轨迹相对重要性
 
 -             tb=dij.^(-b);  % b表示能见度相对重要性
 
 -             td=ta.*tb;
 
 -             pd=tabu(jj,:).*td(path(jj,step),:);
 
 -             pk=pd/sum(pd); % 概率
 
 -             rk=rand;
 
 -             spk=0;
 
 -             j=1;
 
 -             while j<=citynum
 
 -                 if rk<spk+pk(j)
 
 -                     break;
 
 -                 else 
 
 -                     spk=spk+pk(j);
 
 -                     j=j+1;
 
 -                 end
 
 -             end
 
 -             tabu(jj,j)=0;
 
 -             path(jj,step+1)=j;
 
 -         end
 
 -         
 
 -         fitness(jj) = ca_tsp(citynum,path(jj,:),dij);
 
 -     end
 
 -     
 
 -     [maxfitness, index1] = max(fitness); % 最差的路径
 
 -     [minfitness, index2] = min(fitness); % 最优的路径
 
 -     
 
 -     points = [31,1];
 
 -     while(max(points)>citynum)
 
 -         points = ceil(sizepop*rand(1,2));    % 随机选取两个节点
 
 -     end
 
 -     
 
 -     path(index1,:) = path(index2,:);     % 最差的路径 = 最优的路径
 
 - %     path(index1,[points(1),points(2)]) = path(index1,[points(2),points(1)]);
 
 - %     path(index1,min(points):1:max(points)) = path(index1,max(points):-1:min(points));
 
 - %     path(index1,min(points):1:max(points)) = path(index1,[max(points)-1:-1:min(points), max(points)]);
 
 -     path(index1,min(points):1:max(points)) = path(index1,[min(points)+1:1:max(points), min(points)]);
 
 -     fitness(index1) = ca_tsp(citynum,path(index1,:),dij);
 
 -     
 
 -     % dtao初始化
 
 -     dtao = zeros(citynum);
 
 -     for j = 1:sizepop
 
 -         for k = 1:citynum-1
 
 -             dtao( path(j,k),path(j,k+1) ) = dtao( path(j,k),path(j,k+1) ) + q/fitness(j);
 
 -             dtao( path(j,k+1),path(j,k) ) = dtao( path(j,k), path(j,k+1) );
 
 -         end
 
 -         dtao( path(j,citynum),path(j,1) ) = dtao( path(j,1),path(j,citynum) );
 
 -     end
 
 -     
 
 -     [minfitness,index] = min(fitness);
 
 -     if minfitness<fitnesszbest
 
 -         fitnesszbest = minfitness;   % 最优路径长度
 
 -         zbest = path(index,:);       % 最优路径
 
 -     end
 
 -  
 
 -     tao = r*tao + dtao;  % r表示轨迹持久性
 
 - end
 
 - %% 利用tao来求解一条路径
 
 - ta = tao.^a;      % a表示轨迹相对重要性
 
 - tb = dij.^(-b);   % b表示能见度相对重要性
 
 - td = ta.*tb;
 
 - for i=1:citynum
 
 -     [maxtd,index] = max(td(i,:));  % 每一行的最大值对应的旗标
 
 -     tao_path(i) = index;  % 记录的最优路径
 
 -     td(:, index) = 0;     % 已经选择的标号置0,下一次迭代不会被选中
 
 - end
 
 - new_fitness = ca_tsp(citynum,tao_path,dij);
 
 - if new_fitness<fitnesszbest
 
 -     fitnesszbest = new_fitness;
 
 -     zbest = tao_path;
 
 - end
 
 - %% 
 
 - disp('最优路径')
 
 - zbest
 
  
- figure(1)
 
 - plot(x,y,'ro');
 
 - hold on
 
 - for i=1:citynum
 
 -     if i==citynum
 
 -         plot([x(zbest(i)),x(zbest(1))],[y(zbest(i)),y(zbest(1))],'b-')
 
 -     else
 
 -         plot([x(zbest(i)),x(zbest(i+1))],[y(zbest(i)),y(zbest(i+1))],'b-')
 
 -     end
 
 -     text(x(zbest(i)),y(zbest(i))+1,num2str(zbest(i)))
 
 - end
 
  复制代码 
案例分享: 
【1】改进的蚁群算法的机器人路径寻优:http://pan.baidu.com/s/1c2GUJPA 
【2】蚁群算法函数寻优 
 
 
 |   
 
 
 
 |