### [MDVRP] solve multi depot VRP Problem with distance based on Matlab genetic algorithm (including single line local optimization) [including Matlab source code phase 1170]

Ziji Shengguang q1564658423 2021-08-05 09:25:32 阅读数:502

mdvrp solve multi depot vrp

## One 、 brief introduction

1 Overview of genetic algorithm
Genetic algorithm (ga) （Genetic Algorithm,GA） It's part of evolutionary computing , It is a computational model that simulates the biological evolution process of Darwin's genetic selection and natural elimination , It is a method to search the optimal solution by simulating the natural evolution process . The algorithm is simple 、 Universal , Strong robustness , Suitable for parallel processing .

2 Characteristics and application of genetic algorithm
Genetic algorithm is a kind of robust search algorithm which can be used for complex system optimization , Compared with the traditional optimization algorithm , It has the following characteristics ：
（1） Take the code of decision variable as the operation object . Traditional optimization algorithms often directly use the actual value of decision variables to optimize calculation , But genetic algorithm uses some form of coding of decision variables as the operation object . This coding method for decision variables , So we can learn from the concepts of chromosome and gene in Biology , It can imitate the genetic and evolutionary incentives of organisms in nature , Genetic operators can also be easily applied .
（2） Directly use fitness as search information . The traditional optimization algorithm not only needs to use the value of the objective function , Moreover, the search process is often constrained by the continuity of the objective function , There may be a need to meet “ The derivative of the objective function must exist ” To determine the search direction . The genetic algorithm only uses the fitness function value transformed by the objective function value to determine the further search range , No other auxiliary information such as derivative value of objective function is required . Directly using the objective function value or individual fitness value can also focus the search range into the search space with higher fitness , To improve search efficiency .
（3） Search information using multiple points , With implicit parallelism . The traditional optimization algorithm is often an iterative search process starting from an initial point in the solution space . A single point provides little search information , So the search efficiency is not high , It is also possible to fall into the local optimal solution and stop ; Genetic algorithm starts the search process of the optimal solution from the initial population composed of many individuals , Instead of searching from a single individual . Of the initial population 、 choice 、 cross 、 Mutation and other operations , Produce a new generation of groups , It includes a lot of group information . This information can avoid searching some unnecessary points , So as to avoid falling into local optimization , Gradually approach the global optimal solution .
（4） Use probabilistic search instead of deterministic rules . Traditional optimization algorithms often use deterministic search methods , The transfer from one search point to another has a definite transfer direction and transfer relationship , This certainty may make the search less than optimal , It limits the application scope of the algorithm . Genetic algorithm is an adaptive search technology , Its choice 、 cross 、 Operations such as mutation are carried out in a probabilistic way , Increases the flexibility of the search process , And it can converge to the optimal solution with a large probability , It has good global optimization ability . but , Crossover probability 、 Mutation probability and other parameters will also affect the search results and search efficiency of the algorithm , Therefore, how to select the parameters of genetic algorithm is an important problem in its application .
Sum up , Because the overall search strategy and optimization search method of genetic algorithm do not depend on gradient information or other auxiliary knowledge , Only the objective function affecting the search direction and the corresponding fitness function need to be solved , Therefore, genetic algorithm provides a general framework for solving complex system problems . It does not depend on the specific area of the problem , Strong robustness to the types of problems , So it is widely used in various fields , Include ： Function optimization 、 Combinatorial optimal production scheduling problem 、 Auto-Control
、 Robotics 、 The image processing （ Image restoration 、 Image edge feature extraction …)、 Artificial life 、 Genetic programming 、 machine learning .

3 The basic flow and implementation technology of genetic algorithm
Basic genetic algorithm （Simple Genetic Algorithms,SGA） Use only the selection operator 、 Crossover operator and mutation operator are three genetic operators , Evolution is simple , It is the basis of other genetic algorithms .

3.1 The basic flow of genetic algorithm
Generate a number of randomly determined lengths （ The length is related to the accuracy of the problem to be solved ） The initial population of coding ;
Each individual is evaluated by fitness function , Individuals with high fitness value were selected to participate in genetic operation , Individuals with low fitness are eliminated ;
Genetically manipulated （ Copy 、 cross 、 variation ） A new generation of population is formed by the collection of individuals , Until the stop criteria are met （ Evolution algebra GEN>=?）;
The best realized individual in the offspring is taken as the execution result of the genetic algorithm . among ,GEN Is the current algebra ;M It's population size ,i Represents the number of populations .

3.2 Implementation technology of genetic algorithm
Basic genetic algorithm （SGA） Coded by 、 Fitness function 、 Genetic operators （ choice 、 cross 、 variation ） And operation parameters .
3.2.1 code
（1） Binary code
The length of binary coded string is related to the accuracy of the problem . We need to ensure that every individual in the solution space can be encoded .
advantage ： Ed 、 The decoding operation is simple , inheritance 、 Crossover is easy to achieve
shortcoming ： The length is large
（2） Other coding methods
Gray code 、 Floating point code 、 Symbolic encoding 、 Multi parameter coding, etc
3.2.2 Fitness function
The fitness function should effectively reflect the gap between each chromosome and the chromosome of the optimal solution of the problem .
3.2.3 Selection operator 3.2.4 Crossover operator
Cross operation refers to the exchange of some genes between two paired chromosomes in some way , And two new individuals ; Crossover operation is an important feature that distinguishes genetic algorithm from other evolutionary algorithms , Is the main way to produce new individuals . Before crossing, individuals in the group need to be paired , Generally, the principle of random pairing is adopted .
Commonly used crossover ：
A single point of intersection
Two point intersection （ Multi-point crossover , The more cross points , The more likely the individual's structure is to be destroyed , Generally, multi-point intersection is not adopted ）
Uniform cross
Arithmetic crossover
3.2.5 Mutation operator
The mutation operation in genetic algorithm refers to replacing the gene values at some loci in the individual chromosome coding string with other alleles at this locus , So as to form a new individual .

