clear
clc
close all
tic
%% 用importdata这个函数来读取文件
shuju= xlsread('shuju.xlsx', 'Sheet1');
bl=0;
cap=100; %车辆最大装载量
%% 提取数据信息
zuobiao=shuju(:,2:3); %所有点的坐标x和y
customer=zuobiao(2:end,:); %顾客坐标
cusnum=size(customer,1); %顾客数
v_num=8; %车辆最多使用数目
demands=shuju(2:end,4); %需求量
a=shuju(2:end,5); %顾客时间窗开始时间[a[i],b[i]]
b=shuju(2:end,6); %顾客时间窗结束时间[a[i],b[i]]
s=shuju(2:end,7); %客户点的服务时间
%% 距离矩阵
h=pdist(zuobiao);
lldist=squareform(h).*1.5; %路路距离矩阵
htdist=squareform(h); %飞机距离矩阵
hydist=squareform(h).*1.2; %%火车距离矩阵
%% 遗传算法参数设置
alpha=100000; %违反的容量约束的惩罚函数系数
belta=0.5;%违反时间窗约束的惩罚函数系数
belta2=0.5;
chesu=[1,5,2];
NIND=300; %种群大小
MAXGEN=500; %迭代次数
Pc=0.9; %交叉概率
Pm=0.05; %变异概率
GGAP=0.9; %代沟(Generation gap)
N=cusnum+v_num-1; %染色体长度=顾客数目+车辆最多使用数目-1
%% 粒子群参数
lx=3;
w=1; %惯性因子
wdamp=0.99; %惯性因子衰减率
c1=1.5; %个体学习因子
c2=2.0; %全局学习因子
XvMin=1; %Xv下限
XvMax=lx; %Xv上限
VvMin=-(lx-1); %Vv下限
VvMax=lx-1; %Vv上限
%% 初始化种群
Chrom=InitPopCW(NIND,N,cusnum,a,demands,cap,XvMin,XvMax,VvMin,VvMax); %构造初始解
%% 输出随机解的路线和总距离
disp('初始种群中的一个随机值:')
[VC,NV,PD]=decode(Chrom{1},cusnum);
% disp(['总距离:',num2str(TD)]);
disp(['车辆使用数目:',num2str(NV)]);
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
%% 优化
gen=1;
figure;
hold on;box on
xlim([0,MAXGEN])
title('优化过程')
xlabel('代数')
ylabel('最优值')
ObjV=calObj(Chrom,cusnum,cap,demands,a,b,s,lldist,htdist,hydist,alpha,belta,belta2,chesu,bl); %计算种群目标函数值
preObjV=min(ObjV);
[~,minInd]=min(ObjV);
gbest_pos=Chrom{minInd,1}(2,:); %假设第一个粒子位置为全局最优位置
gbest_obj=preObjV; %第一个粒子位置的目标函数值
pbest_pos=cell(NIND,1); %初始化各个粒子的个体最优位置
pbest_obj=ObjV; %初始化各个粒子的个体最优的目标函数值
for i=1:NIND
particle=Chrom{i,1}; %第i个粒子
position=particle(2,:); %第i个粒子的位置
pbest_pos{i,1}=position; %初始化这个粒子的个体最优
if pbest_obj(i,1)<gbest_obj
%更新初始种群中的全局最优粒子
gbest_obj=pbest_obj(i,1);
gbest_pos=position;
end
end
%%
while gen<=MAXGEN
%% 计算适应度
ObjV=calObj(Chrom,cusnum,cap,demands,a,b,s,lldist,htdist,hydist,alpha,belta,belta2,chesu,bl); %计算种群目标函数值
line([gen-1,gen],[preObjV,min(ObjV)]);pause(0.0001)%画图 最优函数
preObjV=min(ObjV);
FitnV=Fitness(ObjV);
%% 选择
SelCh=Select(Chrom,FitnV,GGAP);
%% OX交叉操作
SelCh=Recombin(SelCh,Pc);
%% 变异
SelCh=Mutate(SelCh,Pm);
%% 重插入子代的新种群
Chrom=Reins(Chrom,SelCh,ObjV);
ObjV=calObj(Chrom,cusnum,cap,demands,a,b,s,lldist,htdist,hydist,alpha,belta,belta2,chesu,bl);
%% 更新各个粒子的位置和速度
Chrom1=Chrom;
for i=1:NIND
particle=Chrom{i,1}; %第i个粒子
position=particle(2,:); %第i个粒子的位置
Xv=position(1,:);
velocity=particle(3,:); %第i个粒子的速度
Vv=velocity(1,:);
%% 更新速度
velocity=w*velocity+ +c1*rand([1,N]).*(pbest_pos{i,1}-position)...
+c2*rand([1,N]).*(gbest_pos-position);
%% 速度越界处理
velocity(1,:)=max(velocity(1,:),VvMin);
velocity(1,:)=min(velocity(1,:),VvMax);
%% 更新位置
position=position+velocity;
position(1,:)=ceil(position(1,:)); %对Xv向上取整
%% 速度镜像影响
IsOutside=(position(1,:)<XvMin | position(1,:)>XvMax );
velocity(IsOutside)=-velocity(IsOutside);
%% 位置越界处理
position(1,:)=max(position(1,:),XvMin);
position(1,:)=min(position(1,:),XvMax);
Chrom1{i,1}(2,:)=position;
end
ObjV1=calObj(Chrom1,cusnum,cap,demands,a,b,s,lldist,htdist,hydist,alpha,belta,belta2,chesu,bl);
%% 重插入子代的新种群
Chrom=Copy_of_Reins(Chrom,Chrom1,ObjV1,NIND,ObjV);
%% 打印当前最优解
ObjV=calObj(Chrom,cusnum,cap,demands,a,b,s,lldist,htdist,hydist,alpha,belta,belta2,chesu,bl); %计算种群目标函数值
[~,minInd]=min(ObjV);
for i=1:NIND
if min(ObjV)<ObjV(i)
particle=Chrom{minInd,1};
position=particle(2,:);
pbest_pos{i,1}=position;
pbest_obj(i,1)=ObjV(i);
%% 更新全局最优
if preObjV<gbest_obj
gbest_pos=pbest_pos{i,1};
gbest_obj=pbest_obj(i,1);
end
end
end
disp(['第',num2str(gen),'代最优解:'])
fprintf('\n')
%% 更新迭代次数
gen=gen+1 ;
end
%% 画出最优解的路线图
ObjV=calObj(Chrom,cusnum,cap,demands,a,b,s,lldist,htdist,hydist,alpha,belta,belta2,chesu,bl); %计算种群目标函数值
[minObjV,minInd]=min(ObjV);
%% 输出最优解的路线和总距离
disp('最优解:')
bestChrom=Chrom{minInd(1)};
[VC,NV,PD]=decode(bestChrom,cusnum);
disp('-------------------------------------------------------------')
[cost]=costFuction(VC,NV,PD,a,b,s,lldist,htdist,hydist,demands,cap,alpha,belta,belta2,chesu,bl);
disp(['总成本:',num2str(cost)]);
disp(['路路:',num2str(1)]);
disp(['航天:',num2str(2)]);
disp(['海运:',num2str(3)]);
%% 画出最终路线图
draw_Best(VC,zuobiao);
for i=1:NV
part_seq=PD{i}; %每辆车所经过的门店
len=length(part_seq); %每辆车所经过的门店数量
for j=0:len
if j==0
fprintf('%s','选择方式:');
fprintf('%d');
elseif j==len
fprintf('%d->',part_seq(j));
fprintf('%d');
fprintf('\n');
else
fprintf('%d->',part_seq(j));
end
end
end
% toc
研究生毕业难度matlab设计多式联运求解完整版
版权申诉
5星 · 超过95%的资源 37 浏览量
2022-04-13
20:40:52
上传
评论 12
收藏 27KB ZIP 举报
张叔zhangshu
- 粉丝: 1w+
- 资源: 198
最新资源
- __init__.py
- tensorflow-gpu-2.6.5-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-rocm-2.13.1.600-cp38-cp38-manylinux2014-x86-64.whl
- tensorflow-rocm-2.13.0.570-cp311-cp311-manylinux2014-x86-64.whl
- 3ds_1.0_3.6.ipa
- 卷积神经网络(Convolutional Neural Network,CNN
- 网页设计HTML、CSS和JS网页设计HTML、CSS和JS
- 利用Python构建作物病虫害识别分类系统
- 经济学人 2024.3.30
- <!- 表单标签: 1.表单域:form标签包含的区域 2.表单标签:输入input、选择select、文本
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页