1 简介
1.1 TSP介绍
“旅行商问题”(Traveling Salesman Problem,TSP)可简单描述为:一位销售商从n个城市中的某一城市出发,不重复地走完其余n-1个城市并回到原出发点,在所有可能路径中求出路径长度最短的一条。
旅行商的路线可以看作是对n城市所设计的一个环形,或者是对一列n个城市的排列。由于对n个城市所有可能的遍历数目可达(n-1)!个,因此解决这个问题需要O(n!)的计算时间。而由美国密执根大学的Holland教授发展起来的遗传算法,是一种求解问题的高效并行全局搜索方法,能够解决复杂的全局优化问题,解决TSP问题也成为遗传算法界的一个目标。
1.2 遗传算法求解tsp模型
巡回旅行商问题(TSP)是一个组合优化方面的问题,已经成为测试组合优化新算法的标准问题。应用遗传算法解决 TSP 问题,首先对访问城市序列进行排列组合的方法编码,这保证了每个城市经过且只经过一次。接着生成初始种群,并计算适应度函数,即计算遍历所有城市的距离。然后用最优保存法确定选择算子,以保证优秀个体直接复制到下一代。采用有序交叉和倒置变异法确定交叉算子和变异算子。
算法流程
旅行商问题的遗传算法实现
1.初始群体设定
一般都是随机生成一个规模为 N 的初始群体。在这里,我们定义一个s行t列的pop矩阵来表示群体,t 为城市个数 + 1,即 N + 1,s 为样本中个体数目。在本文探讨了 30 个城市的 TSP 问题,此时 t 取值 31,该矩阵中每一行的前 30 个元素表示经过的城市编号,最后一个元素表示适应度函数的取值,即每个个体所求的距离。
2.适应度函数的设计是根据个体适应值对其优劣判定的评价函数。在该问题中用距离的总和作为适应度函数,来衡量求解结果是否最优。
3.选择指以一定的概率从群体中选择优胜个体的操作,它是建立在群体中个体适应度评估基础上的。为了加快局部搜索的速度,在算法中采用最优保存策略的方法,即将群体中适应度最大的个体直接替换适应度最小的个体。它们不进行交叉和变异运算,而是直接复制到下一代,以免交叉和变异运算破坏种群中的优秀解答。
4.交叉算子是产生新个体的主要手段。它是指将个体进行两两配对,以交叉概率 Pc 将配对的父代个体的部分结构加以替换重组生成新个体的操作。本文中采用有序交叉法来实现。有序交叉法的步骤描述如下:
5.变异操作是以较小的概率 Pm 对群体中个体编码串上的某位或者某些位作变动,从而生成新的个体。本文中采用倒置变异法:假设当前个体 X为(1 3 7 4 8 0 5 9 6 2),如果当前随机概率值小于 Pm,则随机选择来自同一个体的两个点mutatepoint(1) 和 mutatepoint(2),然后倒置两点的中间部分,产生新的个体。例如,假设随机选择个体 X 的两个点“7”和“9”,则倒置该两个点的中间部分,即将“4805”变为“5084”,产生新的个体 X 为(1 3 7 5 0 8 4 9 6 2)。
6.终止条件为循环一定的代数。
2 部分代码
%%%%%%%%%% AUV path planning using GA %%%%%%%%%%
%%%%%Run 'RP_coordinate.m' before running main %%%%
clear all;
close all;
tic;%Runtime timer
%% global variables
load ('coor.mat'); %Load data generated by RP_coordinate.m
Popsize =50; %Population size, should be an even integer
%Genetic parameters
%MIXRATE = 0.3;
ITERATION = 10000; %Number of iteration
THRESHOLD = 100;
Pcross = 0.7; %Crossover rate
Pmutation = 0.3; %Mutation rate
%Begin
Parentpop=InitPop(Popsize,RPNUM,adjacency);
Fitnesscurve=[];
Generation = 1;
Fitconst=0; %Number of generations that fitness values remain constant
%% Genetic algorithm
while(Generation <= ITERATION)
if (Fitconst<=THRESHOLD) %Stop iteration if fitness value is constant in threshold number of genreations
fitness = Fitness(Parentpop,adjacency); %Calculate fitness of parents
crossover = Crossover(Parentpop,Pcross); %Crossover
Childpop = Mutation(crossover,Pmutation); %Mutate and get chindren
combopop=[Parentpop;Childpop]; %Combine parents and chindren
combofitness=Fitness(combopop,adjacency); %Calculate overall fitness
nextpop=Select(combopop,combofitness); %Select the first half of best to get 2nd gen
Parentpop=nextpop.pop;
if(Generation ==1)
Best_GApath=Parentpop(1,:);
Best_Fitness=combofitness(nextpop.bestplan);
else
New_Best_Fitness=combofitness(nextpop.bestplan);%Evaluate best solution
New_Best_GApath=Parentpop(1,:);
if(New_Best_Fitness<Best_Fitness)
Best_Fitness=New_Best_Fitness;
Best_GApath=New_Best_GApath;
Fitconst = 0;
%%%%%%%%Visualize planning process%%%%%%%%
% GENERATION=[1:Generation-1];
% GAplancoor = [RP(Best_GApath).x;RP(Best_GApath).y; RP(Best_GApath).z].';
% figure(1);
% for i=1:RPNUM
% subplot(2,1,1); %Plot all rendezvous points
% plot3(RP(i).x,RP(i).y,RP(i).z,'o');
% text(RP(i).x,RP(i).y, RP(i).z,num2str(i));
% hold on;
% subplot(2,1,2);
% plot(RP(i).x,RP(i).y,'o');
% text(RP(i).x,RP(i).y,num2str(i));
% hold on;
% end
% subplot(2,1,1);
% plot3(GAplancoor(:,1),GAplancoor(:,2),GAplancoor(:,3),'r-.');
% title('3D Path of AUV');
% grid on;
% hold off;
% subplot(2,1,2);
% plot(GAplancoor(:,1),GAplancoor(:,2),'r-.');
% title('2D Path of AUV');
% grid on;
% hold off;
%%%%%%%%Visualize planning process%%%%%%%%
else
Fitconst=Fitconst+1;
end
end
Fitnesscurve(Generation)=Best_Fitness;
else
break
end
Generation = Generation +1;
end
Best_GApath(RPNUM+1)=Best_GApath(1); %Add the path from end to start
GENERATION=[1:Generation-1];
toc;
%% plot result plan
GAplancoor = [RP(Best_GApath).x;RP(Best_GApath).y; RP(Best_GApath).z].';
figure(1);
for i=1:RPNUM
subplot(2,1,1); %Plot all rendezvous points
plot3(RP(i).x,RP(i).y,RP(i).z,'o');
text(RP(i).x,RP(i).y, RP(i).z,num2str(i));
hold on;
subplot(2,1,2);
plot(RP(i).x,RP(i).y,'o');
text(RP(i).x,RP(i).y,num2str(i));
hold on;
end
subplot(2,1,1);
plot3(GAplancoor(:,1),GAplancoor(:,2),GAplancoor(:,3),'r-.');
title('3D Path of AUV');
grid on;
subplot(2,1,2);
plot(GAplancoor(:,1),GAplancoor(:,2),'r-.');
title('2D Path of AUV');
grid on;
%% Plot iteration of fitness
figure(2);
plot(GENERATION,Fitnesscurve,'r.-');
title('Minimum distance in each generation');
xlabel('Generation');
ylabel('Fitness value');
legend('Best Fitness Value');
set(gca, 'Fontname', 'Times New Roman', 'FontSize', 14);
grid on;
3 仿真结果
4 参考文献
[1]温清芳. 遗传算法求解TSP问题的MATLAB实现[J]. 韶关学院学报, 2007, 28(6):5.
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。