Calling a function within an fmincon slows it down?

3 views (last 30 days)
Hi! I'm pretty new to MATLAB.
So I have a fmincon optimization problem wherein the function I need to optimize (say O), and the constraint function (say C) is dependent on another function (say F) generated using python (that I run outside). My Python code automatically generates a hello.m file with F inside. F is multivariable and ridiculously massive; the MATLAB text editor struggles to load it even.
What I am doing at the moment looks like this:
optset = fmincon(@(x) O(x),x0,A,b,Aeq,beq,lb,ub,@(x) C(x));
function [c,ceq]=C(x)
hello;
[c,ceq] = function of F;
end
function result=O(x)
hello;
result = function of F;
end
I am however finding this very slow for larger sizes of F.
I was thinking maybe the reason is that for every step in the fmincon optimization, the hello.m file is being loaded over and over again, which is slowing it down significantly. Is this how the fmincon works? If so, is there a way to "store" my function somewhere so that it doesn't keep loading? Or is my function just too big and I have to accept it will take a while?
Thanks for any help!

Answers (2)

Ameer Hamza
Ameer Hamza on 29 Oct 2020
Edited: Ameer Hamza on 29 Oct 2020
I think it is probably being loaded at each iteration. MATLAB JIT compiler will not be able to optimize this part since it cannot know for sure if the file has been changed. If the same data is being loaded in each iteration, then load it once before the call to fmincon() and pass it as a parameter to O and C functions. Read about parameterizing the anonymous function here: https://www.mathworks.com/help/matlab/math/parameterizing-functions.html
For example
run('hello.m'); % I am not sure what variables are created by running this, but let us assume it creates 'a' and 'b'
optset = fmincon(@(x) O(x,a,b),x0,A,b,Aeq,beq,lb,ub,@(x) C(x,a,b));
and define O and C like this
function [c,ceq]=C(x,a,b)
[c,ceq] = function of F;
end
function result=O(x,a,b)
result = function of F;
end
  1 Comment
Rahul Arvind
Rahul Arvind on 3 Nov 2020
At the moment, 'hello.m' isn't creating any variables. It is a vector that looks like this:
F = [[sin(x(1))*1/30*cos(x(2)) ... ... ... ...very long]]
So when I add it within the functions O and C, it works well, but if I were to run it separately, it wouldn't really execute. Converting it into a traditional function doesn't really help, because then for me to execute it, I would need to pass the vector x as an argument -- and I would get out a specific value of the function. What I really want is a copy of this function itself loaded up in some way in the workspace so that it doesn't have to keep opening the file.
thank you so much for your response, and apologies that I am replying so late!

Sign in to comment.


Alan Weiss
Alan Weiss on 4 Nov 2020
One more thing you can try is to reduce the number of function evaluations using this technique.
I am not sure what "hello" is loading into your workspace. It seems to be called independently of the parameters x. So it probably should just be loaded once and stored either in a variable or a function. It can indeed be very time-consuming to load external files repeatedly. But I do not understand your pseudocode, so I could be off base here.
Alan Weiss
MATLAB mathematical toolbox documentation

Tags

Community Treasure Hunt

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

Start Hunting!