您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > 遗传算法求解货郎担问题
[键入公司名称]遗传算法求解TSP问题最短路径参观特定地点[键入作者姓名]2014/12/12指导老师:1问题简介一个考察团想参观同济大学嘉定校区,参观地点如图1黄色标注所示。他们想从其中一点出发,然后依次经过其它点,中间不重复,最后回到起点。请为他们设计一条最短路径。图1同济大学嘉定校区参观点2问题分析这是一个典型的TSP问题。我们可以尝试采用遗传算法来解决。先随机生成若干条有效路径,再选择其中较优的路径进行繁殖,生成下一代。再对下一代进行类似的操作。进过若干代的进化后,将会收敛于一个较优解。具体的流程如图2所示。图2遗传算法流程图3实验结果路径为:657108942136。长度为:3.1434Km如图3所示。其中,适应度函数如图4所示。(由于起点不固定,故同一条路径有多种结果:57108921365、21365710894、36571089421;另外,多次运行程序,还出现另一个结果:98107563124、75631249810。但两者的长度一样,实际中两者长度应该差不多)编码和初始化种群计算适应度函数选择(轮盘赌法)交叉(分类匹配算法)变异(基于次序的变异算子)最优个体保存法超过最大代数结束是否图3路径图图4适应度函数变化趋势图4源程序clc,clear;%%距离矩阵MatrixDis1=1e3*[00.38690.40670.64000.74460.62430.86171.09310.66591.08230.386900.12140.25400.42820.34460.62040.70670.30570.73320.40670.121400.26820.34640.24200.51300.72280.38330.67660.64000.25400.268200.29300.29530.53200.45830.19010.51490.74460.42820.34640.293000.12990.24130.55300.48180.34540.62430.34460.24200.29530.129900.27610.65020.48030.47490.86170.62040.51300.53200.24130.276100.74300.72180.39841.09310.70670.72280.45830.55300.65020.743000.46250.43160.66590.30570.38330.19010.48180.48030.72180.462500.65671.08230.73320.67660.51490.34540.47490.39840.43160.65670];N=length(MatrixDis1);%%初始种群Num_Population=50;%种群规模Init_Population=zeros(Num_Population,N);fori=1:Num_PopulationInit_Population(i,:)=randperm(N);end%%进化过程k=0;Gen_Max=1000;%最大进化代数Population=Init_Population;Best_Fitness=zeros(1,Gen_Max);Best_Individual=zeros(Gen_Max,N);while(kGen_Max)k=k+1;%%计算适应度函数,距离的倒数Distance=zeros(1,Num_Population);fori=1:Num_Populationforj=1:N-1Distance(i)=Distance(i)+MatrixDis1(Population(i,j),Population(i,j+1));endDistance(i)=Distance(i)+MatrixDis1(Population(i,N),Population(i,1));endFitness=1./Distance;[Best_Fitness(k),Best_Index]=max(Fitness);Best_Individual(k,:)=Population(Best_Index,:);%%选择(轮盘赌法)Select_Index=zeros(1,Num_Population);P=Fitness/sum(Fitness);Q=P;fori=1:Num_Population-1Q(i+1)=Q(i)+P(i+1);endfori=1:Num_PopulationQ0=rand;forj=1:Num_PopulationifQ0Q(j)Select_Index(i)=j;break;endendendSelect_Population=Population(Select_Index,:);%%%最优个体保存策略%[BBest_Fitness,BBest_Index]=max(Best_Fitness);%选择迄今为止的最优个体%BBest_Individual=Best_Individual(BBest_Index(1),:);%%[Least_Fitness,Least_Index]=min(Fitness);%把种群中最差的个体替换掉%Select_Population(Least_Index(1),:)=BBest_Individual;%%交叉(分类匹配算子)Probability_Cross=0.5;%交叉概率Cross_Index=[];fori=1:Num_Population%选择交叉个体ifrandProbability_CrossCross_Index=[Cross_Index,i];endendCross_N=fix(length(Cross_Index)/2);fori=1:Cross_NCross_Point1=fix(1+9*rand);%产生交叉子串Cross_Point2=fix(1+9*rand);Cross_Point=[min(Cross_Point1,Cross_Point2),max(Cross_Point1,Cross_Point2)];Cross_Population1=Select_Population(Cross_Index(2*i-1),:);Cross_Population1_2=Cross_Population1;Cross_Population2=Select_Population(Cross_Index(2*i),:);Cross_Population2_1=Cross_Population2;forj=Cross_Point(1):Cross_Point(2)[ab]=find(Cross_Population2_1==Cross_Population1(j));Cross_Population2_1(b)=[];endCross_Population2_1=[Cross_Population1(Cross_Point(1):Cross_Point(2)),Cross_Population2_1];forj=Cross_Point(1):Cross_Point(2)[ab]=find(Cross_Population1_2==Cross_Population2(j));Cross_Population1_2(b)=[];endCross_Population1_2=[Cross_Population2(Cross_Point(1):Cross_Point(2)),Cross_Population1_2];Select_Population(Cross_Index(2*i-1),:)=Cross_Population1_2;Select_Population(Cross_Index(2*i),:)=Cross_Population2_1;end%%变异(基于次序的变异算子)Probability_Mutate=0.25;%变异概率Mutate_Index=[];fori=1:Num_Population%选择变异个体ifrandProbability_MutateMutate_Index=[Mutate_Index,i];endendfori=1:length(Mutate_Index)Mutate_Point1=fix(1+9*rand);%产生变异点Mutate_Point2=fix(1+9*rand);Mutate_Population=Select_Population(Mutate_Index(i),:);Select_Population(Mutate_Index(i),Mutate_Point1)=Mutate_Population(Mutate_Point2);Select_Population(Mutate_Index(i),Mutate_Point2)=Mutate_Population(Mutate_Point1);end%%最优个体保存策略[BBest_Fitness,BBest_Index]=max(Best_Fitness);%选择迄今为止的最优个体BBest_Individual=Best_Individual(BBest_Index(1),:);DDistance=zeros(1,Num_Population);%计算适应度函数fori=1:Num_Populationforj=1:N-1DDistance(i)=DDistance(i)+MatrixDis1(Select_Population(i,j),Select_Population(i,j+1));endDDistance(i)=DDistance(i)+MatrixDis1(Select_Population(i,N),Select_Population(i,1));endFFitness=1./DDistance;[Least_Fitness,Least_Index]=min(FFitness);%把种群中的最差的个体替换掉Select_Population(Least_Index(1),:)=BBest_Individual;%%新的种群Population=Select_Population;endplot(1:Gen_Max,Best_Fitness);xlabel('Gen');ylabel('Fitness');
本文标题:遗传算法求解货郎担问题
链接地址:https://www.777doc.com/doc-2009868 .html