版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、接种疫苗函数:%function inoculateChromosome=immunity(chromosomeGroup,bacterinChromosome,parameter)%parameter:1,随机制取染色体接种。2,每个染色体都接种。3,每个染色体都接种,但接种的位置是随机的%这个函数实现对染色体的疫苗接种%由染色体(可能解的2进制)顺序号找到可能解:%x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);%把解代入非线性方程组计算误差函数:functionError=nonLinearSumErro
2、r1(x);判定程是否得解函数:solution,isTrue=isSolution(x,funtionError,solutionSumError);%选择最优染色体函数:%bestChromosome,leastFunctionError=best_worstChromosome(fatherChromosomeGroup,functionError);%误差比较函数:从两个染色体中,选出误差较小的染色体%holdBestChromosome,holdLeastFunctionError.% =compareBestChromosome(holdBestChromosome,holdLea
3、stFunctionError,.% bestChromosome,leastFuntionError)%为染色体定义概率函数,好的染色体概率高,坏染色体概率低%p=chromosomeProbability(functionError);%按概率选择染色体函数:%slecteChromosomeGroup=selecteChromome(fatherChromosomeGroup,p);%父代染色体杂交产生子代染色体函数%sonChrmosomeGroup=crossChromosome(slecteChromosomeGroup,2);%防止染色体超出解空间的函数%chromosomeGr
4、oup=checkSequence(chromosomeGroup,solutionSum)%变异函数%fatherChromosomeGroup=varianceCh(sonChromosomeGroup,0.8,solutionN);%通过实验有如下结果:%1。染色体应当多一些%2。通过概率选择染色体,在迭代早期会有效选出优秀的染色体,使解的误差迅速降低,%但随着迭代的进行,概率选择也会导致某种染色体在基因池中迅速增加,使染色体趋同,%这就减少了物种的多样性,反而难以逼近解%3。不用概率选择,仅采用染色体杂交,采用保留优秀染色体,也可以得到解%4。单纯免疫效果不好,杂交+免疫效果比较好程序
5、开始运行clear,clc;%清理内存,清屏circleN=200;%迭代次数format long%构造可能解的空间,确定染色体的个数、长度solutionSum=4;leftBoundary=-10;rightBoundary=10;distance=1;chromosomeSum=500;solutionSumError=0.1;%solutionSum:非线性方程组的元数(待解变量的个数);leftBoundary:可能解的左边界;%rightBoundary:可能解的右边界;distance:可能解的间隔,也是解的精度%chromosomeSum:染色体的个数;solveSumErr
6、or:解的误差oneDimensionSet=leftBoundary:distance:rightBoundary;%oneDimensionSet:可能解在一个数轴(维)上的集合oneDimensionSetN=size(oneDimensionSet,2);%返回oneDimensionSet中的元素个数solutionN=oneDimensionSetNsolutionSum;%解空间(解集合)中可能解的总数binSolutionN=dec2bin(solutionN);%把可能解的总数转换成二进制数chromosomeLength=size(binSolutionN,2);%由解空间
7、中可能解的总数(二进制数)计算染色体的长度程序初始化%随机生成初始可能解的顺序号,+1是为了防止出现0顺序号solutionSequence=fix(rand(chromosomeSum,1)*solutionN)+1;for i=1:chromosomeSum%防止解的顺序号超出解的个数if solutionSequence(i)>solutionN;solutionSequence(i)=solutionN;endend%染色体是解集合中的序号,它对应一个可能解%把解的十进制序号转成二进制序号fatherChromosomeGroup=dec2bin(solutionSequence
8、,chromosomeLength);holdLeastFunctionError=Inf;%可能解的最小误差的初值holdBestChromosome=0;%对应最小误差的染色体的初值开始计算compute=1;circle=0;while compute%开始迭代求解%1:由可能解的序号寻找解本身(关键步骤)x=chromosome_x(fatherChromosomeGroup,oneDimensionSet,solutionSum);%2:把解代入非线性方程计算误差functionError=nonLinearSumError1(x);%把解代入方程计算误差solution,minEr
9、ror,isTrue=isSolution(x,functionError,solutionSumError);%isSolution函数根据误差functionError判定方程是否已经解开,isTrue=1,方程得解。solution是方程的解if isTrue=1'方程得解'solutionminErrorreturn%结束程序end%3:选择最好解对应的最优染色体bestChromosome,leastFunctionError=best_worstChromosome(fatherChromosomeGroup,functionError);%4:保留每次迭代产生的最
10、好的染色体%本次最好解与上次最好解进行比较,如果上次最好解优于本次最好解,保留上次最好解;%反之,保留本次最好解。保留的最好染色体放在holdBestChromosome中holdBestChromosome,holdLeastFunctionError.=compareBestChromosome(holdBestChromosome,holdLeastFunctionError,.bestChromosome,leastFunctionError);circle=circle+1%minError%solutionholdLeastFunctionErrorif circle>cir
11、cleNreturnend%5:把保留的最好的染色体holdBestChromosome加入到染色体群中order=round(rand(1)*chromosomeSum);if order=0order=1;endfatherChromosomeGroup(order,:)=holdBestChromosome;functionError(order)=holdLeastFunctionError;%6:为每一条染色体(即可能解的序号)定义一个概率(关键步骤)%好的染色体概率高,坏的概率低。依据误差functionError计算概率p,trueP=chromosomeProbability(
12、functionError);if trueP ='Fail''可能解严重不适应方程,请重新开始'return%结束程序end按照概率筛选染色体(关键步骤)%fa=bin2dec(fatherChromosomeGroup)%显示父染色体%从父染体中选择优秀染色体%selecteChromosomeGroup=selecteChromosome(fatherChromosomeGroup,p);%8:染色体杂交(关键步骤)%sle=bin2dec(selecteChromosomeGroup)%显示选择出来的解的序号(染色体)%用概率筛选出的染色体selecte
13、ChromosomeGroup进行杂交,产生子代染色体%sonChromosomeGroup=crossChromosome(selecteChromosomeGroup,2);%不用概率筛选出的染色体selecteChromosomeGroup进行杂交,而直接用上一代(父代)的sonChromosomeGroup=crossChromosome(fatherChromosomeGroup,2);%sonChromosomeGroup=immunity(fatherChromosomeGroup,holdBestChromosome,3);%把疫苗接种到其它染色体中sonChromosomeG
14、roup=immunity(sonChromosomeGroup,holdBestChromosome,3);%cro=bin2dec(sonChromosomeGroup)%显示杂交后的子代染色体sonChromosomeGroup=checkSequence(sonChromosomeGroup,solutionN);%检查杂交后的染色体是否越界%9:变异%不杂交直接变异%fatherChromosomeGroup=varianceCh(fatherChromosomeGroup,0.1,solutionN);%杂交后变异fatherChromosomeGroup=varianceCh(s
15、onChromosomeGroup,0.5,solutionN);fatherChromosomeGroup=checkSequence(fatherChromosomeGroup,solutionN);%检查变异后的染色体是否越界end接种疫苗函数,这是和遗传算法唯一不同的函数,可以用它代替染色体的交叉操作。%chromosomeGroup:染色体组%bachterinChromosome:疫苗染色体,即最好的染色体。从这个染色体上取疫苗%parameter:接种疫苗的参数,即用什么方法接种%inoculateChromosome:接种疫苗后的染色体function inoculateChr
16、omosome=immunity(chromosomeGroup,bacterinChromosome,parameter)chromosomeGroupSum,chromosomeLength=size(chromosomeGroup);row,bacterinChromosomeLength=size(bacterinChromosome);%chromosomeGroupSum:染色体的条数;chromosomeLength:染色体的长度switch parametercase 1%随机选择染色体进行接种for i=1:chromosomeGroupSum%从疫苗染色体上定位疫苗head
17、Dot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色体上左边的点位if headDot=0%防止出现0点位headDot=1;endtailDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色体上右边的点位if tailDot=0%防止出现0点位tailDot=1;endif tailDot>headDot%防止右边的点位大于左边的点位dot=headDot;headDot=tailDot;tailDot=dot;end接种randChromosomeSequence=round(rand(1)*ch
18、romosomeGroupSum);%随机产生1条染色体的序号,对这条染色体进行接种if randChromosomeSequence=0%防止产生0序号randChromosomeSequence=1;endinoculateChromosome(i,:).%先把输入染色体传给输出=chromosomeGroup(randChromosomeSequence,:);%执行免疫,即从疫苗染色体上取出一段基因做疫苗,再注入到其它染色体中inoculateChromosome(i,headDot:tailDot).=bacterinChromosome(1,headDot:tailDot);end
19、case 2 %所有染色体挨个接种for i=1:chromosomeGroupSum%从疫苗染色体上定位疫苗headDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色体上左边的点位if headDot=0%防止出现0点位headDot=1;endtailDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色体上右边的点位if tailDot=0%防止出现0点位tailDot=1;endif tailDot>headDot%防止右边的点位大于左边的点位dot=headDot;headDot=tailD
20、ot;tailDot=dot;end%接种inoculateChromosome(i,:)=chromosomeGroup(i,:);%先把输入染色体传给输出%执行免疫,即从疫苗染色体上取出一段基因做疫苗,再注入到其它染色体中inoculateChromosome(i,headDot:tailDot).=bacterinChromosome(1,headDot:tailDot);endcase 3 %接种位置是随机的for i=1:chromosomeGroupSum%从疫苗染色体上定位疫苗headDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色体上左边的点位if headDot=0%防止出现0点位headDot=1;endtailDot=fix(rand(1)*bacterinChromosomeLength);%疫苗在染色体上右边的点位if tailDot=0%防止出现0点位tailDot=1;endif tailDot>headDot%防止右边的点位大于左边的点位dot=headDot;headDot=tailDot;tailDot=dot;end在染色体上随机定位接种位置inoculateDot=fix(rand(1)*chromosomeLength);%随机选择染色体的接种点
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高支模支撑体系专项施工方案
- 小叶性肺炎宣教
- 2025核心制度试题及答案
- 2025年车间调度试题及答案
- 教师课堂教学技能训练
- 多媒体课件设计与开发案例
- 教师口语技能发音训练
- 恒芯数码团队介绍
- 甜品店员工培训
- 2025年人教A版选修3生物上册阶段测试试卷含答案
- 电力公司安全生产监督制度
- 2025广东深圳市特区建工招聘考前自测高频考点模拟试题及1套参考答案详解
- 中建三方协议书岗位
- 2025年及未来5年中国等离子手术刀行业市场调查研究及发展战略规划报告
- 保税区培训知识课件
- 2025危险化学品经营单位安全管理人员考试试题及答案
- 2025年河北省石家庄市公安辅警招聘知识考试题(含答案)
- 板式换热器清洗施工方案
- 2025年介入心脏病学临床技能考核答案及解析
- 苏联入侵阿富汗
- 2025广东清远市公安局第二次选调事业编制人员18人笔试备考试题及答案解析
评论
0/150
提交评论