Stopping fminsearch immediately?

12 Ansichten (letzte 30 Tage)
Allan Paolo
Allan Paolo am 13 Sep. 2021
Kommentiert: Walter Roberson am 13 Sep. 2021
So I have fminsearch running a custom function func, with outputs A, B, and C: [A, B, C] = func(x, y, z).
I used the function fminsearch to optimize the parameters x, y, and z. My problem is that it takes too long as it gets stuck on invalid trial values for x, y, and z. I was able to determine that this happens when the trial results A, B, and C are very large. While I know that fminsearch would self-correct and that it would just re-assign values of x, y, and z, I find this very slow and I wanted to just exit fminsearch.
Therefore, I wanted it such that fminsearch would stop when A >= 1000, B >= 500, and C >= 500. I placed this on func(x, y, z):
if A >= 1000 || B >= 500 || C >= 500
A = nan
B = nan
C = nan
error('Poor x, y, and z values. Re-iterating.')
end
Sadly, while the function would exit, fminsearch would still try to continue solving the problem. Is there a way to stop fminsearch from doing this, or to accomplish my requirement that fminsearch would stop when it calculates A >= 1000 || B >= 500 || C >= 500?
Thank you very much.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 13 Sep. 2021
Also you can specify MaxFunEvals and MaxIter
  9 Kommentare
Walter Roberson
Walter Roberson am 13 Sep. 2021
y = 200;
options = optimset('OutputFcn', @(x,optimValues,state)output_opt(x,optimValues,state,y);
[x, Z] = fminsearch(@(x)func(x, y),[1000, 1000, 1000], options)
Walter Roberson
Walter Roberson am 13 Sep. 2021
[~, A, B, C] = func(x, y)
That requires recalculating everything that was calculated in func(). Using memoize() would avoid that. memoize() acts as a cache.
if stop
end
That is not needed.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Optimization finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by