?? 遺傳算法matlab實現源程序 .txt
字號:
新浪博客 > 小劉的生活 > 正文 打印
遺傳算法matlab實現源程序
--------------------------------------------------------------------------------
http://blog.sina.com.cn 2007年04月13日15:00 小劉
標簽:
幾經修煉,終成正果。
借以慰藉我孤獨而高傲的靈魂。
clc;
clear;
%各份訂單基本數據
phen=[1 2 3 4 5 6 7 8 9 10 11 12 13 14
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;
for kem=1:popsize
population(kem,:)=randperm(hromlength);
end
population;
%評價目標函數值
for uim=1:popsize
vector=population(uim,:);
obj(uim)=hanshu(hromlength,vector,phen);
end
%obj
%min(obj)
clear uim;
objmin=min(obj);
for sequ=1:popsize
if obj(sequ)==objmin
opti=population(sequ,:);
end
end
clear sequ;
fmax=22000;
%==
for gen=1:maxgen
%選擇操作
%將求最小值的函數轉化為適應度函數
for indivi=1:popsize
obj1(indivi)=1/obj(indivi);
end
clear indivi;
%適應度函數累加總合
total=0;
for indivi=1:popsize
total=total+obj1(indivi);
end
clear indivi;
%每條染色體被選中的幾率
for indivi=1:popsize
fitness1(indivi)=obj1(indivi)/total;
end
clear indivi;
%各條染色體被選中的范圍
for indivi=1:popsize
fitness(indivi)=0;
for j=1:indivi
fitness(indivi)=fitness(indivi)+fitness1(j);
end
end
clear j;
fitness;
%選擇適應度高的個體
for ranseti=1:popsize
ran=rand;
while (ran>1||ran<0)
ran=rand;
end
ran;
if ran<=fitness(1)
newpopulation(ranseti,:)=population(1,:);
else
for fet=2:popsize
if (ran>fitness(fet-1))&&(ran<=fitness(fet))
newpopulation(ranseti,:)=population(fet,:);
end
end
end
end
clear ran;
newpopulation;
%交叉
for int=1:2:popsize-1
popmoth=newpopulation(int,:);
popfath=newpopulation(int+1,:);
popcross(int,:)=popmoth;
popcross(int+1,:)=popfath;
randnum=rand;
if(randnum< P>
cpoint1=round(rand*hromlength);
cpoint2=round(rand*hromlength);
while (cpoint2==cpoint1)
cpoint2=round(rand*hromlength);
end
if cpoint1>cpoint2
tem=cpoint1;
cpoint1=cpoint2;
cpoint2=tem;
end
cpoint1;
cpoint2;
for term=cpoint1+1:cpoint2
for ss=1:hromlength
if popcross(int,ss)==popfath(term)
tem1=popcross(int,ss);
popcross(int,ss)=popcross(int,term);
popcross(int,term)=tem1;
end
end
clear tem1;
end
for term=cpoint1+1:cpoint2
for ss=1:hromlength
if popcross(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
clear tem1;
end
end
clear term;
end
clear randnum;
popcross;
%變異操作
newpop=popcross;
for int=1:popsize
randnum=rand;
if randnum
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);
if cpoint22==0;
cpoint22=1;
end
end
temp=newpop(int,cpoint12);
newpop(int,cpoint12)=newpop(int,cpoint22);
newpop(int,cpoint22)=temp;
end
end
newpop;
clear cpoint12;
clear cpoint22;
clear randnum;
clear int;
for ium=1:popsize
vector1=newpop(ium,:);
obj1(ium)=hanshu(hromlength,vector1,phen);
end
clear ium;
obj1max=max(obj1);
for ar=1:popsize
if obj1(ar)==obj1max
newpop(ar,:)=opti;
end
end
clear population;
clear objmin;
clear objmean;
%遺傳操作結束
population=newpop;
for ium=1:popsize
vector2=population(ium,:);
obj(ium)=object(hromlength,vector2,phen);
end
objmin=min(obj);
objmean=mean(obj);
clear opti;
for sequ1=1:popsize
if obj(sequ1)==objmin
opti=population(sequ1,:);
end
end
solution=objmin;
final(gen)=objmin;
final1(gen)=objmean;
end
opti
solution
plot(final);
hold on;
plot(final1,'--')
hold off
%目標函數值子函數
function[cost]=hanshu(hromlength,vector,phen)
wmax=20000;
ct=1.2;
ch=0.5;
for num=1:hromlength
line=vector(num);
s(:,num)=phen(:,line);
end
m=1;
cshort=0;
chold=0;
ctrans=0;
while m<=hromlength
j=m;
weight=s(4,j);
day=s(6,j);
dis=sqrt(s(2,j)^2+s(3,j)^2);
while ((j< P>
weight=weight+s(4,j+1);
if (s(6,j+1)< P>
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;
文章來源:http://blog.sina.com.cn/s/blog_3ea92bd5010009rt.html
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -