4 x' h- N6 U1 h+ _% Initialize the Population! } m. R0 @5 X# @" v6 o
pop = zeros(popSize,n);5 ~' V0 w, ]: s8 e
pop(1, = (1:n);( B* w K5 @7 u. P: L7 ?6 P
for k = 2:popSize, L* w% Q/ U) ^- c/ G1 O
pop(k, = randperm(n); 2 F+ C6 X+ [, t* iend* X }: }+ r I
; [- O. E# r' D8 }. o% Run the GA6 R! K' t0 H0 \* r
globalMin = Inf;' H1 j/ O8 w5 } v2 ~
totalDist = zeros(1,popSize);( i6 R, v- T9 Z
distHistory = zeros(1,numIter);4 p; b. H, I, }; t( [6 Z' O5 A/ m
tmpPop = zeros(4,n); % b2 g6 s6 C, u' anewPop = zeros(popSize,n); ]0 l+ v0 Q( O$ @) [, T& G9 _if showProg- q# `# l p+ }1 o7 v* ^( L
pfig = figure('Name','TSP_GA | Current Best Solution','Numbertitle','off'); ) O2 W' }, A/ oend$ w4 a% v% s* B) {
for iter = 1:numIter , ^ l" x) D p) @ % Evaluate Each Population Member (Calculate Total Distance)- J) X9 E2 c5 [3 T+ }# r
for p = 1:popSize, I* }- y$ k! `: f1 b
d = dmat(pop(p,n),pop(p,1)); % Closed Path! e' ~, Q; v% ?0 U' n1 t
for k = 2:n& n0 R; Q& t* H' k5 v8 f/ I
d = d + dmat(pop(p,k-1),pop(p,k)); & ^9 i5 H" @* s: k; [$ ^ end7 K! o. C3 y. j; j) z
totalDist(p) = d; ) k, B. f( J+ a, ]& _ end ( x( v4 y0 l6 {( q0 s! n+ M- h- v
% Find the Best Route in the Population9 X5 w& w" `/ `' t
[minDist,index] = min(totalDist); c: q$ P: ^ \# _( Y6 \6 v2 k/ ^ distHistory(iter) = minDist;9 u! c& O, _ n* Z2 y4 \
if minDist < globalMin) k: p) n$ M# c- G- y( q& }; Y
globalMin = minDist; 9 R; K/ K# r; y, g# E ?: P1 ^ optRoute = pop(index,;, w. c& X- H6 `! s
if showProg 5 a$ ~& v2 K9 Z: ?% T/ d8 O& M; } % Plot the Best Route 1 p" W; Q7 c+ w* F6 i: K& V% y figure(pfig); 0 m9 D& Z. K7 |3 R- j rte = optRoute([1:n 1]);- p8 }6 ? w% K" b4 j8 u$ R9 M
if dims > 2, plot3(xy(rte,1),xy(rte,2),xy(rte,3),'r.-'); ' u; ^/ {4 C1 w# S else plot(xy(rte,1),xy(rte,2),'r.-'); end 2 r, B$ P4 \0 D$ L: z' D+ C# y title(sprintf('Total Distance = %1.4f, Iteration = %d',minDist,iter)); : A' b5 q. ^3 R# A7 k1 a. n9 M end5 ]8 z+ W$ c. K) m
end 8 h% i2 E0 z" n7 s9 @2 n; J/ A' d9 p$ b
% Genetic Algorithm Operators 8 y) q" ~$ f1 h% R. a( d, D9 u randomOrder = randperm(popSize);7 d% _. k2 i. S' E. [ Y# T
for p = 4:4:popSize ! X1 Y* z/ [9 Z% P; s rtes = pop(randomOrder(p-3:p),; 6 c; M0 p0 h) c% a& m dists = totalDist(randomOrder(p-3:p)); % y% i6 ~ i$ c& A8 ?- ]8 _ [ignore,idx] = min(dists); %#ok" t. f) `- z1 v) q, ~
bestOf4Route = rtes(idx,;9 I1 M z: i- Q
routeInsertionPoints = sort(ceil(n*rand(1,2)));2 h ~6 }* C2 f+ M6 ]
I = routeInsertionPoints(1); $ q' } k% d z' |& Z+ i J = routeInsertionPoints(2); ' l0 `) M9 _ S. n1 U& T( [, j for k = 1:4 % Mutate the Best to get Three New Routes g E5 M1 W' C* h8 f/ o tmpPop(k, = bestOf4Route; 6 Y: U6 ^. C( W1 W3 Q$ B* A switch k 2 c9 B) @. e$ {8 \: T3 D: s case 2 % Flip3 r8 n6 ]7 t2 k+ Z1 n9 S. }3 E
tmpPop(k,I:J) = tmpPop(k,J:-1:I); 9 p. |# j O& b" X, `# N# | case 3 % Swap/ {0 V" c# g' b
tmpPop(k,[I J]) = tmpPop(k,[J I]);" a( H9 H# U. L
case 4 % Slide/ a) q* I4 {* Y8 f }; L
tmpPop(k,I:J) = tmpPop(k,[I+1:J I]);" F3 ? S T' Q, b6 A4 m
otherwise % Do Nothing8 K, B$ K/ y$ o, X0 ?0 u4 p8 K/ M
end- H% S3 e N0 K' w4 T: f% N
end 4 A) w6 s1 w: F( U8 N: r4 Z newPop(p-3:p, = tmpPop; $ j4 q N+ L6 c- |2 c end 8 j7 N6 g' d @9 q% N& D pop = newPop; 4 ^1 t( G- m+ X- [+ i3 qend 2 A& T% J8 m( M3 o( Q) y5 n; _" K2 n8 ]' Z2 d2 d! }0 t- q
if showResult; @% R1 F+ O, y5 u
% Plots the GA Results" X" J2 |3 r7 p2 {+ f1 L' y
figure('Name','TSP_GA | Results','Numbertitle','off'); ; ^! Z8 X0 S4 } subplot(2,2,1); ; t8 I3 T$ I$ Y1 g7 O6 L ^% @* X pclr = ~get(0,'DefaultAxesColor'); * N1 D% z5 I: I3 h+ j+ L+ W- r if dims > 2, plot3(xy(:,1),xy(:,2),xy(:,3),'.','Color',pclr);5 r/ U# B U; z; l! I
else plot(xy(:,1),xy(:,2),'.','Color',pclr); end$ g% I; V5 z6 t# U6 I
title('City Locations'); . j) z8 n. \2 P. M# g4 f- Y! ] subplot(2,2,2); 0 I2 @2 X- A _( z8 |( ]9 N imagesc(dmat(optRoute,optRoute));+ r$ h+ u; w9 _: B1 g$ j8 [
title('Distance Matrix'); % M" M9 m( Q# q- C subplot(2,2,3);. l- V+ A% I: r, N% E1 Q( o& \
rte = optRoute([1:n 1]);) b- Q# p: A& y" l
if dims > 2, plot3(xy(rte,1),xy(rte,2),xy(rte,3),'r.-'); , I% {& S/ A M' j else plot(xy(rte,1),xy(rte,2),'r.-'); end ! R0 V/ d: N' d& r7 x title(sprintf('Total Distance = %1.4f',minDist)); 0 l; j1 d* M7 w( v; T subplot(2,2,4);- W; D `! T: q" Z; R c3 ]
plot(distHistory,'b','LineWidth',2); 5 I' X0 o4 J- V2 T' p$ v title('Best Solution History'); , s& e* x. B r4 G% C, Y set(gca,'XLim',[0 numIter+1],'YLim',[0 1.1*max([1 distHistory])]);6 q6 ^0 R0 v1 I8 b
end) X3 J6 @: U; B" {6 d7 ]0 G