【问题标题】:matlab fitting function in genetic algorithm (ga function) doesn't accept output遗传算法中的matlab拟合函数(ga函数)不接受输出
【发布时间】:2014-08-23 00:01:57
【问题描述】:

大家好,我是 matlab 新手,我对 ga 函数有一些问题。

我必须使用遗传算法找到应用于时间序列问题的网络的最佳输入和输出延迟整数值。

我编写了一个拟合函数,使用 2 个变量作为输入和输出延迟创建网络并返回性能值。

我想要 1 到 10 之间的 2 次延迟,所以我使用这样的 ga 函数

[x,fval,exitflag,output,population,scores] = ga(@rendimentoRete, 2, [], [], [], [], [1 1], [10 10], [], [1 2])

我有这个错误“在调用“D:\Documents\MATLAB\rendimentoRete.m>rendimentoRete”期间未分配输出参数“out”(可能还有其他参数)。”

这是我的拟合函数 (如果有人喜欢这与语法突出显示 http://pastebin.com/5iwzwhi0 的 pastebin 上的功能相同)

function out = rendimentoRete(iDelay, oDelay)
if nargin < 2
    return;
end

%%carico le variabili dal workspace in modo che la funzione di fitness
%%conservi come varibili di ingresso solo i delay
load baseSet.mat;

inputSeries = tonndata(first_period,false,false);
targetSeries = tonndata(first_usd_ise,false,false);

% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:iDelay;
feedbackDelays = 1:oDelay;
hiddenLayerSize = 8;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);

% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer states.
% Using PREPARETS allows you to keep your original time series data unchanged, while
% easily customizing it for networks with differing numbers of delays, with
% open loop or closed loop feedback modes.
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);

% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

% Train the Network
for j=1:10
    [net,tr] = train(net,inputs,targets,inputStates,layerStates);
end

% Test the Network
outputs = net(inputs,inputStates,layerStates);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs);      %%alt -> MSE

%prova1
out = performance;

%{
%prova2
%converto l'output a un vettore e approssimo i risultati ai valori 1 e -1
%convert outputs cell to array
outputs_array = cell2mat(outputs);

%convertiamo outputs_array
for k=1:(268-outputDelay)
    if outputs_array(k)>0
        outputs_array(k)=1;
    else
        outputs_array(k)=-1;
    end
end
out = performance + calcoloMape(outputDelay, first_usd_ise, outputs_array) + calcoloPgcp(outputDelay, first_usd_ise, outputs_array);
%}
end

随着 baseSet.mat 的加载,我正在加载 first_period 和 first_usd_ise,因为它们总是相同的。

我不知道该怎么做,因为当我单独使用该功能时会给我带来很好的结果。 有人可以解释我哪里出错了。

【问题讨论】:

  • 对于nargin&lt;2没有分配输出,设置断点并检查您的函数是否获得所需数量的参数。
  • 没有nargin&lt;2 我得到这个错误Error using rendimentoRete (line 15) Not enough input arguments. 其中第15 行是feedbackDelays = 1:oDelay; 所以该函数不包含第二个变量。

标签: matlab neural-network


【解决方案1】:

根据 MA​​TLAB 的ga 的文档,适应度函数的签名描述为

...应该接受长度为 nvars 的行向量并返回一个标量值。

这意味着您的函数签名必须从具有两个输入变量的一个更改,例如

function out = rendimentoRete(iDelay, oDelay)

到只有一个输入变量的签名,例如

function out = rendimentoRete(inputVars)

其中inputVars 是一个向量,您可以借此获得两个变量

iDelay = inputVars(1);
oDelay = inputVars(2);

修改你的函数签名和上面的前两行(你可以去掉margin检查),试试看,看看会发生什么!

【讨论】:

  • 这一刻我的第一个念头就是把头砸在桌子上。非常感谢杰夫
猜你喜欢
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
  • 2017-04-16
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 2019-12-26
相关资源
最近更新 更多