www.pudn.com > yichuan.rar > yichuan.m, change:2006-12-27,size:10252b


f1=[696,704,694,698,695,694,660,658,658,655,658,663,697,694,693,696,695,696,666,660,665,663,663,664]; 
a=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  2 3 1 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  3 4 2 1 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  2 3 4 5 1 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  3 4 5 6 1 2 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  4 5 6 7 1 2 3 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  5 6 7 8 1 2 3 4 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  6 7 8 9 1 2 3 4 5 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  7 8 9 10 1 2 3 4 5 6 11 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  8 9 10 11 1 2 3 4 5 6 7 12 13 14 15 16 17 18 19 20 21 22 23 24; 
  9 10 11 12 1 2 3 4 5 6 7 8 13 14 15 16 17 18 19 20 21 22 23 24; 
  10 11 12 13 1 2 3 4 5 6 7 8 9 14 15 16 17 18 19 20 21 22 23 24; 
  11 12 13 14 1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 20 21 22 23 24; 
  12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 16 17 18 19 20 21 22 23 24; 
  13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 17 18 19 20 21 22 23 24; 
  14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 18 19 20 21 22 23 24; 
  15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 19 20 21 22 23 24; 
  16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 20 21 22 23 24; 
  17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 21 22 23 24; 
  18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 23 24 22; 
  19 20 21 22 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 24 23; 
  20 21 22 23 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; 
  19 20 21 22 23 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18; 
  18 19 20 21 22 23 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17; 
  15 16 17 18 19 20 21 22 23 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14; 
  12 13 14 15 16 17 18 19 20 21 22 23 24 1 2 3 4 5 6 7 8 9 10 11; 
  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 1 2 3 4 5 6 7 8; 
  3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 1; 
  7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 21 22 23 24 17 18 19 20; 
  5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 24 20 22 21 23 2 3 4 1]; 
pc=0.4;%交叉概率% 
pv=0.01;%变异概率% 
for n=1:20000 
 
%计算各种群的适应度 
for i=1:30 
  for j=1:24 
    a1(i,j)=f1(a(i,j)); 
  end 
end 
 
for i=1:30 
  m=a(i,:); 
f(i)=max(0,10^9-(10^3*(((sum(m(1:4))^2+sum(m(5:8))^2+sum(m(9:12))^2+sum(m(13:16))^2+sum(m(17:20))^2+sum(m(21:24))^2)/6-(sum(m)/6)^2)+max(0,(((m(1)+m(2)+m(3)+m(4))-(m(5)+m(6)+m(7)+m(8)))^2-64))+max(0,(((m(5)+m(6)+m(7)+m(8))-(m(9)+m(10)+m(11)+m(12)))^2-64))+max(0,(((m(9)+m(10)+m(11)+m(12))-(m(13)+m(14)+m(15)+m(16)))^2-64))+max(0,(((m(13)+m(14)+m(15)+m(16))-(m(17)+m(18)+m(19)+m(20)))^2-64))+max(0,(((m(17)+m(18)+m(19)+m(20))-(m(21)+m(22)+m(23)+m(24)))^2-64))+max(0,(((m(21)+m(22)+m(23)+m(24))-(m(1)+m(2)+m(3)+m(4)))^2-64))))); 
end 
 
shiying=f; 
c=shiying; 
 
for j=1:30 
  if c(j)==max(c) 
    c(j)=-1; 
    t(1)=j; 
    d1=a(j,:); 
    break; 
  end 
end 
 
for j=1:30 
  if c(j)==max(c) 
    t(2)=j; 
    d2=a(j,:); 
    break; 
  end 
end 
 
%保留两个适应度最大的不参与复制、交叉和变异 
 
for j=max(t):29 
  a(j,:)=a(j+1,:); 
end 
 
for j=min(t):29 
  a(j,:)=a(j+1,:); 
end 
 
a(29,:)=d2; 
a(30,:)=d1; 
x=a;                          
 
%相当于将适应度最大的排到矩阵的最后两行,其他的依次向前进一位    
 
