LuigiDB
LuigiDB

Reputation: 72

matlab fitting function in genetic algorithm (ga function) doesn't accept output

Hi everyone i'm new to matlab and i have some problem with the ga function.

I must find the best integer values of input and output delays for a net applied to a time series problem using genetic algorithm.

I write a fitting function that using 2 variables as input and output delays create the net and return the performance value.

I want 2 delays between 1 and 10 so i use the ga function like this

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

And i have this error "Output argument "out" (and maybe others) not assigned during call to"D:\Documents\MATLAB\rendimentoRete.m>rendimentoRete"."

This is my fitting function (if someone prefer this is the same function on pastebin with Syntax Highlighting http://pastebin.com/5iwzwhi0)

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

with the load of baseSet.mat i'm loading first_period and first_usd_ise because they are always the same.

I can't figure out what to do because when i use the function stand alone gives me good result. Someone can explain me where i mistake.

Upvotes: 0

Views: 662

Answers (1)

Geoff
Geoff

Reputation: 1603

According to the documentation for MATLAB's ga, the signature of the fitness function is described as

...should accept a row vector of length nvars and return a scalar value.

That means that your function signature has to change from one with two input variables like

function out = rendimentoRete(iDelay, oDelay)

to a signature with just one input variable like

function out = rendimentoRete(inputVars)

where inputVars is a vector whereby you can obtain the two variables as

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

Modify your function signature and first two lines to the above (you can remove the margin check), try it out, and see what happens!

Upvotes: 1

Related Questions