一、简介
1 量子免疫克隆
二、源代码
clear;
C=[253 245 243 239 239 239 238 238 237 232 231 231 230 229 228 227 224 217 213 207 203 201 195 194 191 187 187 177 175 171 169 168 165 164 161 160 158 150 149 147 141 140 139 136 135 132 128 126 122 120 119 116 116 114 111 110 105 105 104 103 93 92 90 79 78 77 76 76 75 73 62 62 61 60 60 59 57 56 53 53 51 50 44 44 42 42 38 36 34 28 27 24 22 18 12 10 7 4 4 1];
W=[253 245 243 239 239 239 238 238 237 232 231 231 230 229 228 227 224 217 213 207 203 201 195 194 191 187 187 177 175 171 169 168 165 164 161 160 158 150 149 147 141 140 139 136 135 132 128 126 122 120 119 116 116 114 111 110 105 105 104 103 93 92 90 79 78 77 76 76 75 73 62 62 61 60 60 59 57 56 53 53 51 50 44 44 42 42 38 36 34 28 27 24 22 18 12 10 7 4 4 1];
V=6666;
a=2.40;
q=(sqrt(1/2)).*ones(20,100);
[N,L] = size(q); ger =500; pm=0.0088; pc=0.88; fat=0.1;
disp(sprintf('Number of generations: %d',ger));
disp(sprintf('Population size: %d',N/2));
% General parameters & Initial operations
vmfit = []; it = 1; vx = []; T=[]; vw=[];
f='dot(v,C1,2)';
updatef=0;
% Generations
t0 = clock;
c1=0;
while it <= ger
itemp=[1 3 5 7 9 11 13 15 17 19];
v=observe(q(itemp,:));
rate=C./W;
[a,ind] = sort(rate);
for j=1:N/2
i1=1;
%repair
knoverfilled=0;
if dot(v(j,:),W)>6666
knoverfilled=1;
end
while knoverfilled==1
v(j,ind(i1))=0;
if dot(v(j,:),W)<=6666
knoverfilled=0;
end
i1=i1+1;
end
v(j,:)=v(j,:);
end
%and/or Crossver
for i=1:N/2
cind(i)=i;
end
for i=1:N/2
point=unidrnd(N/2-i+1);
temp=cind(i);
cind(i)=cind(i+point-1);
cind(i+point-1)=temp;
end
for i=1:2:N/2
p=rand(1);
if(p<pc)
c1=and(v(cind(i),:),v(cind(i+1),:));
c2=or(v(cind(i),:),v(cind(i+1),:));
v(cind(i),:)=c1;
v(cind(i+1),:)=c2;
end
end
for j=1:N/2
i1=1;
%repair
knoverfilled=0;
if dot(v(j,:),W)>6666
knoverfilled=1;
end
while knoverfilled==1
v(j,ind(i1))=0;
if dot(v(j,:),W)<=6666
knoverfilled=0;
end
i1=i1+1;
end
v(j,:)=v(j,:);
end
C1=ones(N/2,1)*C;
fit=eval(f);
[sol,indb] = max(fit);
maxf=sol;
max1=v(indb,:);
minf=min(fit);
if updatef>=sol
maxf=updatef;
max1=updatec;
else
maxf=sol;
max1=v(indb,:);
end
%量子变异
for i=1:2:size(q,1)
for j=1:size(q,2)
if v((i+1)/2,j)==max1(1,j)
if max1(1,j)==0
s=0;
angle=0;
else
if q(i,j)*q(i+1,j)>0
s=1;
angle=0.015;
end
if q(i,j)*q(i+1,j)<0
s=-1;
angle=0.015;
end
if q(i,j)==0
s=0;
angle=0.015;
end
if q(i+1,j)==0
s=1;
angle=0.015;
end
end
elseif v((i+1)/2,j)==0
if maxf>fit((i+1)/2)
s=0;
angle=0;
else
if q(i,j)*q(i+1,j)>0
s=-1;
angle=0.015;
end
if q(i,j)*q(i+1,j)<0
s=1;
angle=0.015;
end
if q(i,j)==0
s=1;
angle=0.015;
end
if q(i+1,j)==0
s=0;
angle=0.015;
end
end
else
if maxf>fit((i+1)/2)
if q(i,j)*q(i+1,j)>0
s=-1;
angle=0.015;
end
if q(i,j)*q(i+1,j)<0
s=1;
angle=0.015;
end
if q(i,j)==0
s=1;
angle=0.015;
end
if q(i+1,j)==0
s=0;
angle=0.015;
end
else
if q(i,j)*q(i+1,j)>0
s=1;
angle=0.015;
end
if q(i,j)*q(i+1,j)<0
s=-1;
angle=0.015;
end
if q(i,j)==0
s=0;
angle=0.015;
end
if q(i+1,j)==0
s=1;
angle=0.015;
end
end
end
o=s*angle*pi;
u=[cos(o) -sin(o);sin(o) cos(o)];
temp1=[q(i,j);q(i+1,j)];
temp2=u*temp1;
q(i,j)=temp2(1,1);
q(i+1,j)=temp2(2,1);
end
三、运行结果
四、备注
完整代码或者代写添加QQ 912100926
往期回顾>>>>>>
【背包问题】粒子群求解背包问题【Matlab 208期】
【背包问题】遗传算法求解多背包问题【Matlab 209期】
【背包问题】PSO算法求解0/1背包问题【Matlab 328期】
【背包问题】禁忌搜索求解0/1背包问题【Matlab源码 329期】
【背包问题】matlab遗传算法求解多背包问题【Matlab 330期】
【背包问题】matlab离散粒子群算法求解01背包问题【Matlab源码 331期】