声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 966|回复: 2

[编程技巧] 请哪位高手帮我看看遗传算法的程序吧

[复制链接]
发表于 2007-11-18 21:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
怎么总是求不出最优解呢 如能指点一二 小弟不胜感激
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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,:);
        [child1,child2]= 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_group]= 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_group]= 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 [child1,child2]= 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=[parent1(:,1:cross_point) parent2(:,cross_point+1:stringlength)];
    child2=[parent2(:,1:cross_point) parent1(:,cross_point+1:stringlength)];
    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 [child]= 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 编辑 ]

GA.txt

3.75 KB, 下载次数: 12

回复
分享到:

使用道具 举报

发表于 2007-11-18 22:31 | 显示全部楼层
没有mat文件,别人用你的程序能跑起来么?
发表于 2007-11-18 22:55 | 显示全部楼层
我也一直搞不懂:你要别人帮你检查错误,你总得描述清楚、把所有信息给完整才行。真的不知道这是什么问题,智力水平?
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-12-12 08:38 , Processed in 0.090560 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表