MATLAB 路径规划

## 二、源代码

`````` 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
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)));
end
end
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
else
end
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)));
end
end
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
```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
```