请选择 进入手机版 | 继续访问电脑版

QQ登录

只需要一步,快速开始

 注册地址  找回密码

免疫算法

2020-2-29 11:16| 发布者: madio| 查看: 462| 评论: 9|原作者: kelimasa

摘要: 染色体(即可能解的序号)定义一个概率(关键步骤) %%%%%%%%%%%%%%%好的染色体概率高,坏的概率低。依据误差functionError计算概率 =chromosomeProbability(functionError); if trueP =='Fail' '可能解严重不适应方 ...
染色体(即可能解的序号)定义一个概率(关键步骤)
%%%%%%%%%%%%%%%好的染色体概率高,坏的概率低。依据误差functionError计算概率
[p,trueP]=chromosomeProbability(functionError);
if trueP =='Fail'
'可能解严重不适应方程,请重新开始'
return%结束程序
end
%%%%%%%%%%%%%%%7:按照概率筛选染色体(关键步骤)
%fa=bin2dec(fatherChromosomeGroup)%显示父染色体
%从父染体中选择优秀染色体
%selecteChromosomeGroup=selecteChromosome(fatherChromosomeGroup,p);
%%%%%%%%%%%%%%%8:染色体杂交(关键步骤)
%sle=bin2dec(selecteChromosomeGroup)%显示选择出来的解的序号(染色体)
%用概率筛选出的染色体selecteChromosomeGroup进行杂交,产生子代染色体
%sonChromosomeGroup=crossChromosome(selecteChromosomeGroup,2);
%不用概率筛选出的染色体selecteChromosomeGroup进行杂交,而直接用上一代(父代)的
sonChromosomeGroup=crossChromosome(fatherChromosomeGroup,2);
%sonChromosomeGroup=immunity(fatherChromosomeGroup,holdBestChromosome,3);
%把疫苗接种到其它染色体中
sonChromosomeGroup=immunity(sonChromosomeGroup,holdBestChromosome,3);
%cro=bin2dec(sonChromosomeGroup)%显示杂交后的子代染色体
sonChromosomeGroup=checkSequence(sonChromosomeGroup,solutionN);%检查杂交后的染色体是否越界
%%%%%%%%%%%%%%%9:变异
%不杂交直接变异
%fatherChromosomeGroup=varianceCh(fatherChromosomeGroup,0.1,solutionN);
%杂交后变异
fatherChromosomeGroup=varianceCh(sonChromosomeGroup,0.5,solutionN);
fatherChromosomeGroup=checkSequence(fatherChromosomeGroup,solutionN);%检查变异后的染色体是否越界
end

 

接种疫苗函数,这是和遗传算法唯一不同的函数,可以用它代替染色体的交叉操作。

