现代算法(一) 基因算法

  2019-03-10 16:37:59

  转眼间就到了第三周,刚考完Python二级,江苏省的题目有点太那个了吧,什么都有,第一道程序题给的最后结果都是错的?

花了我好长时间在那里,一直在找设置精度的函数,然鹅,回到宿舍用matlab计算也是python上跑的结果.呵呵,今天终于开始看算法了

第一个算法就是基因算法.话不多说开始.

  基因算法原理就不重述了,直接上代码

 

  程序设计中选择的二进制编码,种群规模二 20,染色体杂交时的概率为 0.25,变异时概 率为 0.01,适应度函数为函数本身,终止条件为达到预定最大迭代次数。 

Objfunction & initial group

%n - 繁衍n代 不加上第一代,初始化父代
function gene(n)
f = @(x1,x2)21.5+x1*sin(4*pi*x1)+x2*sin(20*pi*x2);

% initial group
%种群数为20个
V =cell(1,20);
for i = 1:20
    temp1 = round(rand(1,33));
    %33 = 18+15=5+13+2+13
    temp2 = vec2str(temp1);
    V{i} = temp2;
end
clear temp1 temp2

record = adapt(V,f);
maxrec = maxrecord(record,V);
%自然选择,保存最适合
tic
for i=1:n
    V = chfather(V,record);
    V = opcrossover(V);
    V = variation(V);
    record = adapt(V,f);
    temp = maxrecord(record,V);
    if (temp(1,1)>maxrec(1,1))
        maxrec = temp;
        mark = i;
    end
end
time = toc;
format long
fprintf('       x1: %f\n',maxrec(2));
fprintf('       x2: %f\n',maxrec(3));
fprintf('f(x1,x2) : %f\n',maxrec(1));
fprintf(' from: %1.0f(th) generation \n',mark);
fprintf('    time: %f s\n',time);

    

 

  

vec2str
%Char2str
%将向量转化成字符串
%bin2dec()中的参数为字符串!
function str = vec2str(vec)
len = length(vec);
str = '';
for i=1:len
    temp = num2str(vec(i));
    str = strcat(str,temp);
end
end

 

 
adapt
function record = adapt(V,f)
record = zeros(20,1,'double');
for i=1:20
    temp1 =V{i};
    temp2 = temp1(1:18);
    temp3 = temp1(19:end);
    record(i,1) = f(bin_x(temp2,1),bin_x(temp3,2));
end
end

 

maxrecord
function maxrec = maxrecord(record,V)
[m,i] = max(record);
maxrec(1,1) = m;
temp1 = V{i};
temp2 = temp1(1:18);
temp3 = temp1(19:end);
maxrec(2,1) = bin_x(temp2,1);
maxrec(3,1) = bin_x(temp3,2);
end

 

 chfather
%选择父代,似乎染色体进行了异位
function V = chfather(V,record)
F = sum(record);
%pk-平均,qk-累计
pk = record/F;
qk = zeros(20,1,'double');
for i=1:20
    qk(i) = sum(pk(1:i));
end
%模拟转盘转动20次
r = rand(20,1);
for i = 1:20
    k = 1;
    while(r(i)>qk(k))
        k = k+1;
    end
    r(i) = k;
end
temp = V;
for i =1:20
    V{i} = temp{r(i)};
end
end

 

 opcorssover
function V = opcorssover(V)
%交换概率?pc?
%one point corssover
pc = 0.25;
l = 1;
while(l==1)
    r = rand(20,1);
    mk = find(r<pc);
    l = numel(mk);
end
if mod(numel(mk),2) == 1
    mk = mk(1:end-1);
end
ri = randi([1,32],1,numel(mk)/2);
for i = 1:numel(mk)/2
    [V{mk(2*(i-1)+1)},V{mk(2*(i-1)+2)}] = onecross(V{mk(2*(i-1)+1)},...
        V{mk(2*(i-1)+2)},ri(i));
end
end

 

variation
function V = variation(V)
pm = 0.01;
for i = 1:20
    r = rand(33,1);
    k = find(r<pm);
    for j = 1:numel(k)
        V{i} = vari(V{i},k(j));
    end
end
end

 

Vari
function gnew = vari(g_old,pos)
gnew = g_old;
if gnew(pos) =='1'
    gnew(pos) ='0';
else
    gnew(pos) ='1';
end
end

 

 onecorss
function [g1,g2]=onecross(gen1,gen2,pos)
len = numel(gen1);
g1 = gen1(1:pos);
g2 = gen2(1:pos);
g1(pos+1:len) = gen2(pos+1:end);
g2(pos+1:len) = gen1(pos+1:end);
end

 

bin_x
function num = bin_x(bin,opt)
switch opt
    case 1
        num = -3.0+bin2dec(bin)*((12.1-(-3.0))/(2^18-1));
        %12+3.0 = 16 ,0,1,2,3,4
        %6-4 = 2 ,0,1,2,
        %精度为0.5^13
    case 2
        num = 4.1+bin2dec(bin)*((5.8-4.1)/(2^15-1));
end
end

 


 

 

posted @ 2019-03-10 17:09  27315  阅读(1138)  评论(0编辑  收藏  举报