Genetic Algorithms
1, the basic idea
2, algorithm principle
3, code implementation
4, the results of screenshots
5, summary
1. The basic idea
Learn the advantages of two algorithms, complementary advantages and disadvantages, to overcome the disadvantages of two algorithms, using a genetic algorithm fast time efficiency, time efficiency than ant algorithm. Efficiency and solution accuracy is better than genetic algorithm. This improves the algorithm time efficiency and accuracy of the two algorithms for solving bound.
2, algorithm principle
The principle is to use fast algorithm genetic algorithm, global convergence and the randomness of the results obtained, resulting in the initial information about the distribution of prime problems, genetic algorithm executing the ant colony algorithm in use, certain elements in the initial distribution of information case, full parallelism with AA, positive feedback, the efficiency of solving the high precision characteristics.
3, code implementation
main% Clear; CLC; %%%%%%%%%%%%%%% input parameter %%%%%%%% N = 50; number of cities %% M = 100; %% two populations number ITER = 500; %% number of iterations % C_old = C; m = 2; %% out adaptation values were normalized acceleration index Pc = 0.8; %% crossover probability Pmutation = 0.05; %% mutation probability %% generated coordinate city = randn POS (N, 2); %% generating a distance matrix between cities D = zeros (N, N); for I =. 1: N for J = I +. 1: N DIS = (POS (I,. 1) - POS (J,. 1)) ^ 2+ (POS (I, 2) -POS (J, 2)) ^ 2;.. D (I, J) = ^ DIS (0.5); D (J, I) = D (I, J); End End %% generating an initial population POPM = zeros (M, N); for I =. 1: M POPM (I,:) = randperm (N);% random arrangement, such as [245 613] End %% randomly select a population POPM = R & lt (. 1, :); Figure (. 1); Scatter (POS (:,. 1), POS (:, 2), 'RX');% draw all cities coordinate axis ([- 3 3 -3 3 ]); Figure (2); plot_route (POS, R & lt); %% drawing connecting lines between the cities corresponding to the initial population Axis ([- -3. 3. 3. 3]); %% initial population fitness function and fitness zeros = (M,. 1); len = zeros (M,. 1); for I =. 1:% M calculated for each chromosome corresponding to the total length len (i, 1) = myLength (D, popm (i, :)) ; End the maxlen = max (len);% maximum loop minlen = min (len);% minimum loop Fitness Fit = (len, m, the maxlen, the minlen); RR = find (len == the minlen); found% minimum subscript assigned RR R = POPM (RR (1,1),:);% extracting the chromosomal assignment of R for I =. 1: N fprintf ( '% D', R (I)); the R% sequentially printed End fprintf ( '\ n-'); nn = M; Fitness = Fitness / SUM (Fitness); distance_min = zeros (ITER + 1,1) ; %% minimum population of each iteration of the total length of the path A = popm_sel (nnper (I), :); = 0 ITER; the while ITER <= the ITER fprintf ( 'Iterative% d times \ n-', ITER); %% selection operation P = Fitness / SUM (Fitness);. Q = cumsum (P);% accumulated for i = . 1: (. 1-M) LEN_1 (I,. 1) = myLength (D, POPM (I, :)); R & lt = RAND; tmp = Find (R & lt <= Q); popm_sel (I,:) = POPM (tmp (. 1), :); End [Fmax, Indmax] = max (Fitness);% find the best contemporary individual popm_sel (M,:) = POPM (Indmax, :); %% crossover nnper = randperm (M); A = popm_sel% (nnper (. 1), :); % B = popm_sel (nnper (2), :); %% for I =. 1: M Pc * 0.5 * B = popm_sel (nnper (I +. 1) ,: ); [A, B] = Cross (A, B); % popm_sel (nnper (. 1),:) = A; % popm_sel(nnper(2),:)=B; popm_sel(nnper(i),:)=A; popm_sel(nnper(i+1),:)=B; end %%变异操作 for i=1:M pick=rand; while pick==0 pick=rand; end if pick<=Pmutation popm_sel(i,:)=Mutation(popm_sel(i,:)); end end %%求适应度函数 NN=size(popm_sel,1); len=zeros(NN,1); for i=1:NN len(i,1)=myLength(D,popm_sel(i,:)); end maxlen=max(len); minlen=min(len); distance_min(iter+1,1)=minlen; fitness=fit(len,m,maxlen,minlen); rr=find(len==minlen); fprintf ( 'the minlen D =% \ n-', the minlen); R & lt popm_sel = (RR (1,1), :); for I =. 1: N fprintf ( '% D', R & lt (I)); End fprintf ( '\ n-'); POPM = []; POPM = popm_sel; ITER = ITER +. 1; % PAUSE (. 1); End % End of the while Figure (. 3) plot_route (POS, R & lt); Axis ([-. 3. 3 - . 3. 3]); Figure (. 4) Plot (distance_min); % crossover function cross.m function [A, B] = cross (A, B) L = length (A); IF L <10 W is = L; ELSEIF ((L / 10) -floor (L / 10))> = RAND && L> 10 W is = ceil (L / 10) +8; the else W is = Floor (L / 10) +8; end %% W is required for the cross-bit number p = unidrnd (L-W + 1);% randomly generates a crossing position %fprintf('p=%d ',p);%交叉位置 for i=1:W x=find(A==B(1,p+i-1)); y=find(B==A(1,p+i-1)); [A(1,p+i-1),B(1,p+i-1)]=exchange(A(1,p+i-1),B(1,p+i-1)); [A(1,x),B(1,y)]=exchange(A(1,x),B(1,y)); end end %连点画图函数 plot_route.m function plot_route(a,R) scatter(a(:,1),a(:,2),'rx'); hold on; plot([a(R(1),1),a(R(length(R)),1)],[a(R(1),2),a(R(length(R)),2)]); hold on; for i=2:length(R) x0=a(R(i-1),1); y0=a(R(i-1),2); x1=a(R(i),1); y1=a(R(i),2); xx=[x0,x1]; yy=[y0,y1]; plot(xx,yy); hold on; End End % of chromosomes from the cost function mylength.m function myLength len = (D, P) P is a permutation% [N, NN] = size (D); len = D (P (. 1, N), P (. 1 ,. 1)); for I =. 1: (. 1-N) len = len + D (P (. 1, I), P (. 1,. 1 + I)); End End % variogram Mutation.m function A = mutation (A) index1, = 0; index2 = 0; nnper = randperm (size (A, 2)); index1, = nnper (. 1); index2 = nnper (2); % fprintf ( 'index1, =% D', index1,); % fprintf ( 'index2 =% D', index2); TEMP = 0; TEMP = A (index1,); A (index1,) = A (index2); A (index2) = TEMP; A = A; End % fitness function fit.m, each iteration is calculated for each chromosome priority within this population, similar to the normalization parameters. The larger the appointment! function fitness = fit (len, m , maxlen, minlen) len = Fitness; for I =. 1: length (len) fitness(i,1)=(1-(len(i,1)-minlen)/(maxlen-minlen+0.0001)).^m; end %对调函数 exchange.m function [x,y]=exchange(x,y) temp=x; x=y; y=temp; end
4, the results of screenshots
Figure 1 Figure 2
3 FIG. 4 FIG.
Figure 5 Figure 6
7 8
图9 图10
下标是基数的图是迭代200次的,分别是城市的坐标图,初始种群对应各城市之间的连线图,最佳路径图,路径长度图。下标是偶数的是迭代500次的,我定义的城市个数是25个,种群的个数是100个,交查概率为0.8.迭代200次的最佳路径是17 19 1 21 18 4 16 11 8 6 3 5 14 20 2 23 22 24 15 9 10 13 7 25 12 ,路径长度是1.858666e+01,迭代500次的最佳路径是11 6 15 20 8 7 12 5 24 25 22 18 19 23 2 1 21 14 10 13 16 3 17 9 4 ,最短路径是1.733842e+01。有上述可看出,当尘世个数、种群个数、交叉概率相等时,迭代的次数越多,则算出来的路径长度越短,路径也不相同。
图11 图12
图13 图14
迭代第500次
minlen=3.422104e+01
43 47 33 30 5 49 27 42 9 28 32 16 11 40 31 3 39 20 44 13 4 38 1 14 23 8 29 22 6 10 24 19 18 45 7 15 48 37 12 26 41 17 25 21 2 34 50 46 36 35
对比上面的,改变城市数量,路径变大。
图15 图16
图17 图18
minlen=3.923642e+01
35 10 48 9 20 19 16 23 24 8 7 12 28 30 44 38 13 33 2 1 14 26 47 34 41 18 39 22 3 27 5 40 6 25 45 11 49 15 21 17 32 29 4 37 36 43 50 42 46 31
以上的图,我改变的是变异概率,改为了0.12,原来的是0.05,对比图11 12 13 14发现最小路径变大,最终形成的最短路径图很不清晰,由此看出,变异概率会影响最短路径的长度和最终路径。
我根据以上的实验,改变参数交叉概率,原来是0.8改为了0.5,发现最小路径在变大。由此看出交叉概率会影响最短路径的长度和最终路径。
5、总结
5.1这两个算法的结合提高了算法的时间性能和优化性能,可以快速明显的看出实验的区别。
5.2两个算法结合起来,减少了参数的调整,避免了大量盲目的去迭代次数。
5.3在遗传算法中产生种群,加快了蚂蚁算法的速度避免了求精确解阶段陷入局部最优。