[TWVRP] solve the pick-up and delivery problem with time window based on Matlab genetic algorithm and simulated annealing algorithm [including Matlab source code issue 1139]

Posted by zrobin01 on Sat, 15 Jan 2022 17:13:25 +0100

1, Introduction
1 overview of genetic algorithm
Genetic Algorithm (GA) is a part of evolutionary computation. It is a computational model simulating Darwin's genetic selection and natural elimination of biological evolution process. It is a method to search the optimal solution by simulating the natural evolution process. The algorithm is simple, universal, robust and 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) The coding of decision variables is used as the operation object. Traditional optimization algorithms often directly use the actual value of decision variables itself for optimization calculation, but genetic algorithm uses some form of coding of decision variables as the operation object. This coding method of decision variables enables us to learn from the concepts of chromosome and gene in biology in optimization calculation, imitate the genetic and evolutionary incentives of organisms in nature, and easily apply genetic operators.
(2) Directly take fitness as search information. The traditional optimization algorithm not only needs to use the value of the objective function, but also the search process is often constrained by the continuity of the objective function. It may also need to meet the requirement that "the derivative of the objective function must exist" to determine the search direction. The genetic algorithm only uses the fitness function value transformed from the objective function value to determine the further search range without other auxiliary information such as the derivative value of the objective function. Directly using the objective function value or individual fitness value can also focus the search range into the search space with higher fitness, so as to improve the search efficiency.
(3) Using the search information of multiple points has implicit parallelism. Traditional optimization algorithms often start from an initial point in the solution space. The search information provided by a single point is not much, so the search efficiency is not high, and it may fall into local optimal solution and stop; Genetic algorithm starts the search process of the optimal solution from the initial population composed of many individuals, rather than from a single individual. The, selection, crossover, mutation and other operations on the initial population produce a new generation of population, including a lot of population information. This information can avoid searching some unnecessary points, so as to avoid falling into local optimization and 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 certain transfer direction and transfer relationship. This certainty may make the search less than the optimal store, which limits the application scope of the algorithm. Genetic algorithm is an adaptive search technology. Its selection, crossover, mutation and other operations are carried out in a probabilistic way, which increases the flexibility of the search process, and can converge to the optimal solution with a large probability. It has a good ability of global optimization. However, crossover probability, mutation probability and other parameters will also affect the search results and search efficiency of the algorithm, so how to select the parameters of genetic algorithm is an important problem in its application.
In conclusion, because the overall search strategy and optimization search method of genetic algorithm do not depend on gradient information or other auxiliary knowledge, and only need to solve the objective function and corresponding fitness function affecting the search direction, genetic algorithm provides a general framework for solving complex system problems. It does not depend on the specific field of the problem and has strong robustness to the types of problems, so it is widely used in various fields, including function optimization, combinatorial optimization, production scheduling problem and automatic control
, robotics, image processing (image restoration, image edge feature extraction...), artificial life, genetic programming, machine learning.

3 basic flow and implementation technology of genetic algorithm
Simple genetic algorithms (SGA) only uses three genetic operators: selection operator, crossover operator and mutation operator. The evolution process is simple and is the basis of other genetic algorithms.

3.1 basic flow of genetic algorithm
Generating a number of initial groups encoded by a certain length (the length is related to the accuracy of the problem to be solved) in a random manner;
Each individual is evaluated by fitness function. Individuals with high fitness value are selected to participate in genetic operation, and individuals with low fitness are eliminated;
A new generation of population is formed by the collection of individuals through genetic operation (replication, crossover and mutation) until the stopping criterion is met (evolutionary algebra gen > =?);
The best realized individual in the offspring is taken as the execution result of genetic algorithm. Where GEN is the current algebra; M is the population size, and i represents the population number.

