資源描述:
《VRP求解matlab程序》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、%%theprocedureofantcolonyalgorithmforVRP%%%%%%%%%%%%%initializetheparametersofantcolonyalgorithmscity=[114521502151264110031592617004130254800512825214006163247210071462464008161242800914223910010163236500111482326001212823112001315621713001412921413001514620830016164208900171412062
2、1001814719310001916419390020129189250021155185180022139182700];d=city(:,2:3);x=d(:,1);y=d(:,2);g=city(:,4);m=10;%螞蟻數(shù)mm=max(size(city));alpha=1;belta=3;%決定tao和miu重要性的參數(shù)lmda=0;rou=0.4;%衰減系數(shù)q0=0.95;%概率tao0=1/(10*400.04);%初始信息素Q=2;%螞蟻循環(huán)一周所釋放的信息素defined_phrm=1.0;%initialpheromonelevelval
3、ueQV=6000;%車輛容量vehicle_best=round(sum(g)/QV)+1;%所完成任務(wù)所需的最少車數(shù)V=40;maxitime=100;best_cost=zeros(1,maxitime);%最好解cost=zeros(maxitime,m);best_tour=[];optcost=inf;opttour=[];maxtt=inf;%計算兩點的距離fori=1:mm;forj=1:mm;dist(i,j)=sqrt((d(i,1)-d(j,1))^2+(d(i,2)-d(j,2))^2);end;end;%給taomiu賦初值fori=
4、1:mm;forj=1:mm;ifi~=j;%s(i,j)=dist(i,1)+dist(1,j)-dist(i,j);%節(jié)約值tao(i,j)=defined_phrm;%defined_phrm=15.0miu(i,j)=1/dist(i,j);end;end;end;%置deltao(i,j)為0fork=1:mm;fork=1:mm;deltao(i,j)=0;end;end;%%開始迭代forNc=1:maxitime%迭代次數(shù)temp=[];fori=1:m%m(10)為螞蟻數(shù)tt=inf;costt=0;sumload=0;cur_pos(i)=
5、1;%從車場出發(fā)rn=setdiff(randperm(mm),1);n=1;%A(n)nn=1;part_sol(nn)=1;%部分路徑的第一步n_sol=0;%螞蟻產(chǎn)生的路徑數(shù)量M_vehicle=500;%是最大車輛數(shù)嗎t(yī)=0;%最佳路徑數(shù)組的元素數(shù)為0hh=1;whilehh~=0%hh為length(rn)whilesumload6、)=rn(k);%選擇重量適合的點n=n+1;%記錄重量適合的點數(shù)end;end;na=length(A);ifna==0breakelse%在滿足容量的點中計算概率,選擇概率大的一點forj=1:nap(j)=10*[(tao(cur_pos(i),A(j)))^alpha]*[(miu(cur_pos(i),A(j)))^belta];%p=compute_prob(A,cur_pos(i),tao,miu,alpha,belta,gama,lmda,i);%???endsump=sum(p);p=p/sump;maxp=1e-8;forj=1:naifp
7、(j)>maxpmaxp=p(j);index_max=j;end;end;forj=1:naifrand<=q0&rand<=p(j)index_max=j;breakendendold_pos=cur_pos(i);cur_pos(i)=A(index_max);%當(dāng)找到一點iftao(old_pos,cur_pos(i))>tao(cur_pos(i),old_pos)tao(cur_pos(i),old_pos)=tao(old_pos,cur_pos(i));elsetao(old_pos,cur_pos(i))=tao(cur_pos(i),old
8、_pos);endtao(old_po