遗传结果与程序Word文件下载.docx
- 文档编号:16601749
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:16
- 大小:37.16KB
遗传结果与程序Word文件下载.docx
《遗传结果与程序Word文件下载.docx》由会员分享,可在线阅读,更多相关《遗传结果与程序Word文件下载.docx(16页珍藏版)》请在冰豆网上搜索。
%dmat=reshape(sqrt(sum((xyz(a,:
)-xyz(a'
%[opt_rte,min_dist]=tsp_ga(xyz,dmat,pop_size,num_iter,show_prog,show_res);
%Seealso:
mtsp_ga,tsp_nn,tspo_ga,tspof_ga,tspofs_ga,distmat
%Author:
JosephKirk
%Email:
jdkirk630@
%Release:
2.2
%ReleaseDate:
6/2/09
%ProcessInputsandInitializeDefaults
tic
clearall
clc
nargs=6;
fork=nargin:
nargs-1
switchk
case0
xy=[116.28,39.54;
121.29,31.14;
117.11,39.09;
106.32,29.32;
126.41,45.45;
125.19,43.52;
123.24,41.5;
111.48,40.49;
114.28,38.02;
112.34,37.52;
117,36.38;
113.42,34.48;
108.54,34.16;
103.49,36.03;
106.16,38.2;
101.45,36.38;
87.36,43.48;
117.18,31.51;
118.5,32.02;
120.09,30.14;
113,28.11;
115.52,28.41;
114.21,30.37;
104.05,30.39;
106.42,26.35;
119.18,26.05;
113.15,23.08;
110.2,20.02;
108.2,22.48;
102.41,25;
90.08,29.39;
114.1,22.18;
113.35,22.14;
121.31,25.03];
case1
xynew(:
1)=(floor(xy(:
1))+(xy(:
1)-floor(xy(:
1)))/60)*pi/180;
xynew(:
2)=(floor(xy(:
2))+(xy(:
2)-floor(xy(:
2)))/60)*pi/180;
%¼
Æ
Ë
ã
È
Î
Ò
â
Á
½
µ
Ä
¾
à
À
ë
£
¬
Ê
¹
Ó
Ã
«
$2*6378.137*\arcsin\sqrt{sin^2(a)+cos(Lat2)*cos(Lat2)*sin^2(b/2)}$
%Lat1Lung1±
í
Aµ
Î
³
¶
Lat2Lung2±
Bµ
»
%a=Lat1¨
CLat2Î
ª
Ö
®
²
î
b=Lung1-Lung2Î
¶
Sizexy=size(xy,1);
Dis=zeros(Sizexy,Sizexy);
fori=1:
Sizexy
forj=1:
WeiDuCha=xynew(i,2)-xynew(j,2);
JingDuCha=xynew(i,1)-xynew(j,1);
Dis(i,j)=2*6378.137*asin(sqrt(sin(WeiDuCha/2)^2+sin(JingDuCha/2)^2*cos(xynew(i,2))*cos(xynew(j,2))));
%Ô
Ê
¼
%%Dis(i,j)=6378.137*acos(((sin(xy(i,2))-sin(xy(j,2)))^2-2*cos(JingDuCha))/2);
%¹
1
%Dis(i,j)=6378.137*acos(cos(xynew(i,2))*cos(xynew(j,2))*cos(JingDuCha)+sin(xynew(i,2))*sin(xynew(j,2)));
end
N=size(xy,1);
a=meshgrid(1:
N);
)).^2,2)),N,N);
dmat=Dis;
case2
pop_size=100;
case3
num_iter=1e4;
case4
show_prog=1;
case5
show_res=1;
otherwise
end
%VerifyInputs
[N,dims]=size(xy);
[nr,nc]=size(dmat);
ifN~=nr||N~=nc
error('
InvalidXYorDMATinputs!
'
)
n=N;
%SanityChecks
pop_size=4*ceil(pop_size/4);
num_iter=max(1,round(real(num_iter
(1))));
show_prog=logical(show_prog
(1));
show_res=logical(show_res
(1));
%InitializethePopulation
pop=zeros(pop_size,n);
fork=1:
pop_size
pop(k,:
)=randperm(n);
%RuntheGA
global_min=Inf;
total_dist=zeros(1,pop_size);
dist_history=zeros(1,num_iter);
tmp_pop=zeros(4,n);
new_pop=zeros(pop_size,n);
ifshow_prog
pfig=figure('
Name'
'
TSP_GA|CurrxytBestSolution'
Numbertitle'
off'
);
foriter=1:
num_iter
%EvaluateEachPopulationMember(CalculateTotalDistance)
forp=1:
d=dmat(pop(p,n),pop(p,1));
%ClosedPath
fork=2:
n
d=d+dmat(pop(p,k-1),pop(p,k));
total_dist(p)=d;
%FindtheBestRouteinthePopulation
[min_dist,index]=min(total_dist);
dist_history(iter)=min_dist;
ifmin_dist<
global_min
global_min=min_dist;
opt_rte=pop(index,:
ifshow_prog
%PlottheBestRoute
figure(pfig);
rte=opt_rte([1:
n1]);
ifdims==3,plot3(xy(rte,1),xy(rte,2),xy(rte,3),'
r.-'
elseplot(xy(rte,1),xy(rte,2),'
title(sprintf('
TotalDistance=%1.4f,Iteration=%d'
min_dist,iter));
%GxyeticAlgorithmOperators
rand_pair=randperm(pop_size);
forp=4:
4:
rtes=pop(rand_pair(p-3:
p),:
dists=total_dist(rand_pair(p-3:
p));
[ignore,idx]=min(dists);
best_of_4_rte=rtes(idx,:
ins_pts=sort(ceil(n*rand(1,2)));
I=ins_pts
(1);
J=ins_pts
(2);
fork=1:
4%MutatetheBesttogetThreeNewRoutes
tmp_pop(k,:
)=best_of_4_rte;
case2%Flip
tmp_pop(k,I:
J)=fliplr(tmp_pop(k,I:
J));
case3%Swap
tmp_pop(k,[IJ])=tmp_pop(k,[JI]);
case4%Slide
J)=tmp_pop(k,[I+1:
JI]);
otherwise%DoNothing
new_pop(p-3:
p,:
)=tmp_pop;
pop=new_pop;
ifshow_res
%PlotstheGAResults
figure('
TSP_GA|Results'
subplot(2,2,1);
ifdims==3,plot3(xy(:
1),xy(:
2),xy(:
3),'
k.'
elseplot(xy(:
2),'
title('
CityLocations'
subplot(2,2,2);
imagesc(dmat(opt_rte,opt_rte));
DistanceMatrix'
subplot(2,2,3);
TotalDistance=%1.4f'
min_dist));
subplot(2,2,4);
plot(dist_history,'
b'
LineWidth'
2);
BestSolutionHistory'
set(gca,'
XLim'
[0num_iter+1],'
YLim'
[01.1*max([1dist_history])]);
%ReturnOutputs
ifnargout
varargout{1}=opt_rte;
varargout{2}=min_dist;
opt_rte
min_dist
toc
遗传结果
1
opt_rte=
Columns1through25
567111922018232122263432273328292542430311716
Columns26through34
14151312108913
min_dist=
1.5384e+004
Elapsedtimeis30.647332seconds.
2、Columns1through25
42529283327323426222123182021911765319810
121315141617313024
Elapsedtimeis26.371099seconds.
>
2、
3、
15141617313024425292833322721232226342021819117
653198101213
1.5360e+004
Elapsedtimeis21.189464seconds.
4、
81514161731302928333227254241312232122263420218
191176531910
1.5723e+004
Elapsedtimeis21.706410seconds.
5、
3426222123121324425273233282930311716141581091
3567111918220
Elapsedtimeis20.604091seconds.
6、
272122263420219182312117653198101315141617
31302442529283332
1.5415e+004
Elapsedtimeis22.219427seconds.
7、
2321273233282925424303117161415131210891356
7111918220342622
Elapsedtimeis20.721589seconds.
8、opt_rte=
71112232122181922034263227332829254243031171614
151310891356
1.5409e+004
Elapsedtimeis20.041199seconds.
9、
322634202191822212312117653198101315141617
31302442529283327
Elapsedtimeis20.573494seconds.
10、opt_rte=
111922018232122263432273328292542430311716141513
12108913567
Elapsedtimeis21.863092seconds.
11、opt_rte=
108913567111223212218192203426322733282925
42430311716141513
Elapsedtimeis21.860107seconds.
12、opt_rte=
Elapsedtimeis19.723652seconds.
13、opt_rte=
302442529283332272123222634202181911765319
81012131514161731
Elapsedtimeis19.024564seconds.
14、opt_rte=
Elapsedtimeis21.513791seconds.
15、opt_rte=
765319810122322214241315141617313025292833
27322634202181911
1.5750e+004
Elapsedtimeis19.410002seconds.
16、opt_rte=
3024425292833322721222634202191823121176531
9810131514161731
Elapsedtimeis19.773357seconds.
17、opt_rte=
Columns1throug
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 遗传 结果 程序