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
Disadvantages: large length
(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 that distinguishes genetic algorithm 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
clc; clear; %% Vehicle scheduling problem with multiple distribution centers %Load data load data.mat %Calculate position matrix m=size(X,1); D=zeros(m,m); for i=1:m for j=1:m D(i,j)=norm(X(i,:)-X(j,:)); D(j,i)=D(i,j); D(i,i)=eps; end end %Calculate the location of the distribution center nume=zeros(20,20); for i=1:20 for j=1:20 temp=[D(:,i) D(:,j)]; [num,pp]=min(temp,[],2); nume(i,j)=sum(num); end end [mum1,index1]=min(nume); [mun2,index2]=min(mum1,[],2); w1=index1(index2); w2=index2; %Assign customer sets to distribution centers H=[]; S=[]; for i=1:20 if D(i,w1)<D(i,w2) H=[H i]; else S=[S i]; end end n1=size(H,2); n2=size(S,2); %Ant colony algorithm to find the minimum total vehicle travel %Set parameters Pop=60;%Ant colony number Alpha=1;%Importance coefficient Beta=1;%Beta: Visibility coefficient gama=2; Rho=0.15;%Volatility coefficient MAXGEN=50;%Number of iterations Q=15;%Information update parameters W=9;%W:Vehicle load capacity T=10; w=[2.5,1.5,1.8,2.0,0.8,1.5,1.0,2.5,3.0,1.7,0.6,0.2,2.4,1.9,2.0,0.7,0.5,2.2,3.1,0.1];%Weight of goods required per customer t=[1.5,3.8,0.5,3,2.6,3.6,1.4,2.4,2,3.4,2,1.2,0.5,0.8,1.3,1.6,1.7,0.5,0.8,1.4];%Volume of goods required by each customer load_w_H=0; load_t_H=0; load_w_S=0; load_t_S=0; Eta=1./D;%Heuristic factor, set as the reciprocal of the distance Tau_H=ones(m,m);%Pheromone matrix Tau_S=ones(m,m); Tabu_H=zeros(Pop,n1+10);%Store and record path generation iter=1; G_best_route_H=[MAXGEN,n1+10];%Best route of each generation G_best_route_S=[MAXGEN,n2+10]; G_best_length_H=zeros(MAXGEN,1); G_best_length_S=zeros(MAXGEN,1); length_ave_H=zeros(MAXGEN,1);%Average length of routes of each generation length_ave_S=zeros(MAXGEN,1); G_best_length=zeros(MAXGEN,1); %Start iteration while iter<=MAXGEN Tabu_H(:,1)=w1*ones(Pop,1); Tabu_S(:,1)=w2*ones(Pop,1); for i=1:Pop visited_H=Tabu_H(i,:); visited_H=visited_H(visited_H>0); to_visit_H=setdiff(H,visited_H); visited_S=Tabu_S(i,:); visited_S=visited_S(visited_S>0); to_visit_S=setdiff(S,visited_S); j=1;d=1; while j<=n1 if ~isempty(to_visit_H) %Select the next factory or return to the warehouse according to the rules x=to_visit_H; for k=1:length(to_visit_H) x(k)=(Tau_H(visited_H(end),to_visit_H(k))^Alpha)*(Eta(visited_H(end),to_visit_H(k))^Beta); end x=x/(sum(x)); %Select the next city according to the principle of probability XC=cumsum(x); Select=find(XC>=rand); if isempty(Select) Select=w1; load_w_H=load_w_H+w(Select); load_t_H=load_w_H+t(Select); else load_w_H=load_w_H+w(to_visit_H(Select(1))); load_t_H=load_t_H+t(to_visit_H(Select(1))); end c1=min((load_w_H)-9,0);c2=min((load_t_H)-10,0); if c1<0&&c2<0 Tabu_H(i,length(visited_H)+1)=to_visit_H(Select(1)); else Select=w1; j=j-1; load_w_H=0; load_t_H=0; Tabu_H(i,length(visited_H)+1)=Select; end end visited_H=Tabu_H(i,:); visited_H=visited_H(visited_H>0); to_visit_H=setdiff(H,visited_H); if visited_H(end)~=w1 Tabu_H(i,1:(length(visited_H)+1))=[visited_H,w1]; end j=j+1; end %Second distribution center while d<=n2 if ~isempty(to_visit_S) %Select the next factory or return to the warehouse according to the rules x=to_visit_S; for k=1:length(to_visit_S) x(k)=(Tau_S(visited_S(end),to_visit_S(k))^Alpha)*(Eta(visited_S(end),to_visit_S(k))^Beta); end x=x/(sum(x)); %Select the next city according to the principle of probability XC=cumsum(x); Select=find(XC>=rand); if isempty(Select) Select=w2; load_w_S=load_w_S+w(Select); load_t_S=load_w_S+t(Select); else load_w_S=load_w_S+w(to_visit_S(Select(1))); load_t_S=load_t_S+t(to_visit_S(Select(1))); end c1=min((load_w_S)-9,0);c2=min((load_t_S)-10,0); if c1<0&&c2<0 Tabu_S(i,length(visited_S)+1)=to_visit_S(Select(1)); else Select=w2; j=j-1; load_w_S=0; load_t_S=0; Tabu_S(i,length(visited_S)+1)=Select; end end figure(1) plot(X(:,1),X(:,2),'o'); hold on plot(X(best_route_H,1),X(best_route_H,2),'o-'); plot(X(best_route_S,1),X(best_route_S,2),'o-'); %plot([X(w1,1),X(w2,1)],[X(w1,2),X(w2,2)],'rs','MarkerSize',9); text([X(w1,1),X(w2,1)],[X(w1,2),X(w2,2)],'\leftarrow Distribution Centre'); for i=1:m text(X(i,1),X(i,2),[' ' num2str(i)]); end figure(2) plot(1:MAXGEN,G_best_length) ; hold on plot(1:MAXGEN,length_ave); xlabel('Number of iterations/second'); ylabel('path length /km'); legend('Change of optimal path length ',' Mean change of path length ');
3, Operation results
4, Remarks
Version: 2014a