|
10遗传算法优化求解TSP问题--视频分享
百度网盘链接:链接:http://pan.baidu.com/s/1geLdLVP 密码:8dra
具体链接在halcom.cn论坛,联系人QQ:3283892722
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
具体的代码如下:
- clc,clear,close all
- warning off
- tic
- %% 具体的问题参数
- % 城市节点的坐标
- 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表示城市数目
- % 城市各节点之间的距离
- 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
- %% 算法参数
- itermax = 1e4; % 迭代次数
- sizepop = 150; % 种群
- % 初始化种群
- for i=1:sizepop
- path(i,:) = randperm(citynum); % 实数编码
- fitness(i) = ca_tsp(citynum,path(i,:),dij); % 目标函数值
- end
- [bestfitness,index] = min(fitness); % 极小值
- zbest = path(index,:); % 最佳个体
- fitnesszbest = bestfitness; % 全局最优
- %% 寻优
- for i=1:itermax
- % 交叉操作
- for j=1:5:sizepop-4
- [minf,flag] = min( fitness(j:j+4) );
- pop(j,:) = path(flag,:); % j:j+4 最优的种群
-
- points = [31,1];
- while(max(points)>citynum)
- points = ceil(sizepop*rand(1,2)); % 随机选取两个节点
- end
-
- pop(j+1,:) = pop(j,:);
- pop(j+1,[points(1),points(2)]) = pop(j+1,[points(2),points(1)]) ; % 策略1
- pop(j+2,:) = pop(j,:);
- pop(j+2,[min(points):1:max(points)]) = pop(j+2,[max(points):-1:min(points)]) ; % 策略2
- pop(j+3,:) = pop(j,:);
- pop(j+3,[min(points):1:max(points)]) = pop(j+3,[max(points)-1:-1:min(points), max(points)]) ; % 策略3
- pop(j+4,:) = pop(j,:);
- pop(j+4,[min(points):1:max(points)]) = pop(j+4,[min(points)+1:1:max(points), min(points)]) ; % 策略4
- fitness(j+0) = ca_tsp(citynum,pop(j+0,:),dij); % 目标函数值
- fitness(j+1) = ca_tsp(citynum,pop(j+1,:),dij); % 目标函数值
- fitness(j+2) = ca_tsp(citynum,pop(j+2,:),dij); % 目标函数值
- fitness(j+3) = ca_tsp(citynum,pop(j+3,:),dij); % 目标函数值
- fitness(j+4) = ca_tsp(citynum,pop(j+4,:),dij); % 目标函数值
-
- [minf,flag] = min( fitness(j:j+4) );
- if minf<fitnesszbest
- fitnesszbest = minf;
- zbest = pop(j+flag-1,:);
- end
- end
-
- path = pop; % 更新最优路径
- end
- toc
- %% 显示结果
- 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
复制代码
|
|