Different number of function evaluations and iterations while using MEX of objective function in patternsearch

조회 수: 2 (최근 30일)
I generated a MEX function of my objective function to speed up the optimization process (pattern search) using Matlab coder (Matlab R2023b). The optimization with pattern search using the MEX function results in identical solutions. However, the number of function evaluations used by patternsearch is different while using the MEX of objective function. Similarly, the number of iterations employed by patternsearch is also different when I use the MEX function. The rest of the settings are identical.
I am not able to figure out the cause behind this discrepancy since MEX is essentially the same function.
Thanks in advance for any help in understanding this!
  댓글 수: 8
aksri
aksri 2023년 12월 6일
Thanks @Torsten and @Steven Lord. Here is how I am calling the function:
%ObjectiveFunction = @(R) MyFun(R,par1,par2,);
ObjectiveFunction = @(R) MyFun_mex(R,par1,par2,); % When using Mex of MyFun
options = optimoptions('patternsearch','Display','iter', ...
'CacheSize',inf,'UseParallel',true,..);
%start optimization
[R,Fval,exitflag,output] = patternsearch(ObjectiveFunction,R0,...
A,b,[],[],LB,UB,[],options);
Feval= output.funccount;
In a single simulation, I am calling patternsearch 4 times for 4 different usecases. It is the last case i.e. the 4th call to patternsearch which gives me different number of function evaluations. The first 3 cases are identical for both MyFun and MyFun_mex.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Execution Speed에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by