Apply Fmincon with GlobalSearch to OptimProblem with anonymous function

4 views (last 30 days)
I am trying to create an optimization problem structure by passing a function handle (@myfun) from the parent to the createOptimProblem method.
function [new_params,gof] = parent(fnHndl,params, lb, ub, varargin)
options = optimset;
options.GradObj = 'off';
options.Hessian = 'lbfgs';
options.Display = 'off';
options.StepTolerance = 10^(-6);
problem = createOptimProblem('fmincon','objective', ...
@(x) fnHndl(x,varargin{:}), ...
'x0', params, 'lb', lb,'ub', ub,'options',options);
gs = GlobalSearch();
[new_params,gof] = run(gs, problem);
end
GlobalSearch() is then used to find lowest objective function value – however, GlobalSearch() never terminates.
On the other hand, not using the parent wrapper function and creating the problem hard-coding the functions name, GlobalSearch finds a solution.
problem = createOptimProblem('fmincon','objective',@(x) myfun(x,varagin{:}), 'x0', params, 'lb', lb,'ub', ub,'options',options);
gs = GlobalSearch();
[new_Params, optimized_GoF] = run(gs, problem);
I cannot precisly say, what is causing the problem and would be thankful for your insight or a suggestion for a workaround.
  2 Comments
Matt J
Matt J on 16 Nov 2021
Edited: Matt J on 16 Nov 2021
The only thing we can surmise from the code that you've posted is that myfun() and fnHndl() are not, as you intend, the same function. Or one of the other inputs (lb,ub, etc...) are not.
Hermenegildo Pomponio
Hermenegildo Pomponio on 16 Nov 2021
Thank you, Matt J for your comment.
I should have supplied a working code snippet instead of a mock-example. Meanwhile, I figured, that the problem slowing down the excution was not related to the globalSearch() method or the way it is called, but scalar operations on tables.
I will delete this question, as unanswerable due to the insufficient context provided.

Sign in to comment.

Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!