Error using MATLAB GA - [left and right sides have a different number of elements]

조회 수: 4 (최근 30일)
Matheus Torquato
Matheus Torquato 2021년 7월 30일
댓글: Alvaro 2022년 12월 28일
I'm running the MATLAB GA to optimise my objective function using:
var1 = (linspace(100, 400, 10));
var2 = (linspace(2, 7, 10));
for i=1:length(var2)
for j=1:length(var1)
objFun = @(x) my_objFun(var1(j),var2(i));
constraintFun = @(x) my_constraintFun(var1(j),var2(i));
try
[x,fval,exitflag,output]= ga(objFun,nvars,A,b,Aeq,beq,lb,ub,constraintFun,IntCon,options);
catch e
fprintf(1,'[ERROR] - The identifier was:\n%s\n',e.identifier);
fprintf(1,'[ERROR] - There was an error! The message was:\n%s\n',e.message);
end
end
end
The GA function is called repetitive times inside a loop. It sometimes runs successfully, other times it throws ERROR 1 and in a few other occasions it throws ERROR 2.
  • ERROR 1
[ERROR] - The identifier was:
MATLAB:matrix:singleSubscriptNumelMismatch
[ERROR] - There was an error! The message was:
Unable to perform assignment because the left and right sides have a different number of elements.
  • ERROR 2
[ERROR] - The identifier was:
MATLAB:griddedInterpolant:NonMonotonicCompVecsErrId
[ERROR] - There was an error! The message was:
Sample points must be unique and sorted in ascending order.
Any idea what that could be?
I'm using try and catches all over my my_objFun and my_constraintFun to make sure this error is not coming from there.
  댓글 수: 4
Matheus Torquato
Matheus Torquato 2021년 8월 5일
편집: Matheus Torquato 2021년 8월 9일
Running it again using the for loop in reverse order did not change anything.
I did not see any difference as well when calling the GA with different outputs.
var1 = (linspace(100, 400, 10));
var2 = (linspace(2, 7, 10));
for i=length(var2):-1:1
for j=length(var1):-1:1
objFun = @(x) my_objFun(x,var1(j),var2(i));
constraintFun = @(x) my_constraintFun(x,var1(j),var2(i));
try
[~,fval,~,output]= ga(objFun,nvars,A,b,Aeq,beq,lb,ub,constraintFun,IntCon,options);
catch e
fprintf(1,'[ERROR] - The identifier was:\n%s\n',e.identifier);
fprintf(1,'[ERROR] - There was an error! The message was:\n%s\n',e.message);
end
end
end
Alvaro
Alvaro 2022년 12월 28일
Could you remove the try-catch block and post the full error stack?

댓글을 달려면 로그인하십시오.

답변 (1개)

Alan Weiss
Alan Weiss 2021년 8월 8일
I do not understand this line:
objFun = @(x) my_objFun(var1(j),var2(i));
Where does the variable x enter objFun? I think that is your main problem. Try calling
objFun(x0)
for some x0 that is a row vector with nvars elements. It probably will not return what you hope it returns.
Alan Weiss
MATLAB mathematical toolbox documentation
  댓글 수: 1
Matheus Torquato
Matheus Torquato 2021년 8월 9일
Thank you for your comment.
I'm sorry about that. That was a typo while writing this snippet pseudocode.
I've corrected
objFun = @(x) my_objFun(var1(j),var2(i));
constraintFun = @(x) my_constraintFun(var1(j),var2(i));
to
objFun = @(x) my_objFun(x,var1(j),var2(i));
constraintFun = @(x) my_constraintFun(x,var1(j),var2(i));
This latter one is what I have been using in my code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by