二、遗传算法简介
1 引言
2 遗传算法理论
2.1 遗传算法的生物学基础
2.2 遗传算法的理论基础
2.3 遗传算法的基本概念
2.4 标准的遗传算法
2.5 遗传算法的特点
2.6 遗传算法的改进方向
3 遗传算法流程
4 关键参数说明
三、部分源代码
%% 清空环境
clear
clc
global W Cmax Mxf1 Mxf2 Mxf3 Mxf4 Q0 b1 b2 b3 b4 N
%% 已知参数
W=[9 14 5 9 8 7 8 7 6 10 10 9 8 6 5 10 8 9 11]; %零流时间
Cmax=[700 900 800 600 600 900 600 300 900 500 700 600 700 500 400 800 700 700 600]; %路段能力
%路段-路径矩阵
Mxf1=[1,1,1,1,1,1,0
0,0,0,0,0,0,1
1,1,1,0,0,0,0
0,0,0,1,1,1,0
0,0,0,0,0,0,0
0,0,0,0,0,0,0
0,0,0,0,0,0,0
0,0,0,0,0,0,0
1,0,0,0,0,0,0
0,1,1,0,0,0,0
0,0,0,1,1,0,0
0,0,0,0,0,1,1
0,1,0,1,0,0,0
0,0,1,0,1,0,0
1,0,0,0,0,0,0
0,0,0,0,0,0,0
1,1,0,1,0,0,0
0,0,0,0,0,0,0
0,0,1,0,1,1,1];
Mxf2=[1,1,1,1,0
0,0,0,0,1
1,1,1,0,0
0,0,0,1,0
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
0,0,0,0,0
1,1,0,0,0
0,0,1,0,0
0,0,0,1,1
0,0,0,0,0
0,0,1,1,1
0,0,0,0,0
0,1,0,0,0
1,0,0,0,0
0,0,0,0,0
0,1,1,1,1
0,0,0,0,0];
Mxf3=[0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0
0,0,0,0,1,1,1,0,0,0
1,1,1,0,0,0,0,0,0,0
0,0,0,1,0,0,0,1,1,1
1,1,1,0,1,1,1,0,0,0
0,0,0,0,0,0,0,1,1,1
0,0,0,1,0,0,0,0,0,0
0,0,0,0,0,0,1,0,0,1
0,0,0,0,1,1,0,1,1,0
0,1,1,0,0,0,0,0,0,0
1,0,0,0,0,0,0,0,0,0
0,0,1,0,0,1,0,0,1,0
0,1,0,0,1,0,0,1,0,0
0,0,0,1,0,0,1,0,0,1
0,0,0,0,0,0,0,0,0,0
0,0,1,1,0,1,1,0,1,1
0,0,0,0,0,0,0,0,0,0
1,1,0,0,1,0,0,1,0,0];
Mxf4=[0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,1,1,1,0
0,0,0,0,0,0,0,0,1
1,1,1,1,1,0,0,0,0
0,0,0,0,0,1,1,1,1
0,0,1,1,1,0,0,0,0
1,1,0,0,0,0,0,0,0
0,0,1,1,0,1,1,0,0
0,0,0,0,1,0,0,1,0
0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0
0,0,0,0,1,0,0,1,1
0,0,0,0,0,0,0,0,0
0,1,0,1,0,0,1,0,0
1,0,1,0,0,1,0,0,0
0,0,0,0,0,0,0,0,0
0,1,0,1,1,0,1,1,1
0,0,0,0,0,0,0,0,0];
b1=length(Mxf1(1,:));
b2=length(Mxf2(1,:));
b3=length(Mxf3(1,:));
b4=length(Mxf4(1,:));
%% OD对需求
Q0=[300 500 700 300];
%% 设置遗传参数
MAXGEN=500; %最大迭代代数
popsize=500; %设置初始参数,种群大小
pc=0.8; %设置交叉概率
pm=0.1; %设置变异概率
N=b1+b2+b3+b4-4; %编码长度
Chrom=zeros(popsize,N);%创建初始种群
for i=1:popsize
Chrom(i,:)=crtzq(N);
end
ObjV=zeros(popsize,1);
for i=1:popsize
ObjV(i)=Objfun(Chrom(i,:));
end
[objv,f]=sort(ObjV);
bjv(1:popsize-1)=objv(1:popsize-1);
Chrom=Chrom(f,:);
chrom=Chrom(1:popsize-1,:);
fsum=sum(bjv);
Pperpopulation=bjv/fsum;
%计算累积概率
cumsump(1)=Pperpopulation(1);
for i=2:popsize-1
cumsump(i)=cumsump(i-1)+Pperpopulation(i);
end
cumsump=cumsump';
Generation=1;
y=zeros(MAXGEN,2);
while Generation<MAXGEN+1
for j=1:2:popsize-1
%选择操作
seln=selection(cumsump);
scro=crossover(chrom,seln,pc);
scnew(j,:)=scro(1,:);
scnew(j+1,:)=scro(2,:);
%变异操作
smnew(j,:)=mutation(scnew(j,:),pm);
smnew(j+1,:)=mutation(scnew(j+1,:),pm);
end
chrom=smnew; %产生了新的种群
Chrom(1:popsize-1,:)=chrom(1:popsize-1,:);
for i=1:1:size(Chrom,1)
[ObjV(i)]=Objfun(Chrom(i,:));
end
[objv,f]=sort(ObjV);
bjv(1:popsize-1)=objv(1:popsize-1);
Chrom=Chrom(f,:);
y(Generation,1)=1e7/Objfun(Chrom(end,:));
y(Generation,2)=mean(1e7./ObjV);
chrom=Chrom(1:popsize-1,:);
fsum=sum(bjv);
Pperpopulation=bjv/fsum;
%计算累积概率
cumsump(1)=Pperpopulation(1);
for i=2:popsize-1
cumsump(i)=cumsump(i-1)+Pperpopulation(i);
end
Generation=Generation+1
end
%子程序:判断遗传运算是否需要进行交叉或变异, 函数名称存储为IfCroIfMut.m
function pcc=IfCroIfMut(mutORcro)
test(1:100)=0;
l=round(100*mutORcro);
test(1:l)=1;
n=round(rand*99)+1;
pcc=test(n);
%子程序:新种群交叉操作,函数名称存储为crossover.m
function scro=crossover(Chrom,seln,pc)
global N
pcc=IfCroIfMut(pc);
if pcc==1
chb=randperm(N-1,1);
scro(1,:)=[Chrom(seln(1),1:chb) Chrom(seln(2),chb+1:N)];
scro(2,:)=[Chrom(seln(2),1:chb) Chrom(seln(1),chb+1:N)];
else
scro(1,:)=Chrom(seln(1),:);
scro(2,:)=Chrom(seln(2),:);
end
四、运行结果
五、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
《智能优化算法及其MATLAB实例(第2版)》包子阳 余继周 杨杉著 电子工业出版社