?? immune_algo.asv
字號:
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','30','0.6','0.5','0.35','80'};
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;
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)+j*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 + -