# 【路径规划】基于matlab遗传结合模拟退火算法仓库拣货小车最优路径规划【含Matlab源码 649期】

1 背景

## 二、源代码

``````clear all;
clc;
tic
%%
Good_N = size(Good_P,1); %货物坐标矩阵一维得出货物数量
for i=1:Good_N
if(mod(Good_P(i,2),2)==0)
if(Good_P(i,3)>(Good_P(i,2)/2-1)*3+1)
if(Good_P(i,1)==1)
Good_P(i,1)=4;
else Good_P(i,1)=3;
end
end
else
if(Good_P(i,3)>(Good_P(i,2)-1)/2*3)
if(Good_P(i,1)==1)
Good_P(i,1)=4;
else Good_P(i,1)=3;
end
end
end
end
%% 遗传参数
POP = 100; %种群大小
MAXGEN = 250;%迭代次数
GAP = 0.9;
PC = 0.6;%交叉概率
%PM = 0.9;%变异概率
PM = 0.05;%变异概率
PM_dec = PM/MAXGEN;%变异概率逐渐减小，保证算法收敛
temper = 90;%模拟退火温度
temper_dec =0.99;%温度下降比例
record_length = zeros(MAXGEN,1);
%约束条件及其费用
t = 1.5; %行驶单位距离所用时间s

Cr = 500; %单位小车基本费用角
Cd =0.04; %单位路程油耗角
Cw = 0.02;%单位载重附加油耗角
Ct = 0.1;%时间惩罚单位成本角
C=[Cr,Cd,Cw,Ct];

%%初始化种群
gen =1;
chrom = Init_Pop(POP,Good_N);
temp = zeros(POP,1);
chrom = [temp,chrom];%路径加入起点0

weight_chrom=cell(size(chrom,1),1);
weight_chrom = Distribute(chrom,D,weight);
weight_chrom_old = weight_chrom;%模拟退火记录原种群
Length = Distance(weight_chrom,Good_P,D); %计算种群个体所对应的路径距离
money = Money(Length,weight_chrom,t,C,weight,time,Good_P,D);%计算种群各个个体的路径总成本；
[min_length,min_index_length] = min(Length);
record_length(gen,:) = Length(min_index_length,:);%记录迭代中最优个体
record_mean_length(gen,:) = mean(Length);%记录迭代中个体平均值

record_money(gen,:) = money(min_index_money);
record_mean_money(gen,:)=mean(money);
money_route_length(gen,:)  = Length(min_index_money);
best_route = weight_chrom{min_index_money,1};
best_length = min_length;
best_money = min_money;
%% 种群更新优化
while(gen<=MAXGEN)
Length = Distance(weight_chrom,Good_P,D);
money = Money(Length,weight_chrom,t,C,weight,time,Good_P,D);
[min_length,min_index_length] = min(Length);
record_length(gen,:) = Length(min_index_length,:);
record_mean_length(gen,:) = mean(Length);

[min_money,min_index_money]=min(money);
record_money(gen,:) = money(min_index_money);
record_mean_money(gen,:)=mean(money);
money_route_length(gen,:)  = Length(min_index_money);
if min_money<best_money;
best_money = min_money;
best_route = weight_chrom{min_index_money,1};
end
if min_length <best_length
best_length = min_length;

end
function Length = Distance(weight_chrom,Good_P,D)
POP = size(weight_chrom,1);
Length = zeros(POP,1);

% for i =1:POP
%     for j = 1:(size(chrom,2)-1)%每个个体两点之间，把Good_P
%        Length(i,:) = Length(i,:)+Two_D ( Good_P,chrom(i,j),chrom(i,j+1),D);
%     end
%        Length(i,:) = Length(i,:) + Two_D(Good_P,chrom(i,end),chrom(i,1),D);
% end
function length = Two_D(Good_P,N1,N2,D)

%%
l = D(1);
if(N1==0 ||N2==0)
if(N1==0 )
A1=0;x1=0;y1=0;
A2= Good_P(N2,1);
x2 = Good_P(N2,2);
y2 = Good_P(N2,3);
end
if(N2==0)
A2=0;x2=0;y2=0;
A1 = Good_P(N1,1);
x1 = Good_P(N1,2);
y1 = Good_P(N1,3);
end
else
A1 = Good_P(N1,1);
x1 = Good_P(N1,2);
y1 = Good_P(N1,3);
A2 = Good_P(N2,1);
x2 = Good_P(N2,2);
y2 = Good_P(N2,3);

end
if(mod(x1,2)==1) x1=x1-1;end
if(mod(x2,2)==1) x2=x2-1;end
length_mid1=abs(x1/2+x1+1-y1)*l;
length_mid2=abs(x2/2+x2+1-y2)*l;
length_midcol = abs(x1-x2)/2*3*sqrt(2)*l;
if(A1==0) L1=0; end;
if(A2==0) L2=0; end;
if(A1==1||A1==2)
L1=(x1/2+x1)*sqrt(2)*l+(x1/2+x1+1-y1)*l;%货物到存取点距离
else
L1=(y1-1-(x1/2-1)*3)*l+(x1/2+x1)*sqrt(2)*l;
end
if(A2==1||A2==2)
L2=(x2/2+x2)*sqrt(2)*l+(x2/2+x2+1-y2)*l;
else
L2=(y2-1-(x2/2-1)*3)*l+(x2/2+x2)*sqrt(2)*l;
end%点1和点2到存取点的距离

if((A1==A2&A1==1)||(A1==A2&&A1==2))%1区到1区 2区到2区
if(x1==x2)
length = abs(y1-y2)*l;
else
length = length_mid1+length_midcol+length_mid2;
end

else if((A1==A2&A1==4)||(A1==A2&&A1==3))%4区到4区 3区到3区
if(x1==x2)
length = abs(y1-y2)*l;
else

else  length = L1+L2;
end
end
``````

## 四、备注

【路径规划】基于matlab遗传结合模拟退火算法仓库拣货小车最优路径规划【含Matlab源码 649期】

(0)
(0)