我编了个程序用GA优化BP,不是优化BP的结构,是以BP的输出作为适应度函数,优化BP的输入得到最大的输出,程序、适应度函数fitness及出现的问题如下,请大家帮我看看啊,谢谢!
p=[ 40 21 2.5 6 5;
40 25 3 6.5 10;
40 30 3.5 7 20;
45 21 2.5 6.5 10;
45 25 3 7 20;
45 30 3.5 6 5;
50 21 3 6 20;
50 25 3.5 6.5 5;
50 30 2.5 7 10;
40 21 3.5 7 10;
40 25 2.5 6 20;
40 30 3 6.5 5;
45 21 3 7 5;
45 25 3.5 6 10;
45 30 2.5 6.5 20;
50 21 3.5 6.5 20;
50 25 2.5 7 5;
50 30 3 6 10];
t=[2.673;3.412;1.315;2.019;1.996;0.885;9.317;4.484;1.768;5.720;2.316;0.678;1.042;1.492;0.765;8.812;1.204;3.130];
T=t';
P=p';
net=newff(minmax(P),[12,1],{'tansig','purelin'},'trainlm');
%训练网络
net.trainParam.show=10;
%net.trainParam.lr=0.05;
%net.trainParam.lr_inc=1.05;
net.trainParam.epochs=10000;
net.trainParam.goal=1e-5;
% net=init(net);
[net,tr]=train(net,P,T);
minmax_var=minmax(P);
minmax_target=minmax(T);
save('result','net','minmax_var','minmax_target');
load('result','net','minmax_var','minmax_target');
%生成初始种群,大小为20
initPop=initializega(20,minmax(P),'fitness');
%100次遗传迭代
[x,endPop,bPop,trace]=ga(minmax(P),...
'fitness',[],...
initPop,[1e-6 1 1],...
'maxGenTerm',100,...
'normGeomSelect',[0.08],...
['arithXover'],[0.3],...
'nonUnifMutation',[0.1]);
figure(1)
plot(endPop(:,1),endPop(:,2),'y*')
figure(2)
plot(trace(:,1),trace(:,2),'r*')
xlabel('Generation');
ylabel('Fitness');
legend('解的变化','种群平均值的变化')
错误提示如下:
??? Error using ==> network.sim
Inputs are incorrectly sized for network.
Matrix must have 5 rows.
Error in ==> fitness at 7
eval=sim(net,P);
Error in ==> initializega at 41
eval(estr);
%这是适应度函数
function [sol,eval]=fitness(P,options)
global net;global isformax;
global minmax_target;
load('result');
min_target=minmax_target(1);
max_target=minmax_target(2);
eval=sim(net,P);
if isformax
eval=eval-min_target+(max_target-min_target);
else
eval=-eval+max_target+(max_target-min_target);
end
|