1 genetic algorithm
Step 1: the setting of relevant parameters generally includes coding length L, population size M, number of iterations G, crossover rate Pc, mutation rate Pm, etc.
Step 2: the solution in the mathematical model cannot be directly used in the algorithm. The solution needs to be processed to construct the chromosome of the fitness function.
Step 3: M individuals are randomly generated to form the initial population Po, and the individuals are orderly distributed in the solution space. Each individual is a solution corresponding to the problem.
Step 4: calculate the fitness of each individual, and carry out selection and other operations in the population according to the fitness.
Step 5: according to the fitness of each individual, select individuals with high fitness for individual selection and other operations. The commonly used methods include roulette and so on.
Step 6: set the crossover rate Pc to control the crossover operation of the parent chromosome, and the crossed individual will enter the new species group instead of the parent.
Step 7: set the mutation rate Pm and control the mutation operation of the parent chromosome. Mutations can lead to the reappearance of genes that do not appear in the population or are eliminated in the selection process, and mutant individuals replace their parents into the new population.
Step 8: judge whether the updated population after the selection, crossover and mutation operations of the next iteration meets the termination conditions. If not, go back to step 4 for cyclic iteration; If it is satisfied, the algorithm will be stopped, and the maximum number of iterations is usually used as the termination criterion.
Step 9: decode the chromosome that meets the termination conditions to obtain the optimal solution of the problem. According to the above steps, we can know the basic process of genetic algorithm
2 code display
clear clc close all tic %% use importdata This function reads the file % shuju=importdata('cc101.txt'); load('cc101'); shuju=c101; % bl=importdata('103.txt'); bl=3; cap=60; %Maximum vehicle load %% Extract data information E=shuju(1,5); %Start time of distribution center time window L=shuju(1,6); %End time of distribution center time window zuobiao=shuju(:,2:3); %Coordinates of all points x and y pszx=zuobiao(1:4,:); customer=zuobiao(5:end,:); %Customer coordinates cusnum=size(customer,1); %Number of customers v_num=20; %Maximum number of vehicles used demands=shuju(5:end,4); %requirement a=shuju(5:end,5); %Customer time window start time[a[i],b[i]] b=shuju(5:end,6); %End time of customer time window[a[i],b[i]] s=shuju(5:end,7); %Customer service time h=pdist(zuobiao); dist=squareform(h); % dist=load('dist.mat'); % dist=struct2cell(dist); % dist=cell2mat(dist); dist=dist./1000;%The distance matrix satisfies the triangular relationship and temporarily uses the distance to represent the cost c[i][j]=dist[i][j] %% Genetic algorithm parameter setting alpha=100000; %Penalty function coefficients for violating capacity constraints belta=90;%Penalty function coefficient for violation of time window constraint belta2=60; chesu=20; NIND=300; %Population size MAXGEN=1000; %Number of iterations Pc=0.9; %Crossover probability Pm=0.05; %Variation probability GGAP=0.9; %generation gap(Generation gap) N=cusnum+v_num-1; %Chromosome length=Number of customers+Maximum number of vehicles used-1 % N=cusnum; %% Initialize population % init_vc=init(cusnum,a,demands,cap); dpszx = struct('ps',[], 'Chrom',[]); dpszx.Chrom=InitPopCW(NIND,N,cusnum,a,demands,cap); %Construct initial solution ps=pszxxz(dpszx.Chrom,cusnum); %% Route and total distance of output random solution disp('A random value in the initial population:') [VC,NV,TD,violate_num,violate_cus]=decode(dpszx.Chrom(1,:),cusnum,cap,demands,a,b,L,s,dist,chesu,bl); % [VC,NV]=cls(dpszx.Chrom(1,:),cusnum); % [~,~,bsv]=violateTW(VC,a,b,s,L,dist,chesu,bl); % disp(['Total distance:',num2str(TD)]); disp(['Number of vehicles used:',num2str(NV),',Total vehicle distance:',num2str(TD)]); disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') %% optimization gen=1; figure; hold on;box on xlim([0,MAXGEN]) title('Optimization process') xlabel('Algebra') ylabel('optimal value') ObjV=calObj(dpszx.Chrom,cusnum,cap,demands,a,b,L,s,dist,alpha,belta,belta2,chesu,bl,ps); %Calculate the value of population objective function preObjV=min(ObjV); %% while gen<=MAXGEN %% Calculate fitness ObjV=calObj(dpszx.Chrom,cusnum,cap,demands,a,b,L,s,dist,alpha,belta,belta2,chesu,bl,ps); %Calculate the value of population objective function line([gen-1,gen],[preObjV,min(ObjV)]);pause(0.0001)%Drawing optimal function preObjV=min(ObjV); FitnV=Fitness(ObjV); %% choice [SelCh,psc]=Select(dpszx.Chrom,FitnV,GGAP,ps); %% OX Cross operation [SelCh,psc]=Recombin(SelCh,Pc,psc,cusnum); %% variation [SelCh,psc]=Mutate(SelCh,Pm,psc,cusnum); %% New populations of reinsertion Progenies [dpszx.Chrom,ps]=Reins(dpszx.Chrom,SelCh,ObjV,psc,ps); %% Print current optimal solution ObjV=calObj(dpszx.Chrom,cusnum,cap,demands,a,b,L,s,dist,alpha,belta,belta2,chesu,bl,ps); %Calculate the value of population objective function [minObjV,minInd]=min(ObjV); disp(['The first',num2str(gen),'Generation optimal solution:']) [bestVC,bestNV,bestTD,best_vionum,best_viocus]=decode(dpszx.Chrom(minInd(1),:),cusnum,cap,demands,a,b,L,s,dist,chesu,bl); disp(['Number of vehicles used:',num2str(bestNV),',Total vehicle distance:',num2str(bestTD)]); fprintf('\n') %% Update iterations gen=gen+1 ; end %% Draw the road map of the optimal solution ObjV=calObj(dpszx.Chrom,cusnum,cap,demands,a,b,L,s,dist,alpha,belta,belta2,chesu,bl,ps); %Calculate the value of population objective function [minObjV,minInd]=min(ObjV); %% Route and total distance of output optimal solution disp('Optimal solution:') bestChrom=dpszx.Chrom(minInd(1),:); bestps=ps(minInd(1),:); [bestVC,bestNV,bestTD,best_vionum,best_viocus]=decode(bestChrom,cusnum,cap,demands,a,b,L,s,dist,chesu,bl); disp(['Number of vehicles used:',num2str(bestNV),',Total vehicle distance:',num2str(bestTD)]); disp('-------------------------------------------------------------') % [cost]=costFuction(bestVC,a,b,s,L,dist,demands,cap,alpha,belta,belta2,chesu,bl,); %% Draw the final road map draw_Best(bestVC,zuobiao,bestps); % save c101.mat % toc
3 simulation results
Optimal solution:
Number of vehicles used: 20, total distance traveled: 186.7797
Distribution route 1: 2 - > 62 - > 61 - > 60 - > 59 - > 56 - > 54 - > 2
Distribution route 2: 2 - > 39 - > 83 - > 7 - > 5 - > 40 - > 2
Distribution route 3: 1 - > 3 - > 2 - > 1 - > 108 - > 19 - > 12 - > 1
Distribution Route 4: 2 - > 80 - > 79 - > 42 - > 2
Distribution route 5: 1 - > 94 - > 58 - > 57 - > 63 - > 73 - > 71 - > 1
Distribution route 6: 4 - > 105 - > 104 - > 103 - > 102 - > 4
Distribution Route 7: 4 - > 38 - > 31 - > 30 - > 29 - > 87 - > 86 - > 89 - > 88 - > 4
Distribution route 8:1 - > 106 - > 70 - > 69 - > 68 - > 67 - > 65 - > 91 - > 1
Distribution Route 9: 2 - > 16 - > 15 - > 14 - > 13 - > 98 - > 2
Distribution Route 10: 3 - > 53 - > 6 - > 66 - > 78 - > 3
Distribution route 11:1 - > 77 - > 76 - > 75 - > 1
Distribution route 12:1 - > 92 - > 90 - > 85 - > 84 - > 82 - > 1
Distribution route 13:1 - > 95 - > 93 - > 101 - > 100 - > 34 - > 28 - > 27 - > 26 - > 25 - > 1
Distribution route 14:1 - > 33 - > 32 - > 1
Distribution route 15: 3 - > 23 - > 22 - > 21 - > 20 - > 45 - > 44 - > 43 - > 3
Distribution route 16: 2 - > 52 - > 51 - > 50 - > 49 - > 48 - > 2
Distribution Route 17: 4 - > 74 - > 72 - > 97 - > 24 - > 4 - > 18 - > 46 - > 96 - > 4
Distribution route 18:1 - > 107 - > 11 - > 10 - > 1
Distribution route 19:4 - > 99 - > 41 - > 35 - > 37 - > 36 - > 4
Distribution route 20: 3 - > 47 - > 55 - > 9 - > 8 - > 64 - > 17 - > 81 - > 3
Code download https://www.cnblogs.com/matlabxiao/p/14883637.html