算法代码片段(一)

来源:互联网 发布:dnf本地安全策略优化 编辑:程序博客网 时间:2024/06/05 01:58
% 初始化参数data=load('last.txt');[n,m]=size(data);% m表示景区个数% Play_Time=data(1,:);%每个景区的逗留时间Transportation=data(2:n,:);%景区与景区之间的行程时间ant = 8;                              % 蚂蚁数量alpha =1;                          % 信息素重要程度因子beta = 5;                          % 引导函数重要程度因子rho = 0.15;                          % 信息素衰减的参数                             Q = 1;                               % 常系数 Tau=ones(m,m); % 信息素矩阵iter_max = 50;                      % 最大迭代次数 iter = 1;                            % 迭代次数初值Eta=1./Transportation;                    % 引导函数Table = zeros(ant,m);                    % 选择过程中路径记录表Route_best = zeros(iter_max,m);       % 各代最佳路径       Length_best = zeros(iter_max,1);     % 各代最佳目标函数值 %%迭代开始while iter <= iter_max     citys_index=1:m;     Table(:,1)=27;% 第一个景点为“西安”        for b=1:ant %逐个蚂蚁构造解                 for i=2:m %逐个旅游城市选择                              tabu=Table(b,1:(i-1));%已访问的景点集合(禁忌表)                    allow_index=~ismember(citys_index,tabu);                    allow=citys_index(allow_index); %待访问的景点                    % 计算转移概率                    P=allow;                    for h = 1:length(allow)                        P(h) = Tau(tabu(end),allow(h))^alpha * Eta(tabu(end),allow(h))^beta;                       end                     % 轮盘赌法选择下一个访问城市                          P = P/sum(P);                         Pc = cumsum(P);                         target_index=find(Pc >= rand);                         if numel(target_index)==0                             for hh=1:length(allow)                                 if Transportation(allow(hh),Table(b,i-1))==0                                    Transportation(allow(hh),Table(b,i-1))=100;                                 end                               end                             [cc,lie]=min(Transportation(allow(1:end),Table(b,i-1)));                             target=allow(lie);                         else                           target=allow(target_index(1));                           end                          Table(b,i)=target;                                                if Play_Time(Table(b,i))==16                         k=k+2;                         else                         if Play_Time(Table(b,i))==8                             k=k+1;                         else                             if Transportation(Table(b,i-1),Table(b,i))+Play_Time(Table(b,i))>=12                                 k=k+1;                             end                              end                     end                     if mod(k,15)==0                         k=1;                     end            end        end%% 计算各个蚂蚁的解的目标函数值       Length = zeros(ant,1);      for i = 1:ant          Route = Table(i,:);          for j = 1:(m - 1)              Length(i) = Length(i) + Transportation(Route(j),Route(j + 1));          end          Length(i) = Length(i) + Transportation(Route(m),Route(1));      end  % 计算最短路径距离及平均距离      if iter == 1          [min_Length,min_index] = min(Length);          Length_best(iter) = min_Length;            Route_best(iter,:) = Table(min_index,:);      else          [min_Length,min_index] = min(Length);          Length_best(iter) = min(Length_best(iter - 1),min_Length);          if Length_best(iter) == min_Length              Route_best(iter,:) = Table(min_index,:);          else              Route_best(iter,:) = Route_best((iter-1),:);          end      end %更新信息素  更新最优解 % 更新信息素      Delta_Tau = zeros(m,m);      % 逐个蚂蚁计算      for i = 1:ant          % 逐个城市计算          for j = 1:(m - 1)              Delta_Tau(Table(i,j),Table(i,j+1)) = Delta_Tau(Table(i,j),Table(i,j+1)) + Q/Length(i);          end          Delta_Tau(Table(i,m),Table(i,1)) = Delta_Tau(Table(i,m),Table(i,1)) + Q/Length(i);      end      Tau = (1-rho) * Tau + Delta_Tau;    % 迭代次数加1,清空路径记录表iter = iter + 1;Table = zeros(ant,m);end%% 结果显示[Shortest_Length,index] = min(Length_best);Shortest_Route = Route_best(index,:);disp(['最短距离:' num2str(Shortest_Length)]);disp(['最短路径:' num2str([Shortest_Route Shortest_Route(1)])]);

0 0