## 2 部分代码

```%遗传算法 VRP 问题 Matlab实现
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%tic%计时器
clear;
clc
W=80; %每辆车的载重量
Citynum=50; %客户数量
Stornum=4;%仓库个数
%C     %%第二三列 客户坐标，第四列 客户需求   51,52,53,54为四个仓库
%淘汰种群
species=[children;Parent];
L=[Lc;Lp];
[m n]=sort(L);
species=species(n(1:G),:);  %更新世代
L=L(n(1:G));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%加入Opt优化
%分配仓库进行opt
temp=initialStor(Citynum,Clist(:,4),W,species(1,:),Stornum,dislist);%存储分配仓库后的结果【52 14 5 52 53 6 9 8 53......】
Rbest=temp;
L_best=L(1);
[m n]=size(temp);
start=1;
car=[];%存放opt优化后的结果
i=2;
while (i<n+1)
if (temp(i)>Citynum)
cur=[];
cur=Opt(i-start,[1:i-start,1:i-start],dislist,temp(start:i-1),Citynum);
car=[car,[cur,cur(1)]];
start=i+1;
i=i+2;
else
i=i+1;
end
end
L1=CalDist(dislist,car,Citynum);%计算进行优化后的回路长度
if( L1<L(1))
fprintf('Opt优化有效! %f --> %f',L(1),L1);
Rbest=car;
car(find(car>Citynum))=[];%去掉编码中的仓库，再加入父代
species=[species;car];
L=[L;L1];
L_best=L1;
end
L_best
toc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
%%
Rbest%最优线路
L_best%最优解
%%画图
[m n]=size(Rbest);
start=1;
temp=[];
i=2;
while(i<=n)
if(Rbest(i)>Citynum)
temp=Rbest(start:i);

plot(Clist(temp,2),Clist(temp,3),'-*')
hold on;
start=i+1;
i=i+2;
else
i=i+1;
end
end
plot(Clist(Citynum+1:Citynum+Stornum,2),Clist(Citynum+1:Citynum+Stornum,3),'or')
%toc```

## 4 参考文献

[1]邓欣. 基于遗传算法的多车场车辆路径问题研究. Diss. 重庆大学.