for i=1:30 
  for j=1:24 
    a1(i,j)=f1(a(i,j)); 
  end 
end            
 
for i=1:30 
  m=a(i,:); 
  f(i)=max(0,10^9-(10^3*(((sum(m(1:4))^2+sum(m(5:8))^2+sum(m(9:12))^2+sum(m(13:16))^2+sum(m(17:20))^2+sum(m(21:24))^2)/6-(sum(m)/6)^2)+max(0,(((m(1)+m(2)+m(3)+m(4))-(m(5)+m(6)+m(7)+m(8)))^2-64))+max(0,(((m(5)+m(6)+m(7)+m(8))-(m(9)+m(10)+m(11)+m(12)))^2-64))+max(0,(((m(9)+m(10)+m(11)+m(12))-(m(13)+m(14)+m(15)+m(16)))^2-64))+max(0,(((m(13)+m(14)+m(15)+m(16))-(m(17)+m(18)+m(19)+m(20)))^2-64))+max(0,(((m(17)+m(18)+m(19)+m(20))-(m(21)+m(22)+m(23)+m(24)))^2-64))+max(0,(((m(21)+m(22)+m(23)+m(24))-(m(1)+m(2)+m(3)+m(4)))^2-64))))); 
end 
 
shiying=f; 
gailv=shiying(1:28)./sum(shiying(1:28));%计算前28个组群在圆盘上的概率 
count=zeros(1,28); 
m=rand(1,28); 
 
for j=1:28 
  for i=1:27 
    if m(j)<sum(gailv(1:(i+1)))&m(j)>sum(gailv(1:i)) 
      count(i+1)=count(i+1)+1; 
      break; 
    else  
      if m(j)<gailv(1)  
      count(1)=count(1)+1; 
      break; 
    else  
      if m(j)==sum(gailv(1:i)) 
          if gailv(i)>=gailv(i+1) 
            count(i)=count(i)+1; 
            break; 
          else count(i+1)=count(i+1)+1; 
            break; 
          end 
      end 
    end 
  end 
end 
 
end        
%按转盘思想标记          
xin=zeros(30,24); 
for i=1:28 
  if count(i)>0 
    for j=1:28 
        if xin(j,:)==zeros(1,24) 
          break; 
        end 
    end 
    for k=j:(sum(count(1:i))) 
        xin(k,:)=x(i,:); 
    end 
  end 
end 
 
xin(29,:)=x(29,:); 
xin(30,:)=x(30,:); 
% 选择    
x=xin; 
 
for j=1:28 
  m=rand(1); 
  if m>pc 
    x(j,:)=zeros(1,24); 
  end 
end     %按照交叉概率构成一组父本群x  
 
k=0; 
for j=1:28 
  if x(j,:)~=zeros(1,24) 
    k=k+1; 
    xuhao(k)=j;%将父本的序号给数组xuhao  
  end 
end 
 
while k<2 
  x=xin; 
for j=1:28 
  m=rand(1); 
  if m>pc 
    x(j,:)=zeros(1,24); 
  end 
end     %按照交叉概率构成一组父本群x 
 
k=0; 
 
for j=1:28 
  if x(j,:)~=zeros(1,24) 
    k=k+1; 
    xuhao(k)=j; %将父本的序号给数组xuhao 
  end 
end 
end 
 
%保证至少有两个父本进行交叉        
 
k=0; 
 
for j=1:28 
  if x(j,:)~=0 
    k=k+1; 
    xz(k,:)=x(j,:); 
  end 
end     %xz矩阵和xuhao是一一对应的    
 
for i=1:k 
  gailv_1(i)=1/k; 
end 
 
m_1=rand(1,2); 
for i=1:(k-1) 
  if m_1(1)<=sum(gailv_1(1:(i+1)))&m_1(1)>sum(gailv_1(1:i)) 
      weizhi_1=i+1; 
    else  
      if m_1(1)<=gailv_1(1)  
      weizhi_1=1; 
      end 
  end 
end 
 
