Calling a function within an fmincon slows it down?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!
0 comentarios
Respuestas (2)
Ameer Hamza
el 29 de Oct. de 2020
Editada: Ameer Hamza
el 29 de Oct. de 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
Alan Weiss
el 4 de Nov. de 2020
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
0 comentarios
Ver también
Categorías
Más información sobre Variables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!