交通分配之随机配流算法matlab源码(含最短路径算法).doc
本文档含两个m文件dial.m和dijkstra1.m,dial需要调用dijkstra1。算例如下:算法如下:Dial.medge=[112234455678;.243565768989;.222221212222;];n=9;weight=inf*ones(n,n);Q=1000;start=1;endn=9;r=zeros(1,n);s=zeros(1,n);theta=1;L=zeros(n);OD=zeros(n);%====================================================================fori=1:nweight(i,i)=0;endfori=1:size(edge,2)weight(edge(1,i),edge(2,i))=edge(3,i);endLmin=zeros(1,n);fori=1:n[dis,path]=dijkstra1(weight,i,9);s(i)=dis;%任意节点到终点s的最小时间[dis,path]=dijkstra1(weight,1,i);r(i)=dis;%起点r到任意节点最小时间endfori=1:nforj=1:nifweight(i,j)>0%所有路段似然值endendendLMax=2000;temp=0;tempr=zeros(1,n);rr=zeros(1,n);rr=r;%出发节点i排序fori=1:nMin=2000;forj=1:nifrr(j)0||OD(temps(j),tempi)==0tempOD=tempOD+OD(temps(j),tempi);endendfori=1:nsumlw=0;iftemps(j)==endnfortempi=1:9sumlw=sumlw+LW(tempi,temps(j));endOD(i,temps(j))=Q*LW(i,temps(j))/sumlw;elsefortempi=1:9sumlw=sumlw+LW(tempi,temps(j));endOD(i,temps(j))=LW(i,temps(j))*tempOD/sumlw;endendendODfunction[distance,path]=dijkstra1(A,s,e)%[DISTANCE,PATH]=DIJKSTRA(A,S,E)%returnsthedistanceandpathbetweenthestartnodeandtheendnode.%%A:adjcentmatrix%s:startnode%e:endnode%initializen=size(A,1);%nodenumberD=A(s,:);%distancevectorpath=[];%pathvectorvisit=ones(1,n);%nodevisibilityvisit(s)=0;%sourcenodeisunvisibleparent=zeros(1,n);%parentnode%theshortestdistancefori=1:n-1%BlueSethasn-1nodestemp=zeros(1,n);count=0;forj=1:nifvisit(j)temp=[temp(1:count)D(j)];elsetemp=[temp(1:count)inf];endcount=count+1;end[value,index]=min(temp);j=index;visit(j)=0;fork=1:nifD(k)>D(j)+A(j,k)D(k)=D(j)+A(j,k);parent(k)=j;endendenddistance=D(e);%theshortestdistancepathifparent(e)==0return;endpath=zeros(1,2*n);%pathpreallocationt=e;path(1)=t;count=1;whilet~=spath=[ppath(1:count)];t=p;count=count+1;endifcount>=2*nerror([ Thepathpreallocationlengthistooshort. ,. Pleaseredefinepathpreallocationparameter. ]);endpath(1)=s;path=path(1:count);