人工免疫算法

人工免疫系统概述

二十世纪八十年代,Farmer等人率先基于免疫网络学说给出了免疫系统的动态模型,并探讨了免疫系统与其它人工智能方法的联系,开始了人工免疫系统的研究。直到1996年12月,在日本首次举行了基于免疫性系统的国际专题讨论会,首次提出了“人工免疫系统” (AIS)的概念。随后,人工免疫系统进入了兴盛发展时期,D. Dasgupta和焦李成等认为人工免疫系统已经成为人工智能领域的理论和应用研究热点,相关论文和研究成果正在逐年增加。1997和1998年IEEE国际会议还组织了相关专题讨论,并成立了“人工免疫系统及应用分会”。D. Dasgupta系统分析了人工免疫系统和人工神经网络的异同,认为在组成单元及数目、交互作用、模式识别、任务执行、记忆学习、系统鲁棒性等方面是相似的,而在系统分布、组成单元间的通信、系统控制等方面是不同的,并指出自然免疫系统是人工智能方法灵感的重要源泉。Gasper等认为多样性是自适应动态的基本特征,而AIS是比GA更好地维护这种多样性的优化方法。

常见的免疫算法

免疫算法是基于免疫机理提出的高效的学习和优化算法,是AIS理论研究的重要内容之一。

1.克隆选择算法(CSA:Clone Selection Algorithm)
由于免疫系统本身的复杂性,有关算法机理的描述还不多见,相关算子还比较少。Castro L. D.、Kim J.、杜海峰、焦李成等基于抗体克隆选择机理相继提出了克隆选择算法。Nohara等基于抗体单元的功能提出了一种非网络的人工免疫系统模型。而目前两个比较有影响的人工免疫网络模型是Timmis等基于人工识别球(Artificial Recognition Ball, AR概念提出的资源受限人工免疫系统(Resource Limited Artificial Immune System, RLAIS)和Leandro等模拟免疫网络响应抗原刺激过程提出的aiNet算法。

2.免疫遗传算法(IGA:Immune Genetic Algorithm)

免疫遗传算法可以看作一种新型融合算法,是一种改进的遗传算法,是具有免疫功能的遗传算法。

3.阴性选择算法(NSA:Negative Selection Algorithm)

阴性选择算法基于生物免疫系统的特异性,借鉴生物免疫系统胸腺T细胞生成时的“阴性选择”(Negative Selection)过程。通过Forrest研究一种用于检测变数据变化的阴性选择算法,用于解决计算机安全领域的问题。该算法通过系统对异常变化的成功监测而使免疫系统发挥作用,而监测成功的关键是系统能够分清自己和非己的信息;随机产生检测器,删除那些测到自己的检测器,以使那些测到非己的检测器保留下来。

4.B细胞网络算法(B—cell Algorthm)

以独特型网络理论和克隆选择理论为基础。Hunt等人模拟生物免疫系统的自学习、自组织机理提出一种人工免疫网络模型——B细胞网络及其算法。

算法将未知问题的解看作抗原,认为只要找到能产生最高亲和度的抗体的B细胞,也就找到了未知问题的解。

实验结果证明,该算法具有较强的寻优能力并保持网络中多种模式和谐并存,有比人工神经网络更快更好的模式识别能力。

   

    

  

 

  

 

主要三个特点: (1)疫苗接种;2)免疫记忆;3)抗体浓度计算,抗体的抑制与促进

%%%%%%%%%%%%%%%%%%%%%%

免疫算法 matlab程序

这是免疫算法。这个算法几乎与遗传算法一样,只是多用了一个免疫函数

免疫算法是遗传算法的变体,它不用杂交,而是采用注入疫苗的方法。疫苗是优秀染色体中的一段基因,把疫苗接种到其它染色体中。

注意:标准遗传算法的一个重要概念是,染色体是可能解的2进制顺序号,由这个序号在可能解的集合(解空间)中找到可能解。

 

这是免疫算法的主程序,它需要调用的函数如下。

接种疫苗函数:

%function inoculateChromosome=immunity(chromosomeGroup,bacterinChromosome,parameter)

%parameter:1,随机制取染色体接种。2,每个染色体都接种。3,每个染色体都接种,但接种的位置是随机的

%这个函数实现对染色体的疫苗接种

%由染色体(可能解的2进制)顺序号找到可能解:

%x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);

%把解代入非线性方程组计算误差函数:functionError=nonLinearSumError1(x);

判定程是否得解函数:[solution,isTrue]=isSolution(x,funtionError,solutionSumError);

%选择最优染色体函数:

%[bestChromosome,leastFunctionError]=best_worstChromosome(fatherChromosomeGroup,functionError);

%误差比较函数:从两个染色体中,选出误差较小的染色体

%[holdBestChromosome,holdLeastFunctionError]...

% =compareBestChromosome(holdBestChromosome,holdLeastFunctionError,...

% bestChromosome,leastFuntionError)

%为染色体定义概率函数,好的染色体概率高,坏染色体概率低

