1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
clear close clc
t0 = clock;
load sj.txt
x=sj(:,1:2:8); x=x(:); y=sj(:,2:2:8); y=y(:); sj=[x y]; d1=[70,40]; sj0=[d1;sj;d1]; sj=sj0*pi/180;
d=zeros(102); for i=1:101 for j=i+1:102 temp=cos(sj(i,1)-sj(j,1))*cos(sj(i,2))*cos(sj(j,2))+sin(sj(i,2))*sin(sj(j,2)); d(i,j)=6370*acos(temp); end end d=d+d';
n = size(sj0,1); D = d;
m = 120; alpha = 1; beta = 5; vol = 0.2; Q = 10; Heu_F = 1./D; Tau = ones(n,n); Table = zeros(m,n); iter = 1; iter_max = 50; Route_best = zeros(iter_max,n); Length_best = zeros(iter_max,1); Length_ave = zeros(iter_max,1); Limit_iter = 0;
while iter <= iter_max start = zeros(m,1); for i = 1:m temp = randperm(n); start(i) = temp(1); end Table(:,1) = start; citys_index = 1:n; for i = 1:m for j = 2:n tabu = Table(i,1:(j - 1)); allow_index = ~ismember(citys_index,tabu); allow = citys_index(allow_index); P = allow; for k = 1:length(allow) P(k) = Tau(tabu(end),allow(k))^alpha * Heu_F(tabu(end),allow(k))^beta; end P = P/sum(P); Pc = cumsum(P); target_index = find(Pc >= rand); target = allow(target_index(1)); Table(i,j) = target; end end Length = zeros(m,1); for i = 1:m Route = Table(i,:); for j = 1:(n - 1) Length(i) = Length(i) + D(Route(j),Route(j + 1)); end Length(i) = Length(i) + D(Route(n),Route(1)); end if iter == 1 [min_Length,min_index] = min(Length); Length_best(iter) = min_Length; Length_ave(iter) = mean(Length); Route_best(iter,:) = Table(min_index,:); Limit_iter = 1;
else [min_Length,min_index] = min(Length); Length_best(iter) = min(Length_best(iter - 1),min_Length); Length_ave(iter) = mean(Length); if Length_best(iter) == min_Length Route_best(iter,:) = Table(min_index,:); Limit_iter = iter; else Route_best(iter,:) = Route_best((iter-1),:); end end Delta_Tau = zeros(n,n); for i = 1:m for j = 1:(n - 1) Delta_Tau(Table(i,j),Table(i,j+1)) = Delta_Tau(Table(i,j),Table(i,j+1)) + Q/Length(i); end Delta_Tau(Table(i,n),Table(i,1)) = Delta_Tau(Table(i,n),Table(i,1)) + Q/Length(i); end Tau = (1-vol) * Tau + Delta_Tau; iter = iter + 1; Table = zeros(m,n); end
[Shortest_Length,index] = min(Length_best); Shortest_Route = Route_best(index,:); Time_Cost=etime(clock,t0); disp(['最短距离:' num2str(Shortest_Length)]); disp(['最短时间:' num2str(Shortest_Length/1000) '小时']); disp(['收敛迭代次数:' num2str(Limit_iter)]); disp(['程序执行时间:' num2str(Time_Cost) '秒']);
figure(1)
plot(x,y,'bO') hold on plot(70,40,'p',... 'MarkerSize',10,... 'MarkerEdgeColor','b',... 'MarkerFaceColor','r') hold on
plot([sj0(Shortest_Route,1);sj0(Shortest_Route(1),1)],... [sj0(Shortest_Route,2);sj0(Shortest_Route(1),2)],'o-'); grid on hold off xlabel('经度') ylabel('纬度') title(['ACA最优化侦查路线(侦查时间:' num2str(Shortest_Length/1000) '小时)']) figure(2)
plot(1:iter_max,Length_best/1000,'b') legend('侦查最短时间') xlabel('迭代次数') ylabel('时间') title('算法收敛轨迹')
|