24小时热门版块排行榜    

查看: 358  |  回复: 3
当前主题已经存档。

angry

金虫 (小有名气)

[交流] 【求助】谁帮忙看下我编写的进化规划算法(EP),优化结果总是不对

在matlab中编写了基本的进化规划算法,用测试函数试了一下,但总是不正确,自己找不出毛病,哪位高手帮忙看下,谢过了!
%EP program  主程序
%initial population
clc;clear;
pop_size = 100;
dimension = 30;
MaxIteration = 2000;
xmax = 100; xmin = -100;
score = zeros(1,2*pop_size);
p = 10;
for k = 1:dimension
    X(:,k)= unifrnd(xmin,xmax,pop_size,1);
end
ETA = 3*ones(pop_size,dimension);
% begin iteration
for iter = 1:MaxIteration
    % calculate function value
    for i = 1 : pop_size
        Funcvalue(i) = f1(X(i,: ));
    end
    min(Funcvalue)
    %create offspring
   
    for i = 1 : pop_size
%         rnd = rand(1);      
        rnd = normrnd(0,1,1,1);
        rnd2 = normrnd(0,1,1,dimension);
        rnd3 = normrnd(0,1,1,dimension);
        exponent = rnd./(sqrt(2*dimension)) + rnd2./sqrt(2*sqrt(dimension));
        NewETA(i, : ) = ETA(i,: ).*exp(exponent);
        NewX(i,: ) = X(i,: ) + rnd3.*NewETA(i,: );
    end
    ETA = NewETA;
%calculate offspring's fitness   
    for i = 1 : pop_size
        NewFuncvalue(i) = f1(NewX(i,: ));
    end
    % put parent's and offspring's fitness into a variable
    compFunValue = [Funcvalue, NewFuncvalue];
    UnitX = [X;NewX];
    compIndex = floor(rand(1,p).*2*pop_size);
    for i = 1 : p
        while compIndex(i) == 0
            compIndex(i) =  floor(rand*2*pop_size);
        end
    end
    score = zeros(1,2*pop_size);
    % condult the pair comparison
    for i = 1 : 2*pop_size
        for j = 1 : p
            if compFunValue(i) < compFunValue(compIndex(j))
                score(i) = score(i) + 1;
            end
        end;
    end
    id(1:2*pop_size) = 1 : 2*pop_size;
    [Y, ID] = BiDirectionBubble(score,id);  %sort the parent and offspring
    for i = 1 : pop_size
        X(i,: ) = UnitX(ID(i),: );
    end
end;

% f1.m 测试函数
function y=f1(x)
% This is sphere function
% x is a vector
d=length(x);
y=0;
for k=1:d
    y=y+x(k)^2;
end

% sort function
function [Y3,ID]=BiDirectionBubble(x,id)
runtimes=size(x,2);
low=1;up=runtimes;
while up>low
    t=low;
    for i=low:up-1;
        if x(i)             temp=x(i);
            x(i)=x(i+1);
            x(i+1)=temp;
            idtemp=id(i);
            id(i)=id(i+1);
            id(i+1)=idtemp;
            t=i;
        end;        
    end;
    up=t;
    for i=up:-1:low+1;
        if x(i)>x(i-1);
            temp=x(i);
            x(i)=x(i-1);
            x(i-1)=temp;
            idtemp=id(i);
            id(i)=id(i-1);
            id(i-1)=idtemp;           
            t=i;
        end;
    end;
    low=t;
end;
Y3=x;
ID=id;
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

suton

金虫 (正式写手)

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖交流
bslt(金币+1,VIP+0):赞一个。。。 7-5 10:02
代码发一份给我, 有空帮你调调
stxie@126.com
2楼2009-07-01 11:42:48
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

angry

金虫 (小有名气)

兄弟  已经发了一份了。
3楼2009-07-01 13:01:44
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

leidm

银虫 (正式写手)


小木虫(金币+0.5):给个红包,谢谢回帖交流
反复调调,总会发现问题
4楼2009-09-11 23:14:14
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 angry 的主题更新
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见