GA error "your fitness function must return a scalar value"

9 views (last 30 days)
Hi all,
I am trying to use GA to minimize a function which is like this:
f=@(x) ((0.0011*(x(2:180)-x(1:179)))'*exp(-0.0078*x(Battery_num*180+1:Battery_num*180+179)))*Battery_cost/20+(0.0011*(x(182:360)-x(181:359)))'*exp(-0.0078*x(Battery_num*180+179+1:Battery_num*180+358))*Battery_cost/20+...
(0.0011*(x(362:540)-x(361:539)))'*exp(-0.0078*x(Battery_num*180+359:Battery_num*180+537))*Battery_cost/20+(0.0011*(x(542:720)-x(541:719)))'*exp(-0.0078*x(Battery_num*180+538:Battery_num*180+716))*Battery_cost/20+...
(0.0011*(x(722:900)-x(721:899)))'*exp(-0.0078*x(Battery_num*180+717:Battery_num*180+895))*Battery_cost/20+
(0.0011*(x(902:1080)-x(901:1079)))'*exp(-0.0078*x(Battery_num*180+896:Battery_num*180+1074))*Battery_cost/20+...
(0.0011*(x(1082:1260)-x(1081:1259)))'*exp(-0.0078*x(Battery_num*180+1075:Battery_num*180+1253))*Battery_cost/20;
however I get an error (after a long time), "Your fitness function must return a scalar value".
The problem is that the function f(x) gives a scalar value when I call it in the command window, so I don't understand why I get this error
any ideas?
thanks in advance
Nikolas

Accepted Answer

Geoff Hayes
Geoff Hayes on 3 Apr 2018
Nikolas - your objective function must return a scalar value. In the above, f is returning a matrix instead of the scalar. If I evaluate your f with some dummy data, then this function returns a 179x179 matrix. A scalar is needed so that the GA can compare the fitness of one member of the population against all other members...
  21 Comments
Nikolas Spiliopoulos
Nikolas Spiliopoulos on 6 Apr 2018
Well I tried something else, I transpose my fitness function so now you can input a row like 1x2513 and you get a scalar (before I was taking a matrix 179x179).
So thank you very much for all these comments!
the problem now is that it takes ages, but I guess tha'ts another question
thanks again!!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!