⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 遗传算法matlab实现源程序 .txt

📁 遗传算法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 + -