이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
GA error "your fitness function must return a scalar value"
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Geoff Hayes
2018년 4월 3일
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
Nikolas Spiliopoulos
2018년 4월 3일
thanks for the answer, however I don't get it why is a matrix
i tried this putting random data in the function above:
Battery_num=7;
Battery_cost=1200;
x=ones(2513,1);
x(1:100,1)=10;
x(101:200,1)=20;
x(201:500,1)=100;
x(501:end)=300;
then I type f(x) and I get a value of 1.8437
many thanks
Nikolas
Geoff Hayes
2018년 4월 3일
I guess it all depends on your x. If I change this to
x=ones(1,2513);
then the answer is a 179x179 matrix. Are you sure that the x being provided to your fitness function is a column and not a row?
Nikolas Spiliopoulos
2018년 4월 3일
alright I see,
I had used fmicon before at the same problem so I was giving an initial vector for x like x=ones(2513,1). However with GA I assume it takes a random initial set on its own, so I don't set somewhere a value for x.
correct me if I'm wrong
thanks again
Nikolas
Nikolas Spiliopoulos
2018년 4월 3일
Actually, I don't give an initial population by myself
I thought matlab does it itself
probably if I give an initial population in this x=ones (2513,1) will solve the problem? thanks
Geoff Hayes
2018년 4월 4일
You may want to define a function to create your initial population. I suspect that you are just setting nvars (see number of variables to 2513 and then MATLAB is generating a 1x2513 array. Rather than using an anonymous function, you could create one that would guard against this problem
function [fitness] = myGAFunction(x)
if isrow(x)
x = x';
end
fitness = ((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;
end
I'm assuming that the above function is nested within your main function and so has access to the battery_* variables.
Nikolas Spiliopoulos
2018년 4월 4일
편집: Nikolas Spiliopoulos
2018년 4월 4일
Hi again, thanks for the reply. Yeah you're right I have set the nvars variable and matlab probably takes an array of 1x2513 instead of 2513x1.
However, being a beginner in matlab I don't really get it how should I write it in the script file. For the moment I have 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;
options = gaoptimset('UseParallel',true,'Display','iter');
nvars=(N*Battery_num)+(N-1)*Battery_num;
[x,fval]=ga(f,nvars,A,b,Aeq,beq,lb,[],[],options);
can you please explain where i put the function? sorry for being a pain many thanks!!
Geoff Hayes
2018년 4월 4일
Hi Nikolas - if we nest our objective function within the main function like
function myGAOptimization
Battery_num = 42;
Battery_cost= 24;
% other variables like A, b, etc.
function [fitness] = myGAFunction(x)
if isrow(x)
x = x';
end
fitness = ((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;
end
options = gaoptimset('UseParallel',true,'Display','iter');
nvars=(N*Battery_num)+(N-1)*Battery_num;
[x,fval]=ga(myGAFunction,nvars,A,b,Aeq,beq,lb,[],[],options);
end
and save it to a file called myGAOptimization.m it should work. (I don't have the Optimization toolkit so can't verify.) See the attached file for an example.
Stephen23
2018년 4월 4일
편집: Stephen23
2018년 4월 4일
If the original function works for an Nx1 vector, but ga provides it with a 1xN vector, then one simple solution would be to add a simple wrapper to reshape the vector:
f = @(x)... ; % your original function
g = @(x) f(x(:)); % convert any x to a column
...
[x,fval] = ga(g,...);
Nikolas Spiliopoulos
2018년 4월 4일
thanks for the answer,
it seems that I am getting the same result though
with some dummy data I 'm getting the same value for
f(x) and g(x)!
Nikolas Spiliopoulos
2018년 4월 4일
I also tried the answer that Geoff suggested however I'm getting the error "not enough input arguments" although I put also battery_num and battery cost inputs :(
function [fitness] = myGAfunction(x,Battery_num,Battery_cost)
Geoff Hayes
2018년 4월 4일
Nikolas - because myGAfunction is nested within the main function, then it has access to the Battery_num and Battery_cost variables that have been declared in the "parent" function. The error "not enough input parameters" is because you have defined your objective function with three input parameters when the GA is providing only one input parameter. Try using the attached code.
Nikolas Spiliopoulos
2018년 4월 5일
Hi again,
I tried the attached code and still getting the same error that's why I put more input variables in the myGAfunction.
Literally don't know how to fix it
thanks very much for the comments, I really appreciate it!
Nikolas Spiliopoulos
2018년 4월 5일
편집: Nikolas Spiliopoulos
2018년 4월 5일
I tried it again, So I manage to run it!
however I get the same error ("about scalar value") don't know why :(
Geoff Hayes
2018년 4월 5일
Try putting a break point in the objective/fitness function and then run your code to see what the input dimensions are and if they make sense.
Nikolas Spiliopoulos
2018년 4월 5일
편집: Nikolas Spiliopoulos
2018년 4월 5일
I did it, I have a vector of x=2513X1 but afterwards the Initial population in the options it's empty ([]). I tried to put x as Initial population but I think it only takes a row!
Geoff Hayes
2018년 4월 5일
Well if you put x as the initial population, aren't you saying that the population only has one member?
Nikolas Spiliopoulos
2018년 4월 5일
Yeah, the problem is I cannot put as an initial population a matrix [2513x200], I can only put a [200x2513] that how the problem comes. Because I think matlab reads the rows so finally my fitness function gives a matrix and not a scalar.
Geoff Hayes
2018년 4월 5일
ok but if you put the breakpoint in the myGAFunction function what are the dimensions of the input? If a row instead of column, then just transpose it.
Nikolas Spiliopoulos
2018년 4월 6일
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!!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
태그
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)