%chromosomeGroup:染色体组
%bachterinChromosome:疫苗染色体,即最好的染色体。从这个染色体上取疫苗
%parameter:接种疫苗的参数,即用什么方法接种
%inoculateChromosome:接种疫苗后的染色体
function inoculateChromosome=immunity(chromosomeGroup,bacterinChromosome,parameter)
[chromosomeGroupSum,chromosomeLength]=size(chromosomeGroup);
[row,bacterinChromosomeLength]=size(bacterinChromosome);
%chromosomeGroupSum:染色体的条数;chromosomeLength:染色体的长度
switch parameter
case 1%随机选择染色体进行接种
for i=1:chromosomeGroupSum
%%%%%%%%%%%%从疫苗染色体上定位疫苗
headDot=fix(rand(1)*bacterinChromosomeLength);
%疫苗在染色体上左边的点位
if headDot==0%防止出现0点位
headDot=1;
end
tailDot=fix(rand(1)*bacterinChromosomeLength);
%疫苗在染色体上右边的点位
if tailDot==0%防止出现0点位
tailDot=1;
end
if tailDot>headDot%防止右边的点位大于左边的点位
dot=headDot;
headDot=tailDot;
tailDot=dot;
end
%%%%%%%%%%%%%接种
randChromosomeSequence=round(rand(1)*chromosomeGroupSum);
%随机产生1条染色体的序号,对这条染色体进行接种
if randChromosomeSequence==0%防止产生0序号
randChromosomeSequence=1;
end
inoculateChromosome(i,:)...%先把输入染色体传给输出
=chromosomeGroup(randChromosomeSequence,:);
%执行免疫,即从疫苗染色体上取出一段基因做疫苗,再注入到其它染色体中
inoculateChromosome(i,headDot:tailDot)...
=bacterinChromosome(1,headDot:tailDot);
end
case 2 %所有染色体挨个接种
for i=1:chromosomeGroupSum
%%%%%%%%%%%%从疫苗染色体上定位疫苗
headDot=fix(rand(1)*bacterinChromosomeLength);
%疫苗在染色体上左边的点位
if headDot==0%防止出现0点位
headDot=1;
end
tailDot=fix(rand(1)*bacterinChromosomeLength);
%疫苗在染色体上右边的点位
if tailDot==0%防止出现0点位
tailDot=1;
end
if tailDot>headDot%防止右边的点位大于左边的点位
dot=headDot;
headDot=tailDot;
tailDot=dot;
end
%%%%%%%%%%%%%接种
inoculateChromosome(i,:)=chromosomeGroup(i,:);%先把输入染色体传给输出
%执行免疫,即从疫苗染色体上取出一段基因做疫苗,再注入到其它染色体中
inoculateChromosome(i,headDot:tailDot)...
=bacterinChromosome(1,headDot:tailDot);
end
case 3 %接种位置是随机的
for i=1:chromosomeGroupSum
%%%%%%%%%%%%从疫苗染色体上定位疫苗
headDot=fix(rand(1)*bacterinChromosomeLength);
%疫苗在染色体上左边的点位
if headDot==0%防止出现0点位
headDot=1;
end
tailDot=fix(rand(1)*bacterinChromosomeLength);
%疫苗在染色体上右边的点位
if tailDot==0%防止出现0点位
tailDot=1;
end
if tailDot>headDot%防止右边的点位大于左边的点位
dot=headDot;
headDot=tailDot;
tailDot=dot;
end
%%%%%%%%%%%%%在染色体上随机定位接种位置
inoculateDot=fix(rand(1)*chromosomeLength);%随机选择染色体的接种点位
if inoculateDot==0
inoculateDot=1;
inoculateChromosome(i,:)=chromosomeGroup(i,:);
inoculateChromosome(i,inoculateDot:tailDot-headDot+1)...
=bacterinChromosome(1,headDot:tailDot);
elseif inoculateDot<=headDot
inoculateChromosome(i,:)=chromosomeGroup(i,:);
inoculateChromosome(i,inoculateDot:inoculateDot+tailDot-headDot)...
=bacterinChromosome(1,headDot:tailDot);
elseif (chromosomeLength-inoculateDot)>=(tailDot-headDot)
inoculateChromosome(i,:)=chromosomeGroup(i,:);
inoculateChromosome(i,inoculateDot:inoculateDot+tailDot-headDot)...
=bacterinChromosome(1,headDot:tailDot);
else
inoculateChromosome(i,:)=chromosomeGroup(i,:);
inoculateChromosome(i,headDot:tailDot)...
=bacterinChromosome(1,headDot:tailDot);
end
end

鸡蛋
2

鲜花

雷人

路过

握手

刚表态过的朋友 (2 人)

发表评论

最新评论

hopeoflight 2011-12-19 14:48
很好,值得学习。。。
数学中国管理员 2011-12-23 23:10
学习下。很好的东西
resile2010 2012-1-6 20:59
大神啊,膜拜。
qukaito 2012-1-8 12:15
不错呀,,,学习了。。。
天弦流香 2014-10-30 17:55
学习下。很好的东西,谢谢楼主
胡孔涛1994 2014-11-27 07:43
审核未通过
盐田港 2014-12-29 14:27
审核未通过
liwenhui 2014-12-31 10:21
这个应该是在MATLAB中实现的吧?我原本以为是LINGO中实现的。
shrewd 2015-1-9 14:18
好!谢谢分享!

查看全部评论(9)

qq
收缩
  • 电话咨询

  • 04714969085

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2024-3-29 07:17 , Processed in 0.341352 second(s), 32 queries .

回顶部