[TSP problem] TSP problem based on matlab differential evolution [including matlab source code 131]

1. Introduction

Differential evolution algorithm is a random heuristic search algorithm formed by simulating the evolutionary development law of natural biological populations based on the principle of "survival of the fittest and survival of the fittest". It retains the global search strategy based on population, adopts real number coding, simple mutation operation based on difference and one-to-one competitive survival strategy, which is simpler than genetic algorithm. At the same time, the unique memory ability of the differential evolution algorithm enables it to dynamically track the current search situation and adjust the search measurement in time, so it has a strong global convergence ability.
So far, the differential evolution algorithm has become an extremely effective method for solving nonlinear, non-differentiable, multi-extreme and high-dimensional complex functions.
In the optimization design, the differential evolution algorithm has the following characteristics compared with the traditional algorithm:
1. The differential evolution algorithm searches from a group, that is, multiple points instead of starting from one point. This is also the algorithm can find the whole with a greater probability The reason for the optimal solution.
2. The algorithm's evolution criterion is based on adaptive information and does not require other auxiliary information, such as the requirement that the function be derivable and continuous.
3. The differential evolution algorithm has inherent parallelism and is suitable for large-scale parallel distributed processing, reducing time cost and overhead.
But the disadvantages are:
1. The difference between individuals in the later stage of the algorithm is reduced, the convergence speed is slow, and it is easy to fall into the local optimum.
2. Without using the prior knowledge of the individual, it may take more iterations to converge to the global optimal
algorithm framework:
Insert picture description here
1. Initialize the population and set the initial evolution algebra t=0, in the feasible solution space of the optimization problem, follow the formula, random Generate NP individuals X that meet the constraints to form the initial population. The selection of NP is generally between 5-10 times the dimension D.
Insert picture description here
2. Mutation operation Mutation operation is a key step in the DE algorithm, and it is also the main difference between it and other evolutionary algorithms. The most basic variation component of the DE algorithm is the difference vector of the parent. Each difference vector corresponds to the difference vector of two different individuals in the parent population. The definition of the difference vector is as follows:
Insert picture description here
Among them, r1, r2, r3 ∈ {1, 2, …, NP} are randomly selected positive integers that are different from each other, and r1, r2, r3 are different from the current target individual vector index number i. It can be seen that the DE algorithm The population size of must be greater than 4, otherwise the mutation operation will not be possible. Vi(t+1) is the mutated individual vector corresponding to the target individual vector Xi(t), Xr3(t) is called the basis vector, and F∈[0, 2] is a constant, which is one of the main control parameters of the DE algorithm, called It is the variation factor or scaling factor, which controls the scaling of the difference vector, that is, the magnitude of the influence on the base vector.
Insert picture description here
3. Crossover operation In order to further increase the diversity of the population, the DE algorithm crosses the target vector individual Xi(t) with its corresponding variant individual Vi(t+1) to generate a test individual, that is, the target individual candidate Ui(t+ 1). In order to ensure the evolution of the target individual Xi(t), it is necessary to ensure that at least one dimensional component in the test individual Ui(t+1) is contributed by the variant individual Vi(t+1), while other dimensional components are determined by the crossover probability factor CR. Therefore, each dimensional component uij(t+1) in the test individual is as follows.
Insert picture description here
Among them, xij(t) represents the j-th dimension component in the target individual vector Xi(t) in the parent population, vij(t+1) is the j-th dimension component in the variant individual Vi(t+1), where i= 1,...,NP,j=1,...,D. rand(j)∈[0,1] is the random number corresponding to the j-th dimension component. The crossover probability factor CR∈[0,1] is another main control parameter of the DE algorithm, which determines the proportion of the variant individual Vi(t+1) in the generated test individual Ui(t+1). k is the coefficient corresponding to the i-th individual, which is generally an integer randomly selected from the sequence [1, 2, …,D] to ensure that at least one dimensional component of the candidate individual Ui(t+1) comes from the variant individual Vi (t+1).

4. Select operation (survival of the fittest)
Insert picture description here

Second, the source code

close all
clear
clc
 
%edit by zhang
% 2014-3-15
 
city=[1304,2312;3639,1315;4177,2244;3712,1399;3488,1535;3326,1556;...
    3238,1229;4196,1004;4312,790;4386,570;3007,1970;2562,1756;2788,1491;...
    2381,1676;1332,695;3715,1678;3918,2179;4061,2370;3780,2212;3676,2578;4029,2838;...
    4263,2931;3429,1908;3507,2367;3394,3201;3439,3201;2935,3240;3140,3550;2545,2357;2778,2826;2370,2975];
 
city_num=size(city,1);
%计算两两城市之间的距离
for i=1:city_num
    for j=1:city_num
        distance(i,j)=sqrt((city(i,1)-city(j,1))^2 + (city(i,2)-city(j,2))^2);
    end
end
%相比于其他算法,迭代次数略长
NP=20;
NG=2000;
F=0.6;
CR=0.5;
 
