Java教程

【TWVRP】基于matlab节约算法求解带时间窗的电动车路径规划问题【含Matlab源码 1169期】

本文主要是介绍【TWVRP】基于matlab节约算法求解带时间窗的电动车路径规划问题【含Matlab源码 1169期】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、简介

在这里插入图片描述
基本思想
在这里插入图片描述
行时通过这一条弧。

迭代步骤
在这里插入图片描述

二、源代码

 function [outcome1,outcome2,outcome3]=cw(Numberoffacilities,assignofpoint,ttimeu,timewindow,distMatrix,quantity,Qofcar,p1)    
    H=1;
    outcome3=zeros(1,Numberoffacilities);%与设施点的伪编号是一一对应的   
    %%%%%%%找到实际分配的设施点,因为之前的chrom中为1的点,可能%%%%%%%%%%%%%
    facilitypop=zeros(1:Numberoffacilities);
    facilitypop(assignofpoint)=1;
    trueSelectefacilities=find(facilitypop(1,:)==1);     %被选择的设施编号    
    trueselectNumberoffacilities=size(trueSelectefacilities,2);
    outcome1=cell(1,trueselectNumberoffacilities);
    outcome2=cell(1,trueselectNumberoffacilities);    
    for i=1:trueselectNumberoffacilities%此时的设施为i
        a=trueSelectefacilities(i);%设施的伪编号        
        pointofsubroute=find(assignofpoint==a);%属于该设施点的所有需求点的伪编号
        Numberofpointsofsubroute=size(pointofsubroute,2);%该设施点的需求点数量           
        judge=zeros(1,Numberofpointsofsubroute);                           %判断需求点的位置情况       
        chrom1=zeros(1,Numberofpointsofsubroute);%最后输入的是伪编号        
        chrom2=sort(pointofsubroute);                                      %需求点伪编号从小到大排序        
        originalchrom2=chrom2;
        
        %%对关键节点先分配一个车辆给它?????????????????????对不对?
        %for j=1:Numberofpointsofsubroute            
            %if timewindow(chrom2(j),2)==timewindow(chrom2(j),3)                  
                %chrom1(j)=H;
                %H=H+1;                
            %end
        %end      
        %%%%%%%%计算到达时间%%%%%%%%%%%%%%%%%%%%        
        arrivetime=zeros(1,Numberofpointsofsubroute);%需求点的到达时间,与当前的originalchrom2位置一一对应,与分配给该设施点的需求点一一对应        
        for j=1:Numberofpointsofsubroute            
            if ttimeu(chrom2(j)+Numberoffacilities,a)<timewindow(chrom2(j),2)                
                arrivetime(j)=timewindow(chrom2(j),2);                
            else
                arrivetime(j)=ttimeu(chrom2(j)+Numberoffacilities,a);                
            end
        end        
        %%%%%%%%%%%%%%%%%%%%%%%%%路径节约值列表%%%%%%%%%%%%%%%%%%%%%%        
        savingnumber=zeros(Numberofpointsofsubroute);%与分配给该设施的需求点的伪编号一一对应        
        for j=1:Numberofpointsofsubroute            
            for z=1:Numberofpointsofsubroute                
                if j~=z                       
                    savingnumber(j,z)=distMatrix(chrom2(j)+Numberoffacilities,a)+distMatrix(chrom2(z)+Numberoffacilities,a)-distMatrix(chrom2(j)+Numberoffacilities,chrom2(z)+Numberoffacilities);                    
                end
            end
        end        
        [a b]=max(savingnumber);%a输入值,b中对应的数字为行,对应的列数为列        
        [c d]=max(a);%c输入具体的当前节约值,d为列数         
        e=b(d);%e为行 
        %%%在节约值列表中e行d列是当前最大的节约量
        E=chrom2(e);%此时的E为节约量最大的需求点伪编号之一
        D=chrom2(d);%此时的D为节约量最大的需求点伪编号之1       
        done=1;            
        while(done<2)                
            if judge(e)==0&&judge(d)==0                 
                PD=0;                
                a=quantity(E,2)+quantity(D,2);
                aa=quantity(E,3)+quantity(D,3);
                pp1=normcdf(Qofcar,a,sqrt(aa));
                a2=find(chrom2==D);                
                b2=find(chrom2==E);
                if pp1>p1                       
                    EFj=arrivetime(e)+ttimeu(E+Numberoffacilities,D+Numberoffacilities)-arrivetime(d);                      
                    if EFj<0                             
                        aheadtime=arrivetime(d)-timewindow(D,2);                           
                        if aheadtime>=-EFj                                
                            PD=1;                                 
                        end
                    elseif EFj==0
                        PD=1;                        
                    elseif EFj>0                          
                        delaytime=timewindow(D,3)-arrivetime(d);                            
                        if delaytime>=EFj                             
                            PD=1;                               
                        end
                    end
                    if PD==1                        
                        chrom1(e)=H;                            
                        chrom1(d)=H;%H为车辆编号                          
                        judge(e)=1;%与设施直接相连的起点为1                          
                        judge(d)=2;%与设施直接相连的终点为2                           
                        H=H+1;                            
                        arrivetime(d)=arrivetime(d)+EFj;
                        if b2>a2
                            chrom2(a2)=E;
                            chrom2(b2)=D;
                        end
                    end
                end
                savingnumber(e,d)=0;                
            elseif judge(e)==0&&judge(d)==1;                 
                a2=find(chrom2==D);                
                b2=find(chrom2==E);                
                a=find(chrom1==chrom1(a2));                
                b=sum(quantity(chrom2(a),2))+quantity(E,2);
                bb=sum(quantity(chrom2(a),3))+quantity(E,3);                
                pp1=normcdf(Qofcar,b,sqrt(bb)); 
                c=size(a,2);
                if pp1>p1                       
                    EFj=arrivetime(e)+ttimeu(E+Numberoffacilities,D+Numberoffacilities)-arrivetime(d);                    
                    if EFj<0                          
                        a1=10000;                            
                        for j=1:c                            
                            b1=find(originalchrom2==chrom2(a(j)));                            
                            Aheadtime=arrivetime(b1)-timewindow(chrom2(a(j)),2);                             
                            if Aheadtime<a1                                     
                                aheadtime=Aheadtime;                                   
                                a1=Aheadtime;                                 
                            end
                        end
                        if aheadtime>=-EFj                            
                            PD=1;                             
                        end
                    elseif EFj==0                        
                        PD=1;                             
                    elseif EFj>0                           
                        a1=10000;                         
                        for j=1:c                               
                            b1=find(originalchrom2==chrom2(a(j)));                            
                            Delaytime=timewindow(chrom2(a(j)),3)-arrivetime(b1);                              
                            if Delaytime<a1                                       
                                delaytime=Delaytime;                                     
                                a1=Delaytime;                                     
                            end
                        end
                        if delaytime>=EFj                            
                            PD=1;                                 
                        end
                    end
                    
                    if PD==1                        
                        chrom1(b2)=chrom1(a2);                          
                        a1=find(chrom1==chrom1(a2));                          
                        CHROM2=chrom2;                          
                        chrom2(a1(1))=CHROM2(b2);                          
                        k=2;                             
                        while(k<=c+1)                               
                            chrom2(a1(k))=CHROM2(a(k-1));                               
                            k=k+1;                              
                        end
                        judge(d)=10;%10为内点                        
                        judge(e)=1;                          
                    end
                end                
                savingnumber(e,d)=0;
            elseif judge(e)==0&&judge(d)==2                
                a2=find(chrom2==D);                
                b2=find(chrom2==E);                
                a=find(chrom1==chrom1(a2));                    
                b=sum(quantity(chrom2(a),2))+quantity(E,2);
                bb=sum(quantity(chrom2(a),3))+quantity(E,3);
                pp1=normcdf(Qofcar,b,sqrt(bb));  
                c=size(a,2);                        
                if pp1>p1                        
                    EFj=arrivetime(d)+ttimeu(E+Numberoffacilities,D+Numberoffacilities)-arrivetime(e);                      
                    if EFj<0                             
                        aheadtimee=arrivetime(e)-timewindow(E,2);                                
                        aheadtimed=arrivetime(d)-timewindow(D,2);                           
                        if aheadtimee<aheadtimed                              
                            aheadtime=aheadtimee;                              
                        else
                            aheadtime=aheadtimed;                            
                        end
                        if aheadtime>=-EFj                            
                            PD=1;                                
                        end
                    elseif EFj==0                        
                        PD=1;                             
                    elseif EFj>0                          
                        delaytimee=timewindow(E,3)-arrivetime(e);                          
                        delaytimed=timewindow(D,3)-arrivetime(d);                             
                        if delaytimee<delaytimed                                
                            delaytime=delaytimee;                               
                        else
                            delaytime=delaytimed;                            
                        end
                        if delaytime>=EFj                            
                            PD=1;                               
                        end
                    end
                    if PD==1                        
                        chrom1(b2)=chrom1(a2);                             
                        a1=find(chrom1==chrom1(a2));                         
                        CHROM2=chrom2;                          
                        k=1;                              
                        while(k<=c)                               
                            chrom2(a1(k))=CHROM2(a(k));                               
                            k=k+1;                                    
                        end
                        chrom2(a1(k))=E;                        
                        judge(d)=10;                         
                        judge(e)=2;                           
                    end
                end
                savingnumber(e,d)=0;
            elseif judge(e)==1&&judge(d)==0                 
                a2=find(chrom2==D);                
                b2=find(chrom2==E);                
                a=find(chrom1==chrom1(b2));                   
                b=sum(quantity(chrom2(a),2))+quantity(D,2);
                bb=sum(quantity(chrom2(a),3))+quantity(D,3);                
                pp1=normcdf(Qofcar,b,sqrt(bb));            
                c=size(a,2);                  
                if pp1>p1                        
                    EFj=arrivetime(e)+ttimeu(E+Numberoffacilities,D+Numberoffacilities)-arrivetime(d);                    
                    if EFj<0                         
                        a1=10000;                            
                        for j=1:c                            
                            b1=find(originalchrom2==chrom2(a(j)));                             
                            Aheadtime=arrivetime(b1)-timewindow(chrom2(a(j)),2);                            
                            if Aheadtime<a1                                      
                                aheadtime=Aheadtime;                                   
                                a1=Aheadtime;                                   
                            end
                        end
                        if aheadtime>=-EFj                            
                            PD=1;                                
                        end
                    elseif EFj==0                        
                        PD=1;                            
                    elseif EFj>0                           
                        a1=10000;                          
                        for j=1:c                              
                            b1=find(originalchrom2==chrom2(a(j)));                             
                            Delaytime=timewindow(chrom2(a(j)),3)-arrivetime(b1);                               
                            if Delaytime<a1                                        
                                delaytime=Delaytime;                                    
                                a1=Delaytime;                                 
                            end
                        end

三、运行结果

在这里插入图片描述

四、备注

版本:2014a

这篇关于【TWVRP】基于matlab节约算法求解带时间窗的电动车路径规划问题【含Matlab源码 1169期】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!