Stopping fminsearch immediately?

18 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2021-9-13
Also you can specify MaxFunEvals and MaxIter
  9 个评论
Walter Roberson
Walter Roberson 2021-9-13
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 2021-9-13
[~, 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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by