In terms of the ability to generate new individuals in the operation of genetic algorithm , Cross operation is the main method to generate new individuals , It determines the global search ability of genetic algorithm ; Mutation is only an auxiliary method to generate new individuals , But it is also an essential operation step , It determines the local search ability of genetic algorithm . The combination of crossover operator and mutation operator completes the global search and local search of the search space , Thus, the genetic algorithm can complete the optimization process of the optimization problem with good search performance .

3.2.6 Operation parameters 4 The basic principle of genetic algorithm
4.1 Pattern theorem 4.2 Building block hypothesis
With low order 、 The definition length is short , The pattern whose fitness value is higher than the average fitness value of the population is called gene block or building block .
Building block hypothesis ： Individual gene blocks are selected 、 cross 、 The role of genetic operators such as mutation , Can be spliced together , Form individual coding strings with higher fitness .
The building block hypothesis illustrates the basic idea of using genetic algorithm to solve various problems , That is, better solutions can be produced by directly splicing building blocks together .

## Two 、 Source code

``````% Genetic algorithm (ga) VRP problem Matlab Realization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%tic% timer
clear;
clc
W=80; % The load capacity of each vehicle
Citynum=50; % Number of customers
Stornum=4;% Number of warehouses
%C %% The second and third Columns Customer coordinates , The fourth column customer demand 51,52,53,54 For four warehouses
load('p01-n50-S4-w80.mat'); % Load test data ,n Customer service points ,S Number of warehouses ,w Vehicle load capacity
G=100;% The population size
[dislist,Clist]=vrp(C);%dislist Is the distance matrix ,Clist For the point coordinate matrix and customer needs
L=[];% Save the loop length of each population
for i=1:G
Parent(i,:)=randperm(Citynum);% Randomly generated path
L(i,1)=curlist(Citynum,Clist(:,4),W,Parent(i,:),Stornum,dislist);
end
Pc=0.8;% Cross ratio
Pm=0.3;% Variation ratio
species=Parent;% population
children=[];% Filial generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
disp(' Running , It takes a long time , One moment please .........')
g=1;
for generation=1:g
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
fprintf('\n The second %d Sub iteration , common %d Time ..........',generation,g);
Parent=species;% Children become parents
children=[];% Filial generation
Lp=L;
% Select cross parent
[n m]=size(Parent);
% cross , Handle on behalf of
for i=1:n
for j=i:n
if rand<Pc crossover end end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [n m]=size(Parent); for i=1:n if rand<Pm parent=Parent(i,:);% Mutant individuals  X=floor(rand*Citynum)+1; Y=floor(rand*Citynum)+1; Z=parent(X); parent(X)=parent(Y); parent(Y)=Z; % Gene exchange variation  children=[children;parent]; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Calculate the fitness of offspring ( Path length ) ( This one takes a long time ) [m n]=size(children); Lc=zeros(m,1);% Offspring fitness  for i=1:m Lc(i,1)=curlist(Citynum,Clist(:,4),W,children(i,:),Stornum,dislist); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Eliminate offspring   Before remaining G It's an optimal solution  [m n]=size(children); if(m>G)
[m n]=sort(Lc);
children=children(n(1:G),:);
Lc=Lc(n(1:G));
end
% Eliminate the population
species=[children;Parent];
L=[Lc;Lp];
[m n]=sort(L);
species=species(n(1:G),:); % Renewal generation
L=L(n(1:G));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Join in Opt Optimize
% Assign warehouse for opt
temp=initialStor(Citynum,Clist(:,4),W,species(1,:),Stornum,dislist);% Store the results after warehouse allocation 【52 14 5 52 53 6 9 8 53......】
Rbest=temp;
L_best=L(1);
[m n]=size(temp);
start=1;
car=[];% Deposit opt The result of optimization
i=2;
while (i<n+1) if (temp(i)>Citynum)
cur=[];
cur=Opt(i-start,[1:i-start,1:i-start],dislist,temp(start:i-1),Citynum);
car=[car,[cur,cur(1)]];
start=i+1;
i=i+2;
else
i=i+1;
end
end
function F=CalDist(dislist,s,Citynum)% Calculate the loop path distance
DistanV=0;% The initial distance is 0
n=size(s,2);%n by s The number of columns in a matrix ,s It should be a matrix with several points in a line
for i=1:(n-1)
if(s(i)>Citynum && s(i+1)>Citynum)
else
DistanV=DistanV+dislist(s(i),s(i+1));
end
end
F=DistanV;
end
%OX Sequential crossover strategy
P1=Parent(i,:);
P2=Parent(j,:);
% Select tangent point , Exchange the middle part also Repair genes
X=floor(rand*(m-2))+2;
Y=floor(rand*(m-2))+2;
if X>Y
Z=X;
X=Y;
Y=Z;
end
change1=P1(X:Y);
change2=P2(X:Y);
% Start repairing Order Crossover
%1. List genes
p1=[P1(Y+1:end),P1(1:X-1),change1];
p2=[P2(Y+1:end),change2,P2(1:X-1)];
%2.1 Delete existing genes P1
for i=1:length(change2)
p1(find(p1==change2(i)))=[];
end
%2.2 Delete existing genes P2
for i=1:length(change1)
p2(find(p2==change1(i)))=[];
end
%3.1 Repair P1
P1=[p1(m-Y+1:end),change2,p1(1:m-Y)];
%3.1 Repair P2
P2=[change1,p2(m-Y+1:end),p2(1:m-Y)];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
children=[children;P1;P2];
```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
```

## 3、 ... and 、 Running results edition ：2014a