%p=chromosomeProbability(functionError);

%按概率选择染色体函数:

%slecteChromosomeGroup=selecteChromome(fatherChromosomeGroup,p);

%父代染色体杂交产生子代染色体函数

%sonChrmosomeGroup=crossChromosome(slecteChromosomeGroup,2);

%防止染色体超出解空间的函数

%chromosomeGroup=checkSequence(chromosomeGroup,solutionSum)

%变异函数

%fatherChromosomeGroup=varianceCh(sonChromosomeGroup,0.8,solutionN);

%通过实验有如下结果:

%1。染色体应当多一些

%2。通过概率选择染色体,在迭代早期会有效选出优秀的染色体,使解的误差迅速降低,

%但随着迭代的进行,概率选择也会导致某种染色体在基因池中迅速增加,使染色体趋同,

%这就减少了物种的多样性,反而难以逼近解

%3。不用概率选择,仅采用染色体杂交,采用保留优秀染色体,也可以得到解

%4。单纯免疫效果不好,杂交+免疫效果比较好

 

程序开始运行

 

clear,clc;%清理内存,清屏

circleN=200;%迭代次数

format long

 

%%%%%%%%%%%%%%%构造可能解的空间,确定染色体的个数、长度

solutionSum=4;leftBoundary=-10;rightBoundary=10;

distance=1;chromosomeSum=500;solutionSumError=0.1;

%solutionSum:非线性方程组的元数(待解变量的个数);leftBoundary:可能解的左边界;

%rightBoundary:可能解的右边界;distance:可能解的间隔,也是解的精度

%chromosomeSum:染色体的个数;solveSumError:解的误差

oneDimensionSet=leftBoundary:distance:rightBoundary;

%oneDimensionSet:可能解在一个数轴(维)上的集合

oneDimensionSetN=size(oneDimensionSet,2);%返回oneDimensionSet中的元素个数

solutionN=oneDimensionSetN^solutionSum;%解空间(解集合)中可能解的总数

binSolutionN=dec2bin(solutionN);%把可能解的总数转换成二进制数

chromosomeLength=size(binSolutionN,2);%由解空间中可能解的总数(二进制数)计算染色体的长度

 

程序初始化

%随机生成初始可能解的顺序号,+1是为了防止出现0顺序号

solutionSequence=fix(rand(chromosomeSum,1)*solutionN)+1;

for i=1:chromosomeSum%防止解的顺序号超出解的个数

if solutionSequence(i)>solutionN;

solutionSequence(i)=solutionN;

end

end

%染色体是解集合中的序号,它对应一个可能解

%把解的十进制序号转成二进制序号

fatherChromosomeGroup=dec2bin(solutionSequence,chromosomeLength);

holdLeastFunctionError=Inf;%可能解的最小误差的初值

holdBestChromosome=0;%对应最小误差的染色体的初值

 

开始计算

compute=1;

circle=0;

while compute%开始迭代求解

%%%%%%%%%%%%%1:由可能解的序号寻找解本身(关键步骤)

x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);

%%%%%%%%%%%%%2:把解代入非线性方程计算误差

functionError=nonLinearSumError1(x);%把解代入方程计算误差

[solution,minError,isTrue]=isSolution(x,functionError,solutionSumError);

%isSolution函数根据误差functionError判定方程是否已经解开,isTrue=1,方程得解。solution是方程的解

if isTrue==1

'方程得解'

solution

minError

return%结束程序

end

%%%%%%%%%%%%%3:选择最好解对应的最优染色体

[bestChromosome,leastFunctionError]=best_worstChromosome(fatherChromosomeGroup,functionError);

%%%%%%%%%%%%%4:保留每次迭代产生的最好的染色体

%本次最好解与上次最好解进行比较,如果上次最好解优于本次最好解,保留上次最好解;

%反之,保留本次最好解。保留的最好染色体放在holdBestChromosome中

[holdBestChromosome,holdLeastFunctionError]...

=compareBestChromosome(holdBestChromosome,holdLeastFunctionError,...

bestChromosome,leastFunctionError);

circle=circle+1

%minError

%solution

holdLeastFunctionError

if circle>circleN

return

end

%%%%%%%%%%%%%%5:把保留的最好的染色体holdBestChromosome加入到染色体群中

order=round(rand(1)*chromosomeSum);

if order==0

order=1;

end

fatherChromosomeGroup(order,:)=holdBestChromosome;

functionError(order)=holdLeastFunctionError;

 

%%%%%%%%%%%%%%%6:为每一条染色体(即可能解的序号)定义一个概率(关键步骤)

%%%%%%%%%%%%%%%好的染色体概率高,坏的概率低。依据误差functionError计算概率

[p,trueP]=chromosomeProbability(functionError);

if trueP =='Fail'

'可能解严重不适应方程,请重新开始'

return%结束程序

end

 

 

按照概率筛选染色体(关键步骤)

%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

 

 

posted @ 2016-01-26 01:12  Hand&Head  阅读(11226)  评论(1编辑  收藏  举报