fsfs

main.m clc; clear all; global n_point; n_point = 100; %点的个数 Gm = 200;%迭代次数 Np = 20; %种群数 Pc = 0.9; %交叉概率 Pm = 0.1;%变异概率 n = 3; % 路径数 x = ini(Np,n); for j = 1 : Np; [fit(j), tc(j)] = fitness(x(j,:),n); end; for i = 1:Gm; %select x1 = select(x,fit,tc); %crossover x2 = crossover(x1,Pc); %mutation x3 = mutate(x2,Pm,n); %精英选择 for j = 1 : Np; [fit1(j),tc1(j)] = fitness(x3(j,:),n); end; fit2 = [fit, fit1]; tc2 = [tc, tc1]; x4 = [x; x3]; list1 = find(tc2 == 0); if size(list1,2) >= Np; [fit3, index] = sort(fit2(list1),'descend'); fit = fit2(list1(index(1:Np))); tc = tc2(list1(index(1:Np))); x = x4(list1(index(1:Np)),:); else n1 = Np - size(list1,2); list2 = find(tc2 ~= 0); [tc3, index] = sort(tc2(list2)); 22 tc = [tc2(list1), tc2(list2(index(1:n1)))]; fit = [fit2(list1),fit2(list2(index(1:n1)))]; x = x4([list1,list2(index(1:n1))],:); end; if isempty(find(tc == 0)); max_fit(i) = 0; [min_tc(i),index] = min(tc); opt_x(i,:) = x(index,:); else list3 = find(tc == 0); [max_fit(i),index] = max(fit(list3)); min_tc(i) = 0; opt_x(i,:) = x(list3(index),:); end; i min_tc(i) max_fit(i) end; [aa,bb,cc,dd] = fitness(opt_x(Gm,:),n); %画出代数跟最优函数值之间的关系图 figure(1) subplot(2,1,1) plot(1:Gm,max_fit,'b'); subplot(2,1,2) plot (1:Gm,min_tc,'k'); plot_x(opt_x(Gm,:),n) fitness.m function [fit, tc, yy, TT] = fitness(x1,n) load('initial.mat') global n_point; %检查点01向量x end_p = x1((n_point - 3)*n+1); c_p1 = x1((n_point - 3)*n+2); c_p2 = x1((n_point - 3)*n+3); v = 100; %速度,单位m/min x_k = [1,end_p,c_p1,c_p2];%起点终点必经检查点 x_num = 1:n_point; x_num(x_k) = []; for i = 1 : n; x(i,:) = x1((n_point - 4)*i-(n_point - 5):(n_point - 4)*i); 23 end; delta_t = x1((n_point - 4)*n+1:(n_point - 4)*n+n); num = []; %路径y c(1:4) = 0; for k = 1:n; y = []; check_p = [x_num(find(x(k,:))),c_p1,c_p2];%除起点终点外所有检 查点 % check_p1 = check_p; % y(1) = 1; % for i = 2 : size(check_p,2)+1; % [~,index] = min(d(y(i-1),check_p1)); % y(i) = check_p1(index); % check_p1(index) = []; % end; % y(size(check_p,2)+2) = end_p; y = ga_tsp(check_p,end_p); %约束惩罚值 dc(1) = 16 - sum(x(k,:)); dc(2) = 0; for i = 3:size(y,2); [~,index] = min(d(y(i),y(1:i-1))); if index ~= i-1; dc(2) =dc(2) + 1; end; end; T = []; T(1) = 0; total_d(k) = 0;%总长度 total_h(k) = 0;%总难度 %total_hh(k) = 0;%总高程 for i = 2:size(y,2); T(i) = T(i-1) + t(i-1) + d(y(i-1),y(i))/v; total_d(k) = total_d(k) + d(y(i-1),y(i)); total_h(k) = total_h(k) + t(i-1); % if h(i) > h(i-1); % total_hh(k) = total_hh(k) + (h(i) - h(i-1)); % end; end; total_h(k) = total_h(k) + t(end_p); end_T = T(size(y,2)) + t(end_p); 24 dc(2) = 0;%去掉约束2 dc(3) = 2*60 - end_T; dc(4) = end_T - 3*60; num(k) = floor((3*60 - end_T)/delta_t(k)) + 1; if num(k) <= 0; num(k) = 0; end; dc(find(dc<0)) = 0; c(1:4) = c(1:4) + dc(1:4); yy{k} = y; TT{k} = T; end; c(5) = 0; for i = 2:n_point; if i ~= end_p; point_t = []; j = 1; for k = 1:n; if (~isempty((find(yy{k} == i)))); point_t(j:(j+num(k)-1)) = TT{k}(find(yy{k} == i))+(0:(num(k)-1))*delta_t(k); j = j+num(k); end; end; point_t = sort(point_t); if size(point_t,2) >= 5; for m = 5 : size(point_t,2); if (point_t(m) - point_t(m-4)) <= 1; dc(5) = 1 - point_t(m) + point_t(m-4); if dc(5) < 0; dc(5) = 0; end; c(5) = c(5) + dc(5); end; end; end; end; end; total_d_a = mean(total_d); total_h_a = mean(total_h); % total_hh_a = mean(total_hh); c(6) = sum((abs(total_d - total_d_a)/total_d_a)>0.1) + sum((abs(total_h - total_h_a)/total_h_a)>0.1) ;%+sum((abs(total_hh - total_hh_a)/total_hh_a)>0.1); 25 c(7) = 0; for k = 1:n; for m = (k+1):n; n1 = size(yy{k},2); n2 = size(yy{m},2); lujing1 = [(yy{k}(1:n1-1))',(yy{k}(2:n1))']; lujing2 = [(yy{m}(1:n2-1))',(yy{m}(2:n2))']; lujing = [lujing1;lujing2]; lujing3 = unique(lujing,'rows'); n3 = size(lujing3,1); if (n1+n2-2-n3)*2/(n1+n2-2) > 0.2; c(7) = c(7) + 1; end; end; end; if t(end_p) > 2; c(8) = 1; end; fit = sum(num); tc = sum(c); end select.m function x1 = select(x,fit,tc) %锦标赛选择法 n = size(x,1); for i = 1 : n; x2 = randi(n,[1,2]); if tc(x2(1)) == 0 && tc(x2(2)) == 0; [a, b] = max(fit(x2)); x1(i,:) = x(x2(b),:); elseif tc(x2(1)) > 0 && tc(x2(2)) > 0; [a, b] = min(tc(x2)); x1(i,:) = x(x2(b),:); else a = x2(find(tc(x2) == 0)); x1(i,:) = x(a,:); end; end; end crossover.m 26 function x2 = crossover(x1,Pc) n = size(x1,1); index = randperm(n); x1 = x1(index,:); x2 = x1; for i = 1 :2: n-1; r = rand(); if r < Pc for j = 1 : size(x1,2)-3; p = rand(); if p < 0.5; x2(i,j) = x1(i+1,j); x2(i+1,j) = x1(i,j); end; end; x2(i,size(x1,2)-2:size(x1,2)) = x1(i+1,size(x1,2)-2:size(x1,2)); x2(i+1,size(x1,2)-2:size(x1,2)) = x1(i,size(x1,2)-2:size(x1,2)); end; end; end mutate.m function x3 = mutate(x2,Pm,n) [m,k] = size(x2); global n_point; p = rand(m,k); x3 = x2; for i = 1 : m; for j = 1 : n*(n_point - 4); if p(i,j) < Pm; x3(i,j) = 1 - x3(i,j); end; end; for j = n*(n_point - 4)+1 : n*(n_point - 4)+n; if p(i,j) < Pm; x3(i,j) = rand()*5; end; end; if rand()<5*Pm; x3(i,(n_point - 3)*n+1:(n_point - 3)*n+3) = randperm((n_point - 1),3)+1; end; 27 n1 = sum(x3(i,1:n*(n_point - 4))); if n1 > 21*n; r1 = randperm(n1); r2 = r1(1:(n1-21*n)); dp = find(x3(i,1:n*(n_point - 4)) == 1); x3(i,dp(r2)) = 0; end; end; end ga_tsp.m function y1 = ga_tsp(check_p,end_p) load('initial.mat'); city_num = size(check_p,2); maxGEN = 200; popSize = 10; % 遗传算法种群大小 crossoverProbabilty = 0.9; %交叉概率 mutationProbabilty = 0.1; %变异概率 %初始化种群 for i = 1:popSize; check_p1 = check_p; y = []; y(1) = 1; for j = 2 : (city_num+1); [~,index] = sort(d(y(j-1),check_p1)); if size(check_p1,2) >= 3 && i ~= 1; bb= index(randi(3)); else bb = index(1); end; y(j) = check_p1(bb); check_p1(bb) = []; end; y(city_num+2) = end_p; pop(i,:) = y(2:city_num+1); end; fit = fitness1(pop,end_p,d); for i = 1 : maxGEN; %选择 v_sum = sum(1./fit); 28 cc = (1./fit)./v_sum; pc = cumsum(cc,2); for j = 1:popSize; r = rand(); tar = find(pc>=r); pop1(j,:) = pop(tar(1),:); end; %交叉 index = randperm(popSize); pop1 = pop1(index,:); for j = 1:2:(popSize-1); a = pop1(j,:); b = pop1(j+1,:); r = rand(); if r < crossoverProbabilty; L=length(a); r1=randsrc(1,1,[1:L]); r2=randsrc(1,1,[1:L]); if r1~=r2; a0=a;b0=b; s=min([r1,r2]); e=max([r1,r2]); for ii=s:e; a1=a;b1=b; a(ii)=b0(ii); b(ii)=a0(ii); x=find(a==a(ii)); y=find(b==b(ii)); i1=x(x~=ii); i2=y(y~=ii); if ~isempty(i1); a(i1)=a1(ii); end if ~isempty(i2); b(i2)=b1(ii); end; end; end; end; pop2(j,:) = a; pop2(j+1,:) = b; end; %变异 for j = 1:popSize; 29 r = rand(); if r < mutationProbabilty; xx = randperm(city_num); x1 = pop2(j,xx(1)); pop2(j,xx(1)) = pop2(j,xx(2)); pop2(j,xx(2)) = x1; end; end; %精英选择 fit1 = fitness1(pop2,end_p,d); fit2 = [fit,fit1]; [~,index] = sort(fit2); pop3 = [pop;pop2]; fit = fit2(index(1:popSize)); pop = pop3(index(1:popSize),:); [min_fit(i),index1] = min(fit); best_pop(i,:) = pop(index1,:); %min_fit(i) end; y1 = [1,best_pop(maxGEN,:),end_p]; %plot(point(y1,1),point(y1,2),'r*-'); end

posted @ 2023-05-27 01:16  Kaiser_Kell  阅读(44)  评论(0编辑  收藏  举报