3.2 implementation technology of genetic algorithm
Basic genetic algorithm (SGA) consists of coding, fitness function, genetic operators (selection, crossover, mutation) and operating parameters.
3.2.1 coding
(1) Binary coding
The length of binary coded string is related to the accuracy of the problem. It is necessary to ensure that every individual in the solution space can be encoded.
Advantages: simple operation of encoding and decoding, easy implementation of heredity and crossover
(2) Other coding methods
Gray code, floating point code, symbol code, multi parameter code, 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, so as to form two new individuals; Crossover operation is an important feature of genetic algorithm, which is different from other evolutionary algorithms. It is the main method to generate new individuals. Before crossing, individuals in the group need to be paired, which generally adopts the principle of random pairing.
Common crossing methods:
Single point intersection
Two point crossing (multi-point crossing. The more crossing points, the greater the possibility of individual structure damage. Generally, multi-point crossing is not adopted)
Uniform crossing
Arithmetic Crossover
3.2.5 mutation operator
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 process of genetic algorithm, crossover operation is the main method to generate new individuals, which 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, which 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, so that the genetic algorithm can complete the optimization process of the optimization problem with good search performance.

3.2.6 operating parameters 4 basic principle of genetic algorithm
4.1 mode theorem 4.2 building block assumptions
Patterns with low order, short definition length and fitness value higher than the average fitness value of the population are called gene blocks or building blocks.
Building block hypothesis: individual gene blocks can be spliced together through genetic operators such as selection, crossover and mutation to 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 the building blocks together.
2, Source code

function [R,minvalue]=Run_VRP(D,RP,demand,popsize,k,capacity,original,C,Pc,Pm,farm)
%RUN_VRP Summary of this function goes here
%   Detailed explanation goes here

N=size(RP,1);% %N Is the number of requests
%farm
R=farm(1,:);%A random solution(individual)->Used to store the optimal solution (shortest path)
evalue=zeros(popsize,1);%Storage path length
alph = 0.9; %Annealing attenuation coefficient
counter=0;
while counter < C
for i = 1:popsize
evalue(i,1) = myEvalue(D,RP,farm(i,:),k,capacity,original,demand); %Calculate objective function
end
minvalue=min(evalue);%Find the current optimal solution
coor=find(evalue==minvalue);%Return is in len The shortest path in the middle path is evalue Location of the; coordinate(i,1)
R=farm(coor(1,1),:);%Update optimal solution
if counter == 0 %Calculate the initial annealing temperature
t = temp_inatial(evalue,popsize);
end
farm = select(farm,evalue,popsize,k,D,RP,capacity,original,demand,t);%simulated annealing+Roulette->choice
%farm =crossover(RP,farm,N,k,popsize,Pc);
%farm = variation(farm,N,k,popsize,Pm);
for i = 1:popsize
evalue(i,1) = myEvalue(D,RP,farm(i,:),k,capacity,original,demand); %Calculate objective function
end
maxvalue=max(evalue);%Find the current worst solution
coor=find(evalue==maxvalue);%Return is in len Coordinates of the longest path in the(i,1)
farm(coor(1,1),:) = R;%Replace the worst solution with the best solution
counter = counter + 1; %What's the matter today? I forgot this. I was fined 50 push ups
t = t * alph; %Annealing cooling
end %mainLoop
end %function
a1=find(p_p==0);
p_p(a1)=1;
for i = 1:k
end
m = 0;
for i = 1:N-1
if p(1,i) ==0  %Mark the number of the vehicle
m = m + 1;
end
cost(m,1) = cost(m,1) + 0.55*D(p_p(i),p_p(i+1))/60000;%Note that there is a difference of 1 between matrix subscript and node code
time(m,1) =time(m,1)+D(p_p(i),p_p(i+1))/v; %Calculate the arrival time of each point
if p(1,i+1)>=1&&p(1,i+1)<=size(R,1)
if time(m,1)<Timesetup(p(1,i+1),1)
time(m,1) = Timesetup(p(1,i+1),1);
end
else
if p(1,i+1)>=size(R,1)+1&&p(1,i+1)<=2*size(R,1)
if time(m,1)-R(p(1,i+1)-size(R,1),4)/60-1>0  %30 It refers to the limited service time. If it is exceeded, penalty fees will be incurred
latetime(p(1,i+1)-size(R,1),1)=time(m,1)-R(p(1,i+1)-size(R,1),4)/60-1;
cost(m,1)= cost(m,1)+ M2*latetime(p(1,i+1)-size(R,1),1);
end
end
end
if p(i+1)==0
else
end
cost(m,1) = cost(m,1) + M1;
else
cost(m,1) = cost(m,1) + M1;
end
end
end
for i = 1:k
evalue
evalue = evalue + cost(i,1);%The total cost of this path
end
function evalue = myEvalue( D,R,p,k,capacity,original,demand)
%MYEVALUE Summary of this function goes here
%   Detailed explanation goes here
%   myEvalue( D,p )  D Is the distance matrix, p It's a chromosome
%***\\\MinZ{x) = a^K{x) + a^Distix) + a^Wait{x)///***
M1 = 1000000; %Penalty coefficient,Penalty capacity limit
M2 = 120;    %Penalty coefficient,Penalty time limit
N = size(p,2);  %Node of path
Timesetup=R(:,4)/60+0.1;%5 Refers to the preparation time of the pick-up area
evalue = 0;
cost = zeros(k,1)+100;  %Store the driving path length of each vehicle
time= zeros(k,1)+0.1;  %Rolling time domain
latetime=zeros(size(R,1),1);%%Store late time to be penalized
v = 50000;  %v It's the speed,Used to cost Function to time
p_p=zeros(N,1);%Store real path

