楼主: mayuqi
2660 7

[实际应用] matlab遗传算法源程序 [推广有奖]

  • 1关注
  • 2粉丝

已卖:758份资源

大专生

36%

还不是VIP/贵宾

-

威望
0
论坛币
9620 个
通用积分
1.9750
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
852 点
帖子
44
精华
0
在线时间
27 小时
注册时间
2009-10-23
最后登录
2014-11-6

楼主
mayuqi 发表于 2012-2-6 18:05:49 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
matlab遗传算法源代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:MATLAB matla atlab 遗传算法 Mat 算法 matlab 源程序

美丽的不是这个世界,而是接受了这个世界的你的眼睛

沙发
石瑞 在职认证  发表于 2012-2-6 18:18:10
关注一下
假如爱有天意!

藤椅
mayuqi 发表于 2012-2-6 20:05:58
不好意思~~附件没传上来~~
美丽的不是这个世界,而是接受了这个世界的你的眼睛

板凳
wlou65 发表于 2012-2-6 20:49:31
请挂附件啊!真的能运行吗

报纸
pange365 发表于 2012-3-8 15:40:13
没附件呀
敛气凝神

地板
saicc 发表于 2012-4-14 00:38:03
????????

7
tracychenxia 发表于 2013-6-11 19:40:56
楼主逗呢

8
matlab-007 发表于 2015-2-6 19:31:46
matlab遗传算法源代码
function Main()
%定义全局变量
global VariableNum  POPSIZE   MaxGens PXOVER  PMutation

VariableNum=3  %变量个数
POPSIZE=50     %种群大小
MaxGens=1000   %种群代数
PXOVER=0.8     %交叉概率
PMutation=0.2  %变异概率
%读取数据文件
load E:\现代优化算法\遗传算法\bound.txt
VarBound=bound(:,1:2);

global Pop newPop
Pop=zeros(POPSIZE+1,VariableNum);
newPop=zeros(POPSIZE+1,VariableNum);
%初始化种群
for i=1:POPSIZE
    for j=1:VariableNum
        Pop(i,j)=VarBound(j,1)+rand()*(VarBound(j,2)-VarBound(j,1));        
    end
end
%计算适应值
fitnessList=zeros(POPSIZE,1);
for i=1:POPSIZE
    fitnessList(i,1)=fitness(Pop(i,1:VariableNum));
end
%保存最好值和最坏值
Best=zeros(1,VariableNum+1);
Worst=zeros(1,VariableNum+1);
maxvalue=max(fitnessList);
indexMax=find(fitnessList==maxvalue,1,'first');
Best(1,1:VariableNum)=Pop(indexMax,1:VariableNum);
Best(1,VariableNum+1)=maxvalue;
minvalue=min(fitnessList);
indexMin=find(fitnessList==minvalue,1,'first');
Worst(1,1:VariableNum)=Pop(indexMin,1:VariableNum);
Worst(1,VariableNum+1)=minvalue;
genetation=1;
while genetation<MaxGens
    %计算适应度区间
    sumfit=sum(abs(fitnessList));
    relativeFitness=zeros(POPSIZE,1);
    relativeFitness=abs(fitnessList)/sumfit;
    for i=2:POPSIZE
        relativeFitness(i)=relativeFitness(i-1)+relativeFitness(i);
    end
    %选择操作
    newPop=Select(Pop,relativeFitness);
    %交叉操作
    newPop=Xcross(newPop,VariableNum,PXOVER);
    %变异操作
    newPop=Mutation(newPop,VariableNum,PMutation,VarBound);
    %计算新种群适应值
    for i=1:POPSIZE
        fitnessList(i,1)=fitness(newPop(i,1:VariableNum));
    end
    %保存最好值和替换最坏值
    maxvalue=max(fitnessList);
    indexMax=find(fitnessList==maxvalue,1,'first');
    minvalue=min(fitnessList);
    indexMin=find(fitnessList==minvalue,1,'first');
    if Best<maxvalue
        Best(1,1:VariableNum)=newPop(indexMax,1:VariableNum);
        Best(1,VariableNum+1)=maxvalue;
    else
        newPop(indexMin,1:VariableNum)=Best(1,1:VariableNum);
        fitnessList(indexMin,1)=Best(1,VariableNum+1);
    end
    %用子代替换父代
    Pop=newPop;
    genetation=genetation+1;
end
Best

=========================================================

%选择操作

function newPop=Select(Pop,Rfitness)
for i=1:length(Rfitness)
    r=rand();
    index=1;
    for j=1:length(Rfitness)
        if r<=Rfitness(j,1)
            index=j;
            break;
        end
    end
    newPop(i,:)=Pop(index,:);
end
   
======================================

%交叉操作

function newPop=Xcross(Pop,VariableNUM,CrossRate)
point=1;
sizePop=length(Pop);
for i=0:sizePop/2
    Xrate=rand();
    if Xrate<CrossRate  %如果交叉
        first_index=round(rand()*(sizePop-2)+1);
        second_index=round(rand()*(sizePop-2)+1);
        while first_index==second_index   %排除两个个体一样的情况
            second_index=round(rand()*(sizePop-2)+1);
        end
        if VariableNUM>1
           if VariableNUM==2
              point=1;
           else
               point=round(rand()*(VariableNUM-2)+1);
           end
           tempOne=zeros(1,point);
           tempOne(1,1:point)=Pop(first_index,1:point);
           Pop(first_index,1:point)=Pop(second_index,1:point);
           Pop(second_index,1:point)=tempOne(1,1:point);
       end
    end
end
newPop=zeros(size(Pop),1);
newPop=Pop;
   
====================================================

%变异操作

function newPop=Mutation(Pop,VariableNUM,MutationRate,bound)
point=1;
sizePop=length(Pop);
for i=1:sizePop
    for j=1:VariableNUM
        Mrate=rand();
        if Mrate<MutationRate  %如果发生变异
         Pop(i,j)= rand()*(bound(j,2)-bound(j,1))+ bound(j,1);  
        end
    end
end
newPop=zeros(size(Pop),1);
newPop=Pop;

=================================================

%适应值函数或目标函数

%函数 x1^2-x1*x2+x3
function value=fitness(varargin)
n=varargin{1,1};
value=n(1,1)^2-n(1,1)*n(1,2)+n(1,3);

================================================

实例:bound文件

1 10
0 5
10 20   

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 02:00