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

📄 immune_algo.m

📁 人工免疫算法基于遗传MATLAB代码很有用哦
💻 M
字号:
function Immune_Algo(prompt,def)
  %'种群总数N','个体串长L','细胞克隆规模M','克隆选择率α(0.1,1)','抗体抑制半径σ(0,0.5)','自我抗体产生比率? (0.05,0.08)','终止代数R'
%close all
clear all
clc
%[file,path]=uigetfile('*.*','打开数据文件');
prompt={'适应度函数Func:','边界范围Bounds:','自变量个数Nvar:','种群总数N:','细胞克隆规模M:',...
        '克隆选择率α(0.5,1):','克隆复制因子β(0.5,0.9):','抗体抑制阈值σ(0,0.5):','终止代数R:'};
%def={'x + 10*sin(5*x)+7*cos(4*x)','[0,9]','1','50','20','0.4','0.5','0.35','30'};
 
def={'((1*cos((1+1)*x+1))+(2*cos((2+1)*x+2))+(3*cos((3+1)*x+3))+(4*cos((4+1)*x+4))+(5*cos((5+1)*x+5))).*((1*cos((1+1)*y+1))+(2*cos((2+1)*y+2))+(3*cos((3+1)*y+3))+(4*cos((4+1)*y+4))+(5*cos((5+1)*y+5)))',...
        '[-10,10]','2','50','20','0.4','0.5','0.35','30'};
dlgTitle='input';%'参数输入';
lineNo=1;
answer=inputdlg(prompt,dlgTitle,lineNo,def);
answer=char(answer);
func=(answer(1,:));
bounds=str2num(answer(2,:));
num=str2num(answer(4,:));
nvar=str2num(answer(3,:));
memo=str2num(answer(5,:));
rfa=str2num(answer(6,:));
deta=str2num(answer(7,:));
banj=str2num(answer(8,:));
maxgen=str2num(answer(9,:));

bits=ceil(log2((bounds(:,2)-bounds(:,1))' ./ (1e-6)));          % Chrom's length
len=nvar*bits+1;
%pop=zeros(num,len);
%filename1=strcat(path,file);
%fid1=fopen(filename1,'r');
n1=round(rfa*num);memo=n1;
pop_memo=zeros(memo,len);
%if n1>memo, printf('细胞克隆规模M应 > αN.'); end
%n3=round(pb*num);n2=num-n3;
if nvar==1
         FieldD=[bits;bounds(1);bounds(2);1;0;1;1];
else   FieldD=[rep([bits],[1,nvar]);rep([bounds(1);bounds(2)],[1,nvar]);rep([1;0;1;1],[1,nvar])];
end  

tic
%1.初始化抗体群
 %   pe=round(memo./2);
%    pop=[pop_memo(1:pe,:);round(rand(num-pe,len))];
pop=[zeros(1,len);round(rand(num-2,len));ones(1,len)];
BestPop=zeros(maxgen,len-1);Trace=zeros(maxgen,4);%分配初始解空间

%循环开始
for it=1:maxgen
 
%2.calculate affi
   p=bs2rv(pop(:,1:len-1), FieldD);
if nvar==1
    x=p;                    %计算初始种群的十进制转换
elseif nvar==2 
     x=p(:,1);
     y=p(:,2);
 else printf('the nvar>3!\n');
end
    pop(:,len) = eval(func);
 %   aff = 1./(exp(0.5.*fit)+1);
 %   pop(:,len)=aff;
 
    [MaxValue,Index]=max(pop(:,len));
    BestPop(it,:)=pop(Index,1:len-1);
    Trace(it,1)=MaxValue;                       % the best
    Trace(it,2)=mean(pop(:,len));               % mean
    Trace(it,3)=min(pop(:,len));                % min
 %   Trace(it,4)=b2f(BestPop(it,:),bounds); % the best ch  

 %3.clonal selection得Bn
    % 在matlab中对矩阵的值按某一列升序排序。
     result = sortrows(pop,len);
     result=flipud(result);
     pop_b=result(1:n1,:);
 % Bn----pop_b
 
% 4.ceil clone
%   pr=
%   pop_temp=[pop_b;zeros(memo-n1,len)];
   s=0;
  for k=1:n1
      mi=round(deta*num/k);%round(num.*pr.*pop_b(k,len));
      if mi>=1
          s=s+mi;
      end
       for l=1:mi
        pop_temp((s-mi+l),:)=pop_b(k,:);
       end
     
   end

% 6.亲和突变得克隆集Cn
% newpop(k,1:len-1)
  for k=1:s
%     newpop(k,1:len-1)=pop_temp(k,1:len-1);
     if rand(1)< 0.1%exp(-pop_temp(k,len))
          mpoint=round(rand*(len-2))+1;
           if (pop_temp(k,mpoint)==0)
           pop_temp(k,mpoint)=1;
          elseif (pop_temp(k,mpoint)==1)
           pop_temp(k,mpoint)=0;
         end
     end
  end

 %.calculate affi
  p=bs2rv(pop_temp(:,1:len-1), FieldD);
if nvar==1
     x=p;                    %计算初始种群的十进制转换
elseif nvar==2 
     x=p(:,1);
     y=p(:,2);
 else printf('the nvar>3!\n');
end
    aff = eval(func);
 %   aff = 1./(exp(0.5.*fit)+1);
    pop_temp(:,len)=aff;
 % Cn---pop_temp
   
    [qiw]=qiwang(pop_temp,banj);%计算期望值  值0.35影响大
    NewPop1=zeros(s,len);
    NewPop1(:,1:len-1)=pop_temp(:,1:len-1);
    NewPop1(:,len)=qiw';  
  % 在matlab中对矩阵的值按某一列升序排序。
      result = sortrows(NewPop1,len);
      result=flipud(result);
      pop_memo=result(1:memo,:);


   
   
     pop=[result(1:memo,:);round(rand(num-memo,len))];
%     a(it)=max(pop(:,len))
 
end

t=1:maxgen;
plot(t,Trace(:,1)');
title('函数优化的免疫算法');xlabel('进化世代数(gen)');ylabel('每一代最优适应度(maxfitness)');
[MaxFval,I]=max(Trace(:,1));
 p=bs2rv(BestPop(I,:), FieldD);
if nvar==1
     X=p;                    %计算初始种群的十进制转换
elseif nvar==2 
     X=p(:,1)+i*p(:,2);
end

hold on;  plot(I,MaxFval,'*');
%hold on; plot(t,Trace(:,2),'r');
text(I-2,MaxFval-0.05,['FMax=' num2str(MaxFval)]);
str1=sprintf('进化到 %d 代 ,自变量为 %s 时,得本次求解的最优值 %f\n对应染色体是:%s\n use times: %5.2f',...
             I,num2str(X),MaxFval,num2str(BestPop(I,:)),toc);
disp(str1);


%计算抗体浓度及期望值—距离法
function [qiw]=qiwang(pop,det)
  [nm,len]=size(pop);
 det=1/(1+sqrt(det*nm));
for t=1:nm
     cv=0;
    for j=1:nm
        d=0;
        for k=1:(len-1)
           if(pop(t,k)~=pop(j,k))
               d=d+1;
           end
        end
        hn=1/(1+sqrt(d));
%      Aff=1/(1+hn);  
      if (hn>=det)
          cv=cv+1;
      end
  end
  % or qiw(t)=pop(t,len)*exp(-cv/nm)
 qiw(t)=pop(t,len)*nm/cv;
end 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -