遗传算法matlab实现源程序文件.docx
《遗传算法matlab实现源程序文件.docx》由会员分享,可在线阅读,更多相关《遗传算法matlab实现源程序文件.docx(8页珍藏版)》请在冰豆网上搜索。
遗传算法matlab实现源程序文件
clc;
clear;
%各份订单基本数据
phen=[1234567891011121314
41,52,-23,-46,-143,-74,-56,101,73,74,95,86,-35,32
65,23,-76,104,34,38,4,-23,55,-49,39,89,-86,52
7716,9887,12188,8819,4002,6119,3284,4607,5600,4587,9821,13024,6547,2684
500,400,1000,120,0,235,654,241,0,361,120,254,300,150
1,4,2,2,4,4,3,3,3,1,4,5,1,3
2.7,1.8,4,2.5,1.6,1,3.6,5,4.2,1.9,6.4,2.8,1.4,8];
hromlength=14;
popsize=30;
maxgen=500; pc=0.8;
pm=0.04;
forkem=1:
popsize
population(kem,:
)=randperm(hromlength);
end
population;
%评价目标函数值
foruim=1:
popsize
vector=population(uim,:
);
obj(uim)=hanshu(hromlength,vector,phen);
end
%obj
%min(obj)
clearuim;
objmin=min(obj);
forsequ=1:
popsize
ifobj(sequ)==objmin
opti=population(sequ,:
);
end
end
clearsequ;
fmax=22000;
%==
forgen=1:
maxgen
%选择操作
%将求最小值的函数转化为适应度函数
forindivi=1:
popsize
obj1(indivi)=1/obj(indivi);
end
clearindivi;
%适应度函数累加总合
total=0;
forindivi=1:
popsize
total=total+obj1(indivi);
end
clearindivi;
%每条染色体被选中的几率
forindivi=1:
popsize
fitness1(indivi)=obj1(indivi)/total;
end
clearindivi;
%各条染色体被选中的范围
forindivi=1:
popsize
fitness(indivi)=0;
forj=1:
indivi
fitness(indivi)=fitness(indivi)+fitness1(j);
end
end
clearj;
fitness;
%选择适应度高的个体
forranseti=1:
popsize
ran=rand;
while(ran>1||ran<0)
ran=rand;
end
ran;
ifran<=fitness
(1)
newpopulation(ranseti,:
)=population(1,:
);
else
forfet=2:
popsize
if(ran>fitness(fet-1))&&(ran<=fitness(fet))
newpopulation(ranseti,:
)=population(fet,:
);
end
end
end
end
clearran;
newpopulation;
%交叉
forint=1:
2:
popsize-1
popmoth=newpopulation(int,:
);
popfath=newpopulation(int+1,:
);
popcross(int,:
)=popmoth;
popcross(int+1,:
)=popfath;
randnum=rand;
if(randnum
cpoint1=round(rand*hromlength);
cpoint2=round(rand*hromlength);
while(cpoint2==cpoint1)
cpoint2=round(rand*hromlength);
end
ifcpoint1>cpoint2
tem=cpoint1;
cpoint1=cpoint2;
cpoint2=tem;
end
cpoint1;
cpoint2;
forterm=cpoint1+1:
cpoint2
forss=1:
hromlength
ifpopcross(int,ss)==popfath(term)
tem1=popcross(int,ss);
popcross(int,ss)=popcross(int,term);
popcross(int,term)=tem1;
end
end
cleartem1;
end
forterm=cpoint1+1:
cpoint2
forss=1:
hromlength
ifpopcross(int+1,ss)==popmoth(term)
tem1=popcross(int+1,ss);
popcross(int+1,ss)=popcross(int+1,term);
popcross(int+1,term)=tem1;
end
end
cleartem1;
end
end
clearterm;
end
clearrandnum;
popcross;
%变异操作
newpop=popcross;
forint=1:
popsize
randnum=rand;
ifrandnum
cpoint12=round(rand*hromlength);
cpoint22=round(rand*hromlength);
if(cpoint12==0)
cpoint12=1;
end
if(cpoint22==0)
cpoint22=1;
end
while(cpoint22==cpoint12)
cpoint22=round(rand*hromlength);
ifcpoint22==0;
cpoint22=1;
end
end
temp=newpop(int,cpoint12);
newpop(int,cpoint12)=newpop(int,cpoint22);
newpop(int,cpoint22)=temp;
end
end
newpop;
clearcpoint12;
clearcpoint22;
clearrandnum;
clearint;
forium=1:
popsize
vector1=newpop(ium,:
);
obj1(ium)=hanshu(hromlength,vector1,phen);
end
clearium;
obj1max=max(obj1);
forar=1:
popsize
ifobj1(ar)==obj1max
newpop(ar,:
)=opti;
end
end
clearpopulation;
clearobjmin;
clearobjmean;
%遗传操作结束
population=newpop;
forium=1:
popsize
vector2=population(ium,:
);
obj(ium)=object(hromlength,vector2,phen);
end
objmin=min(obj);
objmean=mean(obj);
clearopti;
forsequ1=1:
popsize
ifobj(sequ1)==objmin
opti=population(sequ1,:
);
end
end
solution=objmin;
final(gen)=objmin;
final1(gen)=objmean;
end
opti
solution
plot(final);
holdon;
plot(final1,'--')
holdoff
%目标函数值子函数
function[cost]=hanshu(hromlength,vector,phen)
wmax=20000;
ct=1.2;
ch=0.5;
fornum=1:
hromlength
line=vector(num);
s(:
num)=phen(:
line);
end
m=1;
cshort=0;
chold=0;
ctrans=0;
whilem<=hromlength
j=m;
weight=s(4,j);
day=s(6,j);
dis=sqrt(s(2,j)^2+s(3,j)^2);
while((j
weight=weight+s(4,j+1);
if(s(6,j+1)
cshort=(s(5,j+1))*(s(7,j+1))*0.1+cshort;
chold=(s(4,j+1))*ch+chold;
end
dis=sqrt((s(2,j)-s(2,j+1))^2+(s(3,j)-s(3,j+1))^2);
j=j+1;
end
dis=dis+sqrt(s(2,j)^2+s(3,j)^2);
ctrans=ctrans+dis*weight*ct;
m=j+1;
end
cost=cshort+chold+ctrans;