huohuo 发表于 2007-11-18 21:43

请哪位高手帮我看看遗传算法的程序吧

怎么总是求不出最优解呢 如能指点一二 小弟不胜感激
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Main
clc
clear
global T0_7 T0_8 T0_11 T0_12
global K F DD
global theta
load 'T0_7.mat'
load 'T0_8.mat'
load 'T0_11.mat'
load 'T0_12.mat'
load 'K.mat'
load 'F.mat'
load 'DD.mat'
load 'theta.mat'
groupsize= 100;
var_num= 10;
var_length= 10;
var_min= 100;
var_max= 4000;
Pc= 0.85;
Pm= 0.001;
e= 1e-6;
Initial_group= Initial(groupsize,var_min,var_max,var_num,var_length);

mins= min(Initial_group(:,end));
r=1;
while mins>=e
    select_group= Select(Initial_group,var_num,var_length,groupsize);
    % 交叉
    t= 1;
    while t<= groupsize/2
      parent1_num= floor(rand*groupsize)+1;
      parent2_num= floor(rand*groupsize)+1;
      parent1= select_group(parent1_num,:);
      parent2= select_group(parent2_num,:);
      = Cross(parent1,parent2,Pc,var_num,var_length,groupsize);
      cross_group(t,:)= child1;
      cross_group(t+groupsize/2,:)= child2;
      t=t+1;
    end
    % 变异
    t= 1;
    while t<= groupsize
      parent_num= floor(rand*groupsize)+1;
      parent= select_group(parent_num,:);
      child= Mutation(parent,Pm,var_num,var_length);
      mutation_group(t,:)= child;
      t=t+1;
    end
    Initial_group= mutation_group;
    r= r+1;
    mins= min(mutation_group(:,end));
end


function = Initial(groupsize,var_min,var_max,var_num,var_length)
Initial_group= round(rand(groupsize,(var_num+1)*var_length+1));
for k= 1:var_num
Initial_group(:,var_num*var_length+k)= bi2de(Initial_group(:,(k-1)*var_length+1:k*var_length));
end
for k= 1:groupsize
Initial_group(k,(var_num+1)*var_length+1)= Fun(Initial_group(k,var_num*var_length+1:(var_num+1)*var_length));
end


function = Select(parent_group,var_num,var_length,groupsize)
parent_group(:,(var_num+1)*var_length+1)= 1./parent_group(:,(var_num+1)*var_length+1);
total_fitness= sum(parent_group(:,(var_num+1)*var_length+1));
parent_group(:,(var_num+1)*var_length+1)= parent_group(:,(var_num+1)*var_length+1)/total_fitness;
proa= cumsum(parent_group(:,(var_num+1)*var_length+1));
prob= sort(rand(groupsize,1));
parent_num= 1;
child_num= 1;
while child_num<= groupsize
    if(prob(child_num)< proa(parent_num))
      select_group(child_num,:)= parent_group(parent_num,:);
      child_num= child_num+1;
    else
      parent_num= parent_num+1;
    end
end

function = Cross(parent1,parent2,Pc,var_num,var_length,groupsize)
stringlength= var_num*var_length;
if(rand< Pc)
    cross_point= round(rand*(stringlength-2))+1;
    child1=;
    child2=;
    for k= 1:var_num
      child1(:,stringlength+k)= bi2de(child1(:,(k-1)*var_length+1:k*var_length));
      child2(:,stringlength+k)= bi2de(child2(:,(k-1)*var_length+1:k*var_length));
    end
    for k= 1:var_num
      child1(:,(var_num+1)*var_length+1)= Fun(child1(:,var_num*var_length+1:(var_num+1)*var_length));
      child2(:,(var_num+1)*var_length+1)= Fun(child2(:,var_num*var_length+1:(var_num+1)*var_length));
    end
else
    child1= parent1;
    child2= parent2;
end


function = Mutation(parent,Pm,var_num,var_length)
stringlength= var_num*var_length;
if(rand< Pm)
    mutation_point= round(rand*(stringlength-1))+1;
    child= parent;
    child(mutation_point)= not(parent(mutation_point));
    for k= 1:var_num
      child(:,stringlength+k)= bi2de(child(:,(k-1)*var_length+1:k*var_length));
    end
    for k= 1:var_num
      child(:,(var_num+1)*var_length+1)= Fun(child(:,var_num*var_length+1:(var_num+1)*var_length));
    end
else
    child= parent;
end

[ 本帖最后由 eight 于 2007-11-18 22:31 编辑 ]

花如月 发表于 2007-11-18 22:31

没有mat文件,别人用你的程序能跑起来么?

eight 发表于 2007-11-18 22:55

我也一直搞不懂:你要别人帮你检查错误,你总得描述清楚、把所有信息给完整才行。真的不知道这是什么问题,智力水平?
页: [1]
查看完整版本: 请哪位高手帮我看看遗传算法的程序吧