简体   繁体   中英

Your fitness function must return a scalar value

I tried to perform an optimization using a neural network and the genetic algorithm. I trained a neural network with input p (4x72 matrix) and target t (2x72 matrix) . Regrading the optimization using genetic algorithm, I used the sim function of the neural network as the fitness function. The code I used for it is as follows:

objFcn=@(p) sim(net,p');
%'net' is the neural network I created with p as input and t as target
[xOpt,fVal,exitflag,target]=ga(objFcn,4,[],[],[],[],LB,UB,[],options);

I have provided the LB and UB which are lower bound and upper bounds, respectively. And options , I tried it with

options = gaoptimset('Vectorized','on');
% even vectorized off doesnt solve the problem

Logically, as I used p' in the sim command, the resultant matrix would be 72x2 which is the same as the population for GA. But for some reason, I always get the error saying 'Your fitness function must return a scalar value'.

Please guide me to solve this problem.

The "sim" function returns a matrix with all outputs from your network. You need to compute the squared error from that in order to provide a scalar value to minimize with the GA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM