Call function multiple times but execute once
古いコメントを表示
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 件のコメント
Rik
2019 年 12 月 7 日
Can you provide an example of what you are doing?
joshua Abam
2019 年 12 月 10 日
編集済み: joshua Abam
2019 年 12 月 10 日
Rik
2019 年 12 月 10 日
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
2019 年 12 月 10 日
Rik
2019 年 12 月 10 日
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
2019 年 12 月 11 日
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
2019 年 12 月 11 日
joshua Abam
2019 年 12 月 12 日
joshua Abam
2020 年 1 月 3 日
採用された回答
その他の回答 (1 件)
Steven Lord
2019 年 12 月 10 日
1 投票
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 件のコメント
joshua Abam
2019 年 12 月 10 日
編集済み: Rik
2019 年 12 月 10 日
Rik
2019 年 12 月 10 日
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.
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!