ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【TSP问题】基于改进遗传算法求解旅行商问题matlab源码

2022-01-16 22:02:00  阅读:173  来源: 互联网

标签:ps end nn asz 源码 matlab path 遗传算法 cities


1 算法介绍

模型介绍见这里

2 部分代码

nn=40; % number of cities
asz=10; % area size   asx x asz
​
​
ps=3000; % population size
ng=5000; % number of generation
​
pm=0.01; % probability of mutation of exchange 2 random cities in the path (per gene, per genration)
pm2=0.02; % probability of mutation of exchange 2 peices of path (per gene, per genration)
pmf=0.08; % probability of mutation  of flip random pece of path
​
r=asz*rand(2,nn); % randomly distribute cities
% r(1,:) -x coordinaties of cities
% r(2,:) -y coordinaties of cities
​
% % uncomment to make circle:
% % circle
% al1=linspace(0,2*pi,nn+1);
% al=al1(1:end-1);
% r(1,:)=0.5*asz+0.45*asz*cos(al);
% r(2,:)=0.5*asz+0.45*asz*sin(al);
​
dsm=zeros(nn,nn); % matrix of distancies
for n1=1:nn-1
    r1=r(:,n1);
    for n2=n1+1:nn
        r2=r(:,n2);
        dr=r1-r2;
        dr2=dr'*dr;
        drl=sqrt(dr2);
        dsm(n1,n2)=drl;
        dsm(n2,n1)=drl;
    end
end
​
% start from random closed pathes:
G=zeros(ps,nn); % genes, G(i,:) - gene of i-path, G(i,:) is row-vector with cities number in the path
for psc=1:ps
    G(psc,:)=randperm(nn);
end
​
figure('units','normalized','position',[0.05 0.2 0.9 0.6]);
​
subplot(1,2,1);
​
% to plot best path:
hpb=plot(NaN,NaN,'r-');
ht=title(' ');
​
hold on;
​
% plot nodes numbers
for n=1:nn
    text(r(1,n),r(2,n),num2str(n),'color',[0.7 0.7 0.7]);
end
​
plot(r(1,:),r(2,:),'k.'); % plot cities as black dots
​
​
​
axis equal;
xlim([-0.1*asz 1.1*asz]);
ylim([-0.1*asz 1.1*asz]);
​
subplot(1,2,2);
hi=imagesc(G);
title('color is city number');
colorbar;
xlabel('index in sequence of cities');
ylabel('path number');
​
pthd=zeros(ps,1); %path lengths
p=zeros(ps,1); % probabilities
for gc=1:ng % generations loop
    % find paths length:
    for psc=1:ps
        Gt=G(psc,:);
        pt=0; % path length summation
        for nc=1:nn-1
            pt=pt+dsm(Gt(nc),Gt(nc+1));
        end
        % last and first:
        pt=pt+dsm(Gt(nn),Gt(1));
        pthd(psc)=pt;
    end
    ipthd=1./pthd; % inverse path lengths, we want to maximize inverse path length
    p=ipthd/sum(ipthd); % probabilities
    
    [mbp bp]=max(p); 
    Gb=G(bp,:); % best path 
    
    % update best path on figure:
    if mod(gc,5)==0
        set(hpb,'Xdata',[r(1,Gb) r(1,Gb(1))],'YData',[r(2,Gb) r(2,Gb(1))]);
        set(ht,'string',['generation: ' num2str(gc)  '  best path length: ' num2str(pthd(bp))]);
        set(hi,'CData',G);
        drawnow;
    end
    
    
    % crossover:
    ii=roulette_wheel_indexes(ps,p); % genes with cities numers in ii will be put to crossover
    % length(ii)=ps, then more probability p(i) of i-gene then more
    % frequently it repeated in ii list
    Gc=G(ii,:); % genes to crossover
    Gch=zeros(ps,nn); % childrens
    for prc=1:(ps/2) % pairs counting
        i1=1+2*(prc-1);
        i2=2+2*(prc-1);
        g1=Gc(i1,:); %one gene
        g2=Gc(i2,:); %another gene
        cp=ceil((nn-1)*rand); % crossover point, random number form range [1; nn-1]

        
      
        % two childrens:
        g1ch=insert_begining(g1,g2,cp);
        g2ch=insert_begining(g2,g1,cp);
        Gch(i1,:)=g1ch;
        Gch(i2,:)=g2ch;
    end
    G=Gch; % now children
    
    
    % mutation of exchange 2 random cities:
    for psc=1:ps
        if rand<pm
            rnp=ceil(nn*rand); % random number of sicies to permuation
            rpnn=randperm(nn);
            ctp=rpnn(1:rnp); %chose rnp random cities to permutation
            Gt=G(psc,ctp); % get this cites from the list
            Gt=Gt(randperm(rnp)); % permutate cities
            G(psc,ctp)=Gt; % % return citeis back
         end
    end
    
    % mutation of exchange 2 peices of path:
    for psc=1:ps
        if rand<pm2
            cp=1+ceil((nn-3)*rand); % range [2 nn-2]
            G(psc,:)=[G(psc,cp+1:nn) G(psc,1:cp)];
        end
    end
    
    % mutation  of flip randm pece of path:
    for psc=1:ps
        if rand<pmf
            n1=ceil(nn*rand);
            n2=ceil(nn*rand);
            G(pscs,n1:n2)=fliplr(G(psc,n1:n2));
        end
    end
    
    
​
    
    G(1,:)=Gb; % elitism
    
    
        
end
​

3 仿真结果

4 参考文献

[1]谢胜利, 唐敏, 董金祥. 求解TSP问题的一种改进的遗传算法[J]. 计算机工程与应用, 2002, 38(008):58-60.

[2]文艺, and 潘大志. "用于求解TSP问题的改进遗传算法." 计算机科学 43.0z1(2016):90-92.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

标签:ps,end,nn,asz,源码,matlab,path,遗传算法,cities
来源: https://blog.csdn.net/qq_59747472/article/details/120470396

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有