Call function multiple times but execute once
Mostra commenti meno recenti
Good Day all,
I am kind of stranded and I need all the helps I can possible get.
I have a main code which I am running, inside the main code I reference a function 3 times, the function is multple output, as such, each time I run the main function, it runs the reference function 3 times as well to extract all three output even when all the function are from the same source, which to me is kind of fustrating as it takes time to run the function one time, how much more running it three times. My challenge is how can I possible ajust the code so it can only runs the function one time.
9 Commenti
Rik
il 7 Dic 2019
Can you provide an example of what you are doing?
joshua Abam
il 10 Dic 2019
Modificato: joshua Abam
il 10 Dic 2019
Rik
il 10 Dic 2019
I have never worked with memoization myself, but I can imagine suppressing outputs might influence the result.
Also, are you certain the different inputs to the function are exactly the same? You could try putting the code below in your function to confirm this.
persistent x_input_list
if isempty(x_input_list)
x_input_list={x};
else
if any(cellfun(@(db) isequal(x,db),x_input_list))
error('function ran twice for the same input')
else
%add to database
x_input_list(end+1)={x};
end
end
joshua Abam
il 10 Dic 2019
Rik
il 10 Dic 2019
You called the function without the memoization layer. Of course it will run the function multiple times in such a case. See the code below. The third call will trigger an error, because it doesn't load from the memoization layer.
clc
x=rand;
fh=memoize(@foo);
fh(x);% <-- first call
fh(x);% <-- second call, no error (loaded from memoization)
foo(x);% <-- this will trigger an error
function y=foo(x)
persistent x_input_list
if isempty(x_input_list)
x_input_list={x};
else
if any(cellfun(@(db) isequal(x,db),x_input_list))
error('function ran twice for the same input')
else
%add to database
x_input_list(end+1)={x};
end
end
y=rand;
end
Walter Roberson
il 11 Dic 2019
Why aren't you coding,
function [c, ceq] = nonconst(x)
u = 20;
v = 40;
[total_value, Delta_value, energy_value] = digital(x);
c = total_value-u*v;... ` `%first nonlinear constrain
u*energy_value;... %2nd Nonlinear constrain
v*Delta_value;
ceq = [];
end
joshua Abam
il 11 Dic 2019
joshua Abam
il 12 Dic 2019
joshua Abam
il 3 Gen 2020
Risposta accettata
Più risposte (1)
Steven Lord
il 10 Dic 2019
1 voto
I think it unlikely that ga is calling your function with exactly, down to the last bit, the same inputs multiple times. It's likely calling your function with inputs that are very close to one another, inputs that may even be displayed in the default format as the same, but that are very slightly different. Because of that memoization probably isn't going to help you.
Let me take a step back. Why do you want or need to try to reduce the number of calls ga makes to your function? Is it a performance consideration? Are the objective function evaluations expensive in terms of money (for example, are you planning to use ga to optimize parameters that require running a physical experiment for each set of parameters evaluated?) Is it a stylistic concern?
If you're concern is performance, what about the multiple calls to your objective function are the performance bottleneck? My strong suspicion is that it is your table call in the objective function as the other functions you call in your objective function require much less processing than creating a table. If you're doing this to show the parameter values after each iteration, depending on exactly why you need this information you may be able to instead specify an OutputFcn in some optimoptions that you pass into ga. See the section on ga options in the documentation for optimoptions for more information about the OutputFcn option.
2 Commenti
joshua Abam
il 10 Dic 2019
Modificato: Rik
il 10 Dic 2019
Rik
il 10 Dic 2019
As Steven explained: memoization only works if the input is exactly the same. That is why I suggested adding that code to your function. It will error if it runs twice with the same input. If it does run multiple times, that means the input wasn't the same.
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!