matlab 算法程序.doc_第1页
matlab 算法程序.doc_第2页
matlab 算法程序.doc_第3页
matlab 算法程序.doc_第4页
matlab 算法程序.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一基于matlab Dijkstra算法的实现function l,z=Dijkstra(W)n = size (W,1);for i = 1 :n l(i)=W(1,i); z(i)=1;end i=1;while il(j)+W(j,i) l(i)=l(j)+W(j,i); z(i)=j; if jtffor i=1:Lfval_after,route_after=exchange(route,d);if fval_afterrandroute=route_after;fval=fval_after;else route=route;fval=fval;endendt=alpha*t;endtoc%-function fval=value(route,d)%used for reckoning the goal value of the selected traveling routen=length(d);fval=0;for i=1:n-1fval=fval+d(route(i),route(i+1);end%fval=fval+d(route(n),route(1);% if%is omited,it computes a circle,else%a chain-function fval_after,route_after=exchange(route,d)%changing traveling route by inversing the sequence between two selected 2 locations n=length(d);location1=ceil(n*rand);location2=ceil(n*rand);%the location of two exchanged numberloc1=min(location1,location2);loc2=max(location1,location2);middle_route=fliplr(route(loc1:loc2);%the part route which has been exchangedroute_after=route(1:loc1-1) middle_route route(loc2+1:n);%the after traveling routefval_after=value(route_after,d);%- 三/基于matlab TSP问题蚁群算法的实现load bayg29.txt;%29个城市的距离矩阵Dis = bayg29;CITY= length(bayg29);%CITY = 29;STEP = CITY; %时刻标记ECHO = 1000;ANT =5;T0 = 29*1601.-1;Q = 1; %fix 1 in ACSQ0 = 0.9; %Select DivideAlpha = 0.1; %Global UpdateP = 0.1; %Local UpdateA = 1; %fix 1B = 2; %Nij power% 各条路径的初始权值 for i=1:CITY; for j=1:i-1; T(i,j)=T0; TN(i,j)=(T(i,j).A)*(1/Dis(i,j).B); end; for j=i:CITY; T(i,j)=0; TN(i,j)=0; end; end; save(T.mat,T );% fid = fopen(T.txt,w);% fid2 = fopen(TN.txt,w);% fwrite(fid,T,integer*4);% fwrite(fid2,TN,integer*4);% fclose(fid);% fclose(fid2);for Echo=1:ECHO;% 设置起点 t = 1; %step1 for i =1:ANT; Mileage(i)=0; City(t,i) = ceil(CITY*rand(1);%1-29随机值 for j =2:STEP; % j此处表位置 %City(j,i)=mod(City(t,i)+j,CITY)+1; %wrong if City(t,i)+j-1 CITY % j此处表增量 City(j,i)= City(t,i)+j-1-CITY; else City(j,i)= City(t,i)+j-1; end; end; end; %pause; for t=2:STEP-1;%最后一步不需select过程 %选出下一城市 City(citypos,i) for i =1:ANT; base = City(t-1,i); q = 0; while q =0 q = rand(1); end; if q Q0 % AC select %Prob SUM(i)=0; for j=t:STEP %回路 SUM(i)=SUM(i)+TN(base,City(j,i)+TN(City(j,i),base); end; for j=1:STEP; if jt Prob(i,City(j,i)=0; else Prob(i,City(j,i)=(TN(base,City(j,i)+TN(City(j,i),base)/SUM(i); end; end; %round_select run =0; mile = 0; while run =0 run = rand(1); end; for j=t:STEP; mile=mile + Prob(i,City(j,i); if run (TN(base,City(citypos,i)+TN(City(citypos,i),base) citypos = j; end; end; end;%end of select %citypos is selected temp = City(t,i); City(t,i)=City(citypos,i); City(citypos,i)=temp; Mileage(i)=Mileage(i)+Dis(City(t-1,i),City(t,i)+Dis(City(t,i),City(t-1,i); %Local Update if City(t-1,i)City(t,i) T(City(t-1,i),City(t,i)=T(City(t-1,i),City(t,i)*(1-P)+T0*P; else T(City(t,i),City(t-1,i)=T(City(t,i),City(t-1,i)*(1-P)+T0*P; end; end; %for i =1:ANT; if Echo=1 save(T.mat,T,-APPEND); end; end;%for t=2:STEP-1; %last step. no need to select. for i =1:ANT; % sum the eage (last,first) Mileage(i)=Mileage(i)+Dis(City(STEP,i),City(1,i)+Dis(City(1,i),City(STEP,i); %Local Update if City(1,i)City(STEP,i) T(City(1,i),City(STEP,i)=T(City(1,i),City(STEP,i)*(1-P)+T0*P; else T(City(STEP,i),City(1,i)=T(City(STEP,i),City(1,i)*(1-P)+T0*P; end; end; if Echo=1 end; %Global Update. T,TN minant =1; for i=1:ANT; if Mileage(i)City(j+1,minant) T(City(j,minant),City(j+1,minant)=T(City(j,minant),City(j+1,minant)+(Q/Mileage(minant)*Alpha/(1-Alpha); else T(City(j+1,minant),City(j,minant)=T(City(j+1,minant),City(j,minant)+(Q/Mileage(minant)*Alpha/(1-Alpha); end; end; if City(1,minant)City(STEP,minant) T(City(1,minant),City(STEP,minant)=T(City(1,minant),City(STEP,minant)+(Q/Mileage(minant)*Alpha/(1-Alpha); else T(City(STEP,minant),City(1,minant)=T(City(STEP,minant),City(1,minant)+(Q/Mileage(minant)*Alpha/(1-Alpha); end; for i=1:CITY; for j=1:i-1; T(i,j)=T(i,j)*(1-Alpha); TN(i,j)=(T(i,j).A) *(1.0/Dis(i,j).B); end; end; if Echo=1 end;end %for Echo=1:ECHO; c,i=min(Mileage); save ACS.mat;四/基于matlab TSP问题遗传算法的实现%TSP问题(又名:旅行商问题,货郎担问题)遗传算法通用matlab程序%D是距离矩阵,n为种群个数,建议取为城市个数的12倍,%C为停止代数,遗传到第 C代时程序停止,C的具体取值视问题的规模和耗费的时间而定%m为适应值归一化淘汰加速指数 ,最好取为1,2,3,4 ,不宜太大%alpha为淘汰保护指数,可取为01之间任意小数,取1时关闭保护功能,最好取为0.81.0%R为最短路径,Rlength为路径长度function R,Rlength=geneticTSP(D,n,C,m,alpha)N,NN=size(D);farm=zeros(n,N);%用于存储种群for i=1:nfarm(i,:)=randperm(N);%随机生成初始种群endR=farm(1,:);%存储最优种群len=zeros(n,1);%存储路径长度fitness=zeros(n,1);%存储归一化适应值counter=0;while counter=alpha*randnn=nn+1;FARM(nn,:)=farm(i,:);endendFARM=FARM(1:nn,:);aa,bb=size(FARM);%交叉和变异while aanif nnnFARM=FARM(1:n,:);%保持种群规模为nendfarm=FARM;clear FARMcounter=counter+1endRlength=myLength(D,R);function a,b=intercross(a,b)L=length(a);if L=rand&L10W=ceil(L/10);else W=floor(L/10);endp=unidrnd(L-W+1);%随机选择交叉范围,从p到p+Wfor i=1:W%交叉x=find(a=b(1,p+i-1);y=find(b=a(1,p+i-1);a(1,p+i-1),b(1,p+i-1)=exchange(a(1,p+i-1),b(1,p+i-1);a(1,x),b(1,y)=exchange(a(1,x),b(1,y); endfunction x,y=exchange(x,y)temp=x;x=y;y=temp;% 计算路径的子程序function len=myLength(D,p)N,NN=size(D);len=D(p(1,N),p(1,1);for i=1:(N-1)len=len+D(p(1,i),p(1,i+1);end%计算归一化适应值子程序function fitness=fit(len,m,maxlen,minlen)fitness=len;for i=1:length(len)fitness(i,1)=(1-(len(i,1)-minlen)/(maxlen-minlen+0.000001).m;end 五/基于matlab的floyd算法function d,path=floyd(a,sp,ep)% floyd - 最短路问题 % % Syntax: d,path=floyd(a,sp,ep) % % Inputs: % a - 距离矩阵是指i到j之间的距离,可以是有向的 % sp - 起点的标号 % ep - 终点的标号 % % Outputs: % d - 最短路的距离 % path - 最短路的路径 % % Example: % a =% 0 50 Inf 40 25 10 ;% 50 0 15 20 Inf 25 ;% Inf 15 0 10 20 Inf ;% 40 20 10 0 10 25 ;% 25 Inf 20 10 0 55 ;% 10 25 Inf 25 55 0 ;% d,path=floyd(a,2,5)% % % Other m-files required: none % Subfunctions: none % MAT-files required: none % % See also: OTHER_FUNCTION_NAME1, OTHER_FUNCTION_NAME2 % Author: Xiaoyong Ren%Packaging Engineering, Xian University of Technology . % email : % QQ: 170071606 % Website: % December 2004; Last revision: 24-May-2005 %- BEGIN CODE - n=size(a,1);D=a;path=zeros(n,n);for i=1:n for j=1:n if D(i,j)=inf path(i,j)=j; %j是i的后续点 end endendfor k=1:n for i=1:n for j=1:n if D(i,j)D(i,k)+D(k,j) D(i,j)=D(i,k)+D(k,j); path(i,j)=path(i,k); end end endendp=sp;mp=sp;for k=1:n if mp=ep d=path(mp,ep); p=p,d; mp=d; endendd=D(sp,ep);path=p;%- END OF CODE -六/基于matlab灰色预测GM(1,1)实现function y,p,e=gm_1_1(X,k) %Build the calculating dieplate for the typical gray model. %Example y,p=gm_1_1(200 250 300 350,2) %Designed by NIXIUHUI,Dalian Fisher University. %20 April,2004. Last modified by NXH at 25 September,2004 if nargout3,error(Too many output argument.);end if nargin=1,k=1;x_orig=X; elseif nargin=0|nargin2 error(Wrong number of input arguments.); end x_orig=X; predict=k; %AGO process x=cumsum(x_orig); %compute the coefficient(a and u)- n=length(x_orig); %first generate the matrix B for i=1:(n-1); B(i)=-(x(i)+x(i+1)/2; end B=B ones(n-1,1); %then generate the matrix Y for i=1:(n-1); y(i)=x_orig(i+1); end Y=y; %get the coefficient. a=au(1) u=au(2) au=(inv(B*B)*(B*Y); %- %change the grey model to symbolic expression coef1=au(2)/au(1); coef2=x_orig(1)-coef1; coef3=0-au(1); costr1=num2str(coef1); costr2=num2str(abs(coef2); costr3=num2str(coef3); eq=strcat(costr1,+,costr2,e,costr3,*(t-1); %comparison of calculated and observed value for t=1:n+predict mcv(t)=coef1+coef2*exp(coef3*(t-1); end x_mcv0=diff(mcv); x_mcve=x_orig(1) x_mcv0; x_mcv=diff(mcv(1:end-predict); x_orig_n=x_orig(2:end); x_c_error=x_orig_n-x_mcv; x_error=mean(abs(x_c_error./x_orig_n); if x_error0.2 disp(model disqualification!); elseif x_error0.1 disp(model check out); else disp(model is perfect!); end %predicting model and plot gragh plot(1:n,x_orig,diamond,1:n+predict,x_mcve); p=x_mcve(end-predict+1:end); xlabel(CURVE OF GREY MODEL ANALYSIS); title(GM(1,1); grid on y=eq; e=x_error; p=x_mcve(end-predict+1:end);七/基于matlab灰色关联度计算的实现function r=incident_degree(x0,x1) %compute the incident degree for grey model. %Designed by NIXIUHUI,Dalian Fisher University. %17 August,2004,Last modified by NXH at 21 August,2004 %数据初值化处理 x0_initial=x0./x0(1); temp=size(x1); b=repmat(x1(:,1),1 temp(2); x1_initial=x1./b; %分辨系数选择 K=0.1; disp(The grey interconnect degree is: ); x0_ext=repmat(x0_initial,temp(1) 1); contrast_mat=abs(x0_ext-x1_initial); delta_min=min(min(contrast_mat);%delta_min在数据初值化后实际为零 delta_max=max(max(contrast_mat); a=delta_min+K*delta_max; incidence_coefficient=a./(contrast_mat+K*delta_max);%得到关联系数 r=(sum(incidence_coefficient)/temp(2); %得到邓氏面积关联度八/基于matlab层次分析法的实现disp(请输入判断矩阵A(n阶);A=input(A=);n,n=size(A);x=ones(n,100);y=ones(n,100);m=zeros(1,100);m(1)=max(x(:,1);y(:,1)=x(:,1);x(:,2)=A*y(:,1);m(2)=max(x(:,2);y(:,2)=x(:,2)/m(2);p=0.0001;i=2;k=abs(m(2)-m(1);while kp i=i+1; x(:,i)=A*y(:,i-1); m(i)=max(x(:,i); y(:,i)=x(:,i)/m(i); k=abs(m(i)-m(i-1);enda=sum(y(:,i);w=y(:,i)/a;t=m(i);disp(w);disp(t); %以下是一致性检验CI=(t-n)/(n-1);RI=0 0 0.52 0.89 1.12 1.26 1.36 1.41 1.46 1.49 1.52 1.54 1.56 1.58 1.59;CR=CI/RI(n);if CRx(j) temp=x(i); x(i)=x(j); x(j)=temp; end end end y=x;十/matlab遗传算法实例讲解(转引)【问题】求f(x)=x+10*sin(5x)+7*cos(4x)的最大值,其中0=x=9 【分析】选择二进制编码,种群中的个体数目为10,二进制编码长度为20,交叉概率为0.95,变异概率为0.08 【程序清单】 %编写目标函数 functionsol,eval=fitness(sol,options) x=sol(1); eval=x+10*sin(5*x)+7*cos(4*x); %把上述函数存储为fitness.m文件并放在工作目录下initPop=initializega(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论