for i=1:(k-1) 
  if m_1(2)<=sum(gailv_1(1:(i+1)))&m_1(2)>sum(gailv_1(1:i)) 
      weizhi_2=i+1; 
    else  
      if m_1(2)<=gailv_1(1)  
      weizhi_2=1; 
      end 
  end 
end 
if weizhi_1==weizhi_2 
  t=1; 
else  
  t=0; 
end 
 
while t==1 
  m_1=rand(1,2); 
for i=1:(k-1) 
  if m_1(1)<=sum(gailv_1(1:(i+1)))&m_1(1)>sum(gailv_1(1:i)) 
      weizhi_1=i+1; 
    else  
      if m_1(1)<=gailv_1(1)  
      weizhi_1=1; 
      end 
  end 
end 
 
for i=1:(k-1) 
  if m_1(2)<=sum(gailv_1(1:(i+1)))&m_1(2)>sum(gailv_1(1:i)) 
      weizhi_2=i+1; 
    else  
      if m_1(2)<=gailv_1(1)  
      weizhi_2=1; 
      end 
  end 
end 
 
if weizhi_1==weizhi_2 
  t=1; 
else  
  t=0; 
end 
end 
 
%在父本中随机取两个在新矩阵中的位置weizhi_1,weizhi_2    
 
%即在原矩阵xin中 xuhao(weizhi_1) 和 xuhao(weizhi_2) 位置上的组群发生交叉  
 
for i=1:24 
gailv_2(i)=1/24; 
end 
 
m_2=rand(1,2); 
 
for i=1:23 
if m_2(1)<=sum(gailv_2(1:(i+1)))&m_2(1)>sum(gailv_2(1:i)) 
      weizhi_3=i+1; 
    else  
      if m_2(1)<=gailv_2(1)  
      weizhi_3=1; 
      end 
end 
end 
 
for i=1:23 
if m_2(2)<=sum(gailv_2(1:(i+1)))&m_2(2)>sum(gailv_2(1:i)) 
      weizhi_4=i+1; 
    else  
      if m_2(2)<=gailv_2(1)  
      weizhi_4=1; 
      end 
end 
end 
 
if weizhi_3==weizhi_4 
  k=1; 
else 
  k=0; 
end 
 
while k==1 
  m_2=rand(1,2); 
for i=1:23 
if m_2(1)<=sum(gailv_2(1:(i+1)))&m_2(1)>sum(gailv_2(1:i)) 
      weizhi_3=i+1; 
    else  
      if m_2(1)<=gailv_2(1)  
      weizhi_3=1; 
      end 
end 
end 
 
for i=1:23 
if m_2(2)<=sum(gailv_2(1:(i+1)))&m_2(2)>sum(gailv_2(1:i)) 
      weizhi_4=i+1; 
    else  
      if m_2(2)<=gailv_2(1)  
      weizhi_4=1; 
      end 
end 
end 
 
if weizhi_3==weizhi_4 
  k=1; 
else 
  k=0; 
end 
 
end 
 
%随机取基因位的起点和终点        
 
v1=xin(xuhao(weizhi_1),min(weizhi_3,weizhi_4):max(weizhi_3,weizhi_4)); 
v2=xin(xuhao(weizhi_2),min(weizhi_3,weizhi_4):max(weizhi_3,weizhi_4));    
v1((abs(weizhi_3-weizhi_4)+2):(25+(abs(weizhi_3-weizhi_4))))=xin(xuhao(weizhi_2),:); 
v2((abs(weizhi_3-weizhi_4)+2):(25+(abs(weizhi_3-weizhi_4))))=xin(xuhao(weizhi_1),:); 
for i=1:(abs(weizhi_3-weizhi_4)+1) 
  for j=(abs(weizhi_3-weizhi_4)+2):(24+(abs(weizhi_3-weizhi_4)+1)) 
    if v1(i)==v1(j) 
        v1(j:(24+(abs(weizhi_3-weizhi_4))))=v1(j+1:(25+(abs(weizhi_3-weizhi_4)))); 
        break; 
    end 
  end 
end 
 
