Call function multiple times but execute once
37 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
joshua Abam
am 7 Dez. 2019
Kommentiert: joshua Abam
am 3 Jan. 2020
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 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Steven Lord
am 10 Dez. 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.
2 Kommentare
Rik
am 10 Dez. 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.
Siehe auch
Kategorien
Mehr zu Direct Search finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!