Call function multiple times but execute once
83 views (last 30 days)
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.
More Answers (1)
Steven Lord on 10 Dec 2019
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.