Stopping fminsearch immediately?
12 次查看(过去 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.
0 个评论
采纳的回答
Walter Roberson
2021-9-13
Use the options to invoke an outputFcn https://www.mathworks.com/help/matlab/math/output-functions.html
Also you can specify MaxFunEvals and MaxIter
9 个评论
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
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!