├── ACOTW.m ├── ACO_creation.m ├── GA_population.m └── README.md /ACOTW.m: -------------------------------------------------------------------------------- 1 | function out = ACOTW(car,pos,twcomb) 2 | % prompt = 'Enter the number of passengers = '; 3 | % N = input(prompt); 4 | % pos=100*rand(N*2,2); 5 | % clear all 6 | % clc 7 | % pos=[5.62618912427925,72.2143728021138;44.3679286866576,82.9293071878729;29.6470403866946,24.0455898905690;0.0425542411259583,49.3188166862448;98.8521370163346,2.86878517907319;87.4655301269997,82.5943040673200;14.2617146436496,80.3818800669476;85.0566509436744,12.6219189828099;44.3783933010078,93.1199385332450;90.4875444571824,99.2084006947154;92.7397168816461,88.2598909045976;46.9534457195288,21.9782432431265;71.3354928154916,87.7083107253795;9.80398116732768,99.5052736198553;51.4412744586874,17.2349553305288;69.4834012315226,82.5662827388037;84.3986878655096,30.1815834398412;81.5686052867703,92.6136003928314;14.3009964850661,27.5207701089181;75.8341289433974,77.8374071244509]; 8 | % pos=[94.4365048138129,60.9792045709351;53.3989167664674,40.4214791574910;94.5952652761107,23.2318927975681;23.1650581282914,21.3948728531963;74.9694224876883,63.9074574528537;37.5944867961994,19.7053397090811;66.2295777417929,61.2069769012359;98.2708463182729,37.5963945444074;92.6782767130573,59.0935070157827;21.2231583804602,83.4996926162185]; 9 | % pos = [40,100,100,100,0,80,70,30,0,0; 0,0,30,50,100,0,100,100,80,30]'; 10 | % time=0; 11 | % car=[89.8240902727620,41.9334745300729]; 12 | % car=[43.3783933010078 92.1199385332450]; 13 | % xp=pos(1:N,1); 14 | % yp=pos(1:N,2); 15 | % xd=pos(N+1:2*N,1); 16 | % yd=pos(N+1:2*N,2); 17 | N = length(pos(:,1))/2; 18 | x = [car(1); pos(:,1)]; 19 | y = [car(2); pos(:,2)]; 20 | n = numel(x); 21 | 22 | 23 | timespace= twcomb; % change this for function 24 | 25 | 26 | 27 | % N=(n-1)/2; 28 | for i=1:n-1 29 | for j=i+1:n 30 | D(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2); 31 | D(j,i)=D(i,j); 32 | end 33 | end 34 | % for k=1:2*N-1 35 | % time = time+D(k+1,k); 36 | % end 37 | % time = time+ D(k+1,1); 38 | 39 | % for i = 1:2*N 40 | % timespace(i)=rand(1)*0.7*time; 41 | % end 42 | % % timespace=sort(timespace); 43 | % timespace=[52.2905797700914,10.1906367183525,117.638991189021,75.8571550062872,207.868653750089,371.994487617707,40.4044333837806,190.973712052215,0,353.076197825428,364.253950160035,135.068437184377,330.635849561065,454.515751978195,257.408301844122,392.478967799234,173.401724934767,341.983909200116,101.904381534482,384.561013073019]; 44 | % timepool = 1:length(timespace); 45 | % timespace= [62.8639308188104;191.659578080091;144.480832889154;66.3807021973016;26.9756154588469;190.741651969206;80.2801103910090;225.261029255319;113.453927804677;161.408193666687]; 46 | 47 | % 48 | twcomb=[0,20]; 49 | for i = 1:2*N 50 | tol=20; 51 | twcomb=[twcomb; timespace(i)-tol, timespace(i)+tol]; 52 | % twcomb = [twcomb; 0 300]; 53 | end 54 | val.n=n; 55 | val.x=x; 56 | val.y=y; 57 | val.D=D; 58 | 59 | Cost=@(tour) TourLength(tour,val); 60 | 61 | nVar=val.n; 62 | % conditions 63 | it = 80; 64 | Ant =5*val.n; % check for number of `ants 65 | Q = 100; 66 | tau0=10^0; 67 | alpha=1; 68 | beta=0; 69 | e = 5; 70 | gamma=1; 71 | rho=0.3; % evapouration rate 72 | 73 | 74 | % Initialization 75 | eta=1./val.D; 76 | tau=tau0*ones((nVar),(nVar)); 77 | Bestpath=zeros(it,1); 78 | empty_ant.Tour=[]; 79 | empty_ant.Cost=[]; 80 | ant=repmat(empty_ant,Ant,1); 81 | BestSol.Cost=inf; 82 | out.Cost = 0; 83 | 84 | % ACO Main Loop 85 | 86 | for it=1:it 87 | 88 | % Move Ants 89 | shit = 0; 90 | for k=1:Ant 91 | % disp(k) 92 | % pool for requests 93 | nonpool = N+2:(2*N)+1; 94 | ant(k).Tour=1; 95 | pool = 1:N+1; 96 | % nonpool(nonpool==ant(k).Tour(end)+N)=[]; 97 | omega=[]; 98 | troll = 1; 99 | 100 | for l=2:2*(N)+1 101 | i=ant(k).Tour(end); 102 | for a1 = 1: 2*N+1 103 | if twcomb(a1,2)>= timecost(ant(k).Tour,twcomb,D)+ D(i,a1) 104 | omega(i,a1)= exp((((timecost(ant(k).Tour,twcomb,D)+ D(i,a1)-twcomb(a1,1)))/(twcomb(a1,2)-(timecost(ant(k).Tour,twcomb,D) + D(i,a1))+1))); 105 | else 106 | if a1 ~= 1 107 | end 108 | omega(i,a1) = 0; 109 | end 110 | end 111 | % disp(omega) 112 | % P1=tau(i,:).^alpha.*eta(i,:).^beta; 113 | % max(omega(i,:)) 114 | P=(tau(i,:).^alpha.*eta(i,:).^beta.*omega(i,:).^gamma); 115 | % P=tau(i,:).^alpha.*omega(i,:).^gamma; 116 | % P=[1 P]; 117 | % disp(P) 118 | P(ant(k).Tour)=0; 119 | P(nonpool)=0; 120 | % if max(P)==inf 121 | % disp(P) 122 | % end 123 | if sum(P) == 0 124 | troll = 0; 125 | shit=shit+1; 126 | % disp(l) 127 | break 128 | end 129 | ing = P; 130 | P=(P/sum(P)); 131 | j=RouletteWheel(P); 132 | if isempty(j) 133 | j = find(ing==inf); 134 | end 135 | ant(k).Tour=[ant(k).Tour j]; 136 | % disp(j) 137 | if j<=N+1 138 | pool = [pool j+N]; 139 | end 140 | if isempty(j) 141 | end 142 | nonpool(nonpool==j+N)=[]; 143 | end 144 | if troll == 0 145 | troll = 1; 146 | 147 | else 148 | ant(k).Cost=timecost(ant(k).Tour,twcomb,D); 149 | % disp(k) 150 | if ant(k).Cost 1000000 178 | out.Cost = 500000; 179 | out.Tour = []; 180 | break 181 | else 182 | for k=1:Ant 183 | % if length(ant(k).Tour)== (2*N)+1 184 | tour=ant(k).Tour; 185 | bst = BestSol.Tour; 186 | % tour=[tour tour(1)]; 187 | if isempty(ant(k).Cost) 188 | ant(k).Cost = inf; 189 | end 190 | % bst=[bst bst(1)]; 191 | for l=1:length(tour)-1 192 | i=tour(l); 193 | j=tour(l+1); 194 | % ind = find(bst==i,1); 195 | % if (j==bst(ind+1)) 196 | % tau(i,j)=(1-rho)*tau(i,j)+ Q/ant(k).Cost + (e*Q/BestSol.Cost); 197 | % else 198 | tau(i,j)=(1-rho)*tau(i,j)+Q/ant(k).Cost; 199 | % end 200 | end 201 | % end 202 | end 203 | end 204 | Bestpath(it)=BestSol.Cost; 205 | out.Tour=[]; 206 | % disp(['Iteration ' num2str(it) ': Best path length = ' num2str(Bestpath(it))]); 207 | % figure(1); 208 | % tour=[BestSol.Tour BestSol.Tour(1)]; 209 | % % disp(BestSol.Tour) 210 | % plot(val.x(tour),val.y(tour),'-s','MarkerSize',10,... 211 | % 'MarkerEdgeColor','red',... 212 | % 'MarkerFaceColor',[1 .6 .6]) 213 | % 214 | % xlabel('x'); 215 | % ylabel('y'); 216 | % grid on; 217 | % drawnow; 218 | % Ant = 2*val.n; 219 | end 220 | % Results 221 | % figure; 222 | % plot(Bestpath) 223 | % xlabel('Iteration number'); 224 | % ylabel('length of Best Tour'); 225 | % grid on; 226 | 227 | 228 | 229 | % check=interval(BestSol.Tour,D); 230 | % if isempty(BestSol.Tour) 231 | % out.cool = 0; 232 | % else 233 | % checkmat = timecostmat(BestSol.Tour,twcomb,D); 234 | % cool=0; 235 | % 236 | % for i = 2:length(checkmat) + 1 237 | % checklist(BestSol.Tour(i)-1)=checkmat(i-1); 238 | % end 239 | % 240 | % for i = 1:length(checkmat) 241 | % if checklist(i) > twcomb(i+1,1) && checklist(i) < twcomb(i+1,2) 242 | % cool = cool +1; 243 | % end 244 | % end 245 | % 246 | % out.cool = cool; 247 | % end 248 | % timecostbest = timecost(BestSol.Tour,twcomb,D); 249 | % for i=1:length(tour)-1 250 | % if check(i)<=twcomb(tour(i),2) && check(i)>=twcomb(tour(i),1) 251 | % 252 | % dire(i)= 1; 253 | % else 254 | % dire(i)= 0; 255 | % end 256 | % end 257 | 258 | if out.Cost == 500000 259 | else 260 | out.Cost = BestSol.Cost; 261 | 262 | end 263 | 264 | end 265 | 266 | function L=TourLength(tour,model) 267 | 268 | n=numel(tour); 269 | 270 | % tour=[tour tour(1)]; 271 | 272 | L=0; 273 | for i=1:n-1 274 | L=L+model.D(tour(i),tour(i+1)); 275 | end 276 | 277 | end 278 | function j=RouletteWheel(P) 279 | 280 | r=rand; 281 | 282 | C=cumsum(P); 283 | 284 | j=find(r<=C,1,'first'); 285 | 286 | % k=max(P); 287 | % j=find(P==k); 288 | 289 | end 290 | 291 | function out = interval(tour,D) 292 | out=zeros(1,length(tour(1:end-1))); 293 | for i =1 :length(tour(1:end-1)) 294 | for k = 1:i-1 295 | out(i)=D(tour(k),tour(k+1))+out(i); 296 | end 297 | end 298 | end 299 | 300 | function out = timecost(tour,twcomb,D) 301 | out=0; 302 | for i =1:length(tour)-1 303 | if out >= twcomb(tour(i),1) && out <= twcomb(tour(i),2) 304 | out = out+D(tour(i),tour(i+1)); 305 | elseif out < twcomb(i,1) 306 | out = twcomb(i,1); 307 | else 308 | out =inf; 309 | end 310 | end 311 | end 312 | 313 | function outmat = timecostmat(tour,twcomb,D) 314 | out=0; 315 | outmat=[]; 316 | for i =1:length(tour)-1 317 | if out >= twcomb(tour(i),1) && out <= twcomb(tour(i),2) 318 | out = out+D(tour(i),tour(i+1)); 319 | elseif out < twcomb(i,1) 320 | out = twcomb(i,1); 321 | else 322 | out =inf; 323 | end 324 | outmat=[outmat out]; 325 | end 326 | end -------------------------------------------------------------------------------- /ACO_creation.m: -------------------------------------------------------------------------------- 1 | 2 | clc; 3 | clear all; 4 | close all; 5 | % prompt = 'Enter the number of passengers = '; 6 | % N = input(prompt); 7 | carmat = [2 3]; 8 | cars=[]; 9 | pcombpos=[]; 10 | dcombpos=[]; 11 | carnum=[]; 12 | combtimespace=[]; 13 | for loops = 1:3 14 | N = carmat(randi(1)); 15 | pos=[]; 16 | eta=[]; 17 | tau=[]; 18 | val=[]; 19 | timespace=[]; 20 | tour=[]; 21 | car=100*rand(1,2); 22 | pos=100*rand(N*2,2); 23 | D=[]; 24 | 25 | 26 | % pos=[5.62618912427925,72.2143728021138;44.3679286866576,82.9293071878729;29.6470403866946,24.0455898905690;0.0425542411259583,49.3188166862448;98.8521370163346,2.86878517907319;87.4655301269997,82.5943040673200;14.2617146436496,80.3818800669476;85.0566509436744,12.6219189828099;44.3783933010078,93.1199385332450;90.4875444571824,99.2084006947154;92.7397168816461,88.2598909045976;46.9534457195288,21.9782432431265;71.3354928154916,87.7083107253795;9.80398116732768,99.5052736198553;51.4412744586874,17.2349553305288;69.4834012315226,82.5662827388037;84.3986878655096,30.1815834398412;81.5686052867703,92.6136003928314;14.3009964850661,27.5207701089181;75.8341289433974,77.8374071244509]; 27 | % pos = [72.7349580929689,90.6673424927724;45.9411338748033,88.2143323876479;23.5417502459506,94.7244907813225;70.3620501148626,41.0331547317111;90.3354665098209,56.2650043097417;37.8988673199119,46.3829004866527;34.7316578852631,17.0068281107392;40.6624667905148,84.3897472719139;93.3109383399309,48.1242487160148;18.2816974646092,16.4297491328674;47.0264675147589,77.4953244442090;0.0149817115251194,26.8802609576352;12.9875852605737,38.6217779666874;80.2092466507340,34.7706298031206;66.5129707486672,39.1254594175180;22.5124129029250,73.6064329605265;22.7360302058110,78.8034489268214;95.1170741827675,18.5474076468766;48.8997389329240,0.540358304318545;66.9987348780218,14.4508856229825]; 28 | % xp=pos(1:N,1); 29 | % yp=pos(1:N,2); 30 | % xd=pos(N+1:2*N,1); 31 | % yd=pos(N+1:2*N,2); 32 | 33 | x = [car(1); pos(:,1)]; 34 | y = [car(2); pos(:,2)]; 35 | n = numel(x); 36 | for i=1:n-1 37 | for j=i+1:n 38 | D(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2); 39 | D(j,i)=D(i,j); 40 | end 41 | end 42 | val.n=n; 43 | val.x=x; 44 | val.y=y; 45 | val.D=D; 46 | 47 | Cost=@(tour) TourLength(tour,val); 48 | 49 | nVar=val.n; 50 | % conditions 51 | it = 20; 52 | Ant = val.n; % check for number of `ants 53 | Q = 100; 54 | tau0=10^-6; 55 | alpha=1; 56 | beta=5; 57 | rho=0.7; 58 | e = 5; 59 | 60 | 61 | % Initialization 62 | eta=1./val.D; 63 | tau=tau0*ones(nVar,nVar); 64 | Bestpath=zeros(it,1); 65 | empty_ant.Tour=[]; 66 | empty_ant.Cost=[]; 67 | ant=repmat(empty_ant,Ant,1); 68 | BestSol.Cost=inf; 69 | 70 | 71 | % ACO Main Loop 72 | 73 | for it=1:it 74 | 75 | % Move Ants 76 | for k=1:Ant 77 | % pool for requests 78 | nonpool = N+2:(2*N)+1; 79 | ant(k).Tour = 1; 80 | % ant(k).Tour=randi([1 N]); 81 | pool = 1:N+1; 82 | % nonpool(nonpool==ant(k).Tour(end)+N)=[]; 83 | for l=2:(2*N)+1 84 | i=ant(k).Tour(end); 85 | P=tau(i,:).^alpha.*eta(i,:).^beta; 86 | P(ant(k).Tour)=0; 87 | P(nonpool)=0; 88 | P=P/sum(P); 89 | j=RouletteWheel(P); 90 | ant(k).Tour=[ant(k).Tour j]; 91 | if j<=N 92 | pool = [pool j+N]; 93 | end 94 | nonpool(nonpool==j+N)=[]; 95 | end 96 | ant(k).Cost=Cost(ant(k).Tour); 97 | if ant(k).Cost 3 45 | parent(i).mat = correction(parent(i).mat); 46 | % l=l+1; 47 | % disp(max(sum(child(i).mat'))) 48 | max_sum_parent = max(sum(parent(i).mat')); 49 | end 50 | 51 | end 52 | for i = 1 : population 53 | parent(i).tour = tournum(parent(i).mat,C,P); 54 | mat11 = []; 55 | temptw=[]; 56 | temp=[]; 57 | for k = 1: C 58 | mat11 = cell2mat(struct2cell(parent(i).tour(k))); 59 | for g1 = 1:length(mat11) 60 | mat11 = [mat11 mat11(g1)+length(combpos)/2]; 61 | end 62 | temp = routetoxy(mat11,pos,P); 63 | for g = 1:(length(mat11)) 64 | % if g <= (length(mat11)/2) 65 | temptw(g,1) = combtimespace(mat11(g)); 66 | % else 67 | % temptw(g,1) = combtimespace(mat11(g)+length(mat11)) ; 68 | % end 69 | end 70 | % child(i).xy(:,1,k)=temp(:,1); 71 | % child(i).xy(:,2,k)=temp(:,2); 72 | if isempty(temp) 73 | opttourlength(k) = 0; 74 | else 75 | out = ACOTW(cars(k,:),temp,temptw); 76 | opttourlength(k)=out.Cost; 77 | parent(i).tour(k).xy=out.Tour; 78 | end 79 | end 80 | parent(i).tourlength=sum(opttourlength); 81 | end 82 | T = struct2table(parent); 83 | sortedT = sortrows(T, 'tourlength'); 84 | parent = table2struct(sortedT); 85 | for iter = 1 : itern 86 | tourlengthmat=[]; 87 | opttourlength=[]; 88 | child=[]; 89 | inctour=[]; 90 | nextchild=[]; 91 | temp=[]; 92 | tempchild=[]; 93 | out=[]; 94 | % s1=sum(parent(1).mat'); 95 | % s2=sum(parent(2).mat'); 96 | % for i= 1:(population) % check for second iteration for child 3 and 4 97 | % max_sum_parent = max(sum(parent(i).mat')); 98 | % while max_sum_parent> 5 99 | % parent(i).mat = correction(parent(i).mat); 100 | % % l=l+1; 101 | % % disp(max(sum(child(i).mat'))) 102 | % max_sum_parent = max(sum(parent(i).mat')); 103 | % end 104 | % max_sum_parent = max(sum(parent(i).mat)); 105 | % min_sum_parent = min(sum(parent(i).mat)); 106 | % while max_sum_parent ~= 1 || min_sum_parent == 0 107 | % parent(i).mat = horicorrection(parent(i).mat); 108 | % % l=l+1; 109 | % % disp(max(sum(child(i).mat'))) 110 | % max_sum_parent = max(sum(parent(i).mat)); 111 | % min_sum_parent = min(sum(parent(i).mat)); 112 | % end 113 | % 114 | % end 115 | 116 | % for i = 1 : population 117 | % parent(i).tour = tournum(parent(i).mat,C,P); 118 | % mat11 = []; 119 | % temptw=[]; 120 | % temp=[]; 121 | % for k = 1: C 122 | % mat11 = cell2mat(struct2cell(parent(i).tour(k))); 123 | % for g1 = 1:length(mat11) 124 | % mat11 = [mat11 mat11(g1)+length(combpos)/2]; 125 | % end 126 | % temp = routetoxy(mat11,pos,P); 127 | % for g = 1:(length(mat11)) 128 | % % if g <= (length(mat11)/2) 129 | % temptw(g,1) = combtimespace(mat11(g)); 130 | % % else 131 | % % temptw(g,1) = combtimespace(mat11(g)+length(mat11)) ; 132 | % % end 133 | % end 134 | % % child(i).xy(:,1,k)=temp(:,1); 135 | % % child(i).xy(:,2,k)=temp(:,2); 136 | % if isempty(temp) 137 | % opttourlength(k) = 0; 138 | % else 139 | % out = ACOTW(cars(k,:),temp,temptw); 140 | % opttourlength(k)=out.Cost; 141 | % parent(i).tour(k).xy=out.Tour; 142 | % end 143 | % end 144 | % parent(i).tourlength=sum(opttourlength); 145 | % end 146 | T = struct2table(parent); 147 | sortedT = sortrows(T, 'tourlength'); 148 | parent = table2struct(sortedT); 149 | 150 | for j = 1:2: new_generation 151 | a1= randi(fitness_selection); 152 | a2= randi(fitness_selection); 153 | if rand(1)>0 154 | out = horisplit(parent(a1).mat,parent(a2).mat); 155 | else 156 | out = vertisplit1(parent(a1).mat,parent(a2).mat); 157 | end 158 | if rand(1)<0.7 159 | child(j).mat=mutation(out.child1); 160 | child(j+1).mat=mutation(out.child2); 161 | else 162 | child(j).mat=out.child1; 163 | child(j+1).mat=out.child2; 164 | end 165 | % child(population+j).mat=parent(j).mat; 166 | % child(population+j+1).mat=parent(j+1).mat; 167 | end 168 | childi = child; 169 | for i= 1:(new_generation) % check for second iteration for child 3 and 4 170 | 171 | max_sum_child = max(sum(child(i).mat)); 172 | min_sum_child = min(sum(child(i).mat)); 173 | while max_sum_child ~= 1 || min_sum_child == 0 174 | child(i).mat = horicorrection(child(i).mat); 175 | % l=l+1; 176 | % disp(max(sum(child(i).mat'))) 177 | max_sum_child = max(sum(child(i).mat)); 178 | min_sum_child = min(sum(child(i).mat)); 179 | end 180 | max_sum_child = max(sum(child(i).mat')); 181 | while max_sum_child > 3 182 | child(i).mat = correction(child(i).mat); 183 | % l=l+1; 184 | % disp(max(sum(child(i).mat'))) 185 | max_sum_child = max(sum(child(i).mat')); 186 | end 187 | 188 | end 189 | 190 | thin = []; 191 | thick = []; 192 | 193 | for i = 1:new_generation 194 | thin(i) = max(sum(child(i).mat)); 195 | thinner(i) = min(sum(child(i).mat)); 196 | thick(i) = max(sum(child(i).mat')); 197 | if thinner(i) == 0 || thin(i) ~= 1 || thick(i) > 5 198 | end 199 | end 200 | 201 | for i = 1 : new_generation 202 | child(i).tour = tournum(child(i).mat,C,P); 203 | mat11 = []; 204 | temptw=[]; 205 | temp=[]; 206 | for k = 1: C 207 | mat11 = cell2mat(struct2cell(child(i).tour(k))); 208 | for g1 = 1:length(mat11) 209 | mat11 = [mat11 mat11(g1)+length(combpos)/2]; 210 | end 211 | temp = routetoxy(mat11,pos,P); 212 | for g = 1:(length(mat11)) 213 | % if g <= (length(mat11)/2) 214 | temptw(g,1) = combtimespace(mat11(g)); 215 | % else 216 | % temptw(g,1) = combtimespace(mat11(g)+length(mat11)) ; 217 | % end 218 | end 219 | % child(i).xy(:,1,k)=temp(:,1); 220 | % child(i).xy(:,2,k)=temp(:,2); 221 | if isempty(temp) 222 | opttourlength(k) = 0; 223 | else 224 | out = ACOTW(cars(k,:),temp,temptw); 225 | opttourlength(k)=out.Cost; 226 | child(i).tour(k).xy=out.Tour; 227 | end 228 | end 229 | child(i).tourlength=sum(opttourlength); 230 | end 231 | new_population = table2struct(sortrows([struct2table(parent); struct2table(child)],'tourlength')); 232 | parent = new_population(1:population); 233 | 234 | thin = []; 235 | thick = []; 236 | 237 | for i = 1:new_generation 238 | thin(i) = max(sum(child(i).mat)); 239 | thinner(i) = min(sum(child(i).mat)); 240 | thick(i) = max(sum(child(i).mat')); 241 | if thinner(i) == 0 || thin(i) ~= 1 || thick(i) > 5 242 | end 243 | end 244 | besttourlength(iter)=parent(1).tourlength; 245 | disp(iter) 246 | ccheck = 0; 247 | for h =1 248 | for j = 1: population 249 | if parent(h).mat == parent(j).mat 250 | ccheck = ccheck+1; 251 | end 252 | end 253 | end 254 | 255 | if ccheck == population 256 | 257 | end 258 | 259 | end 260 | %% 261 | % for i = 1: length(parent(1).tour) 262 | % plot(parent(1).tour(i).xy(1,:),parent(1).tour(i).xy(2,:),'o-'); 263 | % hold on 264 | % end 265 | % drawnow 266 | % hold off 267 | % 268 | % x=1:iter; 269 | % figure() 270 | % plot(x,besttourlength) 271 | %% 272 | 273 | 274 | %% calculating route for ACO 275 | function col = tournum(child,C,P) 276 | idx = find(child == 1); 277 | idx = idx'; 278 | 279 | for i=1:C 280 | car(i).tour = []; 281 | end 282 | for l=1:P 283 | r = rem(idx(l),C); 284 | if r==0 285 | r=C; 286 | end 287 | car(r).tour= [car(r).tour idx(l)]; 288 | % car(r,length(car(r,:))+1)=idx(l); 289 | end 290 | for k = 1:C 291 | [row,col(k).tour]=ind2sub(size(child),car(k).tour); 292 | end 293 | 294 | end 295 | %% 296 | function out = correction(child) 297 | s1=sum(child'); 298 | maxk=max(s1); 299 | maxpass=find(s1 == maxk); 300 | idn=find(child(maxpass(1),:) == 1); % put randomperm for multiple maximum 301 | pk=randperm(length(idn)); 302 | child(maxpass(1),idn(pk(1)))=0; 303 | mink=min(s1); 304 | minpass=find(s1 == mink); 305 | minp=randperm(length(minpass)); 306 | child(minpass(minp(1)), idn(pk(1)))=1; 307 | out = child; 308 | end 309 | function out = horicorrection(child) 310 | s1=sum(child); 311 | maxk=max(s1); 312 | while maxk >= 2 313 | maxpass=find(s1 == maxk); 314 | for i=1:length(maxpass) 315 | idn=find(child(:,maxpass(i)) == 1); % put randomperm for multiple maximum 316 | pk=randperm(length(idn)); 317 | child(idn(pk(1)),maxpass(i))=0; 318 | s1=sum(child); 319 | maxk=max(s1); 320 | end 321 | end 322 | mink=min(s1); 323 | while mink == 0 324 | zeropass=find(s1 == 0); 325 | for i=1:length(zeropass) 326 | idn=find(child(:,zeropass(i)) == 0); % put randomperm for multiple maximum 327 | pk=randperm(length(idn)); 328 | child(idn(pk(1)),zeropass(i))=1; 329 | s1=sum(child); 330 | mink=min(s1); 331 | end 332 | % minpass=find(s1 == mink); 333 | % minp=randperm(length(minpass)); 334 | % child(minpass(minp(1)), idn(pk(1)))=1; 335 | end 336 | out = child; 337 | end 338 | 339 | function out = vertisplit1(parent1,parent2) 340 | S=size(parent1); 341 | k1 = randi(length(parent1)); 342 | k2 = randi(length(parent1)); 343 | k = sort([k1 k2]); 344 | child1=zeros(S); 345 | child2=zeros(S); 346 | child1(:,1:k(1))=parent1(:,1:k(1)); 347 | child1(:,k(1)+1:k(2))= parent2(:,k(1)+1:k(2)); 348 | child1(:,k(2)+1:S(2))= parent1(:,k(2)+1:S(2)); 349 | 350 | child2(:,1:k(1))=parent2(:,1:k(1)); 351 | child2(:,k(1)+1:k(2))= parent1(:,k(1)+1:k(2)); 352 | child2(:,k(2)+1:S(2))= parent2(:,k(2)+1:S(2)); 353 | 354 | out.child1=child1; 355 | out.child2=child2; 356 | end 357 | 358 | function out = vertisplit2(parent1,parent2) 359 | 360 | S=size(parent1); 361 | k = randi(length(parent1)); 362 | child1=zeros(S); 363 | child2=zeros(S); 364 | child1(:,1:k)=parent1(:,1:k); 365 | child1(:,k+1:S(2))= parent2(:,k+1:S(2)); 366 | child2(:,1:k)=parent2(:,1:k); 367 | child2(:,k+1:S(2))= parent1(:,k+1:S(2)); 368 | out.child1=child1; 369 | out.child2=child2; 370 | 371 | end 372 | function out = horisplit(parent1,parent2) 373 | S=size(parent1); 374 | k = randi((S(1))); 375 | if k == S(1) 376 | k=k-1; 377 | end 378 | child1=zeros(S); 379 | child2=zeros(S); 380 | child1(1:k,:)=parent1(1:k,:); 381 | child1(k+1:S(1),:)= parent2(k+1:S(1),:); 382 | child2(1:k,:)=parent2(1:k,:); 383 | child2(k+1:S(1),:)= parent1(k+1:S(1),:); 384 | out.child1=child1; 385 | out.child2=child2; 386 | end 387 | function out = mutation(parent1) 388 | % j=size(parent1); 389 | % parent1=zeros(j); 390 | % for i=1:j(2) 391 | % k=randi(j(1)); 392 | % parent1(k,i)=1; 393 | % end 394 | % out=parent1; 395 | j= size(parent1); 396 | for m=1:3 397 | k=randi(j(2)); 398 | maxpass=find(parent1(:,k) == 1); 399 | parent1(maxpass,k)=0; 400 | l=randi(j(1)); 401 | parent1(l,k)=1; 402 | end 403 | out=parent1; 404 | 405 | end 406 | %% ACO function 407 | 408 | function out = Antcolony(pos) 409 | x = pos(:,1); 410 | y = pos(:,2); 411 | n = numel(x); 412 | N=n/2; 413 | for i=1:n-1 414 | for j=i+1:n 415 | D(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2); 416 | D(j,i)=D(i,j); 417 | end 418 | end 419 | val.n=n; 420 | val.x=x; 421 | val.y=y; 422 | val.D=D; 423 | 424 | Cost=@(tour) TourLength(tour,val); 425 | 426 | nVar=val.n; 427 | % conditions 428 | it = 30; 429 | Ant = val.n; % check for number of `ants 430 | Q = 100; 431 | tau0=10^-6; 432 | alpha=1; 433 | beta=5; 434 | rho=0.5; 435 | e = 5; 436 | 437 | 438 | % Initialization 439 | eta=1./val.D; 440 | tau=tau0*ones(nVar,nVar); 441 | Bestpath=zeros(it,1); 442 | empty_ant.Tour=[]; 443 | empty_ant.Cost=[]; 444 | ant=repmat(empty_ant,Ant,1); 445 | BestSol.Cost=inf; 446 | 447 | 448 | % ACO Main Loop 449 | 450 | for it=1:it 451 | 452 | % Move Ants 453 | for k=1:Ant 454 | % pool for requests 455 | nonpool = N+1:2*N; 456 | ant(k).Tour=randi([1 N]); 457 | pool = [1:N ant(k).Tour(end)+N]; 458 | nonpool(nonpool==ant(k).Tour(end)+N)=[]; 459 | for l=2:2*N 460 | i=ant(k).Tour(end); 461 | P=tau(i,:).^alpha.*eta(i,:).^beta; 462 | P(ant(k).Tour)=0; 463 | P(nonpool)=0; 464 | P=P/sum(P); 465 | j=RouletteWheel(P); 466 | ant(k).Tour=[ant(k).Tour j]; 467 | if j<=N 468 | pool = [pool j+N]; 469 | end 470 | nonpool(nonpool==j+N)=[]; 471 | end 472 | ant(k).Cost=Cost(ant(k).Tour); 473 | if ant(k).Cost