for i=1:(abs(weizhi_3-weizhi_4)+1) 
  for j=(abs(weizhi_3-weizhi_4)+2):(24+(abs(weizhi_3-weizhi_4)+1)) 
    if v2(i)==v2(j) 
        v2(j:(24+(abs(weizhi_3-weizhi_4))))=v2(j+1:(25+(abs(weizhi_3-weizhi_4)))); 
        break; 
    end 
  end 
end 
 
xin(xuhao(weizhi_1),:)=v1(1:24); 
xin(xuhao(weizhi_2),:)=v2(1:24); 
 
%交叉后形成新的组群 
 
xin 
x=xin;          
 
for j=1:28 
  m=rand(1); 
  if m>pv 
    x(j,:)=zeros(1,24); 
  end 
end     %按照变异概率构成一组父本群x 
 
k=0; 
for j=1:28 
  if x(j,:)~=zeros(1,24) 
    k=k+1; 
    xuhao(k)=j;   %将父本的序号给数组xuhao 
  end 
end 
 
k=0; 
 
if sum(xuhao)~=0 
  for j=1:28 
  if x(j,:)~=zeros(1,24) 
    k=k+1; 
    xz(k,:)=x(j,:); 
  end 
  end 
end 
for i=1:k 
gailv_3(i)=1/k; 
end 
 
m_3=rand(1); 
for i=1:(k-1) 
if m_3<=sum(gailv_3(1:(i+1)))&m_3>sum(gailv_3(1:i)) 
      weizhi_1=i+1; 
    else  
      if m_3<=gailv_3(1)  
      weizhi_1=1; 
      end 
end 
end 
 
%让xin矩阵中第xuhao(weizhi_1)个组群发生变异 
 
for i=1:24 
gailv_4(i)=1/24; 
end 
 
m_4=rand(1,2); 
for i=1:23 
if m_4(1)<=sum(gailv_4(1:(i+1)))&m_4(1)>sum(gailv_4(1:i)) 
      weizhi_2=i+1; 
    else  
      if m_4(1)<=gailv_4(1)  
      weizhi_2=1; 
      end 
end 
end 
 
for i=1:23 
if m_4(2)<=sum(gailv_4(1:(i+1)))&m_4(2)>sum(gailv_4(1:i)) 
      weizhi_3=i+1; 
    else  
      if m_4(2)<=gailv_4(1)  
      weizhi_3=1; 
      end 
end 
end 
 
if weizhi_2==weizhi_3 
  k=1; 
else 
  k=0; 
end 
while k==1 
  m_4=rand(1,2); 
for i=1:23 
if m_4(1)<=sum(gailv_4(1:(i+1)))&m_4(1)>sum(gailv_4(1:i)) 
      weizhi_2=i+1; 
    else  
      if m_4(1)<=gailv_4(1)  
      weizhi_2=1; 
      end 
end 
end 
 
for i=1:22 
if m_4(2)<=sum(gailv_4(1:(i+1)))&m_4(2)>sum(gailv_4(1:i)) 
      weizhi_3=i+1; 
    else  
      if m_4(2)<=gailv_4(1)  
      weizhi_3=1; 
      end 
end 
end 
 
if weizhi_2==weizhi_3 
  k=1; 
else 
  k=0; 
end 
end 
 
%xin(xuhao(weizhi_1),:)随机生成的数的位置是weizhi_2,weizhi_3 
 
for i=min(weizhi_2,weizhi_3):max(weizhi_2,weizhi_3) 
  h=xin(xuhao(weizhi_1),i); 
  xin(xuhao(weizhi_1),i)=xin(xuhao(weizhi_1),(max(weizhi_2,weizhi_3)+min(weizhi_2,weizhi_3)-i)); 
  xin(xuhao(weizhi_1),(max(weizhi_2,weizhi_3)+min(weizhi_2,weizhi_3)-i))=h; 
end 
 
a=xin; 
 
y1(n)=mean(f); 
 
y2(n)=min(f); 
 
y3(n)=max(f); 
 
end 
 
a