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

4 views (last 30 days)
Matheus Torquato
Matheus Torquato on 30 Jul 2021
I'm running the MATLAB GA to optimise my objective function using:
var1 = (linspace(100, 400, 10));
var2 = (linspace(2, 7, 10));
fori = 1:长度(var2)
forj=1:length(var1)
objFun= @(x) my_objFun(var1(j),var2(i));
constraintFun = @(x) my_constraintFun(var1(j),var2(i));
尝试
[X,FVAL,EXITFLAG,输出] = GA(OBJFUN,NVARS,A,B,AEQ,BEQ,LB,UB,UB,CONDERTFUN,INTCON,INTCON,options);
catche
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.
  • 错误1
[ERROR] - The identifierwas:
MATLAB:matrix:singleSubscriptNumelMismatch
[错误] - 有一个错误!消息是:
Unableto perform assignment because the left and right sides have a different number of elements.
  • 错误2
[ERROR] - The identifierwas:
MATLAB:griddedInterpolant:NonMonotonicCompVecsErrId
[错误] - 有一个错误!消息是:
Samplepoints 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.
3 Comments
Matheus Torquato
Matheus Torquato on 5 Aug 2021
再次使用FO循环以相反顺序运行它不会改变任何内容。
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));
fori=length(var2):-1:1
forj =长度(var1):-1:1
objFun= @(x) my_objFun(x,var1(j),var2(i));
constraintfun = @(x)my_constraintfun(x,var1(j),var2(i));
尝试
[~,fval,~,output]= ga(objFun,nvars,A,b,Aeq,beq,lb,ub,constraintFun,IntCon,options);
catche
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

Sign in to comment.

Answers (1)

Alan Weiss
Alan Weiss 2021年8月8日
I do not understand this line:
objFun= @(x) my_objFun(var1(j),var2(i));
变量在哪里 x enter objFun ? I think that is your main problem. Try calling
objFun(x0)
对于一些 x0 that is a row vector with n vars elements. It probably will not return what you hope it returns.
Alan Weiss
MATLAB数学工具箱文档
1 Comment
Matheus Torquato
Matheus Torquato on 9 Aug 2021
Thank you for your comment.
对此我感到抱歉。那是编写此片段伪代码时的错别字。
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.

Sign in to comment.

s manbetx 845


发布

R2020a

美军陆军nity Treasure Hunt

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

开始狩猎!