马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
源程序如下:
function f1=f(x)
f1=x(:,1).*x(:,1)/4+x(:,2).*x(:,2);
function f2=f(x)
f2=x(:,1).*(1-x(:,2))+10;
NIND=100;
MAXGEN=50;
NVAR=2;
PRECI=20;
GGAP=0.9;
trace1[ ];trace2[ ];trace3[ ];
FieldD=[rep([PRECI],[1,NVAR]);[1,1;4,2];rep([1;0;1;1],[1,NVAR])];
Chrom=crtbp(NIND,NVAR*PRECI);
v=bs2rv(Chrom,FieldD);
gen=1;
while gen<MAXGEN,
[NIND,N]=size(Chrom);
M=fix(NIND/2);
ObjV1=f1(v(1:M,:));
FitnV1=ranking(ObjV1);
SelCh1=select('sus',Chrom(1:M,:),FitnV1,GGAP);
ObjV2=f2(v(1:M,:));
FitnV2=ranking(ObjV2);
SelCh2=select('sus',Chrom(1:M,:),FitnV2,GGAP);
SelCh=[SelCh1;SelCh2];
SelCh=recombin('xovsp',SelCh,0.7);
Chrom=mut(SelCh);
v=bs2rv(Chrom,FieldD);
trace1(gen,1)=min(f1(v));
trace1(gen,2)=sum(f1(v))/length(f1(v));
trace2(gen,1)=min(f2(v))/length(f2(v));
trace2(gen,2)=sum(f2(v));
trace3(gen,1)=min(f1(v)+f2(v));
trace3(gen,2)=sum(f1(v))/length(f1(v))+sum(f2(v))/length(f2(v));
gen=gen+1;
end
figure(1);clf;
plot(trace1(:,1));hold on;plot(trace1(:,2),'-.');
plot(trace1(:,1),'.');plot(trace(:,2),'.');grid;
legend('解的变化','种群均值的变化')<
xlabel('迭代次数');ylabel('目标函数值');
figure(3);clf;
plot(trace3(:,1));hold on;
plot(trace3(:,2),'-.');
plot(trace3(:,1);'.');
plot(trace3(:,2),'.');grid;
legend('解的变化','种群均值的变化');
xlabel('迭代次数');ylabel('目标函数值');
figure(4);clf;plot(f1(v);hold on;
plot(f2(v),'r-.');grid;
运行出现错误:
??? function f1=f(x)
Error: Function definitions are not permitted at the prompt or in scripts.
期盼解答一下,如何排除错误·~谢谢了运行环境是m7
[ 本帖最后由 xinyuxf 于 2007-7-12 19:52 编辑 ] |