pop=zeros(NP,city_num);
x_old=zeros(NP,city_num);
x_new=zeros(NP,city_num);
pi_old=zeros(NP,city_num);
pi_new=zeros(NP,city_num);
 
for i=1:NP
    for j=1:city_num
       pop(i,j)=5*rand();
    end
end
 
k=1;
 
x_old=pop;
 
while k<=NG
    %将实数编码转换成工件顺序编码
    for i=1:NP
      x_old_decode(i,:)=LOVdecode(x_old(i,:));
    end
    %----find the best value--------------%
    ind_best  = x_old(1,:);
    for i=2:NP
        pi_best=LOVdecode(ind_best);
        
        if CalLength(distance,pi_best)>CalLength(distance,x_old_decode(i,:))
            ind_best = x_old(i,:);
        end
    end
    
    BestFit=CalLength(distance , LOVdecode(ind_best));%最短时间
    Best=LOVdecode(ind_best);%最佳路径
     %%--------mutaiton---------------------%
     for i=1:NP
         R=randperm(NP);
         r1=R(1);
         r2=R(2);
         r3=R(3);
         
         if i==r1
             r1=R(4);
         else if i==r2
                 r2=R(4);
             else if i==r3
                     r3=R(4);
                 end
             end
         end
         
 
       
     x_old = x_new; 
     k=k+1;
end
BSF=Best;
BestL=BestFit;
%绘制路径图
for i=1:city_num-1 
        plot([city(BSF(i),1),city(BSF(i+1),1)],[city(BSF(i),2),city(BSF(i+1),2)],'bo-'); 
        hold on; 
end 
plot([city(BSF(city_num),1),city(BSF(1),1)],[city(BSF(city_num),2),city(BSF(1),2)],'ro-');
title('差分进化TSP')
 
disp('最佳路径');
disp(BSF);
disp('最短路径长度');
disp(BestL)

Three, running results

Insert picture description here

Four, remarks

Complete code or writing to add QQ2449341593 past review
>>>>>>
[ VRP] based on matlab genetic algorithm with time window vehicle routing problem [including Matlab source code 002]
[path planning] based on matlab A* algorithm to solve the three-dimensional path Planning problem [including Matlab source code 003]
[Path planning] Matlab artificial bee colony-based path planning [Matlab source code 004 included]
[Path planning] Matlab ant colony to solve multiple traveling salesman MTSP problems [including Matlab source code 005]
[ Path planning] UAV path planning based on matlab ant colony algorithm [including Matlab source code 008]
[Path planning] Matlab genetic algorithm to solve multiple VRP problems [including Matlab source code 010]
[Path planning] based on matlab genetic algorithm Center VRP solution [Including Matlab source code 011]
[Path planning] Three-dimensional UAV path planning based on matlab particle swarm [Including Matlab source code 015]
[ Path planning] Based on matlab using genetic algorithm to prepare open vehicle paths for multiple logistics centers Problem [Including Matlab source code 017]
[Path planning] Robot grid path planning based on matlab particle swarm [Including Matlab source code 018]
[Path planning] Solving the shortest path based on matlab ant colony algorithm [Including Matlab source code 019]
[Path Planning] Matlab immune algorithm-based logistics center location problem [including Matlab source code 020 period]
[Path planning] Matlab artificial bee colony-based UAV three-dimensional path planning [including Matlab source code 021 period]
[Path planning] Robot optimal path planning based on matalb grid map-genetic algorithm [including Matlab source code 022]
[Path planning] Matlab grid map-genetic algorithm-based robot optimal path planning [including Matlab source code 027 period]
[path] planning based on ant colony matlab multi-UAV attacks scheduling with Matlab source code [034]
[path] three-dimensional path planning based on ant colony matlab matlab source with planning [043]
[path] planning based on particle matlab Colony optimization ant colony's shortest path solution [including Matlab source code 076]
[Path planning] based on matlab ant colony algorithm to solve multi-center VRP problem [including Matlab source code 111]
[path planning] based on matlab ant colony algorithm to solve with time windows Multi-center VRP problem [Including Matlab source code 112]
[Path planning] Based on matlab ant colony algorithm to solve multi-center VRP problem with time window [Including Matlab source code 113]
[Path planning] Multi-center VRP solution based on matalb genetic algorithm [ Contains Matlab source code 114]
[Path planning] Matlab simulated annealing to solve VRP problem [Contains Matlab source code 115]
[Path planning] Matlab A star-based raster path planning [Contains Matlab source code 116]
[Path planning] Based on matlab A two-way optimization particle swarm grid map path planning with cross factors [including Matlab source code 117]
[TSP] Based on matlab ant colony algorithm to solve traveling salesman TSP problem with GUI [including Matlab source code 118]
[Path planning] Matlab ant colony algorithm grid map path planning [including Matlab source code 119]

Guess you like

Origin blog.csdn.net/TIQCmatlab/article/details/113097689