for i=1:N
if p(1,i)>=1&&p(1,i)<=size(R,1)
p_p(i,1)= R(p(1,i),2);
else
if p(1,i)>=size(R,1)+1&&p(1,i)<=2*size(R,1)
p_p(i,1)= R(p(1,i)-size(R,1),1);
end
end
end
a1=find(p_p==0);
p_p(a1)=1;
u=0;

for i=1:N
if p_p(i,1)==1
u=u+1;
end
if u==2
ibreak = i;
break;
end
end
p_1=zeros(1,ibreak);
p_2=zeros(1,N-ibreak+1);
p_1(1,:)=p(1,1:ibreak);
p_1(1,1)=3;
p_2(1,2:N-ibreak+1)=p(1,ibreak+1:N);
p_2(1,1)=1;
p_p_1=zeros(ibreak,1);
p_p_2=zeros(N-ibreak+1,1);
p_p_1(:,1)=p_p(1:ibreak,1);
p_p_1(1,1)=77;
p_p_2(2:N-ibreak+1,1)=p_p(ibreak+1:N,1);
p_p_2(1,1)=76;

m=0;
for j=1:k
if j==1
p_p_p=p_p_1;
p_pp=p_1;
else
if j==2
p_p_p=p_p_2;
p_pp=p_2;
end
end
m = m + 1;
for i=1:size(p_p_p)-1
cost(m,1) = cost(m,1) + 0.55*D(p_p_p(i),p_p_p(i+1))/60000;%Note that there is a difference of 1 between matrix subscript and node code
time(m,1) =time(m,1)+D(p_p_p(i),p_p_p(i+1))/v; %Calculate the arrival time of each point
if p(1,i+1)>=size(R,1)+1&&p(1,i+1)<=2*size(R,1)
if time(m,1)-R(p(1,i+1)-size(R,1),4)/60-0.5>0  %30 It refers to the limited service time. If it is exceeded, penalty fees will be incurred
latetime(p(1,i+1)-size(R,1),1)=time(m,1)-R(p(1,i+1)-size(R,1),4)/60-0.5;
cost(m,1)= cost(m,1)+ M2*latetime(p(1,i+1)-size(R,1),1);
end
end
if p(i+1)==0
else
end
cost(m,1) = cost(m,1) + M1;
else
cost(m,1) = cost(m,1) + M1;
end
end

end

end

3, Operation results  4, Remarks
Version: 2014a

Topics: MATLAB